Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test(guards): fail when a source scan guards nothing (#3878)
Worktrunk Botaeb00982e975
diff --git a/tests/integration_tests/output_system_guard.rs b/tests/integration_tests/output_system_guard.rsindex c22b68c66..ec2ba865e 100644--- a/tests/integration_tests/output_system_guard.rs+++ b/tests/integration_tests/output_system_guard.rs@@ -128,10 +128,11 @@ fn check_no_unexpected_stdout_writes() {}fn scan_directory(dir: &Path, tokens: &[&str], violations: &mut Vec<String>, scan_root: &Path) {- let entries = match fs::read_dir(dir) {- Ok(e) => e,- Err(_) => return,- };+ // A directory the walk can't read contributes no violations, so swallowing+ // the error lets the guard pass over a tree it never inspected — a missing+ // or relocated `src/` reads as a clean crate. Surface it instead.+ let entries = fs::read_dir(dir)+ .unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", dir.display()));for entry in entries.flatten() {let path = entry.path();@@ -228,6 +229,43 @@ const STD_STDERR_ALLOWED_PATHS: &[&str] = &["remove_dir.rs",];+/// Every allowlist entry names a file that still exists.+///+/// Both allowlists are matched by path string, so a rename leaves a dead entry+/// behind rather than failing. That is worse than clutter: the exemption stays+/// armed at the old path, so a *new* file arriving there — `src/help.rs` is the+/// shape, a name a future refactor could plausibly reuse — inherits a+/// permission nobody granted it, and the guard stays green while it writes to+/// stdout. Pinning existence turns the rename into a failing test at the moment+/// it happens, when the reviewer still knows whether the exemption should move+/// with the file or go.+#[test]+fn allowlisted_paths_still_exist() {+ let src_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");++ let stale: Vec<String> = [+ ("STDOUT_ALLOWED_PATHS", STDOUT_ALLOWED_PATHS),+ ("STD_STDERR_ALLOWED_PATHS", STD_STDERR_ALLOWED_PATHS),+ ]+ .iter()+ .flat_map(|(list_name, paths)| {+ paths+ .iter()+ .filter(|relative| !src_dir.join(relative).is_file())+ .map(move |relative| format!("{list_name}: src/{relative}"))+ })+ .collect();++ assert!(+ stale.is_empty(),+ "allowlist entries naming files that no longer exist:\n\n{}\n\n\+ The entry exempts whatever file later occupies that path, so a stale one\n\+ silently pre-approves stdout (or std's stderr macros) for code nobody reviewed.\n\+ Move the entry to the file's new path, or drop it.",+ stale.join("\n")+ );+}+/// Narration on stderr goes out through anstream, at every site.////// `worktrunk::styling`'s `eprint!`/`eprintln!` are anstream's and strip ANSI@@ -262,10 +300,8 @@ fn check_stderr_macros_come_from_styling() {}fn scan_stderr_macros(dir: &Path, src_dir: &Path, violations: &mut Vec<String>) {- let entries = match fs::read_dir(dir) {- Ok(e) => e,- Err(_) => return,- };+ let entries = fs::read_dir(dir)+ .unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", dir.display()));for entry in entries.flatten() {let path = entry.path();diff --git a/tests/integration_tests/packaged_assets.rs b/tests/integration_tests/packaged_assets.rsindex 96b3ced1a..6aedc9a94 100644--- a/tests/integration_tests/packaged_assets.rs+++ b/tests/integration_tests/packaged_assets.rs@@ -52,10 +52,6 @@ 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);- 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);@@ -93,9 +89,12 @@ fn embedded_assets_ship_in_package() {}fn scan_directory(dir: &Path, manifest_dir: &Path, assets: &mut BTreeSet<String>) {- let Ok(entries) = fs::read_dir(dir) else {- return;- };+ // A directory the walk can't read yields no assets, so swallowing the+ // error lets an unreadable or relocated `src/` read as a crate that embeds+ // nothing — every violation below goes unchecked. Surface it instead.+ let entries = fs::read_dir(dir)+ .unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", dir.display()));+for entry in entries.flatten() {let path = entry.path();if path.is_dir() {diff --git a/tests/integration_tests/snapshot_formatting_guard.rs b/tests/integration_tests/snapshot_formatting_guard.rsindex 0096b8a10..ae9a69772 100644--- a/tests/integration_tests/snapshot_formatting_guard.rs+++ b/tests/integration_tests/snapshot_formatting_guard.rs@@ -178,25 +178,18 @@ 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)) {- let mut seen = 0usize;for root in ["src", "tests", "docs"] {- visit_snap_files(&project_root.join(root), &mut |path, content| {- seen += 1;- f(path, content);- });+ visit_snap_files(&project_root.join(root), &mut f);}- // Every caller asserts absence over the corpus (~1200 files); an empty- // walk — say, after a directory-layout change — would pass vacuously.- assert!(- seen > 500,- "expected the full snapshot corpus, saw {seen} files"- );}fn visit_snap_files(dir: &Path, f: &mut impl FnMut(&Path, &str)) {- let Ok(entries) = fs::read_dir(dir) else {- return;- };+ // 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.+ let entries = fs::read_dir(dir)+ .unwrap_or_else(|e| panic!("{} unreadable during the snapshot scan: {e}", dir.display()));+for entry in entries.flatten() {let path = entry.path();if path.is_dir() {diff --git a/tests/integration_tests/version_build.rs b/tests/integration_tests/version_build.rsindex da9179e79..aaa9119a0 100644--- a/tests/integration_tests/version_build.rs+++ b/tests/integration_tests/version_build.rs@@ -43,9 +43,12 @@ fn vergen_env_vars_are_read_optionally() {}fn scan_directory(dir: &Path, src_dir: &Path, violations: &mut Vec<String>) {- let Ok(entries) = fs::read_dir(dir) else {- return;- };+ // Swallowing this error would let a missing or relocated `src/` read as a+ // crate with no `env!("VERGEN_…")` in it — the vacuous pass that makes this+ // guard, the one standing between a bare `env!` and another #3123, useless.+ let entries = fs::read_dir(dir)+ .unwrap_or_else(|e| panic!("{} unreadable during the src/ scan: {e}", dir.display()));+for entry in entries.flatten() {let path = entry.path();if path.is_dir() {