Skip to content

🐛 fix(tests): isolate every spawned gwm from the contributor's state - #726

Merged
kbrdn1 merged 9 commits into
devfrom
fix/#724-isolate-test-state
Nov 12, 2026
Merged

kbrdn1 merged 9 commits into
devfrom
fix/#724-isolate-test-state

Conversation

@kbrdn1

@kbrdn1 kbrdn1 commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Description

cargo test wrote the contributor's real undo journal, and left worktrees under the real home. Every test now spawns gwm through one builder, tests/common::gwm(), which points gwm's user-level files (undo journal, trust ledger, global config, aliases.toml, agent-session scan) and $HOME at a per-test scratch directory, and a guard fails if another test file resolves the built binary itself.

Closes #724

Type of change

  • ✨ Feature (new functionality)
  • 🐛 Fix (bug fix)
  • 🚑️ Hotfix (critical production fix)
  • ♻️ Refactor (no behaviour change)
  • 📝 Docs
  • ✅ Tests
  • ⚡ Perf
  • 🔧 Chore / CI / build

Changes

  • ♻️ 379 spawn sites (Command::cargo_bin("gwm").unwrap() x377, std::process::Command::new(env!("CARGO_BIN_EXE_gwm")) x2) now call common::gwm(). Mechanical, no behaviour change on its own.
  • ✅ tests/suite_isolation_tests.rs (red first, commit 05451dda):
    • a gwm remove run by the suite is journaled in the test's scratch journal;
    • on unix, a worktree created with no [worktree] base lands under the test's scratch home;
    • the builder sets every variable the consumer docs' isolation table prescribes (read off docs/5.integrations/4.daemon-consumers.md, so the suite and the documented recipe cannot drift), each pointing inside the scratch dir;
    • the routing guard: across tests/ and benches/, only tests/common/mod.rs may reach the binary (assert_cmd's cargo lookup in every form, CARGO_BIN_EXE_, Command::new("gwm") off PATH), plus a synthetic-source proof that each spelling is flagged.
  • 🐛 common::gwm() sets GWM_HISTORY_FILE, GWM_TRUST_LEDGER, GWM_NO_GLOBAL_CONFIG=1, XDG_CONFIG_HOME, XDG_DATA_HOME, GWM_AGENTS_HOME and HOME. The scratch dir is a thread-local TempDir under CARGO_TARGET_TMPDIR, one per test (libtest runs a test per thread, nextest per process), removed when the test's thread ends: a remove and its undo in one test still share a journal, two tests never do.
  • ✅ The three trust-bypass tests that relied on the default base (create_with_allow_bootstrap_flag_bypasses_the_prompt, create_with_gwm_allow_bootstrap_env_bypasses_the_prompt, allow_bootstrap_succeeds_even_when_ledger_is_malformed) now pin a [worktree] base, so they stay out of the real profile on Windows too, where $HOME does not move {home}.
  • 📝 Consumer docs (EN + FR) name the suite's builder as the reference helper and say what HOME does for the default base. Changelog entry under Fixed. The stale block comment in cli_binary.rs about pinning the base is corrected.

Tests

  • cargo test passes locally (full suite at d2c57e0f: 112 binaries, 3594 passed, 0 failed; the docs commit was re-checked with every binary that reads the docs or the changelog)
  • cargo fmt --check passes
  • cargo clippy -- -D warnings passes (--all-targets)
  • New tests added under tests/ (TDD: a PR adding behaviour without tests is sent back)

Checklist

  • Branch follows <type>/#<issue>-<description>
  • Commits follow Gitmoji + Conventional Commits (see CONTRIBUTING.md)
  • CHANGELOG.md updated under ## [Unreleased]
  • README updated if CLI surface changed (no CLI change)
  • examples/gwm.toml.example updated if config schema changed (no schema change)
  • No unwrap() on user-facing paths (return GwmError instead) (test code only)
  • No println! in TUI render code (status bar only)

Linked issues / docs

Notes for reviewers

Before / after, measured without touching any real journal. The full suite ran twice with HOME pointed at a throwaway dir and GWM_HISTORY_FILE, XDG_DATA_HOME, XDG_CONFIG_HOME unset, so the binary resolved the journal and the default base exactly as it would on a contributor's machine, inside that dir:

dev (8e2eed25) this branch (d2c57e0f)
~/Library/Application Support/gwm/history.toml 8 entries: remove-me, drop-branch, bulk-one, bulk-three, twice, batch-ok, post-a, post-b, all from cli_binary.rs absent
~/cc-worktree 3 worktrees: feat-42-trapped, feat-42-env-trapped, feat-99-broken-ledger absent
test results 111 binaries, 3589 passed 112 binaries, 3594 passed

No in-process test wrote the journal: every entry came from a spawned binary. The worktree leak is the same defect: the three trust tests around cli_binary.rs:4341-4496 write a .gwm.toml with no [worktree] base. On a machine with a global config, the leak followed the global base instead (21 $TMPDIR/.tmp*-worktrees dirs on the reporting machine), which is why GWM_NO_GLOBAL_CONFIG=1 is in the builder, as it already is in CI.

The guards fire on the real tree. Mutated in place and run: a bypass reintroduced in em_dash_guard_tests.rs fails only_the_builder_spawns_the_binary with left: ["tests/common/mod.rs", "tests/em_dash_guard_tests.rs"]; dropping GWM_TRUST_LEDGER from the builder fails the docs-derived test on that variable; dropping HOME fails the base test.

Bounds, stated rather than chased.

  • Windows: dirs::home_dir() does not read $HOME, so on Windows only a pinned base keeps a test's worktree out of the real profile. Every test that needed one pins it now, but nothing turns red if a new one does not. Closing that needs an override in gwm itself: [Task]: let the test suite move the default worktree base on Windows #729.
  • The routing guard does not see a shell snippet that runs gwm off PATH, or a program name held in a variable.
  • One test still runs the real gh with the test process's own HOME (it writes ~/.local/state/gh/device-id, in both runs). That is gh's state, not gwm's, and it is not spawned through gwm; left alone.
  • Leftovers of earlier runs (~/cc-worktree/.tmp*, $TMPDIR/.tmp*-worktrees) are not cleaned up by this PR.

The 377 `Command::cargo_bin("gwm").unwrap()` sites and the two
`std::process::Command::new(env!("CARGO_BIN_EXE_gwm"))` sites now go
through `tests/common::gwm()`. No behaviour change yet: the builder is a
plain `assert_cmd::Command` on the binary. It is the one place the next
commits isolate the spawned gwm from the contributor's state.

refs #724
Red on purpose. Three tests state what `common::gwm()` must do, and fail
against the plain builder of the previous commit:

- a `gwm remove` run by the suite is journaled in the test's scratch
  journal, not in the one the contributor's environment resolves to;
- on unix, a worktree created with no `[worktree] base` lands under the
  test's scratch home, not under `~/cc-worktree`;
- the builder sets every variable the consumer docs prescribe, read off
  the isolation table, each pointing inside the scratch directory.

Plus the routing guard: only `tests/common/mod.rs` may reach the binary,
and a synthetic source proves each bypass spelling is flagged.

refs #724
`common::gwm()` now points the undo journal, the trust ledger, the global
config, `aliases.toml`, the agent-session scan and `$HOME` at a scratch
directory per test (a thread-local `TempDir` under `CARGO_TARGET_TMPDIR`,
removed when the test's thread ends). A local `cargo test` no longer
writes the contributor's real `history.toml`, and a test that creates a
worktree without pinning `[worktree] base` lands under the scratch home
instead of `~/cc-worktree` or the base of the contributor's global config.

On Windows `dirs::home_dir()` does not read `$HOME`, so there the default
base still needs a pinned `[worktree] base`; the comment says so.

refs #724
The consumer docs already prescribed one helper that sets the isolation
variables on every spawned gwm; gwm's own suite now is that helper, so
the docs name it, and say what `HOME` does for the default worktree base
on Linux and macOS but not on Windows. Changelog entry under Fixed.

closes #724
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ceb87e95-8d80-459c-a4d1-2151cdd022bd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
Pre-merge checks: Passed 1 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Title check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
Passed Passed checks: 1 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

`create_with_allow_bootstrap_flag_bypasses_the_prompt`,
`create_with_gwm_allow_bootstrap_env_bypasses_the_prompt` and
`allow_bootstrap_succeeds_even_when_ledger_is_malformed` wrote a
`.gwm.toml` with no `[worktree] base`, so their worktrees went to the
default `{home}/cc-worktree/{repo}`. The builder's `$HOME` covers that on
unix only: on Windows they still landed in the real profile. They were
the only three in the suite (a full run with a throwaway home found
exactly these under `cc-worktree`).

refs #724
The docs-derived check skipped any row it could not parse, and its only
floor was that `GWM_HISTORY_FILE` was among the rows read. A reworded
row (`` `GWM_TRUST_LEDGER` = `<file>` ``) dropped out silently, and the
builder could then lose that variable with every test green. Every body
row of the table must now open on one `NAME=value` code span.

refs #724
"Fails if anything spawns the binary around it" gave the guard more
credit than it has: it reads Rust sources for the built binary's path
and for `gwm` off `PATH`, and does not see a shell snippet. The docs
and the changelog now say that.

refs #724
`statusline_render_shape_survives_the_pipe` kept its fn-local
`use std::process::Command;` after e8e5dd1 moved its spawn to
`common::gwm()`. The file is `cfg(all(windows, feature = "daemon"))`, so
macOS and Linux never compile it, and CI's `RUSTFLAGS: -D warnings` would
have turned the unused import into a red `test (windows-latest)`.

refs #724
The changelog still credited the routing guard with catching a test that
runs `gwm` off `PATH`, which it sees only as a literal
`Command::new("gwm")`, not in a shell snippet. The test file's header
and the builder's doc said "every user-level file"; they now say which.

refs #724
@kbrdn1
kbrdn1 merged commit d16f0b2 into dev Nov 12, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant