Repository navigation
test(guards): one walk behind the guards that scan source as text - #3914
Conversation
Six guards read `src/`, or the snapshot corpus, as text rather than compiling
it — no stray `println!` outside the allowlist, no `eprintln!` resolving to
std's macro instead of anstream's, no bare `env!("VERGEN_…")`, no `include_str!`
of a path the package won't ship, no host path in a `.snap`, and nothing but
`src/testing/mod.rs` spawning `wt` through `CARGO_BIN_EXE_wt`. Each asserts
absence, and each carried its own copy of the same recursion: read the
directory, iterate entries, recurse into subdirectories, read and check files
with a matching extension.
The copies drifted. #3911 closed a swallowed read in one of them and the
identical swallow survived in the rest, which is how a review ended up listing
remaining sites by hand. `tests/common/source_scan.rs` owns the walk now and
all six call it; the guards keep what is theirs — which extension, what counts
as a violation, and their own coverage assertion.
Six swallowed reads go with it. `output_system_guard` had `entries.flatten()`
in both its walks and `Err(_) => return` in both its file readers;
`version_build` had `entries.flatten()` and `let Ok(..) else { return }`.
`flatten()` is the one that hides, dropping a per-entry `io::Error` without
looking like a return: a `println!` in a file whose `stat` fails went
unreported, and so did a bare `env!("VERGEN_…")` in a file that failed to read.
`visit_files` returns how many files it visited, because absence assertions
also pass over an empty walk. Three guards had nothing that would fail in that
case and now assert the count. The other three already assert something an
empty walk cannot satisfy — a non-empty asset set, a per-root snapshot count,
an offender list equal to one named file — so a count beside it would be a
second mechanism for one guarantee.
`output_system_guard`'s allowlist checks now run against a file the walk has
already read rather than short-circuiting before the read. An allowlisted file
that cannot be read is a file in the tree that cannot be read, and says so.
`tests/CLAUDE.md` records the rule, beside "a test's setup is part of its
proof" — the same failure in different dress. Nothing wrote it down, which is
why each guard was free to rediscover it.
Verified by reaching rather than by green, since a walk that visits nothing
passes every one of these tests: a `println!` injected three levels deep in
`src/trace/profile.rs` is reported with its line, the spawn-pin needle injected
under `tests/` is reported, and pointing a scan at a readable directory holding
no `.rs` files fires the new coverage assert.
…over Two walks in `test_plugin_payloads_are_canonical` assert absence and count nothing, so an empty tree satisfies both over zero entries. The symlink invariant walks `plugins/worktrunk/skills` asserting no entry is a symlink; if the mirror never generated, the walk visits nothing and passes. `is_dir()` runs first, so the directory has to exist, but an existing empty one is exactly the case a failed mirror leaves behind. The `skills/` loop below has the same shape for SKILL.md. Neither reads file contents, so `visit_files` isn't a drop-in — they carry their own counts instead. Same rule as the source-scanning guards, applied where it also holds: an assertion of absence needs separate evidence that there was something to assert over.
…the scan `visit_files` returning a count left the obligation in prose: `tests/CLAUDE.md` said which guards must assert it and why the others need not, and a seventh caller would inherit none of that. That is the shape of the bug this replaced — a per-caller duty four of five copies quietly skipped. `#[must_use]` turns forgetting into a compile error under `-D warnings`, and turns "my proof is elsewhere" into a `let _ =` a reader can see and check. The `scan` label claimed to name the guard and didn't: three call sites passed the same `"src/ scan"`, so a permissions failure read identically whichever was running, and only libtest's thread name distinguished them. They now name the guard. The file-contents panic was also worded exactly like the directory-listing one, separated only by whether the path happened to be a file; it says so now. With the recursion inside `visit_files`, the `scan_*` adapters were one-line single-caller wrappers whose second parameter was provably the same value as their first at every call site — that shape only made sense while they recursed. Inlined, which drops the wrapper and the duplicate argument together. `packaged_assets`' comment still credited panics to `scan_file`, which now takes `contents` and reads nothing, and the coverage asserts said what happened without saying what it cost. Verified `#[must_use]` bites: dropping one `let _ =` fails the build with "unused return value of `visit_files` that must be used".
worktrunk-bot
left a comment
There was a problem hiding this comment.
Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.
Nothing on correctness at 2b69fb4: each guard's panic wording survives the extraction, the #[must_use] + let _ = pairing lands on exactly the two guards whose own assertion already fails over an empty walk (assets non-empty, offenders == vec!["src/testing/mod.rs"]), and for_each_snapshot keeps asserting the count per root. cargo check --test integration is clean with no warnings.
Three small things inline — two in tests/CLAUDE.md, one stray doc line in the new module.
|
All three applied at The inventory undercount is the one that mattered: the section listed five guards where On the stray Current state at
|
|
Confirmed at The stray- Still in draft, so I'll hold the full pass until it's marked ready. The six call sites
|
worktrunk-bot
left a comment
There was a problem hiding this comment.
Full pass now that it's out of draft. Correctness holds: the six call sites map one-to-one onto visit_files, each guard keeps its own extension, violation predicate, and coverage claim, and the three that discard the count each have an assertion an empty walk genuinely can't satisfy (!assets.is_empty(), the per-root seen > 0, offenders == ["src/testing/mod.rs"]). #[must_use] on the recursive call is satisfied by the visited +=, and &mut f reborrows cleanly across the three snapshot roots.
One thing in the PR description rather than the code: it says "two walks in config_show.rs's test_plugin_payloads_are_canonical", but no such test exists — the mirrored and skill_dirs counters land in test_plugin_layout_is_consolidated. Worth correcting before merge, since the description becomes the squash message and that name is the only handle a future reader has for finding those two walks.
Six guards read
src/, or the snapshot corpus, as text rather than compiling it — no strayprintln!outside the allowlist, noeprintln!resolving to std's macro instead of anstream's, no bareenv!("VERGEN_…"), noinclude_str!of a path the package won't ship, no host path in a.snap, and nothing butsrc/testing/mod.rsspawningwtthroughCARGO_BIN_EXE_wt. Each asserts absence over what its walk handed it, and that polarity is what makes a swallowed read dangerous here in a way it isn't for code that acts on what it finds: a file that drops out of the walk is indistinguishable from a file with nothing wrong in it, soviolationsstays empty and the test passes green over less than it claims.Each carried its own copy of the same recursion — read the directory, iterate entries, recurse into subdirectories, read and check files with a matching extension. The copies drifted, and #3911 is the evidence: closing a swallowed read in one left the identical swallow in the others, and the review that caught it had to list remaining sites by hand.
tests/common/source_scan.rsowns the walk now and all six call it. The guards keep what is actually theirs: which extension, what counts as a violation, and their own coverage assertion.Six swallowed reads go with it.
output_system_guard.rshadentries.flatten()in both its walks andErr(_) => returnin both its file readers;version_build.rshadentries.flatten()andlet Ok(..) else { return }.flatten()is the one that hides, dropping a per-entryio::Errorwithout looking like areturnat all — aprintln!in a file whosestatfails went unreported bycheck_no_unexpected_stdout_writes, and a bareenv!("VERGEN_…")in a file that failed to read went unreported byvergen_env_vars_are_read_optionally, the guard standing between that and another #3123.visit_filesreturns how many files it visited, because an absence assertion also passes over an empty walk. Three guards had nothing that would fail in that case and now assert the count. The other three already assert something an empty walk cannot satisfy — a non-empty asset set, a per-root snapshot count, an offender list equal to one named file — so a count beside those would be a second mechanism for one guarantee.The return is
#[must_use], which matters more than it looks. Left advisory, the classification of who needs the count would live only in prose, and a seventh caller would inherit none of it — the same shape as the bug being fixed, a per-caller obligation that four of five copies quietly skipped. As amust_useit is a compile error under-D warnings, and "my proof is elsewhere" becomes alet _ =a reviewer can see and check.The
scanlabel names the calling guard. It previously claimed to and didn't: three call sites passed the same"src/ scan", so a permissions failure read identically whichever guard was running, and only libtest's thread name told them apart. The file-contents panic was likewise worded exactly like the directory-listing one, separated only by whether the path happened to be a file.One behaviour change worth naming:
output_system_guard's allowlist checks now run against a file the walk has already read, rather than short-circuiting before the read. An allowlisted file that can't be read is a file in the tree that can't be read, and now says so instead of being skipped.tests/CLAUDE.mdrecords the rule, beside "a test's setup is part of its proof" — the same failure in different dress. Nothing wrote the hazard down, which is why each guard was free to rediscover it.The rule holds outside the source scans too, so two walks in
config_show.rs'stest_plugin_layout_is_consolidatednow count what they assert absence over. One walksplugins/worktrunk/skillsasserting no entry is a symlink — if the mirror never generated, it visits nothing and passes;is_dir()runs first, but an existing empty directory is exactly what a failed mirror leaves. Theskills/loop below it has the same shape forSKILL.md. Neither reads file contents, sovisit_filesisn't a drop-in and they carry their own counts.Test plan
cargo fmt --checkandclippy --all-targetsclean; the 15 tests across the guard files pass.Green proves least here, since a walker that silently visits nothing passes every one of those tests. So each behaviour was reached for directly:
Reach checks
The walk recurses and reads. A
println!injected three levels deep insrc/trace/profile.rs, a file on no allowlist:The walk covers roots outside
src/. TheCARGO_BIN_EXE_wtneedle injected into a file undertests/:The coverage assert fires. A scan pointed at a directory that exists, reads fine, and holds no
.rsfiles:The per-root snapshot count fires on a partial move.
testsalone — 1,088 of the 1,156 snapshots — swapped for a directory holding none, leavingsrc's 64 anddocs' 4 in place:An aggregate non-empty check survives that case, which is why the count is per root. Every edit was reverted after its run.