A test that shells out to a real CLI with side effects can change the user's real config
Symptom#
After cargo test (or any test run), the developer's real tool configuration has changed: for example a plugin and a marketplace installed in their real Claude Code, pointing at a temporary folder that no longer exists.
Cause#
The code under test runs an external command with side effects (here claude plugin marketplace add and claude plugin install) through a program name that defaults to the real command unless an environment variable names a fake. Tests that forget the fake, often older tests written before the command was added, run the real command against the real $HOME.
Fix#
Make the program fail closed in test builds, so a forgotten fake fails instead of reaching the real tool:
Do the same for every path taken from the environment (XDG_DATA_HOME, XDG_CACHE_HOME, HOME), and give integration tests their own HOME, XDG_*, working directory and a fake script on PATH. Clean up anything that leaked with the tool's own commands, not by editing its files.
Evidence#
In rkb (project rkb), a doctor unit test called the install plan without the fake and ran the real claude: it left rkb@rkb, a rkb marketplace and ~/.claude/plugins/cache/rkb/rkb/0.1.0-664efa454a8a in the real Claude Code. claude plugin uninstall rkb@rkb and claude plugin marketplace remove rkb removed them; after the cfg!(test) guard, repeated full test runs left the real config unchanged. Sharing the real cache folder also made a test fail now and then in parallel runs.
Later case: Variant in reagent: older CLI integration tests ran the reagent binary without a temp XDG_STATE_HOME, so 28 test sessions landed in the real $HOME/.local/state/reagent/sessions. Fix: set XDG_STATE_HOME to a per-test temp folder in every test file (7 files needed it), then check after a full run that the state folder holds only auth files.
Also found by searching
- claude plugin list shows rkb@rkb from a marketplace pointing at /var/folders/.../T/.tmpXXXX after cargo test
- running my test suite installed a plugin into my real Claude Code config pointing at a deleted temp folder
- how do I stop unit tests from running a real external CLI with side effects against my real HOME?
- tests forgot the fake program env var and invoked the real binary, polluted ~/.claude, cfg!(test) guard
- after cargo test my real Claude Code has a plugin and marketplace installed pointing at a deleted tmp dir
- unit test ran the real external CLI against my home directory and changed my config
- how do I make sure tests never invoke the real command-line tool with side effects in Rust?
- cfg!(test) guard so program name falls back to a nonexistent binary, isolate HOME and XDG in tests