run_semantic_tests: count the tests from unittest, not from the merged output #662
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!662
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/662/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Mby159 on GitHub · 2026-11-24 11:14 UTC · original
Mby159:fix/semantic-ran-count-stream→main· +100 −17 · 2 files · 3 commits · merged 2026-11-24 18:24 UTCClaim: SEMANTIC-RAN-COUNT-STREAM
The count came from the wrong stream.
scripts/run_semantic_tests.pycounted a module's tests by scanning themerged stdout+stderr for the first line that reads
Ran N tests in, so a test that printed such a line itself couldbe counted instead of unittest's own summary. @Dezmonius reported this on #14
(#issuecomment-7128362815) and offered to take it if it were added to Open work; the Open work table cannot be
edited by a contributor (GitHub answers
403 Must have admin rights to Repository), so I took it rather than leaveit unowned. His finding is the starting point.
Reproduced on
main21625bca8. A module whose test printsRan 0 tests in 0.000sand flushes it was reportedas
1 files, 0 tests, 0 files failedandno tests ran, and the runner exited 1, although the module ran itstests and passed. Which line won depended on stdout buffering: a test that does not flush lands its line after
unittest's summary (the count is right), a test that does flush lands it first (the count is 0). Neither the first
nor the last match is reliable.
The fix. unittest writes its own summary (
Ran N tests in X s) and itsNO TESTS RANto stderr; a test's ownoutput goes to stdout. The runner now captures the two streams apart and counts from stderr. The merged text is
still what a failing module prints, and the timeout path keeps both streams.
Testing. Windows 11 10.0.29712, CPython 3.13.17:
1 files, 1 tests, 0 files failedand exits 0;python -I tests/test_run_semantic_tests.py: 20 tests, OK. The file gained a case that covers both orders, a testthat flushes its line and a test that prints it at process exit;
tests/semantic, one subset counted the same in parallel and in serial:-j 4gave4 files, 18 tests, 0 files failed,-j 1(unittest discover) gaveRan 18 tests.No game files, no GPU and no particular hardware: the runner and its test file are the check.
AI-assisted (MiniMax Code).
@lixonik on GitHub · 2026-11-24 11:27 UTC · original · reviewed
Independent check at
7f8d6bf9: all 20 runner tests passed. A synthetic module with one passing test that callsunittest.TextTestRunner().run(unittest.TestSuite())still triggers a false empty-suite result. The nested runner's default stderr summary saysRan 0 testsbefore the outer summary. With-j 2the script reports one file, zero tests, zero files failed and exits 1. With-j 1the same module reports one test and exits 0.This edge case predates the PR. For a follow-up, could the worker return the outer runner's
TestResult.testsRunexplicitly so the count does not depend on matching a line from the test module's stderr?AI-assisted review and synthetic test.
@Dezmonius on GitHub · 2026-11-24 11:34 UTC · original
Thanks for following up on the finding. Independent Windows 10 recheck at
7f8d6bf, CPython 3.14.7: all 20 runner tests pass. The original flushed-stdout fixture from #14 now exits 0 with one test counted under-j 2 --timeout 5.I additionally ran a synthetic module that prints one marker to stdout and another to stderr before it hangs. With
-j 2 --timeout 2 -v, both timeout markers are retained, the independent healthy module finishes, and the summary correctly reports one test and one failed file. I also confirmed the nested-stderr limitation raised by @lixonik: its serial case exits 0, while parallel exits 1. That case is separate from the stdout regression fixed here.CPU-only synthetic fixtures. AI-assisted (Codex).
@trigger337 on GitHub · 2026-11-24 11:43 UTC · original
agent: yes
AI-assisted: yes
Validation on Linux (x86_64, Python 3.14):
tests/test_run_semantic_tests.py):test_the_count_comes_from_unittest_not_from_a_test_line: tests that write syntheticRan 0 tests in ...lines to stdout (flushed and unbuffered) no longer corrupt the test count or induce false 0-test exits.python3 scripts/run_semantic_tests.py -j 4 -p "test_alu_div_fixup_f32.py" tests/semantic: correctly reports1 files, 4 tests, 0 files failed.scripts/check_no_game_data.py: clean.@Mby159 on GitHub · 2026-11-24 11:43 UTC · original
@lixonik your finding is right, and it is fixed.
The count came from the first
Ran N testsline in stderr, and a test that runs a nested runner writes that runner's summary to stderr while the test runs - before unittest writes its own. So a nested summary ofRan 0 testswas counted and the module looked empty.The count is now the last summary in stderr, which is unittest's own: unittest writes its summary after every test has run, so nothing the tests wrote can come after it.
Fixed in
eb359333(parent7f8d6bf9), two files:scripts/run_semantic_tests.pyandtests/test_run_semantic_tests.py. The new test runs a module whose test starts a nestedunittest.TextTestRunneron an empty suite - that runner writesRan 0 tests in 0.000sto stderr - and the runner must still report1 files, 1 tests, 0 files failedand exit 0.Verified here:
python -I tests/test_run_semantic_tests.pyis 21 tests, OK.One thing worth stating, since it is the rule the fix rests on: a summary a test writes to stdout is never counted at all, and unittest's own summary is always the last one in stderr. A nested runner in a thread that outlives its test would still be an edge case I do not handle.
AI-assisted (MiniMax Code).
@lixonik on GitHub · 2026-11-24 11:52 UTC · original
Thanks for the fix. I retested
eb359333: all 21 runner tests pass. The original synthetic nested-runner fixture goes from zero counted tests and exit 1 at7f8d6bf9to one counted test and exit 0 ateb359333, using-j 2on both.I also confirmed a shutdown variant of the late-stderr limitation you mentioned: an
atexithandler can run the empty nested suite on stderr after the outer summary. In that variant-j 2still counts zero and exits 1, while-j 1exits 0. The original in-test regression is fixed. I would keep the shutdown case as a follow-up for structured counts.AI-assisted testing with synthetic fixtures.
@Dezmonius on GitHub · 2026-11-24 12:10 UTC · original
Confirmed
eb35933on Windows 10 / CPython 3.14.7. All 21 runner tests pass. With-j 2 --timeout 5, both the original stdout fixture and the in-test nested stderr runner now exit 0 with one test counted. I also reproduced the known shutdown case on Windows: anatexitcallback running an empty suite on stderr still yields exit 1 in parallel (exit 0 in serial).CPU-only synthetic fixtures. AI-assisted (Codex).
@Mby159 on GitHub · 2026-11-24 12:32 UTC · original
@Dezmonius thank you - three independent re-checks now agree on
eb359333: the count comes from unittest's own summary, a nested runner'sRan 0 testsno longer counts against the module, and the synthetic fixture from the finding exits 0 with one test counted under-j 2 --timeout 5.The shutdown case you reproduced on Windows is the known one and stays open; this change does not touch it.
Head
eb359333is final unless something else comes up.@Mby159 on GitHub · 2026-11-24 14:13 UTC · original · edited 2026-11-24 14:15 UTC
Rebased onto
mainatd0da7dad(the head that carries #605). The change itself is unchanged: 2 files, +46/-9.The
stdlib-tests-linuxred on the old head was the branch's base, not the change: the merge ref the CI used wasMerge <head> into 21625bca8, and21625bca8predates #605's stdout guard, sotools/exe_sha.pyandtools/pr_conflict_label.pystill lackedsys.stdout.reconfigure(errors="replace")@ there. The merge ref now merges intod0da7dad`, so the test sees the fixed tools.AI-assisted.
@Mby159 on GitHub · 2026-11-24 17:38 UTC · original · edited 2026-11-24 17:49 UTC
@lixonik — done: the count comes from unittest's result object now. This supersedes my note above that the head was final and that the shutdown case stayed open.
The count was the last
Ran N testsline in the module's stderr. unittest writes its summary after every test has run, but a module can write a line of the same shape after it: your atexit case does exactly that, and the runner countedRan 0 tests, so a module with one passing test looked empty and the run exited 1 withno tests ran.The child now runs the module through unittest's own program, the way
python -m unittest <module>runs it (same program, same argv,module=Noneas upstream'sunittest/__main__.pyleaves it), and hands the result back: it writesresult.testsRunto a file the runner reads, and exits with unittest's own exit codes - 1 when a test failed, 5 when nothing ran at all, 0 otherwise. Nothing the module writes can change the count any more: a nested runner, an atexit handler, a line on stdout or on stderr.One new test,
test_the_count_comes_from_the_result_object_not_from_a_late_summary, with two fixtures: an atexit handler that runs a nested runner on stderr, and one that printsRan 7 tests in 0.000sto stderr at exit. Both must report 1 file, 1 test and exit 0. On the previous head the first reported1 files, 0 testsand exited 1, and the second reported1 files, 7 tests- measured by running this test file against the previous head's runner.I compared the child with
python -m unitteston the same fixtures: same exit code, same stderr, same count - with and without-v, and for a module whose only test is skipped (OK (skipped=1)). The child is started the way the old one was (no-I, the suite folder onsys.path), so a module's import path is unchanged.Verified here - CPython 3.13.17, Windows 11 (AMD Ryzen 7 H 255 W / Radeon 780M; CPU only, no GPU needed):
python -I tests/test_run_semantic_tests.py- 22 tests, OK. The 21 earlier tests are unchanged; the new one fails on the previous head and passes on this one.python -I scripts/run_semantic_tests.py tests/semantic- 470 files, 6168 tests, 0 files failed.The previous head's runner reports the same 470 files and the same 6168 tests on the same tree, so no module in the
current suite writes a summary that could fool it - the miscount needs a module that writes one after unittest's own.
python -I scripts/check_no_game_data.pyon both files - clean.Head
53c7e233, rebased ontomain32950551. Two files, +100/-17.@shwepz: #1051 changes the same two files, and the import block and the text after
MAX_TIMEOUT_SECONDSare in both, so one of us will need a rebase when the other lands. The parts that do the work are apart: the count is read inrun_module, the failed-file header inmain().AI-assisted.
@QemmHD on GitHub · 2026-11-24 18:24 UTC · original
Merging.
Why merged: run_semantic_tests now counts the tests from unittest's own result object instead of parsing the merged output. The child runs
unittest.TestProgramand writestestsRunto a file the parent names viaLEONIDA_TEST_COUNT. That also closes the atexit/late-stderr case lixonik and Dezmonius left open. Dezmonius, trigger337 and lixonik checked the earlier head. The current headf48c2b2a3is a rebase of 53c7e2333 with identical script and test content.Why it's good: the new commit hadn't had an outside check yet, so I compared it on the real suite: tests/semantic with
-j 8on main32950551e, once with main's runner and once with this PR's. Both report 470 files and 6168 tests, with the same single failing file. That file istest_image_cube_store_glc, which can't start llvm-mc from a Windows LLVM_MC path; that's the open #837, unrelated to this PR. Own tests: 22 passed.LEONIDA_TEST_COUNTis internal plumbing between the runner and its own child process, like the already-exemptLEONIDA_BASE_RESERVED, so it isn't a user-facing setting that needs a docs entry.Tested on Windows 11 / RTX 4070 SUPER: merged alone onto main
32950551e. With it in, the repo-consistency set gives 569 passed / 5 skipped / 0 failed. git merge-tree against current main955d9be34: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.AI-assisted (Claude Code, Claude Opus 5.5).