Skip to content

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

test: make snapshot env blocks robust to the contributor's environment (#3037)

Worktrunk Bot4bf6ef750bb5
diff --git a/tests/CLAUDE.md b/tests/CLAUDE.md
index 707a82aff..41328b287 100644
--- a/tests/CLAUDE.md
+++ b/tests/CLAUDE.md
@@ -461,6 +461,30 @@ before dismissing:
`add_filter` does **not** work on the `env:` block — it only substitutes on
captured snapshot content; use a redaction.
+`GIT_*` / `WORKTRUNK_*` scrub markers are a special case the harness already
+neutralizes — you don't redact them per-key. `isolate_subprocess_env` scrubs
+whichever of those keys exist in the *parent* environment via
+`Command::env_remove`, and insta-cmd records each removal as `KEY: ""` — so
+which ones appear depends on the host (CI has `GIT_EDITOR`; a contributor's box
+might have `GIT_PAGER`, neither, or both). A dynamic `.env` redaction in
+`add_standard_env_redactions` (`strip_host_scrubbed_env_markers`) drops every
+empty-valued `GIT_*` / `WORKTRUNK_*` entry, so regenerating on any machine
+produces the same block — you don't have to match CI's `GIT_*` environment. The
+explicitly-named unconditional scrubs (`NO_COLOR`, `SHELL`, `PSModulePath`) are
+host-independent and left intact. Because the `info:` metadata is never
+compared, a metadata-only difference never triggers a rewrite, so committed
+snapshots from before this redaction keep their stale markers until their body
+next changes and they're regenerated.
+
+The predicate keys on the empty *value*, so it can't distinguish a scrub marker
+from an affirmatively-set empty `GIT_*` / `WORKTRUNK_*` var — both serialize as
+`KEY: ""`. So an affirmatively-set empty one is dropped too: the known case is
+`test_list_config_env_override_validation_failure`, which sets
+`WORKTRUNK_WORKTREE_PATH=""` as the test's subject. That value is
+host-independent (not the churn this targets), so its `WORKTRUNK_WORKTREE_PATH:
+""` line stays in the committed snapshot until the body next changes, at which
+point the regen drops it — harmless, since the block is never compared.
+
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
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 37f3fe0ad..3009a39eb 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -622,6 +622,69 @@ pub fn add_standard_env_redactions(settings: &mut insta::Settings) {
".env.CARGO_LLVM_COV_TARGET_DIR",
"[CARGO_LLVM_COV_TARGET_DIR]",
);
+
+ // Drop the host-dependent `GIT_*` / `WORKTRUNK_*` scrub markers from the
+ // recorded `env:` block so it depends only on what the test affirmatively
+ // sets — identically on every contributor's machine.
+ //
+ // `isolate_subprocess_env` scrubs every `GIT_*` / `WORKTRUNK_*` key it finds
+ // in the *parent* environment via `Command::env_remove`, and insta-cmd
+ // records each removed key as `KEY: ""` (it serializes `get_envs()`, which
+ // includes removals). Which keys appear is therefore a property of the host:
+ // CI has `GIT_EDITOR`, a contributor's box might have `GIT_PAGER`, neither,
+ // or both. insta never *compares* the `info:` block (only stdout/stderr/exit),
+ // so this only churns regenerated snapshots from machine to machine — but
+ // that churn is exactly what makes contributors think they have to match
+ // their local environment to CI. Normalizing it away here means they don't.
+ //
+ // This runs as its own pass after the per-key `.env.*` redactions above, so
+ // affirmatively-set *non-empty* vars (config paths → `[TEST_CONFIG]`,
+ // coverage → `[LLVM_PROFILE_FILE]`, etc.) already hold non-empty placeholders
+ // and are kept. The unconditional, explicitly-named scrubs (`NO_COLOR`,
+ // `SHELL`, `PSModulePath`) are deterministic across hosts and intentionally
+ // left as-is.
+ //
+ // Caveat: the predicate keys on the empty *value*, which a scrub marker and
+ // an affirmatively-set empty `GIT_*` / `WORKTRUNK_*` var share — in the
+ // recorded YAML both serialize as `KEY: ""`, so this pass can't tell them
+ // apart and drops the affirmatively-set one too. The known case is
+ // `test_list_config_env_override_validation_failure`, which sets
+ // `WORKTRUNK_WORKTREE_PATH=""` as the test's subject (it triggers the
+ // "worktree-path cannot be empty" warning). That value is host-independent,
+ // so it isn't the churn this targets, but the next regen of its snapshot
+ // will drop the `WORKTRUNK_WORKTREE_PATH: ""` line — harmless, since insta
+ // never compares the `env:` block.
+ settings.add_dynamic_redaction(".env", |content, _path| {
+ strip_host_scrubbed_env_markers(content)
+ });
+}
+
+/// Drop empty-valued `GIT_*` / `WORKTRUNK_*` entries from an insta-cmd `env`
+/// map node. These are usually the [`isolate_subprocess_env`](worktrunk::testing)
+/// scrub markers whose presence depends on the host environment; removing them
+/// makes the recorded block host-independent. An affirmatively-set empty value
+/// (e.g. `WORKTRUNK_WORKTREE_PATH=""`) is indistinguishable from a scrub marker
+/// in the recorded YAML and is dropped too — see the caveat on
+/// [`add_standard_env_redactions`].
+fn strip_host_scrubbed_env_markers(
+ content: insta::internals::Content,
+) -> insta::internals::Content {
+ use insta::internals::Content;
+
+ let Content::Map(entries) = content else {
+ return content;
+ };
+ let kept = entries
+ .into_iter()
+ .filter(|(key, value)| {
+ let is_scrub_key = key
+ .as_str()
+ .is_some_and(|k| k.starts_with("GIT_") || k.starts_with("WORKTRUNK_"));
+ let is_empty = value.as_str() == Some("");
+ !(is_scrub_key && is_empty)
+ })
+ .collect();
+ Content::Map(kept)
}
fn canonical_home_dir() -> Option<PathBuf> {
@@ -1400,4 +1463,54 @@ mod tests {
assert_snapshot!(linux_home_above_tempdir, @"▲ [TEST_CONFIG] failed");
});
}
+
+ /// The `env:` redaction drops the host-dependent `GIT_*` / `WORKTRUNK_*`
+ /// scrub markers (`KEY: ""`) while keeping everything a test affirmatively
+ /// sets — including affirmatively-empty non-scrub vars and already-redacted
+ /// scrub vars that hold a placeholder. A non-map node passes through.
+ #[test]
+ fn strip_host_scrubbed_env_markers_normalizes_env_block() {
+ use insta::internals::Content;
+
+ let env = Content::Map(vec![
+ // Host-dependent scrub markers — dropped.
+ (Content::from("GIT_EDITOR"), Content::from("")),
+ (Content::from("GIT_PAGER"), Content::from("")),
+ (Content::from("WORKTRUNK_LEAKED"), Content::from("")),
+ // Affirmatively set vars — kept.
+ (Content::from("CLICOLOR_FORCE"), Content::from("1")),
+ (Content::from("LANG"), Content::from("C")),
+ // A scrub-prefixed var the test set to a real (redacted) value — kept.
+ (
+ Content::from("WORKTRUNK_CONFIG_PATH"),
+ Content::from("[TEST_CONFIG]"),
+ ),
+ // A scrub-prefixed var set to "0", not removed — kept.
+ (
+ Content::from("WORKTRUNK_TEST_CLAUDE_INSTALLED"),
+ Content::from("0"),
+ ),
+ // Unconditional, explicitly-named scrubs are not GIT_/WORKTRUNK_ — kept.
+ (Content::from("NO_COLOR"), Content::from("")),
+ ]);
+
+ let Content::Map(kept) = strip_host_scrubbed_env_markers(env) else {
+ panic!("expected a map back");
+ };
+ let keys: Vec<&str> = kept.iter().filter_map(|(k, _)| k.as_str()).collect();
+ assert_eq!(
+ keys,
+ vec![
+ "CLICOLOR_FORCE",
+ "LANG",
+ "WORKTRUNK_CONFIG_PATH",
+ "WORKTRUNK_TEST_CLAUDE_INSTALLED",
+ "NO_COLOR",
+ ]
+ );
+
+ // Non-map nodes pass through untouched.
+ let scalar = Content::from("not a map");
+ assert_eq!(strip_host_scrubbed_env_markers(scalar.clone()), scalar);
+ }
}