Merge pull request #557 from ddddddddwp/fix/semantic-parallel-initializer

Why merged: a package initializer no longer runs twice, and a semantic run in which no test ran no longer counts as a pass.
Why it's good: on main 1c93c86: runner tests 17 -> 19; the full semantic run with the new runner still selects 465 files / 6108 tests; repo-consistency set 563 -> 565 (its own new tests), 0 failures.

AI-assisted (Claude Code, Claude Opus 5.5).
This commit is contained in:
QemmHD
2026-11-24 03:54:16 -05:00
committed by GitHub
2 changed files with 38 additions and 3 deletions

View File

@ -16,7 +16,7 @@ It runs the files `python -m unittest discover -s tests/semantic` finds (`test*.
`python -m unittest <module>` from that folder, so a file sees the same import path as under discover. The files can
run side by side because each one writes only into its own temporary folder; a new test must do the same. With -j 1,
or on a single core, it runs that discover command itself. Prints the output of every failing file and one summary
line, and exits 1 if a file fails or none is found. Standard library only.
line, and exits 1 if a file fails, none is found, or no test ran. Standard library only.
`--timeout SECONDS` limits each module subprocess in parallel mode, or the entire discover subprocess with
`-j 1`. The value must be positive and at most 86400 seconds (24 hours), keeping it within platform wait limits.
@ -74,8 +74,12 @@ def positive_seconds(value):
def run_module(directory, module, verbose, timeout=None):
"""(return code, tests run, output) of `python -m unittest <module>` run from `directory`."""
cmd = [sys.executable, "-m", "unittest", *(["-v"] if verbose else []), module]
"""(return code, tests run, output) of `python -m unittest <target>` run from `directory`."""
# `python -m unittest pkg.__init__` imports pkg and then runs the same file again as pkg.__init__, so a
# registration in the initializer happens twice. Running the package itself imports it once; module_path() is
# still the initializer's path, so the jobs stay sorted by the file they run.
target = module[:-len(".__init__")] if module.endswith(".__init__") else module
cmd = [sys.executable, "-m", "unittest", *(["-v"] if verbose else []), target]
try:
p = subprocess.run(cmd, cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
errors="replace", timeout=timeout)
@ -132,6 +136,11 @@ def main(argv=None):
took = time.monotonic() - start
summary = f"{len(modules)} files, {total} tests, {len(failed)} files failed in {took:.0f} s with {a.jobs} jobs"
print(summary + (": " + ", ".join(sorted(failed)) if failed else ""))
if total == 0:
# An empty package initializer is a job that legitimately runs no tests; a run where every job did is not a
# pass (discover, which -j 1 forwards to, exits 5 for it).
print(f"no tests ran in {directory}")
return 1
return 1 if failed else 0

View File

@ -175,6 +175,32 @@ class RunnerTest(unittest.TestCase):
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("init-boom", result.stdout + result.stderr)
def test_package_initializer_runs_once_in_parallel(self):
# `python -m unittest pkg.__init__` imports pkg and then runs the same file again as pkg.__init__, so this
# registration happens twice; the runner hands unittest the package name instead.
init = ("import unittest\n"
"registrations = []\n"
"registrations.append(__name__)\n"
"class T(unittest.TestCase):\n"
" def test_once(self):\n"
" self.assertEqual(registrations, ['pkg'])\n")
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"pkg/__init__.py": init})
result = cli(tmp, "-j", str(jobs))
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_empty_package_alone_is_not_a_false_green(self):
# -j 1 forwards to discover, which exits 5 when nothing ran; a parallel run whose only job is an empty
# initializer must not report success either.
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"pkg/__init__.py": ""})
result = cli(tmp, "-j", str(jobs))
self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
if jobs == 2:
self.assertIn("no tests ran", result.stdout)
def test_all_pass(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_p1.py": PASS, "test_p2.py": PASS})