|
| 1 | +# Extension for "unittest" that adds the ability to run via "micropython -m unittest". |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from fnmatch import fnmatch |
| 7 | +from micropython import const |
| 8 | + |
| 9 | +from unittest import TestRunner, TestResult, TestSuite |
| 10 | + |
| 11 | + |
| 12 | +# Run a single test in a clean environment. |
| 13 | +def _run_test_module(runner: TestRunner, module_name: str, *extra_paths: list[str]): |
| 14 | + module_snapshot = {k: v for k, v in sys.modules.items()} |
| 15 | + path_snapshot = sys.path[:] |
| 16 | + try: |
| 17 | + for path in reversed(extra_paths): |
| 18 | + if path: |
| 19 | + sys.path.insert(0, path) |
| 20 | + |
| 21 | + module = __import__(module_name) |
| 22 | + suite = TestSuite(module_name) |
| 23 | + suite._load_module(module) |
| 24 | + return runner.run(suite) |
| 25 | + finally: |
| 26 | + sys.path[:] = path_snapshot |
| 27 | + sys.modules.clear() |
| 28 | + sys.modules.update(module_snapshot) |
| 29 | + |
| 30 | + |
| 31 | +_DIR_TYPE = const(0x4000) |
| 32 | + |
| 33 | + |
| 34 | +def _run_all_in_dir(runner: TestRunner, path: str, pattern: str, top: str): |
| 35 | + result = TestResult() |
| 36 | + for fname, ftype, *_ in os.ilistdir(path): |
| 37 | + if fname in ("..", "."): |
| 38 | + continue |
| 39 | + if ftype == _DIR_TYPE: |
| 40 | + result += _run_all_in_dir( |
| 41 | + runner=runner, |
| 42 | + path="/".join((path, fname)), |
| 43 | + pattern=pattern, |
| 44 | + top=top, |
| 45 | + ) |
| 46 | + if fnmatch(fname, pattern): |
| 47 | + module_name = fname.rsplit(".", 1)[0] |
| 48 | + result += _run_test_module(runner, module_name, path, top) |
| 49 | + return result |
| 50 | + |
| 51 | + |
| 52 | +# Implements discovery inspired by https://docs.python.org/3/library/unittest.html#test-discovery |
| 53 | +def _discover(runner: TestRunner): |
| 54 | + parser = argparse.ArgumentParser() |
| 55 | + # parser.add_argument( |
| 56 | + # "-v", |
| 57 | + # "--verbose", |
| 58 | + # action="store_true", |
| 59 | + # help="Verbose output", |
| 60 | + # ) |
| 61 | + parser.add_argument( |
| 62 | + "-s", |
| 63 | + "--start-directory", |
| 64 | + dest="start", |
| 65 | + default=".", |
| 66 | + help="Directory to start discovery", |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + "-p", |
| 70 | + "--pattern ", |
| 71 | + dest="pattern", |
| 72 | + default="test*.py", |
| 73 | + help="Pattern to match test files", |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "-t", |
| 77 | + "--top-level-directory", |
| 78 | + dest="top", |
| 79 | + help="Top level directory of project (defaults to start directory)", |
| 80 | + ) |
| 81 | + args = parser.parse_args(args=sys.argv[2:]) |
| 82 | + |
| 83 | + path = args.start |
| 84 | + top = args.top or path |
| 85 | + |
| 86 | + return _run_all_in_dir( |
| 87 | + runner=runner, |
| 88 | + path=path, |
| 89 | + pattern=args.pattern, |
| 90 | + top=top, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +# Could make this require os.path. |
| 95 | +PATH_SEP = getattr(os, "sep", "/") |
| 96 | + |
| 97 | + |
| 98 | +# foo/bar/x.y.z --> foo/bar, x.y |
| 99 | +def _dirname_filename_no_ext(path): |
| 100 | + split = path.rsplit(PATH_SEP, 1) |
| 101 | + if len(split) == 1: |
| 102 | + dirname, filename = "", split[0] |
| 103 | + else: |
| 104 | + dirname, filename = split |
| 105 | + return dirname.replace(PATH_SEP, "/"), filename.rsplit(".", 1)[0] |
| 106 | + |
| 107 | + |
| 108 | +# This is called from unittest when __name__ == "__main__". |
| 109 | +def discover_main(): |
| 110 | + failures = 0 |
| 111 | + runner = TestRunner() |
| 112 | + |
| 113 | + if len(sys.argv) == 1 or ( |
| 114 | + len(sys.argv) >= 2 |
| 115 | + and _dirname_filename_no_ext(sys.argv[0])[1] == "unittest" |
| 116 | + and sys.argv[1] == "discover" |
| 117 | + ): |
| 118 | + # No args, or `python -m unittest discover ...`. |
| 119 | + result = _discover(runner) |
| 120 | + failures += result.failuresNum or result.errorsNum |
| 121 | + else: |
| 122 | + for test_spec in sys.argv[1:]: |
| 123 | + try: |
| 124 | + os.stat(test_spec) |
| 125 | + # File exists, strip extension and import with its parent directory in sys.path. |
| 126 | + dirname, module_name = _dirname_filename_no_ext(test_spec) |
| 127 | + result = _run_test_module(runner, module_name, dirname) |
| 128 | + except OSError: |
| 129 | + # Not a file, treat as named module to import. |
| 130 | + result = _run_test_module(runner, test_spec) |
| 131 | + |
| 132 | + failures += result.failuresNum or result.errorsNum |
| 133 | + |
| 134 | + # Terminate with non zero return code in case of failures. |
| 135 | + sys.exit(failures) |
0 commit comments