diff --git a/releasenotes/notes/scanner-loader-context-managers-5d80d95dc3d33fe0.yaml b/releasenotes/notes/scanner-loader-context-managers-5d80d95dc3d33fe0.yaml new file mode 100644 index 0000000..46ad8e6 --- /dev/null +++ b/releasenotes/notes/scanner-loader-context-managers-5d80d95dc3d33fe0.yaml @@ -0,0 +1,31 @@ +--- +fixes: + - | + Instances of ``reno.scanner.Scanner`` and ``reno.loader.Loader`` will now + corectly close all open files related to the scanned Git repo when the + ``close()`` method is called. Alternatively, these classes may be used as + a context manager. Previously, Python would attempt to close these files + itself, resulting in a ``ResourceWarning`` warning being emitted. +features: + - | + The ``reno.scanner.Scanner`` and ``reno.loader.Loader`` classes can now + be used as context managers. For example:: + + import reno.scannner + + with reno.scanner.Scanner(...) as scanner: + pass + + This will ensure any open files pertaining to the scanned Git repo are + correctly closed, avoiding ``ResourceWarning`` instances otherwise seen. + + A ``close()`` method is also provided for both, allowing use outside of + context managers. For example:: + + import reno.loader + + loader = reno.loader.Loader(...) + try: + pass + finally: + loader.close() diff --git a/reno/cache.py b/reno/cache.py index 660e5ad..c288cd0 100644 --- a/reno/cache.py +++ b/reno/cache.py @@ -21,50 +21,48 @@ from reno import scanner def build_cache_db(conf, versions_to_include): - s = scanner.Scanner(conf) + with scanner.Scanner(conf) as s: + branches = [conf.branch] + if not conf.branch: # if no branch requested, scan all + branches += s.get_series_branches() - branches = [conf.branch] - if not conf.branch: # if no branch requested, scan all - branches += s.get_series_branches() + notes = collections.OrderedDict() + for branch in branches: + notes.update(s.get_notes_by_version(branch)) - notes = collections.OrderedDict() - for branch in branches: - notes.update(s.get_notes_by_version(branch)) + # Default to including all versions returned by the scanner. + if not versions_to_include: + versions_to_include = list(notes.keys()) - # Default to including all versions returned by the scanner. - if not versions_to_include: - versions_to_include = list(notes.keys()) + # Build a cache data structure including the file contents as well + # as the basic data returned by the scanner. + file_contents = {} + for version in versions_to_include: + for filename, sha in notes[version]: + body = s.get_file_at_commit(filename, sha) + # We want to save the contents of the file, which is YAML, + # inside another YAML file. That looks terribly ugly with + # all of the escapes needed to format it properly as + # embedded YAML, so parse the input and convert it to a + # data structure that can be serialized cleanly. + y = yaml.safe_load(body) + file_contents[filename] = y - # Build a cache data structure including the file contents as well - # as the basic data returned by the scanner. - file_contents = {} - for version in versions_to_include: - for filename, sha in notes[version]: - body = s.get_file_at_commit(filename, sha) - # We want to save the contents of the file, which is YAML, - # inside another YAML file. That looks terribly ugly with - # all of the escapes needed to format it properly as - # embedded YAML, so parse the input and convert it to a - # data structure that can be serialized cleanly. - y = yaml.safe_load(body) - file_contents[filename] = y - - cache = { - 'notes': [ - {'version': k, 'files': v} - for k, v in notes.items() - ], - 'dates': [ - {'version': k, 'date': v} - for k, v in s.get_version_dates().items() - ], - 'file-contents': file_contents, - } - return cache + cache = { + 'notes': [ + {'version': k, 'files': v} + for k, v in notes.items() + ], + 'dates': [ + {'version': k, 'date': v} + for k, v in s.get_version_dates().items() + ], + 'file-contents': file_contents, + } + return cache -def write_cache_db(conf, versions_to_include, - outfilename=None): +def write_cache_db(conf, versions_to_include, outfilename=None): """Create a cache database file for the release notes data. Build the cache database from scanning the project history and diff --git a/reno/linter.py b/reno/linter.py index 2f94b7d..e7423e3 100644 --- a/reno/linter.py +++ b/reno/linter.py @@ -27,22 +27,24 @@ def lint_cmd(args, conf): notes = glob.glob(os.path.join(notesdir, '*.yaml')) error = 0 - load = loader.Loader(conf, ignore_cache=True) allowed_section_names = [conf.prelude_section_name] + \ [s[0] for s in conf.sections] uids = {} - for f in notes: - LOG.debug('examining %s', f) - uid = scanner._get_unique_id(f) - uids.setdefault(uid, []).append(f) + with loader.Loader(conf, ignore_cache=True) as ldr: + for f in notes: + LOG.debug('examining %s', f) + uid = scanner._get_unique_id(f) + uids.setdefault(uid, []).append(f) - content = load.parse_note_file(f, None) - for section_name in content.keys(): - if section_name not in allowed_section_names: - LOG.warning('unrecognized section name %s in %s', - section_name, f) - error = 1 + content = ldr.parse_note_file(f, None) + for section_name in content.keys(): + if section_name not in allowed_section_names: + LOG.warning( + 'unrecognized section name %s in %s', + section_name, f, + ) + error = 1 for uid, names in sorted(uids.items()): if len(names) > 1: diff --git a/reno/lister.py b/reno/lister.py index f176aad..7f052e7 100644 --- a/reno/lister.py +++ b/reno/lister.py @@ -21,16 +21,16 @@ def list_cmd(args, conf): "List notes files based on query arguments" LOG.debug('starting list') reporoot = conf.reporoot - ldr = loader.Loader(conf) - if args.version: - versions = args.version - else: - versions = ldr.versions - for version in versions: - notefiles = ldr[version] - print(version) - for n, sha in notefiles: - if n.startswith(reporoot): - n = n[len(reporoot):] - print('\t%s (%s)' % (n, sha)) + with loader.Loader(conf) as ldr: + if args.version: + versions = args.version + else: + versions = ldr.versions + for version in versions: + notefiles = ldr[version] + print(version) + for n, sha in notefiles: + if n.startswith(reporoot): + n = n[len(reporoot):] + print('\t%s (%s)' % (n, sha)) return diff --git a/reno/loader.py b/reno/loader.py index fcc7e2d..18d451f 100644 --- a/reno/loader.py +++ b/reno/loader.py @@ -30,8 +30,7 @@ def get_cache_filename(conf): class Loader(object): "Load the release notes for a given repository." - def __init__(self, conf, - ignore_cache=False): + def __init__(self, conf, ignore_cache=False): """Initialize a Loader. The versions are presented in reverse chronological order. @@ -70,24 +69,37 @@ class Loader(object): if (not self._ignore_cache) and cache_file_exists: LOG.debug('loading cache file %s', self._cache_filename) + with open(self._cache_filename, 'r', encoding=self._encoding) as f: self._cache = yaml.safe_load(f.read()) - # Save the cached scanner output to the same attribute - # it would be in if we had loaded it "live". This - # simplifies some of the logic in the other methods. - self._scanner_output = collections.OrderedDict( - (n['version'], n['files']) - for n in self._cache['notes'] - ) - self._tags_to_dates = collections.OrderedDict( - (n['version'], n['date']) - for n in self._cache['dates'] - ) + + # Save the cached scanner output to the same attribute + # it would be in if we had loaded it "live". This + # simplifies some of the logic in the other methods. + self._scanner_output = collections.OrderedDict( + (n['version'], n['files']) + for n in self._cache['notes'] + ) + self._tags_to_dates = collections.OrderedDict( + (n['version'], n['date']) + for n in self._cache['dates'] + ) else: self._scanner = scanner.Scanner(self._config) self._scanner_output = self._scanner.get_notes_by_version() self._tags_to_dates = self._scanner.get_version_dates() + def close(self): + """Close any files opened by this loader.""" + if self._scanner is not None: + self._scanner.close() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.close() + @property def versions(self): "A list of all of the versions found." diff --git a/reno/report.py b/reno/report.py index f45d11e..057cc67 100644 --- a/reno/report.py +++ b/reno/report.py @@ -16,20 +16,22 @@ from reno import loader def report_cmd(args, conf): "Generates a release notes report" - ldr = loader.Loader(conf) encoding = conf.options['encoding'] - if args.version: - versions = args.version - else: - versions = ldr.versions - text = formatter.format_report( - ldr, - conf, - versions, - title=args.title, - show_source=args.show_source, - branch=args.branch, - ) + + with loader.Loader(conf) as ldr: + if args.version: + versions = args.version + else: + versions = ldr.versions + text = formatter.format_report( + ldr, + conf, + versions, + title=args.title, + show_source=args.show_source, + branch=args.branch, + ) + if args.output: with open(args.output, 'w', encoding=encoding) as f: f.write(text) diff --git a/reno/scanner.py b/reno/scanner.py index 0379745..a4e88a8 100644 --- a/reno/scanner.py +++ b/reno/scanner.py @@ -533,6 +533,16 @@ class Scanner(object): ) self._encoding = conf.options['encoding'] + def close(self): + """Close any files opened by this scanner.""" + self._repo.close() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.close() + def _get_ref(self, name): if name: candidates = [ diff --git a/reno/semver.py b/reno/semver.py index 7573c4a..dbf3344 100644 --- a/reno/semver.py +++ b/reno/semver.py @@ -22,76 +22,85 @@ LOG = logging.getLogger(__name__) def compute_next_version(conf): "Compute the next semantic version based on the available release notes." LOG.debug('starting semver-next') - ldr = loader.Loader(conf, ignore_cache=True) - LOG.debug('known versions: %s', ldr.versions) + with loader.Loader(conf, ignore_cache=True) as ldr: + LOG.debug('known versions: %s', ldr.versions) - # We want to include any notes in the local working directory or - # in any commits that came after the last tag. We should never end - # up with more than 2 entries in to_include. - to_include = [] - for to_consider in ldr.versions: - if to_consider == '*working-copy*': - to_include.append(to_consider) - continue - # This check relies on PEP 440 versioning - parsed = version.Version(to_consider) - if parsed.post: - to_include.append(to_consider) - continue - break + # We want to include any notes in the local working directory or + # in any commits that came after the last tag. We should never end + # up with more than 2 entries in to_include. + to_include = [] + for to_consider in ldr.versions: + if to_consider == '*working-copy*': + to_include.append(to_consider) + continue - # If we found no commits then we're sitting on a real tag and - # there is nothing to do to update the version. - if not to_include: - LOG.debug('found no staged notes and no post-release commits') - return ldr.versions[0] + # This check relies on PEP 440 versioning + parsed = version.Version(to_consider) + if parsed.post: + to_include.append(to_consider) + continue - LOG.debug('including notes from %s', to_include) + break - candidate_bases = to_include[:] - if candidate_bases[0] == '*working-copy*': - candidate_bases = candidate_bases[1:] + # If we found no commits then we're sitting on a real tag and + # there is nothing to do to update the version. + if not to_include: + LOG.debug('found no staged notes and no post-release commits') + return ldr.versions[0] - if not candidate_bases: - # We have a real tag and some locally modified files. Use the - # real tag as the basis of the next version. - base_version = version.Version(ldr.versions[1]) - else: - base_version = version.Version(candidate_bases[0]) + LOG.debug('including notes from %s', to_include) - LOG.debug('base version %s', base_version) + candidate_bases = to_include[:] + if candidate_bases[0] == '*working-copy*': + candidate_bases = candidate_bases[1:] - inc_minor = False - inc_patch = False - for ver in to_include: - for filename, sha in ldr[ver]: - notes = ldr.parse_note_file(filename, sha) - for section in conf.semver_major: - if notes.get(section, []): - LOG.debug('found breaking change in %r section of %s', - section, filename) - return '{}.0.0'.format(base_version.major + 1) - for section in conf.semver_minor: - if notes.get(section, []): - LOG.debug('found feature in %r section of %s', - section, filename) - inc_minor = True - break - for section in conf.semver_patch: - if notes.get(section, []): - LOG.debug('found bugfix in %r section of %s', - section, filename) - inc_patch = True - break + if not candidate_bases: + # We have a real tag and some locally modified files. Use the + # real tag as the basis of the next version. + base_version = version.Version(ldr.versions[1]) + else: + base_version = version.Version(candidate_bases[0]) + + LOG.debug('base version %s', base_version) + + inc_minor = False + inc_patch = False + + for ver in to_include: + for filename, sha in ldr[ver]: + notes = ldr.parse_note_file(filename, sha) + + for section in conf.semver_major: + if notes.get(section, []): + LOG.debug('found breaking change in %r section of %s', + section, filename) + return '{}.0.0'.format(base_version.major + 1) + + for section in conf.semver_minor: + if notes.get(section, []): + LOG.debug('found feature in %r section of %s', + section, filename) + inc_minor = True + break + + for section in conf.semver_patch: + if notes.get(section, []): + LOG.debug('found bugfix in %r section of %s', + section, filename) + inc_patch = True + break major = base_version.major minor = base_version.minor patch = base_version.micro + if inc_patch: patch += 1 + if inc_minor: minor += 1 patch = 0 + return '{}.{}.{}'.format(major, minor, patch) diff --git a/reno/setup_command.py b/reno/setup_command.py index 7369e61..3968680 100644 --- a/reno/setup_command.py +++ b/reno/setup_command.py @@ -127,13 +127,15 @@ class BuildReno(cmd.Command): ) log.info('wrote cache file to %s', cache_filename) - ldr = loader.Loader(conf) - text = formatter.format_report( - ldr, - conf, - ldr.versions, - title=self.distribution.metadata.name, - ) + with loader.Loader(conf) as ldr: + text = formatter.format_report( + ldr, + conf, + ldr.versions, + title=self.distribution.metadata.name, + ) + with open(self.output_file, 'w') as f: f.write(text) + log.info('wrote release notes to %s', self.output_file) diff --git a/reno/sphinxext.py b/reno/sphinxext.py index 3d4571e..b40c5c1 100644 --- a/reno/sphinxext.py +++ b/reno/sphinxext.py @@ -111,22 +111,23 @@ class ReleaseNotesDirective(rst.Directive): os.path.join(conf.reporoot, notesdir), branch or 'current branch')) - ldr = loader.Loader(conf) - if version_opt is not None: - versions = [ - v.strip() - for v in version_opt.split(',') - ] - else: - versions = ldr.versions - LOG.info('got versions %s' % (versions,)) - text = formatter.format_report( - ldr, - conf, - versions, - title=title, - branch=branch, - ) + with loader.Loader(conf) as ldr: + if version_opt is not None: + versions = [ + v.strip() + for v in version_opt.split(',') + ] + else: + versions = ldr.versions + LOG.info('got versions %s' % (versions,)) + text = formatter.format_report( + ldr, + conf, + versions, + title=title, + branch=branch, + ) + source_name = '<%s %s>' % (__name__, branch or 'current branch') result = statemachine.ViewList() for line_num, line in enumerate(text.splitlines(), 1): diff --git a/reno/tests/test_formatter.py b/reno/tests/test_formatter.py index 81b00b1..ee6e713 100644 --- a/reno/tests/test_formatter.py +++ b/reno/tests/test_formatter.py @@ -49,6 +49,13 @@ class TestFormatterBase(base.TestCase): ignore_cache=False, ) + def tearDown(self): + # we don't need to worry about closing this after since we're not + # actually using a real Git repo here (see the mock above), but we'll + # do so to enforce the contract + self.ldr.close() + super().tearDown() + class TestFormatter(TestFormatterBase): diff --git a/reno/tests/test_loader.py b/reno/tests/test_loader.py index 41f023a..a89ee91 100644 --- a/reno/tests/test_loader.py +++ b/reno/tests/test_loader.py @@ -65,8 +65,8 @@ class TestValidate(base.TestCase): This is a single string. It should be converted to a list. """)) self.assertIsInstance(note_bodies['issues'], str) - ldr = self._make_loader(note_bodies) - parse_results = ldr.parse_note_file('note1', None) + with self._make_loader(note_bodies) as ldr: + parse_results = ldr.parse_note_file('note1', None) self.assertIsInstance(parse_results['issues'], list) def test_invalid_note_with_prelude_as_list(self): @@ -75,8 +75,8 @@ class TestValidate(base.TestCase): - The prelude should not be a list. ''')) self.assertIsInstance(note_bodies['prelude'], list) - ldr = self._make_loader(note_bodies) - ldr.parse_note_file('note1', None) + with self._make_loader(note_bodies) as ldr: + ldr.parse_note_file('note1', None) self.assertIn('does not parse as a single string', self.logger.output) def test_invalid_note_with_colon_as_dict(self): @@ -86,8 +86,8 @@ class TestValidate(base.TestCase): - dict: But this is parsed as a mapping (dictionary), which is bad. ''')) self.assertIsInstance(note_bodies['issues'][-1], dict) - ldr = self._make_loader(note_bodies) - ldr.parse_note_file('note1', None) + with self._make_loader(note_bodies) as ldr: + ldr.parse_note_file('note1', None) self.assertIn('instead of a string', self.logger.output) def test_invalid_note_with_unrecognized_key(self): @@ -98,8 +98,8 @@ class TestValidate(base.TestCase): This is an issue but we're using an unrecognized section key. ''')) self.assertIsInstance(note_bodies, dict) - ldr = self._make_loader(note_bodies) - ldr.parse_note_file('note1', None) + with self._make_loader(note_bodies) as ldr: + ldr.parse_note_file('note1', None) self.assertIn( 'The foobar section of note1 is not a recognized section.', self.logger.output) @@ -114,8 +114,8 @@ class TestValidate(base.TestCase): This is an issue but we're missing the top-level 'issues' key. ''')) self.assertIsInstance(note_bodies, list) - ldr = self._make_loader(note_bodies) - self.assertRaises(ValueError, ldr.parse_note_file, 'note1', None) + with self._make_loader(note_bodies) as ldr: + self.assertRaises(ValueError, ldr.parse_note_file, 'note1', None) self.assertIn( 'does not appear to be structured as a YAML mapping', self.logger.output) diff --git a/reno/tests/test_scanner.py b/reno/tests/test_scanner.py index 4df1f0a..d691df8 100644 --- a/reno/tests/test_scanner.py +++ b/reno/tests/test_scanner.py @@ -217,8 +217,8 @@ class BasicTest(Base): def test_non_python_no_tags(self): filename = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -231,8 +231,8 @@ class BasicTest(Base): def test_python_no_tags(self): self._make_python_package() filename = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -246,8 +246,8 @@ class BasicTest(Base): filename = self._add_notes_file() self.repo.add_file('not-a-release-note.txt') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -260,8 +260,8 @@ class BasicTest(Base): def test_note_commit_tagged(self): filename = self._add_notes_file() self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -274,8 +274,8 @@ class BasicTest(Base): def test_tag_with_v_prefix(self): filename = self._add_notes_file() self.repo.git('tag', '-s', '-m', 'tag with v prefix', 'v1.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -289,8 +289,8 @@ class BasicTest(Base): self._make_python_package() self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') filename = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -305,8 +305,8 @@ class BasicTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0rc1') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') filename = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -321,8 +321,8 @@ class BasicTest(Base): self.repo.add_file('ignore-1.txt') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') self.repo.add_file('ignore-2.txt') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -337,8 +337,8 @@ class BasicTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') f1 = self._add_notes_file() f2 = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -353,8 +353,8 @@ class BasicTest(Base): f1 = self._add_notes_file(commit=False) f2 = self._add_notes_file() self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -370,8 +370,8 @@ class BasicTest(Base): f1 = self._add_notes_file() self.repo.git('tag', '-s', '-m', 'first tag', '2.0.0') f2 = self._add_notes_file() - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -391,8 +391,8 @@ class BasicTest(Base): f2 = f1.replace('slug1', 'slug2') self.repo.git('mv', f1, f2) self.repo.commit('rename note file') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -411,8 +411,8 @@ class BasicTest(Base): f2 = f1.replace('slug1', 'slug0') self.repo.git('mv', f1, f2) self.repo.commit('rename note file') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -431,8 +431,8 @@ class BasicTest(Base): with open(os.path.join(self.reporoot, f1), 'w') as f: f.write('---\npreamble: new contents for file') self.repo.commit('edit note file') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -451,8 +451,8 @@ class BasicTest(Base): f2 = f1.replace('slug1', 'slug2') self.repo.git('mv', f1, f2) self.repo.commit('rename note file') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -474,8 +474,8 @@ class BasicTest(Base): 'slug1-0000000000000001') self.repo.git('mv', f1, f2) self.repo.commit('rename note file') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -497,8 +497,8 @@ class BasicTest(Base): self.c.override( earliest_version='2.0.0', ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -518,8 +518,8 @@ class BasicTest(Base): self.repo.git('rm', f1) self.repo.commit('remove note file') self.repo.git('tag', '-s', '-m', 'first tag', '2.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -546,8 +546,8 @@ class BasicTest(Base): '--pretty=%H %d', '--name-only') self.addDetail('git log', text_content(log_results)) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -573,8 +573,8 @@ class BasicTest(Base): status_results = self.repo.git('status') self.addDetail('git status', text_content(status_results)) # Now run the scanner - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() self.assertEqual( {'*working-copy*': [ (os.path.join('releasenotes', 'notes', basename), @@ -598,8 +598,8 @@ class BasicTest(Base): status_results = self.repo.git('status') self.addDetail('git status', text_content(status_results)) # Now run the scanner - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() # Take the staged version of the file, but associate it with # tagged version 1.0.0 because the file was added before that # version. @@ -623,8 +623,8 @@ class BasicTest(Base): status_results = self.repo.git('status') self.addDetail('git status', text_content(status_results)) # Now run the scanner - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() # Take the staged version of the file, but associate it with # tagged version 1.0.0 because the file was added before that # version. @@ -647,8 +647,8 @@ class BasicTest(Base): self.c.override( earliest_version=None, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -672,8 +672,8 @@ class BasicTest(Base): self.c.override( earliest_version=None, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -698,8 +698,8 @@ class IgnoreTest(Base): self.c.override( ignore_notes=[f1], ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -717,8 +717,8 @@ class IgnoreTest(Base): self.c.override( ignore_notes=[os.path.basename(f1)], ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -736,8 +736,8 @@ class IgnoreTest(Base): self.c.override( ignore_notes=[scanner._get_unique_id(f1)], ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -758,8 +758,8 @@ class IgnoreTest(Base): scanner._get_unique_id(f2), ], ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -775,8 +775,8 @@ class FileContentsTest(Base): def test_basic_file(self): # Prove that we can get a file we have committed. f1 = self._add_notes_file(contents='well-known-contents') - r = scanner.RenoRepo(self.reporoot) - contents = r.get_file_at_commit(f1, 'HEAD') + with scanner.RenoRepo(self.reporoot) as r: + contents = r.get_file_at_commit(f1, 'HEAD') self.assertEqual( b'well-known-contents', contents, @@ -786,8 +786,8 @@ class FileContentsTest(Base): # Returns None when the file does not exist at all. # (we have to commit something, otherwise there is no HEAD) self._add_notes_file(contents='well-known-contents') - r = scanner.RenoRepo(self.reporoot) - contents = r.get_file_at_commit('no-such-dir/no-such-file', 'HEAD') + with scanner.RenoRepo(self.reporoot) as r: + contents = r.get_file_at_commit('no-such-dir/no-such-file', 'HEAD') self.assertEqual( None, contents, @@ -799,8 +799,8 @@ class FileContentsTest(Base): with open(os.path.join(self.reporoot, f1), 'w') as f: f.write('new contents for file') self.repo.commit('edit note file') - r = scanner.RenoRepo(self.reporoot) - contents = r.get_file_at_commit(f1, 'HEAD') + with scanner.RenoRepo(self.reporoot) as r: + contents = r.get_file_at_commit(f1, 'HEAD') self.assertEqual( b'new contents for file', contents, @@ -813,12 +813,11 @@ class FileContentsTest(Base): with open(os.path.join(self.reporoot, f1), 'w') as f: f.write('new contents for file') self.repo.commit('edit note file') - self.scanner = scanner.Scanner(self.c) - r = scanner.RenoRepo(self.reporoot) - head = r.head() - parent = r.get_parents(head)[0] - parent = parent.decode('ascii') - contents = r.get_file_at_commit(f1, parent) + with scanner.RenoRepo(self.reporoot) as r: + head = r.head() + parent = r.get_parents(head)[0] + parent = parent.decode('ascii') + contents = r.get_file_at_commit(f1, parent) self.assertEqual( b'initial-contents', contents, @@ -830,8 +829,8 @@ class FileContentsTest(Base): f1 = self._add_notes_file(contents='initial-contents') with open(os.path.join(self.reporoot, f1), 'w') as f: f.write('new contents for file') - r = scanner.RenoRepo(self.reporoot) - contents = r.get_file_at_commit(f1, 'HEAD') + with scanner.RenoRepo(self.reporoot) as r: + contents = r.get_file_at_commit(f1, 'HEAD') self.assertEqual( b'initial-contents', contents, @@ -843,8 +842,8 @@ class FileContentsTest(Base): f1 = self._add_notes_file(contents='initial-contents') with open(os.path.join(self.reporoot, f1), 'w') as f: f.write('new contents for file') - r = scanner.RenoRepo(self.reporoot) - contents = r.get_file_at_commit(f1, None) + with scanner.RenoRepo(self.reporoot) as r: + contents = r.get_file_at_commit(f1, None) self.assertEqual( 'new contents for file', contents, @@ -858,8 +857,8 @@ class PreReleaseTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0a1') f1 = self._add_notes_file('slug1') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0a2') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -875,8 +874,8 @@ class PreReleaseTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0b1') f1 = self._add_notes_file('slug1') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0b2') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -892,8 +891,8 @@ class PreReleaseTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0rc1') f1 = self._add_notes_file('slug1') self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0.0rc2') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -909,8 +908,8 @@ class PreReleaseTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', 'v1.0.0.0a1') f1 = self._add_notes_file('slug1') self.repo.git('tag', '-s', '-m', 'first tag', 'v1.0.0.0a2') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -935,8 +934,8 @@ class PreReleaseTest(Base): self.c.override( collapse_pre_releases=True, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -958,8 +957,8 @@ class PreReleaseTest(Base): self.c.override( collapse_pre_releases=True, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -984,8 +983,8 @@ class PreReleaseTest(Base): self.c.override( collapse_pre_releases=True, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1015,8 +1014,8 @@ class MergeCommitTest(Base): time.sleep(0.1) # force a delay between commits self.repo.add_file('ignore-2.txt') self.repo.git('tag', '-s', '-m', 'second tag', '2.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1047,8 +1046,8 @@ class MergeCommitTest(Base): self.repo.git('show') self.repo.add_file('ignore-2.txt') self.repo.git('tag', '-s', '-m', 'second tag', '2.0.0') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1081,8 +1080,8 @@ class MergeCommitTest(Base): self.repo.add_file('ignore-2.txt') self.repo.git('tag', '-s', '-m', 'third tag', '2.0.0') self.repo.add_file('ignore-3.txt') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1119,8 +1118,8 @@ class MergeCommitTest(Base): self.repo.add_file('ignore-2.txt') self.repo.git('tag', '-s', '-m', 'third tag', '2.0.0') self.repo.add_file('ignore-3.txt') - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1181,8 +1180,8 @@ class NullMergeTest(Base): # The scanner should skip over the null-merge and include the # notes that come before the version being merged in, up to # the base of the previous branch. - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1200,8 +1199,8 @@ class NullMergeTest(Base): self.c.override( ignore_null_merges=False, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1245,6 +1244,10 @@ class BranchBaseTest(Base): self.repo.git('checkout', 'master') self.scanner = scanner.Scanner(self.c) + def tearDown(self): + self.scanner.close() + super().tearDown() + def test_current_branch_no_extra_commits(self): # checkout the branch and then ask for its base self.repo.git('checkout', 'not-master') @@ -1317,8 +1320,8 @@ class BranchTest(Base): f21 = self._add_notes_file('slug21') log_text = self.repo.git('log', '--decorate') self.addDetail('git log', text_content(log_text)) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1342,8 +1345,8 @@ class BranchTest(Base): self.c.override( branch='stable/2', ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1370,8 +1373,8 @@ class BranchTest(Base): self.c.override( stop_at_branch_base=False, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1407,8 +1410,8 @@ class BranchTest(Base): branch='stable/4', collapse_pre_releases=False, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1444,8 +1447,8 @@ class BranchTest(Base): branch='stable/4', collapse_pre_releases=True, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1482,8 +1485,8 @@ class BranchTest(Base): branch='stable/4', collapse_pre_releases=True, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1516,8 +1519,8 @@ class BranchTest(Base): self.c.override( branch='stable/4', ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1550,8 +1553,8 @@ class BranchTest(Base): self.c.override( branch='stable/4', ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1582,8 +1585,8 @@ class BranchTest(Base): self.c.override( branch='stable/4', ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1599,8 +1602,8 @@ class BranchTest(Base): self.repo.git('checkout', '2.0.0') self.repo.git('checkout', '-b', 'stable/2') self.repo.git('checkout', 'master') - scanner1 = scanner.Scanner(self.c) - head1 = scanner1._get_ref('stable/2') + with scanner.Scanner(self.c) as scanner1: + head1 = scanner1._get_ref('stable/2') self.assertIsNotNone(head1) print('head1', head1) # Create a second repository by cloning the first. @@ -1626,8 +1629,8 @@ class BranchTest(Base): cwd=reporoot2, )) c2 = config.Config(reporoot2) - scanner2 = scanner.Scanner(c2) - head2 = scanner2._get_ref('origin/stable/2') + with scanner.Scanner(c2) as scanner2: + head2 = scanner2._get_ref('origin/stable/2') self.assertIsNotNone(head2) self.assertEqual(head1, head2) @@ -1635,8 +1638,8 @@ class BranchTest(Base): self.repo.git('checkout', '2.0.0') self.repo.git('checkout', '-b', 'stable/2') self.repo.git('checkout', 'master') - scanner1 = scanner.Scanner(self.c) - head1 = scanner1._get_ref('stable/2') + with scanner.Scanner(self.c) as scanner1: + head1 = scanner1._get_ref('stable/2') self.assertIsNotNone(head1) print('head1', head1) # Create a second repository by cloning the first. @@ -1662,8 +1665,8 @@ class BranchTest(Base): cwd=reporoot2, )) c2 = config.Config(reporoot2) - scanner2 = scanner.Scanner(c2) - head2 = scanner2._get_ref('stable/2') + with scanner.Scanner(c2) as scanner2: + head2 = scanner2._get_ref('stable/2') self.assertIsNotNone(head2) self.assertEqual(head1, head2) @@ -1680,8 +1683,8 @@ class BranchTest(Base): self.c.override( earliest_version=None, ) - self.scanner = scanner.Scanner(self.c) - raw_results = self.scanner.get_notes_by_version() + with scanner.Scanner(self.c) as s: + raw_results = s.get_notes_by_version() results = { k: [f for (f, n) in v] for (k, v) in raw_results.items() @@ -1720,6 +1723,10 @@ class ScanStopPointPrereleaseVersionsTest(Base): self.repo.git('tag', '-s', '-m', 'third tag', '2.0.0') self.repo.git('checkout', 'master') + def tearDown(self): + self.scanner.close() + super().tearDown() + def test_beta_collapse(self): self.assertEqual( '1.0.0.0rc1', @@ -1808,6 +1815,10 @@ class ScanStopPointRegularVersionsTest(Base): self.repo.git('tag', '-s', '-m', 'third tag', '2.0.2') self.repo.git('checkout', 'master') + def tearDown(self): + self.scanner.close() + super().tearDown() + def test_invalid_earliest_version(self): self.assertIsNone( self.scanner._find_scan_stop_point( @@ -1850,45 +1861,43 @@ class GetRefTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '1.0.0') self.repo.git('branch', 'stable/foo') self.repo.git('tag', 'bar-eol') + self.scanner = scanner.Scanner(self.c) + + def tearDown(self): + self.scanner.close() + super().tearDown() def test_signed_tag(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref('1.0.0') expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_unsigned_tag(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref('bar-eol') expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_eol_tag_from_branch(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref('stable/bar') expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_head(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref(None) expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_stable_branch(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref('stable/foo') expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_stable_branch_with_origin_prefix(self): - self.scanner = scanner.Scanner(self.c) ref = self.scanner._get_ref('origin/stable/foo') expected = self.scanner._repo.head() self.assertEqual(expected, ref) def test_no_such_value(self): - self.scanner = scanner.Scanner(self.c) self.assertRaises( ValueError, self.scanner._get_ref, @@ -1909,17 +1918,17 @@ class TagsTest(Base): self.repo.git('tag', '-s', '-m', 'first tag', '3.0.0') def test_master(self): - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_tags_on_branch(None) + with scanner.Scanner(self.c) as s: + results = s._get_tags_on_branch(None) self.assertEqual( ['3.0.0', '2.0.0', '1.0.0'], results, ) def test_get_ref(self): - self.scanner = scanner.Scanner(self.c) - ref = self.scanner._get_ref('3.0.0') - expected = self.scanner._repo.head() + with scanner.Scanner(self.c) as s: + ref = s._get_ref('3.0.0') + expected = s._repo.head() self.assertEqual(expected, ref) def test_not_master(self): @@ -1928,8 +1937,8 @@ class TagsTest(Base): self._add_notes_file('slug4') self.repo.git('tag', '-s', '-m', 'not on master', '2.0.1') self.repo.git('checkout', 'master') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_tags_on_branch('not-master') + with scanner.Scanner(self.c) as s: + results = s._get_tags_on_branch('not-master') self.assertEqual( ['2.0.1', '2.0.0', '1.0.0'], results, @@ -1938,8 +1947,8 @@ class TagsTest(Base): def test_unsigned(self): self._add_notes_file('slug4') self.repo.git('tag', '-m', 'first tag', '4.0.0') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_tags_on_branch(None) + with scanner.Scanner(self.c) as s: + results = s._get_tags_on_branch(None) self.assertEqual( ['4.0.0', '3.0.0', '2.0.0', '1.0.0'], results, @@ -1948,8 +1957,8 @@ class TagsTest(Base): def test_tagged_tag_annotated(self): time.sleep(1) self.repo.git('tag', '-s', '-m', 'fourth tag', '4.0.0', '3.0.0') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_tags_on_branch(None) + with scanner.Scanner(self.c) as s: + results = s._get_tags_on_branch(None) self.assertEqual( ['3.0.0', '4.0.0', '2.0.0', '1.0.0'], results, @@ -1958,8 +1967,8 @@ class TagsTest(Base): def test_tagged_tag_lightweight(self): time.sleep(1) self.repo.git('tag', '-m', 'fourth tag', '4.0.0', '3.0.0') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_tags_on_branch(None) + with scanner.Scanner(self.c) as s: + results = s._get_tags_on_branch(None) self.assertEqual( ['3.0.0', '4.0.0', '2.0.0', '1.0.0'], results, @@ -1979,8 +1988,8 @@ class VersionTest(Base): self.repo.git('tag', '-s', '-m', 'third tag', '3.0.0') def test_tagged_head(self): - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_current_version(None) + with scanner.Scanner(self.c) as s: + results = s._get_current_version(None) self.assertEqual( '3.0.0', results, @@ -1988,8 +1997,8 @@ class VersionTest(Base): def test_head_after_tag(self): self._add_notes_file('slug4') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_current_version(None) + with scanner.Scanner(self.c) as s: + results = s._get_current_version(None) self.assertEqual( '3.0.0-1', results, @@ -2002,8 +2011,8 @@ class VersionTest(Base): # single second (certainly not a person). time.sleep(1) self.repo.git('tag', '-s', '-m', 'fourth tag', '4.0.0') - self.scanner = scanner.Scanner(self.c) - results = self.scanner._get_current_version(None) + with scanner.Scanner(self.c) as s: + results = s._get_current_version(None) self.assertEqual( '4.0.0', results, @@ -2452,11 +2461,9 @@ class GetSeriesBranchesTest(Base): self.repo.add_file('test.txt') def test_none(self): - self.scanner = scanner.Scanner(self.c) - self.assertEqual( - [], - self.scanner.get_series_branches(), - ) + with scanner.Scanner(self.c) as s: + branches = s.get_series_branches() + self.assertEqual([], branches) def test_real_branches_sorted_names(self): self.repo.git( @@ -2465,21 +2472,17 @@ class GetSeriesBranchesTest(Base): self.repo.git( 'checkout', '-b', 'stable/b', ) - self.scanner = scanner.Scanner(self.c) - self.assertEqual( - ['stable/a', 'stable/b'], - self.scanner.get_series_branches(), - ) + with scanner.Scanner(self.c) as s: + branches = s.get_series_branches() + self.assertEqual(['stable/a', 'stable/b'], branches) def test_eol_tag(self): self.repo.git( 'tag', '-s', '-m', 'closed branch', 'a-eol', ) - self.scanner = scanner.Scanner(self.c) - self.assertEqual( - ['stable/a'], - self.scanner.get_series_branches(), - ) + with scanner.Scanner(self.c) as s: + branches = s.get_series_branches() + self.assertEqual(['stable/a'], branches) def test_mix_tag_and_branch(self): self.repo.git( @@ -2488,8 +2491,6 @@ class GetSeriesBranchesTest(Base): self.repo.git( 'checkout', '-b', 'stable/b', ) - self.scanner = scanner.Scanner(self.c) - self.assertEqual( - ['stable/a', 'stable/b'], - self.scanner.get_series_branches(), - ) + with scanner.Scanner(self.c) as s: + branches = s.get_series_branches() + self.assertEqual(['stable/a', 'stable/b'], branches)