diff options
| author | bobcatchris <bobcatchris@users.noreply.github.com> | 2026-11-23 23:59:23 -0600 |
|---|---|---|
| committer | bobcatchris <83428450+bobcatchris@users.noreply.github.com> | 2026-11-24 00:37:28 -0600 |
| commit | b45d7c2d14e5a4fd0cf11e72852d79d62df417a5 (patch) | |
| tree | 453dea79330aebadad4f2c123f8bc5169d72cc06 /scripts | |
| parent | c023a365a93e5e3a75290ab70f68c8da4a227700 (diff) | |
SEMANTIC-PACKAGE-TESTS: run package initializers in parallel
unittest discover loads TestCase classes defined in a package's __init__.py regardless of the filename pattern. The parallel runner only selected test*.py files, so a failing package initializer could be skipped while -j 1 reported it. Include package initializers in the parallel module list; an empty initializer with no tests is not a failure. Add regressions for failing initializers and for a pattern that matches no test filenames.
AI-assisted.
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/run_semantic_tests.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/scripts/run_semantic_tests.py b/scripts/run_semantic_tests.py index 29382d5d2..7a1b604a2 100644 --- a/scripts/run_semantic_tests.py +++ b/scripts/run_semantic_tests.py | |||
| @@ -8,7 +8,9 @@ r"""Run the translator regression suite (tests/semantic) one process per test fi | |||
| 8 | 8 | ||
| 9 | `-p` / `--pattern` selects test filenames using unittest discover's glob syntax (default `test*.py`). Quote the | 9 | `-p` / `--pattern` selects test filenames using unittest discover's glob syntax (default `test*.py`). Quote the |
| 10 | pattern so the shell does not expand it. Only Python module filenames are selected, including inside packages; | 10 | pattern so the shell does not expand it. Only Python module filenames are selected, including inside packages; |
| 11 | the pattern matches their basenames, not directory paths. No matching files is an error in serial and parallel runs. | 11 | the pattern matches their basenames, not directory paths. Package `__init__.py` modules are always included, |
| 12 | because discover imports them and runs any TestCase they define regardless of the pattern. No matching files is an | ||
| 13 | error in serial and parallel runs. | ||
| 12 | 14 | ||
| 13 | It runs the files `python -m unittest discover -s tests/semantic` finds (`test*.py` by default, and packages below it), each as | 15 | It runs the files `python -m unittest discover -s tests/semantic` finds (`test*.py` by default, and packages below it), each as |
| 14 | `python -m unittest <module>` from that folder, so a file sees the same import path as under discover. The files can | 16 | `python -m unittest <module>` from that folder, so a file sees the same import path as under discover. The files can |
| @@ -35,15 +37,31 @@ import time | |||
| 35 | MAX_TIMEOUT_SECONDS = 24 * 60 * 60 | 37 | MAX_TIMEOUT_SECONDS = 24 * 60 * 60 |
| 36 | 38 | ||
| 37 | 39 | ||
| 40 | def module_path(directory, module): | ||
| 41 | """Source path of a dotted module name, including a package's `__init__.py`.""" | ||
| 42 | parts = module.split(".") | ||
| 43 | if parts[-1] == "__init__": | ||
| 44 | return os.path.join(directory, *parts[:-1], "__init__.py") | ||
| 45 | return os.path.join(directory, *parts) + ".py" | ||
| 46 | |||
| 47 | |||
| 38 | def test_modules(directory, pattern="test*.py"): | 48 | def test_modules(directory, pattern="test*.py"): |
| 39 | """Dotted names of the test modules unittest discover finds under `directory` (it enters packages only).""" | 49 | """Dotted names of the test modules unittest discover finds under `directory` (it enters packages only).""" |
| 40 | names = [] | 50 | names = [] |
| 41 | for base, dirs, files in os.walk(directory): | 51 | for base, dirs, files in os.walk(directory): |
| 42 | dirs[:] = sorted(d for d in dirs if os.path.isfile(os.path.join(base, d, "__init__.py"))) | 52 | dirs[:] = sorted(d for d in dirs if os.path.isfile(os.path.join(base, d, "__init__.py"))) |
| 43 | prefix = os.path.relpath(base, directory).replace(os.sep, ".") | 53 | prefix = os.path.relpath(base, directory).replace(os.sep, ".") |
| 54 | # Discover imports every package below its start folder even when no filename in it matches the pattern, | ||
| 55 | # and loads TestCase classes defined in the package initializer. Parallel runs must see those too. | ||
| 56 | if prefix != "." and "__init__.py" in files: | ||
| 57 | init = f"{prefix}.__init__" | ||
| 58 | if init not in names: | ||
| 59 | names.append(init) | ||
| 44 | for name in sorted(files): | 60 | for name in sorted(files): |
| 45 | if re.fullmatch(r"[_a-z]\w*\.py", name, re.IGNORECASE) and fnmatch.fnmatch(name, pattern): | 61 | if re.fullmatch(r"[_a-z]\w*\.py", name, re.IGNORECASE) and fnmatch.fnmatch(name, pattern): |
| 46 | names.append(name[:-3] if prefix == "." else f"{prefix}.{name[:-3]}") | 62 | module = name[:-3] if prefix == "." else f"{prefix}.{name[:-3]}" |
| 63 | if module not in names: | ||
| 64 | names.append(module) | ||
| 47 | return names | 65 | return names |
| 48 | 66 | ||
| 49 | 67 | ||
| @@ -67,7 +85,12 @@ def run_module(directory, module, verbose, timeout=None): | |||
| 67 | out = out.decode(locale.getpreferredencoding(False), errors="replace") | 85 | out = out.decode(locale.getpreferredencoding(False), errors="replace") |
| 68 | return 1, 0, out + f"\nTIMED OUT after {timeout:g} s: {module}\n" | 86 | return 1, 0, out + f"\nTIMED OUT after {timeout:g} s: {module}\n" |
| 69 | ran = re.search(r"^Ran (\d+) tests? in", p.stdout, re.M) | 87 | ran = re.search(r"^Ran (\d+) tests? in", p.stdout, re.M) |
| 70 | return p.returncode, int(ran.group(1)) if ran else 0, p.stdout | 88 | count = int(ran.group(1)) if ran else 0 |
| 89 | # Discover imports every package initializer, but an empty one has no tests to run. It is included in the | ||
| 90 | # module list so parallel runs see TestCase classes when there are any; without them, it is not a failure. | ||
| 91 | if p.returncode == 5 and count == 0 and module.endswith(".__init__") and "NO TESTS RAN" in p.stdout: | ||
| 92 | return 0, 0, p.stdout | ||
| 93 | return p.returncode, count, p.stdout | ||
| 71 | 94 | ||
| 72 | 95 | ||
| 73 | def main(argv=None): | 96 | def main(argv=None): |
| @@ -94,7 +117,7 @@ def main(argv=None): | |||
| 94 | print(f"TIMED OUT after {a.timeout:g} s: unittest discover in {directory}", file=sys.stderr) | 117 | print(f"TIMED OUT after {a.timeout:g} s: unittest discover in {directory}", file=sys.stderr) |
| 95 | return 1 | 118 | return 1 |
| 96 | # Largest files first: they tend to run longest, and one started last would set the wall time alone. | 119 | # Largest files first: they tend to run longest, and one started last would set the wall time alone. |
| 97 | modules.sort(key=lambda m: -os.path.getsize(os.path.join(directory, *m.split(".")) + ".py")) | 120 | modules.sort(key=lambda m: -os.path.getsize(module_path(directory, m))) |
| 98 | start, total, failed = time.monotonic(), 0, [] | 121 | start, total, failed = time.monotonic(), 0, [] |
| 99 | with concurrent.futures.ThreadPoolExecutor(a.jobs) as pool: | 122 | with concurrent.futures.ThreadPoolExecutor(a.jobs) as pool: |
| 100 | jobs = {pool.submit(run_module, directory, m, a.verbose, a.timeout): m for m in modules} | 123 | jobs = {pool.submit(run_module, directory, m, a.verbose, a.timeout): m for m in modules} |