Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test(testing): default `wt_command()` CWD to an isolated tempdir (#2649)
Maximilian Roosa5a8a49820e3
diff --git a/src/testing/mod.rs b/src/testing/mod.rsindex 572217caf..093479406 100644--- a/src/testing/mod.rs+++ b/src/testing/mod.rs@@ -361,16 +361,37 @@ pub fn scrub_git_path_vars(cmd: &mut Command) {}}+/// A process-scoped empty directory used as the default `current_dir` for+/// [`wt_command`]. Created lazily on first use and kept alive for the rest of+/// the test process; the OS reaps it from the temp dir afterwards (statics+/// aren't dropped at process exit, so `TempDir::drop` doesn't run).+///+/// The dir is guaranteed to be outside any git repository and to have no+/// `.config/wt.toml`, so `wt` invocations spawned through [`wt_command`] don't+/// pick up the test process's inherited CWD (which is typically the worktrunk+/// repo root, with its own `.config/wt.toml` and git history).+fn isolated_test_cwd() -> &'static Path {+ static ISOLATED_CWD: std::sync::LazyLock<TempDir> =+ std::sync::LazyLock::new(|| TempDir::new().expect("create isolated test cwd"));+ ISOLATED_CWD.path()+}+/// Create a `wt` CLI command with standardized test environment settings.////// The command has the following guarantees:/// - All host `GIT_*` and `WORKTRUNK_*` variables are cleared/// - Color output is forced (`CLICOLOR_FORCE=1`) so ANSI styling appears in snapshots/// - Terminal width set to 150 columns (`COLUMNS=150`)+/// - `current_dir` defaults to a process-scoped empty tempdir (not a git repo,+/// no project config), so `wt` doesn't pick up worktrunk's own+/// `.config/wt.toml` or detect a git repo from the test process's inherited+/// CWD. Tests that need a specific CWD must override via+/// `cmd.current_dir(...)`; `repo.wt_command()` does so automatically.#[must_use]pub fn wt_command() -> Command {let mut cmd = Command::new(wt_bin());configure_cli_command(&mut cmd);+ cmd.current_dir(isolated_test_cwd());cmd}diff --git a/tests/CLAUDE.md b/tests/CLAUDE.mdindex 83d26d1d9..e20c45b5e 100644--- a/tests/CLAUDE.md+++ b/tests/CLAUDE.md@@ -47,6 +47,12 @@ let output = wt_command().output()?;```+`wt_command()`'s default `current_dir` is a process-scoped empty tempdir+(outside any git repo, no project config) — so a bare `wt_command()` won't+pick up the test process's inherited CWD. Tests that need a specific CWD+(e.g., the worktrunk repo root for `readme_sync` help-text capture) must+call `.current_dir(...)` explicitly.+### Method reference| Method | Returns | Use when |diff --git a/tests/integration_tests/config_init.rs b/tests/integration_tests/config_init.rsindex 9e517790f..75cc620d3 100644--- a/tests/integration_tests/config_init.rs+++ b/tests/integration_tests/config_init.rs@@ -23,10 +23,6 @@ fn test_config_init_already_exists(temp_home: TempDir) {settings.bind(|| {let mut cmd = wt_command();cmd.arg("config").arg("create");- // Run from a directory with no project config so the hint is- // deterministic — bare wt_command() otherwise inherits the- // worktrunk repo's CWD and would pick up `.config/wt.toml`.- cmd.current_dir(temp_home.path());set_temp_home_env(&mut cmd, temp_home.path());set_xdg_config_path(&mut cmd, temp_home.path());