Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test: guard committed snapshots against host-specific paths (#3026)
Maximilian Roosbd278e5d1f4b
diff --git a/src/testing/mock_commands.rs b/src/testing/mock_commands.rsindex 581e37db7..31a3483b5 100644--- a/src/testing/mock_commands.rs+++ b/src/testing/mock_commands.rs@@ -291,32 +291,6 @@ API authentication..write(bin_dir);}-/// Create a mock llm command for API endpoint commits.-pub fn create_mock_llm_api(bin_dir: &Path) {- MockConfig::new("llm")- .command(- "_default",- MockResponse::output(- "feat(api): Add user authentication endpoints--Implement login and token refresh endpoints with JWT validation.-Includes comprehensive test coverage and input validation.",- ),- )- .write(bin_dir);-}--/// Create a mock llm command for quickstart documentation.-/// Simple output for clean documentation examples.-pub fn create_mock_llm_quickstart(bin_dir: &Path) {- MockConfig::new("llm")- .command(- "_default",- MockResponse::output("Add authentication module"),- )- .write(bin_dir);-}-/// Create a mock uv command for dependency sync and dev server.pub fn create_mock_uv_sync(bin_dir: &Path) {MockConfig::new("uv")diff --git a/tests/CLAUDE.md b/tests/CLAUDE.mdindex a32a6f272..707a82aff 100644--- a/tests/CLAUDE.md+++ b/tests/CLAUDE.md@@ -461,6 +461,25 @@ before dismissing:`add_filter` does **not** work on the `env:` block — it only substitutes oncaptured snapshot content; use a redaction.+The `args:` block has the same property: a repo path passed as a CLI argument+(`wt -C <root>`) is covered by the `.args[]` redaction in+`add_repo_and_worktree_path_filters`, which rewrites it to `_REPO_…` like the+body filters; any other run-specific argument needs its own redaction.++Path leaks fail `test_no_host_specific_paths_in_snapshots`+(`snapshot_formatting_guard.rs`), which scans every committed `.snap` for+host-specific path markers (other run-specific values — PIDs, timestamps —+still need review-time vigilance). The test exists because insta never *compares* the+`info:` block — a missing redaction passes on the machine that generated the+snapshot and only churns when regenerated elsewhere.++A runner caveat: the `repo` fixture leaks its settings binding (rstest has no+teardown), so under libtest's reused threads a test that binds no settings of+its own — or clones them via `Settings::clone_current()` without+`add_standard_env_redactions` — can still appear redacted. nextest (process+per test) is authoritative — regenerate snapshots with+`cargo insta test --test-runner nextest` when in doubt.+### Inline snapshots over multi-assertWhen a test checks formatted output, use `insta::assert_snapshot!` with andiff --git a/tests/common/mod.rs b/tests/common/mod.rsindex 5edfeda73..ec69b3d4a 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -117,9 +117,17 @@ pub fn pty_safe() {#[rstest::fixture]pub fn repo() -> TestRepo {let repo = TestRepo::standard();- // Bind insta snapshot filters for this test thread. `mem::forget` intentionally- // leaks the scope guard so settings persist without storing the guard in TestRepo.- // Safe: each test sets its own settings, and thread-locals are cleaned up on exit.+ // Bind insta snapshot filters for this test thread, leaking the guard:+ // rstest has no teardown, and the guard can't ride along in TestRepo+ // (insta is a dev-dependency, invisible to the lib crate defining+ // TestRepo; the guard is also !Send by design). Under nextest (process+ // per test) the leak ends with the process. Under libtest the binding+ // persists on the reused worker thread and bleeds into later tests on+ // it — so a test must never rely on settings it didn't bind itself,+ // and a missing redaction can be masked here but exposed under nextest.+ // `test_no_host_specific_paths_in_snapshots` (snapshot_formatting_guard)+ // enforces the observable invariant: no host-specific paths in committed+ // snapshots.let guard =setup_snapshot_settings_for_paths(repo.root_path(), &repo.worktrees).bind_to_scope();std::mem::forget(guard);@@ -677,6 +685,27 @@ fn add_repo_and_worktree_path_filters(settings.add_filter(®ex::escape(&root_str_normalized), "_REPO_");settings.add_filter(®ex::escape(&to_posix_path(root_str)), "_REPO_");+ // Filters rewrite snapshot *content* only; the structured `info` block+ // insta-cmd records is reachable solely via redactions. A test that+ // passes a repo path as a CLI argument (`wt -C <root> list`) would+ // otherwise bake the per-test temp path into the snapshot's `args:`+ // block. Mirror the body filters: root-prefixed args become `_REPO_…`.+ // No POSIX form here — args are built in-process from `root_path()`.+ let arg_prefixes = [root_str.to_string(), root_str_normalized.clone()];+ settings.add_dynamic_redaction(".args[]", move |value, _path| {+ if let Some(arg) = value.as_str() {+ for prefix in &arg_prefixes {+ if let Some(suffix) = arg.strip_prefix(prefix.as_str()) {+ return insta::internals::Content::from(format!(+ "_REPO_{}",+ suffix.replace('\\', "/")+ ));+ }+ }+ }+ value+ });+// In tests, HOME is set to the temp directory containing the repo. Commands being tested// see HOME=temp_dir, so format_path_for_display() outputs ~/repo instead of the full path.// The repo is always at {temp_dir}/repo, so we hardcode ~/repo for the filter.diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 269138f73..11789305d 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -1,10 +1,8 @@use crate::common::{- DAY, HOUR, MINUTE, TestRepo, list_snapshots, make_snapshot_cmd,- mock_commands::create_mock_llm_quickstart, repo, repo_with_remote,+ DAY, HOUR, MINUTE, TestRepo, list_snapshots, make_snapshot_cmd, repo, repo_with_remote,setup_snapshot_settings_for_paths, wt_command,};use insta_cmd::assert_cmd_snapshot;-use path_slash::PathExt as _;use rstest::rstest;/// Creates worktrees with specific timestamps for ordering tests.@@ -2170,25 +2168,16 @@ mod tests {.join(".wt-directive-temp");std::fs::write(&directive_file, "").unwrap();- // Create a cross-platform mock LLM command (uses mock-stub binary system)- // The mock-bin directory is already created by setup_mock_gh() via the repo fixture- let mock_bin_dir = repo.root_path().parent().unwrap().join("mock-bin");- create_mock_llm_quickstart(&mock_bin_dir);-- // Configure the LLM path (Windows needs .exe extension)- let llm_name = if cfg!(windows) { "llm.exe" } else { "llm" };- let llm_path = mock_bin_dir.join(llm_name);-// Merge feature-auth into mainassert_cmd_snapshot!("quickstart_merge", {let mut cmd = make_snapshot_cmd(&repo, "merge", &["main"], Some(&feature_auth));cmd.env("WORKTRUNK_DIRECTIVE_CD_FILE", &directive_file);- // Set MOCK_CONFIG_DIR so mock-stub can find llm.json- cmd.env("MOCK_CONFIG_DIR", &mock_bin_dir);- // Use to_slash_lossy() for Windows compatibility - bash can't handle backslash paths+ // Inline generation command (the suite's standard mock): a binary+ // path here would bake the per-test temp dir into the snapshot's+ // env block, which redactions don't cover for this key.cmd.env("WORKTRUNK_COMMIT__GENERATION__COMMAND",- llm_path.to_slash_lossy().as_ref(),+ "cat >/dev/null && echo 'Add authentication module'",);cmd});@@ -3257,9 +3246,8 @@ fn test_list_nested_worktree_json_is_current(mut repo: TestRepo) {#[test]fn test_list_empty_repo() {let repo = TestRepo::empty();- let guard =+ let _settings_guard =setup_snapshot_settings_for_paths(repo.root_path(), &repo.worktrees).bind_to_scope();- std::mem::forget(guard);// Pre-set default branch so the unborn-HEAD case has something to resolve torepo.run_git(&["config", "worktrunk.default-branch", "main"]);// Should show the branch with empty commit columns and no errorsdiff --git a/tests/integration_tests/snapshot_formatting_guard.rs b/tests/integration_tests/snapshot_formatting_guard.rsindex cacd69923..0096b8a10 100644--- a/tests/integration_tests/snapshot_formatting_guard.rs+++ b/tests/integration_tests/snapshot_formatting_guard.rs@@ -1,22 +1,39 @@-//! Guard test to catch formatting violations in snapshot files+//! Guard tests over the committed snapshot corpus.//!-//! Snapshots capture real output and are approved by reviewers. Subtle formatting-//! issues (stray blank lines, detached hints) can slip through review. This test-//! scans all snapshot files to enforce output formatting rules.+//! Snapshots capture real output and are approved by reviewers. Subtle+//! problems slip through review: formatting issues (stray blank lines,+//! detached hints), and host-specific paths that insta never compares.+//! These tests scan every committed `.snap` file to enforce the rules.//!//! Rules enforced://! - **Hints attach to their subject** — no blank line before `↳`//! - **No double blank lines** — one blank line maximum between elements+//! - **No host-specific paths** — insta compares only snapshot *content*+//! (stdout/stderr/exit code); the `info:` block insta-cmd records+//! (`args:`, `env:`) is metadata that is written but never compared, and+//! `add_filter` doesn't apply to it. A test missing a redaction therefore+//! bakes the generating machine's paths into the committed file while+//! passing everywhere — surfacing only as churn when someone regenerates+//! the snapshot on another machine (and under libtest, the `repo`+//! fixture's leaked settings binding can mask the omission; see `repo()`+//! in `tests/common`). This rule makes the leak fail deterministically.//!-//! When this test fails:+//! When a formatting rule fails://! 1. Fix the source code producing the bad output//! 2. Re-run the failing test to regenerate the snapshot//! 3. If the blank line is intentional (e.g., phase boundary before a status//! item that uses `↳`), add the snapshot to the allowlist with a comment+//!+//! When the host-path rule fails: add a redaction — env values go in+//! `add_standard_env_redactions`, path arguments are covered by the+//! `.args[]` redaction in `add_repo_and_worktree_path_filters` (both in+//! `tests/common/mod.rs`); see tests/CLAUDE.md "Snapshot env drift".use ansi_str::AnsiStr;+use regex::Regex;use std::fs;use std::path::Path;+use std::sync::LazyLock;/// Snapshots where a blank line before `↳` is intentional (phase boundary, not/// a detached hint). Each entry needs a comment explaining why.@@ -103,21 +120,88 @@ fn test_no_double_blank_lines_in_snapshot_output() {}}-fn for_each_snapshot(project_root: &Path, mut f: impl FnMut(&Path, &str)) {- let snap_dirs = [- project_root.join("tests/snapshots"),- project_root.join("tests/integration_tests/snapshots"),- ];+/// Markers that only ever come from the generating machine: macOS per-user+/// temp dirs, the LLVM profile fallback dir (always under the host temp+/// dir), and tempfile's random `.tmpXXXXXX` directories.+static HOST_MARKERS: LazyLock<[Regex; 3]> = LazyLock::new(|| {+ [+ Regex::new(r"/var/folders/").unwrap(),+ Regex::new(r"wt-test-profraw").unwrap(),+ Regex::new(r"\.tmp[A-Za-z0-9]{6}").unwrap(),+ ]+});- for dir in &snap_dirs {- let Ok(entries) = fs::read_dir(dir) else {- continue;- };- for entry in entries.flatten() {- let path = entry.path();- if path.extension().and_then(|s| s.to_str()) != Some("snap") {- continue;+/// Home-style paths are allowed only for the deliberate fake users in docs+/// examples and mocked output; a real username means an unredacted $HOME+/// leaked. `{1,2}` separators: YAML double-quoted scalars escape+/// backslashes, so a Windows path arrives as `C:\\Users\\name`.+static HOME_PATH: LazyLock<Regex> =+ LazyLock::new(|| Regex::new(r"[/\\]{1,2}(?:Users|home)[/\\]{1,2}([A-Za-z0-9._-]+)").unwrap());++const FAKE_USERS: &[&str] = &["me", "user"];++fn line_has_host_specific_path(line: &str) -> bool {+ HOST_MARKERS.iter().any(|re| re.is_match(line))+ || HOME_PATH+ .captures_iter(line)+ .any(|c| !FAKE_USERS.contains(&&c[1]))+}++#[test]+fn test_no_host_specific_paths_in_snapshots() {+ let project_root = Path::new(env!("CARGO_MANIFEST_DIR"));+ let mut violations = Vec::new();++ for_each_snapshot(project_root, |path, content| {+ for (i, line) in content.lines().enumerate() {+ if line_has_host_specific_path(line) {+ let relative = path.strip_prefix(project_root).unwrap_or(path);+ violations.push(format!("{}:{}: {}", relative.display(), i + 1, line.trim()));}+ }+ });++ if !violations.is_empty() {+ panic!(+ "Host-specific paths in {} committed snapshot line(s):\n\n{}\n\n\+ These churn whenever the snapshot is regenerated on another machine.\n\+ Add a redaction: env values in `add_standard_env_redactions`, path\n\+ arguments via the `.args[]` redaction in `add_repo_and_worktree_path_filters`\n\+ (tests/common/mod.rs); see tests/CLAUDE.md \"Snapshot env drift\".\n\+ Deliberate example paths use the fake users in FAKE_USERS.",+ violations.len(),+ violations.join("\n"),+ );+ }+}++/// 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);+ });+ }+ // 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;+ };+ for entry in entries.flatten() {+ 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();f(&path, &content);}@@ -171,6 +255,42 @@ fn extract_output_sections(content: &str) -> Vec<(&str, &str)> {mod tests {use super::*;+ /// The corpus test passes vacuously if the detector rots — pin its+ /// sensitivity on both sides. Leak lines are the shapes actually found+ /// in committed snapshots (#3009 and the `args:` leak this fixed).+ #[test]+ fn test_host_specific_path_detection() {+ let leaks = [+ // macOS per-user temp dir (env value, the #3009 class)+ "LLVM_PROFILE_FILE: /var/folders/v3/8k5q0_6j3/T/wt-test-profraw/cov.profraw",+ // tempfile-crate dir in the args: block+ "- /private/var/folders/wf/s6ycxvvs/T/.tmpSeGxzx/repo",+ // tempfile-crate dir under /tmp (Linux)+ "path: /tmp/.tmpAbC123/repo",+ // real home dirs, Unix and YAML-escaped Windows+ "HOME: /Users/maximilian/workspace",+ "HOME: /home/runner/work",+ r#"USERPROFILE: "C:\\Users\\runneradmin\\AppData""#,+ ];+ for line in leaks {+ assert!(line_has_host_specific_path(line), "should flag: {line}");+ }++ let deterministic = [+ // deliberate fake users in docs examples and mocked output+ "e.g., /Users/me/code/myproject",+ "To /Users/user/workspace/repo/.git",+ r#"PSModulePath: "C:\\Users\\user\\Documents""#,+ // machine-independent literals and placeholders+ "WORKTRUNK_CONFIG_PATH: /nonexistent/wt/config.toml",+ "LLVM_PROFILE_FILE: \"[LLVM_PROFILE_FILE]\"",+ "WORKTRUNK_SYSTEM_CONFIG_PATH: /etc/xdg/worktrunk/config.toml",+ ];+ for line in deterministic {+ assert!(!line_has_host_specific_path(line), "should allow: {line}");+ }+ }+#[test]fn test_extract_output_sections_ignores_mid_line_dashes() {// Simulate a snapshot whose output body contains `--- a/file.txt` (git diff).diff --git a/tests/snapshots/integration__integration_tests__list__list_with_c_flag.snap b/tests/snapshots/integration__integration_tests__list__list_with_c_flag.snapindex fd51e5195..ddadc691b 100644--- a/tests/snapshots/integration__integration_tests__list__list_with_c_flag.snap+++ b/tests/snapshots/integration__integration_tests__list__list_with_c_flag.snap@@ -4,7 +4,7 @@ info:program: wtargs:- "-C"- - /private/var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpSeGxzx/repo+ - _REPO_- listenv:CLICOLOR_FORCE: "1"@@ -12,21 +12,27 @@ info:GIT_EDITOR: ""LANG: CLC_ALL: C+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"NO_COLOR: ""PSModulePath: ""- RUST_LOG: warnSHELL: ""TERM: alacrittyWORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_BASH_INSTALLED: "0"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_CODEX_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_FISH_INSTALLED: "0"+ WORKTRUNK_TEST_GEMINI_INSTALLED: "0"WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"---success: trueexit_code: 0diff --git a/tests/snapshots/integration__integration_tests__list__quickstart_merge.snap b/tests/snapshots/integration__integration_tests__list__quickstart_merge.snapindex 5193da3ad..1fa99db0d 100644--- a/tests/snapshots/integration__integration_tests__list__quickstart_merge.snap+++ b/tests/snapshots/integration__integration_tests__list__quickstart_merge.snap@@ -23,27 +23,33 @@ info:HOME: "[TEST_HOME]"LANG: CLC_ALL: C+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"NO_COLOR: ""OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"PATH: "[PATH]"PSModulePath: ""- RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[TEST_HOME]"WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"- WORKTRUNK_COMMIT__GENERATION__COMMAND: /private/var/folders/v3/8k5q0_6j3_q6gl4h8czr66sh0000gn/T/.tmpW446k6/mock-bin/llm+ WORKTRUNK_COMMIT__GENERATION__COMMAND: "cat >/dev/null && echo 'Add authentication module'"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"WORKTRUNK_DIRECTIVE_CD_FILE: "[DIRECTIVE_CD_FILE]"WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_BASH_INSTALLED: "0"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_CODEX_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_FISH_INSTALLED: "0"+ WORKTRUNK_TEST_GEMINI_INSTALLED: "0"WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"---success: true