Fail semantic runs without a completed unittest verdict

Retain process tracking and interrupt/timeout handling from main.
This commit is contained in:
titoreinaldo
2026-11-27 23:41:32 +00:00
parent d442851972
commit 13127c76db

View File

@ -18,7 +18,8 @@ The files can run side by side because each one writes only into its own tempora
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, none is found, or no test ran. Standard library only.
The count of tests comes from unittest's own result object, not from its output: a module that writes a line shaped like unittest's summary to stderr - a nested runner, or an atexit handler that runs after unittest's own summary - cannot change it.
The test count and verdict come from unittest's result, not from printed summaries. A child exiting with status 0
without a completed result, or after unittest reported failure, fails the run.
`--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.
@ -94,13 +95,10 @@ CHILD_PROGRAM = (
"import json, os, sys, unittest\n"
"program = unittest.TestProgram(module=None, argv=['unittest'] + sys.argv[1:], exit=False)\n"
"result = program.result\n"
"with open(os.environ['LEONIDA_TEST_COUNT'], 'w', encoding='utf-8') as handle:\n"
" json.dump({'testsRun': result.testsRun}, handle)\n"
"if not result.wasSuccessful():\n"
" sys.exit(1)\n"
"elif result.testsRun == 0 and len(result.skipped) == 0:\n"
" sys.exit(5)\n"
"sys.exit(0)\n")
"code = 1 if not result.wasSuccessful() else 5 if not result.testsRun and not result.skipped else 0\n"
"with open(os.environ['LEONIDA_TEST_COUNT'], 'w', encoding='utf-8', newline='\\n') as handle:\n"
" json.dump({'testsRun': result.testsRun, 'exitCode': code}, handle)\n"
"sys.exit(code)\n")
def module_path(directory, module):
@ -139,14 +137,64 @@ def positive_seconds(value):
return seconds
def read_count(path):
"""The number of tests the child ran, as unittest's own result object reported it."""
def read_result(path, process_code):
"""Return the effective exit code, completed test count, and any diagnostic."""
try:
with open(path, encoding="utf-8") as handle:
return int(json.load(handle)["testsRun"])
result = json.load(handle)
count, test_code = result["testsRun"], result["exitCode"]
if type(count) is not int or count < 0 or type(test_code) is not int or test_code not in (0, 1, 5):
raise ValueError("invalid unittest result")
except (OSError, ValueError, KeyError, TypeError):
# The child wrote no result: it did not finish, so no test is known to have run.
return 0
if process_code == 0:
return 1, 0, "process exited without a unittest result"
return process_code, None, ""
if process_code in (0, 5) and process_code != test_code:
return 1, count, f"process exited with status {process_code} but unittest reported {test_code}"
return process_code, count, ""
def result_environment(scratch):
result_path = os.path.join(scratch, "count.json")
return result_path, dict(os.environ, LEONIDA_TEST_COUNT=result_path)
def run_discover(directory, pattern, verbose, timeout):
args = ["discover", "-s", directory, "-p", pattern, *(["-v"] if verbose else [])]
with tempfile.TemporaryDirectory() as scratch:
path, env = result_environment(scratch)
proc = subprocess.Popen([sys.executable, "-c", CHILD_PROGRAM, *args], env=env)
_register_process(proc)
start_time = time.monotonic()
try:
while True:
try:
code = proc.wait(timeout=0.2)
break
except subprocess.TimeoutExpired:
if timeout is not None and time.monotonic() - start_time >= timeout:
raise
except subprocess.TimeoutExpired:
_kill_process_tree(proc)
print(f"TIMED OUT after {timeout:g} s: unittest discover in {directory}", file=sys.stderr)
return 1
except KeyboardInterrupt:
_kill_process_tree(proc)
try:
proc.wait(timeout=0.5)
except (subprocess.TimeoutExpired, OSError):
pass
print("interrupted", file=sys.stderr)
return 130
except BaseException:
_kill_process_tree(proc)
raise
finally:
_unregister_process(proc)
code, _, diagnostic = read_result(path, code)
if diagnostic:
print(f"unittest discover in {directory}: {diagnostic}", file=sys.stderr)
return code
def run_module(directory, module, verbose, timeout=None):
@ -157,8 +205,7 @@ def run_module(directory, module, verbose, timeout=None):
target = module[:-len(".__init__")] if module.endswith(".__init__") else module
cmd = [sys.executable, "-c", CHILD_PROGRAM, *(["-v"] if verbose else []), target]
with tempfile.TemporaryDirectory() as scratch:
count_path = os.path.join(scratch, "count.json")
env = dict(os.environ, LEONIDA_TEST_COUNT=count_path)
result_path, env = result_environment(scratch)
proc = subprocess.Popen(cmd, cwd=directory, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, errors="replace")
_register_process(proc)
@ -181,13 +228,14 @@ def run_module(directory, module, verbose, timeout=None):
raise
finally:
_unregister_process(proc)
count = read_count(count_path)
code, count, diagnostic = read_result(result_path, proc.returncode)
out = stdout + stderr
# Discover imports every package initializer, but an empty one has no tests to run. It is included in the
# module list so parallel runs see TestCase classes when there are any; without them, it is not a failure.
if proc.returncode == 5 and count == 0 and module.endswith(".__init__") and "NO TESTS RAN" in stderr:
if diagnostic:
out += f"\n{diagnostic}\n"
# A package initializer that completed with no tests isn't a failed module.
if code == 5 and count == 0 and module.endswith(".__init__"):
return 0, 0, out
return proc.returncode, count, out
return code, count or 0, out
def main(argv=None):
@ -206,35 +254,7 @@ def main(argv=None):
print(f"no {a.pattern} files in {directory}")
return 1
if a.jobs <= 1:
cmd = [sys.executable, "-m", "unittest", "discover", "-s", directory,
"-p", a.pattern, *(["-v"] if a.verbose else [])]
proc = subprocess.Popen(cmd)
_register_process(proc)
start_time = time.monotonic()
try:
while True:
try:
return proc.wait(timeout=0.2)
except subprocess.TimeoutExpired:
if a.timeout is not None and time.monotonic() - start_time >= a.timeout:
raise
except subprocess.TimeoutExpired:
_kill_process_tree(proc)
print(f"TIMED OUT after {a.timeout:g} s: unittest discover in {directory}", file=sys.stderr)
return 1
except KeyboardInterrupt:
_kill_process_tree(proc)
try:
proc.wait(timeout=0.5)
except (subprocess.TimeoutExpired, OSError):
pass
print("interrupted", file=sys.stderr)
return 130
except BaseException:
_kill_process_tree(proc)
raise
finally:
_unregister_process(proc)
return run_discover(directory, a.pattern, a.verbose, a.timeout)
# Largest files first: they tend to run longest, and one started last would set the wall time alone.
modules.sort(key=lambda m: -os.path.getsize(module_path(directory, m)))
start, total, failed = time.monotonic(), 0, []