Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

test(guards): restore the empty-corpus checks the read_dir panics don't cover (#3911)

Maximilian Roos8e40a2b7ca37
diff --git a/tests/integration_tests/packaged_assets.rs b/tests/integration_tests/packaged_assets.rs
index 6aedc9a94..ab9c616f0 100644
--- a/tests/integration_tests/packaged_assets.rs
+++ b/tests/integration_tests/packaged_assets.rs
@@ -52,6 +52,15 @@ fn embedded_assets_ship_in_package() {
// repo-root-relative forward-slash string.
let mut assets = BTreeSet::new();
scan_directory(&src_dir, manifest_dir, &mut assets);
+ // `scan_directory` and `scan_file` panic when the tree or one of its files
+ // can't be read. This catches the remaining way discovery comes back empty:
+ // every file read, and the `include_str!` / `include_bytes!` / `#[template]`
+ // matching recognizing none of them. The comparison below iterates `assets`,
+ // so an empty set checks nothing and passes.
+ assert!(
+ !assets.is_empty(),
+ "scanned src/ but found no embedded assets — the scanner is likely broken"
+ );
// 2. The set of files `cargo publish` would put in the crates.io archive.
let packaged = cargo_package_list(manifest_dir);
@@ -95,7 +104,16 @@ fn scan_directory(dir: &Path, manifest_dir: &Path, assets: &mut BTreeSet<String>
let entries = fs::read_dir(dir)
.unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", dir.display()));
- for entry in entries.flatten() {
+ for entry in entries {
+ // `flatten()` here would drop a per-entry error, so a file that never
+ // reaches `scan_file` takes whatever it embeds with it while the rest
+ // keep `assets` non-empty — past both the panic and the assert.
+ let entry = entry.unwrap_or_else(|e| {
+ panic!(
+ "an entry in {} unreadable during the src/ scan: {e}",
+ dir.display()
+ )
+ });
let path = entry.path();
if path.is_dir() {
scan_directory(&path, manifest_dir, assets);
@@ -106,9 +124,12 @@ fn scan_directory(dir: &Path, manifest_dir: &Path, assets: &mut BTreeSet<String>
}
fn scan_file(path: &Path, manifest_dir: &Path, assets: &mut BTreeSet<String>) {
- let Ok(contents) = fs::read_to_string(path) else {
- return;
- };
+ // Returning here would drop whatever this file embeds while the rest keep
+ // `assets` non-empty, so the empty-scan assert above wouldn't catch it
+ // either. A `.rs` file under `src/` that isn't readable UTF-8 is broken in a
+ // way the crate wouldn't compile through.
+ let contents = fs::read_to_string(path)
+ .unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", path.display()));
let source_dir = path.parent().unwrap_or(manifest_dir);
for line in contents.lines() {
diff --git a/tests/integration_tests/snapshot_formatting_guard.rs b/tests/integration_tests/snapshot_formatting_guard.rs
index ae9a69772..9a9b9d992 100644
--- a/tests/integration_tests/snapshot_formatting_guard.rs
+++ b/tests/integration_tests/snapshot_formatting_guard.rs
@@ -178,24 +178,55 @@ fn test_no_host_specific_paths_in_snapshots() {
/// Visit every committed `.snap` file. Snapshot dirs live under `src`
/// (unit tests), `tests` (integration tests), and `docs` (demo fixtures).
fn for_each_snapshot(project_root: &Path, mut f: impl FnMut(&Path, &str)) {
+ // Every caller asserts *absence* over the corpus, so snapshots that stop
+ // being walked pass vacuously. The panic in `visit_snap_files` covers a root
+ // that can't be read; this covers one that reads fine and has stopped
+ // holding snapshots. Per root rather than in aggregate, because a layout
+ // change moves one root and leaves the others: `tests` alone holds the large
+ // majority of the corpus, so an aggregate non-empty check would survive
+ // losing it. That makes each root load-bearing — a root that legitimately
+ // stops holding snapshots belongs out of this list, not exempted from it.
for root in ["src", "tests", "docs"] {
- visit_snap_files(&project_root.join(root), &mut f);
+ let mut seen = 0usize;
+ visit_snap_files(&project_root.join(root), &mut |path, content| {
+ seen += 1;
+ f(path, content);
+ });
+ assert!(
+ seen > 0,
+ "{root}/ holds no .snap files — the corpus has moved, so these \
+ assertions are now passing over whatever is left of it"
+ );
}
}
fn visit_snap_files(dir: &Path, f: &mut impl FnMut(&Path, &str)) {
- // Every caller asserts absence over the corpus, so a walk that reads
- // nothing passes vacuously. Swallowing the error is what makes a
- // directory-layout change look like a clean corpus; surface it instead.
+ // A root that can't be read yields no snapshots, and every caller asserts
+ // absence, so swallowing the error would let an unreadable directory read as
+ // a clean corpus. `for_each_snapshot` covers the separate case where the
+ // roots read fine and simply hold nothing.
let entries = fs::read_dir(dir)
.unwrap_or_else(|e| panic!("{} unreadable during the snapshot scan: {e}", dir.display()));
- for entry in entries.flatten() {
+ for entry in entries {
+ // `flatten()` here would drop a per-entry error — a file removed
+ // mid-walk, a failing `stat` — and skip a snapshot without saying so.
+ let entry = entry.unwrap_or_else(|e| {
+ panic!(
+ "an entry in {} unreadable during the snapshot scan: {e}",
+ dir.display()
+ )
+ });
let path = entry.path();
if path.is_dir() {
visit_snap_files(&path, f);
} else if path.extension().and_then(|s| s.to_str()) == Some("snap") {
- let content = fs::read_to_string(&path).unwrap();
+ let content = fs::read_to_string(&path).unwrap_or_else(|e| {
+ panic!(
+ "{} unreadable during the snapshot scan: {e}",
+ path.display()
+ )
+ });
f(&path, &content);
}
}