Skip to content

scripts: tests_for.py names the tests that exercise a change (TESTS-FOR-CHANGE) - #2390

Open
syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:feat/tests-for-change
Open

syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:feat/tests-for-change

Conversation

@syfyra

@syfyra syfyra commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: TESTS-FOR-CHANGE (#1608 (comment))

Depends on: none. It merges cleanly with the two open pull requests that also edit CONTRIBUTING.md (#2151 and #2349, in another section), checked with the scripts/pr_overlap.py of #2349.

What and why

The maintainer guide says to run "the pull request's own tests", and AGENTS.md to run one test file while iterating. Which tests those are was guesswork: tests are found by their name, and a helper that many tools import has no test of its name. tools/world_build.py is named by seven test files, only two of them called test_world_build*, and six more reach it through sandbox.py, sandbox_scene.py and the two drawable tools.

python -I scripts/tests_for.py [PATH ...] reads the repository and answers for the files or folders named, or with no argument for what the branch changes (committed against the merge base with origin/main, plus modified and untracked files). For each file it lists the tests under tests/ that reach it, with the reason:

  • names it: the test imports the file as a module or names its path, as a quoted path or joined from quoted segments (os.path.join, pathlib /, Node's join);
  • imports: a module the test imports reaches the file through imports, with the module it goes through;
  • folder: the test names the folder the file is in and nothing in it, so it reads what is there; for a Markdown file these are tests/test_docs_*.py and tests/test_i18n_index.py, the set CI runs for a documentation change;
  • fixture: the file is under a fixtures folder and a test beside that folder quotes its name;
  • ctest: for runtime/, the ctest tests whose program is built from the file according to runtime/CMakeLists.txt: as a source, through a header a source includes, or through a library the program links. foreach loops and set() lists are followed.

Then it prints the commands, split the way CI splits the tests: python -I for a standalone file, pytest for a file with CI's pytest markers or no __main__ block, the semantic runner, node --test, and ctest -R with the names. Tests that name the file and changed tests always get a command; the wider reasons do when there are at most 20 of them, or with --all. --json gives the whole report. It runs no test and writes nothing.

On this repository (1,048 test files, 492 add_test entries):

  • tools/world_build.py: 7 tests name it, 6 reach it through imports, 11 name the tools folder.
  • tools/rdna2spirv.py: 341 name it and 273 reach it through imports; the command list is 38 lines with the semantic suite as one of them.
  • runtime/shims/ws2_offline.cpp: 16 ctest tests, ws2_offline and 15 whose command runs a program that is built from it too (run_tiny_game, run_argv_modes and others).
  • viewer/index.html: tests/test_viewer_streaming.cjs and tests/test_docs_repo_paths.py name it, and tests/test_viewer_cdn_integrity.py is one of the two that name the folder.

What it cannot see, also said in its docstring: a path that is built at run time, and a ctest program that a CMake function or a generator expression builds. Of the 492 add_test entries, 15 run something it cannot place (gameinput, run_tiny_async and others) and one keeps a ${...} in its name and is left out, so the ctest list is the least there is to run. Paths are matched in the case they have on disk, so Windows gives the same answer as Linux.

CONTRIBUTING.md gets one bullet under "Working on the code".

Tested

Windows 11, Python 3.14.7, git 2.55.0:

  • python -I tests/test_tests_for.py: 32 tests. 28 run on a synthetic checkout of 62 small files kept as data in tests/fixtures/tests_for_checkout.json (its paths would otherwise read as references of the test file to real files, which one of the tests checks), one on a throwaway git repository for the branch mode, and three on this repository.
  • The test file fails on main, where the script does not exist.
  • 65 small changes to the script (a join that takes any two quoted words, imports not followed, the folder rule widened to subfolders, loops not expanded, libraries linked the wrong way round, the pytest markers shortened, and so on) each make at least one test fail; an unchanged copy passes in the same harness.
  • python -I scripts/tests_for.py tools/world_build.py takes 1.8 s here.
  • python -I on the 13 tests/test_docs_*.py, tests/test_i18n_index.py, tests/test_tool_stdout_encoding.py, tests/test_text_write_line_endings.py and tests/test_ci_registration.py; python -I scripts/check_no_game_data.py on the new files.
  • Not tested: Linux and macOS, Python 3.12, and I did not run the commands it prints for the four examples above; the lists were read, not executed.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule in this change.)

🤖 Generated with Claude Code

…OR-CHANGE)

A reviewer runs "the pull request's own tests" and a contributor the tests
of what they changed, but which ones those are was guesswork: tests are
found by their name, and a helper that many tools import has no test of
its name.

scripts/tests_for.py reads the repository and answers for the paths named,
or for the files the current branch changes. For each file it lists the
tests under tests/ that import it or name its path, the ones that reach it
through the modules they import (with the module they go through), the
ones that read its folder, the documentation tests for a Markdown file and
the tests beside a fixture that quote its name. For runtime/ it reads
runtime/CMakeLists.txt and lists the ctest tests whose program is built
from the file: as a source, through a header a source includes, or through
a library the program links, with foreach loops and set() lists followed.

It then prints the commands, split the way CI splits the tests: python -I,
pytest, the semantic runner, node --test and ctest -R. It runs no test.
Paths are matched in the case they have on disk, so the answer is the same
on Windows and Linux.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@wavehs wavehs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS arm64 check of #2390 (TESTS-FOR-CHANGE). Everything this PR adds runs on macOS, so its own suite and the two big suites were measured here.

Tested head 25a8f9e91 merged onto main bdbc7e923 as merge 98c26ecab (clean; the branch is 108 commits behind but the diff is four added/replaced files).

Environment: Apple M3 Pro, arm64 (subgroups 4..32), macOS 27.2.1, CPython 3.15.2, Apple clang 21.0.0, SPIRV-Tools v2026.4, llvm-mc 23.1.5, MoltenVK 1.4.0; KosmicKrisp and lavapipe = Mesa 26.3.0.

The new suite passes, and bites

  • python -I tests/test_tests_for.py: 32/32 OK at the head tree and 32/32 after merging onto bdbc7e923. It is fast (2.8-5.2 s).
  • Negative control (test kept, scripts/tests_for.py sabotaged so Repository.reached stops following imports): 3 failures - test_imports_are_followed_and_say_which_module_they_go_through, test_commands_follow_the_runners, test_tests_are_listed_by_reason_then_by_name. The import graph and the command grouping are pinned.
  • I also ran the tool by hand on this checkout: tools/rdna2spirv.py -> 341 names it, 273 imports (through tests/semantic/support.py), 11 folder; runtime/src/pe_loader.cpp -> 35 ctest; runtime/src/vulkan_replay.h -> 291 ctest; viewer/index.html -> the two viewer tests. The ctest mapping does follow the library/header links, which is the part most likely to be wrong.

No regressions

  • Semantic suite: 500 files / 6,367 tests / 0 files failed, both at main bdbc7e923 and at 98c26ecab. Delta 0 (the new file is in tests/, not tests/semantic/).
  • GPU fixtures (268): MoltenVK 195 PASS / 3 FAIL / 70 SKIP, KosmicKrisp 190 / 5 / 73, at both main and the merge; a line diff of the PASS/FAIL/SKIP output differs only in the per-run temp directory quoted in SKIP reasons. The failures are the pre-existing ones at main (MV: cs_fma_fused, cs_imgatomic_ext, ps_ssharp; KK: cs_bvh, cs_bvh32, cs_imgatomic_ext, ps_cndmask_sign_mods, ps_valu16_mad_sel).
  • Runtime build note, unrelated to this PR: a clean cmake --build runtime/build -j8 at main fails at vk_null_descriptor_test with glslangValidator: Failed to open file .../build/generated/cs_null_descriptors.spv (runtime/CMakeLists.txt:2266-2273 writes into generated/ without creating it; reproduced on two fresh build dirs). mkdir -p build/generated then the build reaches 100%. The GPU binaries (vk_compute_test, vk_store_test, vk_harness) were built before the abort, so the GPU numbers above are from the same tree; I have not counted the aborted build as a pass.

Finding (low, real)
scripts/tests_for.py:344-357 (expand_targets) never checks that a PATH exists. A mistyped path is silently treated as a file nothing names - and because survey's folder reason does not look at the file either, it even lists tests that name the folder. Reproduced on this checkout:

  • python -I scripts/tests_for.py tools/rdna2spirv.py -> 341 names it, 273 imports, exit 0.
  • python -I scripts/tests_for.py tools/rdna2spiry.py (one letter wrong) -> folder (11) with test_alu_frexp.py, test_bad_input_errors.py, ..., a Run: block, and exit 0.

So a reviewer who mistypes a path runs eleven unrelated tests and thinks the change is covered; nothing distinguishes that from a file with genuinely no test. The docstring promises "Exit 0, or 2 on a usage or git error" (line 32). Add an existence check in expand_targets (explicit arguments only, so changed_files can still report a file deleted between --base and --head), e.g. if not os.path.lexists(full): raise GitError(f"{path} does not exist"), and a test beside the others.

Nit
commands() (:422) collapses the ctest list to the whole-suite command whenever there are more than 40, even with --all, although --all's help says "commands for every test found, however many". Running the suite does run every test, so this is wording, not behaviour; either say so in the help or gate the collapse on everything like the other reasons.

No game files. AI-assisted.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2390 — Linux, Python 3.15, no GPU, no game files

Setup. PR head 25a8f9e9 (1 commit, based on d5634def, which is 2 178 commits behind the main at the time of this run, d920d850). Merged into a clean worktree at d920d850 — clean merge, no conflicts. Fedora Linux VM, Python 3.15.2, no Vulkan device needed for anything below.

1. Its own suite

python -I tests/test_tests_for.py     -> Ran 32 tests ... OK

32/32. This is the part anyone can produce; the rest is what I went looking for.

2. Do its claims hold against ground truth

For 12 files I compared the report against an independent grep of tests/ for the file's stem (excluding a test's own filename), then read every divergence by hand.

No false positives. Across all 12 files, extra = 0: every test the tool reports under names it really does contain the file's name.

Most divergences are correct exclusions, and there are a lot of them:

  • # awc_names.py labels a bank from an audio_manifest.json ... — a comment (tests/test_opus_tags.py:300)
  • "sandbox-scene" used as a process label, sandbox in prose (tests/test_sandbox_scene.py:250)
  • tests/semantic/test_*.py docstrings saying "run_gpu_tests.py / check.py with LEONIDA_BARY_FALLBACK=1 check ps_bary" — the runner runs those tests; they do not test the runner
  • tests/test_gamedata_index_report_paths.py:3 naming its own path, which the tool correctly ignores (there is a test for that: test_a_test_that_names_its_own_path_is_not_its_own_test)

On the full list rather than the names it bucket the count drops from 39 to 31, and by inspection 29 of those 31 are correct. Two are not.

3. Reverse control

This is the one that matters, because the tool's answer is the list a verifier runs first.

tools/meshlet_page.py:1047-1050:

if __name__ == "__main__":
    try:  # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
        sys.stdout.reconfigure(errors="replace")
    except (AttributeError, ValueError, OSError):
        pass

tests/test_tool_stdout_encoding.py guards exactly this, two ways: a static check that every __main__ block reconfigures stdout, and MeasuredCrashes, which runs each tool under a private-use-named folder and asserts no UnicodeEncodeError:

CASES = [("analyze_4240_full.py", False), ("meshlet_page.py", False)]
...
r = subprocess.run(PYTHON + [os.path.join(TOOLS, tool)] + argv, ...)

Before any change:

python -I scripts/tests_for.py --all tools/meshlet_page.py
  -> 19 tests
  -> tests/test_tool_stdout_encoding.py is NOT among them

The test reaches the file through os.path.join(TOOLS, tool) where tool comes from a list of bare filenames, so the names it rule does not follow it, and the test never names the tools folder either, so folder does not catch it.

All 19 listed tests PASS. tests/test_tool_stdout_encoding.py PASS.

Mutation. Line 1049 replaced with pass:

the 19 tests the tool recommends test_tool_stdout_encoding.py
before 19/19 PASS PASS
after 19/19 PASS FAIL

After the mutation tests_for.py still returns the same 19 tests, still without test_tool_stdout_encoding.py, and the one test that catches it says so by name:

AssertionError: Lists differ: ['tools/meshlet_page.py'] != []
  : programs whose __main__ block doesn't set errors='replace' on stdout

So for this change the tool prints nineteen green commands and omits the only test that fails. A contributor running what it prints gets no signal at all.

The tree was restored afterwards (git status clean, test_tool_stdout_encoding.py back to OK).

4. The same class, second instance

tests/test_zero_span.py:28-35 runs nine scanners, including this one, through runpy.run_path from a table keyed by bare tool name:

SCANNERS = {
    ...
    "restofworld_index": ["{arc}", "{out}/row.json", "--quiet"],

tests_for.py --all tools/restofworld_index.py lists 19 tests; tests/test_zero_span.py is not among them.

5. Minor

The description's counts are from the old baseline. It says tools/world_build.py: "7 tests name it, 6 reach it through imports, 11 name the tools folder". On d920d850 that is 7 / 6 / 16. The first two match; the third has grown with the suite. Worth refreshing before merge, since a reviewer will compare against it.

Verdict

Mergeable, and correct about everything it does claim. 32/32 on its own suite, zero false positives in the audited sample, and the reasons it gives hold up when read.

But it is not complete, and the gap points the dangerous way. It misses tests that reach a file through a data table of tool names, and those tend to be the tests that actually execute the file rather than mention it. For the case above, the recommended list is all-green while the real test fails.

Two ways to close it, either is enough:

  1. follow a bare filename that is used as a program argument (a value inside a list/tuple/dict passed to runpy.run_path, subprocess or a tools/ join), or
  2. say so in the docstring and --help — "this list is not exhaustive; a green run here is not proof" — so nobody treats it as a complete answer.

Nothing here needs game files or a GPU, so the same runs reproduce anywhere.

AI-assisted: yes — MiMo-V2.6-Flash (OpenCode). It built the worktree, wrote the grep cross-check and the mutation, and drafted this report; I read the divergent lines above rather than trusting either side.

@fiestorra

Copy link
Copy Markdown
Contributor

Verified with its tests and used on a real file. It works, and for a Rust file it answers with the Python tests that reach it and none of the ones inside it - which is the gap worth knowing before this lands.

The suite, on main d920d850d plus the range (its base d5634defe is not behind main, cherry-pick clean):

python -m pytest tests/test_tests_for.py -q
  32 passed   (16.41 s)

with scripts/tests_for.py removed:
  1 error, collected 0 items

So the tests are tied to the script and nothing else was needed to make 32 pass.

Used as a user would, on a file this repository argues about more than any other:

python -I scripts/tests_for.py d3d12/crates/leonida-gpu/src/host.rs
  d3d12/crates/leonida-gpu/src/host.rs
    imports (1): tests/test_game_gate.py (through tools/game_gate.py)
    folder (1): tests/test_d3d12_switches.py
  Run:
    python -I tests/test_d3d12_switches.py
    python -I tests/test_game_gate.py

That is correct for what it models, and the model is Python. host.rs has a module of 636 Rust tests that exercise it directly - host::refresh_tests::only_what_differs_goes_up among them - and the tool names none.

Why that matters here: scripts/tests_for.py says nothing about rust, .rs, cargo or #[test] (I grepped for all four), and d3d12/ is half this repository. For a Python change the answer is the one this tool gives; for a Rust change the answer is "two Python files reach this", which is not the question someone editing host.rs is asking.

What I would suggest, and it is the same suggestion I made on #2817 for a different reason: say the languages it covers in --help or in the output - one line, python only, saves a Rust contributor from concluding the file is untested - and, if the crate is worth it, cargo test -p <crate> <module> is a naming exercise the tool is already shaped for.

One thing it gets right that is easy to miss: imports (1): ... (through tools/game_gate.py) - it says which module the reachability goes through, not just that it exists. That is the part that makes the answer checkable by hand, and it is why the two names above are trustworthy even though the list is short.

AI-assisted: yes - DeepSeek v4.1 (flash), which ran the suite, the control, and the tool.

Written with an AI assistant.

@aslan2142 aslan2142 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Draft review note: TESTS-FOR-CHANGE (scripts/tests_for.py, tests/test_tests_for.py, CONTRIBUTING.md) is cleanly scoped and mergeable on main, but remains a draft with no CI run (note also that Rust crate changes under d3d12/ currently report only Python tests rather than cargo test targets). Please run CI to green and mark ready for review when ready.

(AI-assisted review.)

@aslan2142
aslan2142 marked this pull request as ready for review December 1, 2026 12:36
@aslan2142

Copy link
Copy Markdown
Collaborator

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

@aslan2142 aslan2142 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now ready for review, requesting changes before merge:

  • Missing path-existence validation (scripts/tests_for.py:344-357): as reproduced by @wavehs, expand_targets() never verifies that an explicit CLI path exists; mistyping a path (e.g., tools/rdna2spiry.py) exits 0 and prints unrelated folder tests instead of exiting 2 as promised in scripts/tests_for.py:32 (Exit 0, or 2 on a usage or git error). Please fail closed on missing explicit CLI paths (while keeping deleted-path tolerance for git-diff targets) and add a test.
  • Scope clarity in --help/docstring: as noted by @fiestorra and @imShangLi, please state clearly in --help/output that coverage is Python/CTest-oriented (it does not list Rust cargo test suites under d3d12/ and is not exhaustive for bare-name data tables such as tests/test_zero_span.py:28-35).
  • CI: no CI runs on the current head yet; please get fresh green CI after fixing.

(AI-assisted review.)

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
@merzleckeier

Copy link
Copy Markdown
Contributor

Confirmed and isolated the missing explicit-path behavior mentioned in the review, at 25a8f9e91 + 9f27ba2e1. The PR now needs a local resolution in CONTRIBUTING.md after #2871. On the resolved tree the original 32 tests passed, while my new test rejected the current behavior of exiting 0 for a typo. Checking existence in expand_targets made both missing file paths return 2 with no suggestions. A separate disposable Git test confirmed that a deleted file still appears in the branch-mode report. All 33 tests passed with the local change, and the local help text names its Python, Node.js and CTest scope.

AI-assisted with OpenAI Codex (GPT-6).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants