Skip to content

Commit 64bb32d

Browse files
committed
tests/run-tests: Add composable --include and --exclude options.
The idea that --list-tests would be enough to produce list of tests for tinytest-codegen didn't work, because normal run-tests processing heavily relies on dynamic target capabilities discovery, and test filtering happens as the result of that. So, approach the issue from different end - allow to specify arbitrary filtering criteria as run-tests arguments. This way, specific filters will be still hardcoded, but at least on a particular target's side, instead of constant patching tinytest-codegen and/or run-tests.
1 parent aaeb70b commit 64bb32d

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

tests/run-tests

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ def run_tests(pyb, tests, args, base_path="."):
358358

359359
for test_file in tests:
360360
test_file = test_file.replace('\\', '/')
361+
362+
if args.filters:
363+
# Default verdict is the opposit of the first action
364+
verdict = "include" if args.filters[0][0] == "exclude" else "exclude"
365+
for action, pat in args.filters:
366+
if pat.search(test_file):
367+
verdict = action
368+
if verdict == "exclude":
369+
continue
370+
361371
test_basename = os.path.basename(test_file)
362372
test_name = os.path.splitext(test_basename)[0]
363373
is_native = test_name.startswith("native_") or test_name.startswith("viper_")
@@ -451,14 +461,42 @@ def run_tests(pyb, tests, args, base_path="."):
451461
# all tests succeeded
452462
return True
453463

464+
465+
class append_filter(argparse.Action):
466+
467+
def __init__(self, option_strings, dest, **kwargs):
468+
super().__init__(option_strings, dest, default=[], **kwargs)
469+
470+
def __call__(self, parser, args, value, option):
471+
if not hasattr(args, self.dest):
472+
args.filters = []
473+
if option.startswith(("-e", "--e")):
474+
option = "exclude"
475+
else:
476+
option = "include"
477+
args.filters.append((option, re.compile(value)))
478+
479+
454480
def main():
455-
cmd_parser = argparse.ArgumentParser(description='Run tests for MicroPython.')
481+
cmd_parser = argparse.ArgumentParser(
482+
formatter_class=argparse.RawDescriptionHelpFormatter,
483+
description='Run and manage tests for MicroPython.',
484+
epilog='''\
485+
Options -i and -e can be multiple and processed in the order given. Regex
486+
"search" (vs "match") operation is used. An action (include/exclude) of
487+
the last matching regex is used:
488+
run-tests -i async - exclude all, then include tests containg "async" anywhere
489+
run-tests -e '/big.+int' - include all, then exclude by regex
490+
run-tests -e async -i async_foo - include all, exclude async, yet still include async_foo
491+
''')
456492
cmd_parser.add_argument('--target', default='unix', help='the target platform')
457493
cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device or the IP address of the pyboard')
458494
cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device')
459495
cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username')
460496
cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password')
461497
cmd_parser.add_argument('-d', '--test-dirs', nargs='*', help='input test directories (if no files given)')
498+
cmd_parser.add_argument('-e', '--exclude', action=append_filter, metavar='REGEX', dest='filters', help='exclude test by regex on path/name.py')
499+
cmd_parser.add_argument('-i', '--include', action=append_filter, metavar='REGEX', dest='filters', help='include test by regex on path/name.py')
462500
cmd_parser.add_argument('--write-exp', action='store_true', help='save .exp files to run tests w/o CPython')
463501
cmd_parser.add_argument('--list-tests', action='store_true', help='list tests instead of running them')
464502
cmd_parser.add_argument('--emit', default='bytecode', help='MicroPython emitter to use (bytecode or native)')

0 commit comments

Comments
 (0)