From 44eb0c0eec28dee4a7f901399eda019eeaab5250 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Wed, 24 Mar 2021 11:03:14 +0100 Subject: [PATCH] iter_tests(): new function Add a new generator function `iter_tests()`, which iterates over all tests that are defined in the git configuration. Refactor `cmd_list()` to use it. --- bin/git-test | 77 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/bin/git-test b/bin/git-test index 01910ed..2a1af9f 100755 --- a/bin/git-test +++ b/bin/git-test @@ -402,11 +402,11 @@ FAILURE! """ class Test(object): - def __init__(self, name): + def __init__(self, name, _command=None): self.name = name self.notes_ref = 'tests/%s' % (self.name,) self.full_ref = 'refs/notes/tests/%s' % (self.name,) - self._command = None + self._command = _command def initialize_status(self, msg): cmd = ['git', 'commit-tree', '-m', msg, get_empty_tree()] @@ -534,6 +534,44 @@ class Test(object): self.remove_status(msg) +test_config_re = re.compile(r'^test\.(?P.*)\.(?P[^\.]+)$') + +def iter_tests(): + """Iterate over all tests that are defined in the git configuration.""" + + cmd = ['git', 'config', '--get-regexp', '--null', '^test\.'] + out = check_output(cmd) + lines = [line for line in out.split('\0') if line] + + # A list of test names found (to preserve their order): + names = [] + # A map `{test_name : {subkey : value}}`: + tests = {} + + for line in lines: + (key, value) = line.split('\n', 1) + m = test_config_re.match(key) + + if m: + name = m.group('name') + subkey = m.group('subkey') + + test = tests.get(name) + if not test: + names.append(name) + test = tests[name] = {} + + if subkey == 'command': + tests.setdefault(name, {})['command'] = value + else: + # Unknown subkey. Ignore it. + pass + + for name in names: + test = tests[name] + yield Test(name, _command=test.get('command')) + + def prepare_revision(r): try: cmd = ['git', 'checkout', r] @@ -791,27 +829,24 @@ def cmd_forget_results(parser, options): test_re = re.compile(r'^test\.(?P.*)\.command$') def cmd_list(parser, options): - cmd = ['git', 'config', '--get-regexp', '--null', '^test\.'] - out = check_output(cmd) - lines = [line for line in out.split('\0') if line] - for line in lines: - (key, value) = line.split('\n', 1) - m = test_re.match(key) - if m: - name = m.group('name') - command = value.rstrip() - command_lines = command.split('\n') + for test in iter_tests(): + command = test.command + if command is None: + continue - if not command_lines: - continue + command = command.rstrip() + command_lines = command.split('\n') - print('%s:' % (name,)) - if len(command_lines) == 1: - print(' command = %s' % (command,)) - else: - print(' command:') - for command_line in command_lines: - print(' %s' % (command_line,)) + if not command_lines: + continue + + print('%s:' % (test.name,)) + if len(command_lines) == 1: + print(' command = %s' % (command,)) + else: + print(' command:') + for command_line in command_lines: + print(' %s' % (command_line,)) def cmd_remove(parser, options):