Skip to content

Commit 21048cc

Browse files
authored
Merge pull request #19 from mhagger/iter-tests
Add a new `iter_tests()` function
2 parents e828da7 + 44eb0c0 commit 21048cc

1 file changed

Lines changed: 56 additions & 21 deletions

File tree

bin/git-test

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ FAILURE!
402402
"""
403403

404404
class Test(object):
405-
def __init__(self, name):
405+
def __init__(self, name, _command=None):
406406
self.name = name
407407
self.notes_ref = 'tests/%s' % (self.name,)
408408
self.full_ref = 'refs/notes/tests/%s' % (self.name,)
409-
self._command = None
409+
self._command = _command
410410

411411
def initialize_status(self, msg):
412412
cmd = ['git', 'commit-tree', '-m', msg, get_empty_tree()]
@@ -534,6 +534,44 @@ class Test(object):
534534
self.remove_status(msg)
535535

536536

537+
test_config_re = re.compile(r'^test\.(?P<name>.*)\.(?P<subkey>[^\.]+)$')
538+
539+
def iter_tests():
540+
"""Iterate over all tests that are defined in the git configuration."""
541+
542+
cmd = ['git', 'config', '--get-regexp', '--null', '^test\.']
543+
out = check_output(cmd)
544+
lines = [line for line in out.split('\0') if line]
545+
546+
# A list of test names found (to preserve their order):
547+
names = []
548+
# A map `{test_name : {subkey : value}}`:
549+
tests = {}
550+
551+
for line in lines:
552+
(key, value) = line.split('\n', 1)
553+
m = test_config_re.match(key)
554+
555+
if m:
556+
name = m.group('name')
557+
subkey = m.group('subkey')
558+
559+
test = tests.get(name)
560+
if not test:
561+
names.append(name)
562+
test = tests[name] = {}
563+
564+
if subkey == 'command':
565+
tests.setdefault(name, {})['command'] = value
566+
else:
567+
# Unknown subkey. Ignore it.
568+
pass
569+
570+
for name in names:
571+
test = tests[name]
572+
yield Test(name, _command=test.get('command'))
573+
574+
537575
def prepare_revision(r):
538576
try:
539577
cmd = ['git', 'checkout', r]
@@ -791,27 +829,24 @@ def cmd_forget_results(parser, options):
791829
test_re = re.compile(r'^test\.(?P<name>.*)\.command$')
792830

793831
def cmd_list(parser, options):
794-
cmd = ['git', 'config', '--get-regexp', '--null', '^test\.']
795-
out = check_output(cmd)
796-
lines = [line for line in out.split('\0') if line]
797-
for line in lines:
798-
(key, value) = line.split('\n', 1)
799-
m = test_re.match(key)
800-
if m:
801-
name = m.group('name')
802-
command = value.rstrip()
803-
command_lines = command.split('\n')
832+
for test in iter_tests():
833+
command = test.command
834+
if command is None:
835+
continue
804836

805-
if not command_lines:
806-
continue
837+
command = command.rstrip()
838+
command_lines = command.split('\n')
807839

808-
print('%s:' % (name,))
809-
if len(command_lines) == 1:
810-
print(' command = %s' % (command,))
811-
else:
812-
print(' command:')
813-
for command_line in command_lines:
814-
print(' %s' % (command_line,))
840+
if not command_lines:
841+
continue
842+
843+
print('%s:' % (test.name,))
844+
if len(command_lines) == 1:
845+
print(' command = %s' % (command,))
846+
else:
847+
print(' command:')
848+
for command_line in command_lines:
849+
print(' %s' % (command_line,))
815850

816851

817852
def cmd_remove(parser, options):

0 commit comments

Comments
 (0)