From fd8da7b13d026dd057b9403485a01a464808c4bf Mon Sep 17 00:00:00 2001 From: ecuthiell Date: Sat, 26 Sep 2026 19:49:44 -0600 Subject: [PATCH] test(cmd/bd): scope fresh-workspace BEADS_DIR isolation (#6519) * test(cmd/bd): share fresh-workspace environment isolation (saa-cgh.120.22) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(cmd/bd): restore mode globals after isolation regression (saa-cgh.120.22) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * docs(test): scope environment restoration claim to BEADS_DIR Address the wording nit in PR #6519 review 5189902686 without widening the helper. Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell --- cmd/bd/config_isolation_regression_test.go | 65 ++++++++++++++++++++++ cmd/bd/test_helpers_pure_test.go | 11 ++++ cmd/bd/test_repo_beads_guard_test.go | 2 + engdocs/TESTING.md | 8 +++ 4 files changed, 86 insertions(+) diff --git a/cmd/bd/config_isolation_regression_test.go b/cmd/bd/config_isolation_regression_test.go index e8fdd9975..23e1bdb0b 100644 --- a/cmd/bd/config_isolation_regression_test.go +++ b/cmd/bd/config_isolation_regression_test.go @@ -75,6 +75,7 @@ func TestInitConfigForTestNeutralizesLeakedRepoBeadsDir(t *testing.T) { // package-global viper holding the dispatched workspace's issue-prefix for every // later test that does not call initConfigForTest. func TestDispatchDoesNotPolluteViperIssuePrefix(t *testing.T) { + isolateBeadsDirForTest(t) ensureCleanGlobalState(t) t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") @@ -98,6 +99,70 @@ func TestDispatchDoesNotPolluteViperIssuePrefix(t *testing.T) { } } +// Command setup uses raw os.Setenv. A fresh fixture must contain that mutation +// even when its caller's BEADS_DIR was absent, explicitly empty, or inherited. +func TestIsolateBeadsDirForTestCommandCleanup(t *testing.T) { + oldServerMode, oldProxiedServerMode := serverMode, proxiedServerMode + t.Cleanup(func() { + serverMode, proxiedServerMode = oldServerMode, oldProxiedServerMode + }) + + ensureCleanGlobalState(t) + t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") + + for _, tc := range []struct { + name string + value string + present bool + }{ + {name: "absent"}, + {name: "empty", present: true}, + {name: "inherited", value: filepath.Join(t.TempDir(), ".beads"), present: true}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("BEADS_DIR", tc.value) + if !tc.present { + if err := os.Unsetenv("BEADS_DIR"); err != nil { + t.Fatal(err) + } + } + + t.Run("command", func(t *testing.T) { + isolateBeadsDirForTest(t) + if got := os.Getenv("BEADS_DIR"); got != "" { + t.Fatalf("fresh fixture inherited BEADS_DIR = %q", got) + } + beadsDir := newLeakFixtureRepo(t) + if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"database":"beads","backend":"dolt"}`+"\n"), 0o600); err != nil { + t.Fatalf("write metadata.json: %v", err) + } + config.ResetForTesting() + t.Cleanup(config.ResetForTesting) + t.Cleanup(resetCommandContext) + + // Exercise the actual binding used by command dispatch, without a store. + prepareSelectedCommandContext(beadsDir, false) + got, err := os.Stat(os.Getenv("BEADS_DIR")) + if err != nil { + t.Fatalf("stat bound BEADS_DIR: %v", err) + } + want, err := os.Stat(beadsDir) + if err != nil { + t.Fatalf("stat fixture: %v", err) + } + if !os.SameFile(got, want) { + t.Fatal("command binding did not select the fixture workspace") + } + }) + + if got, present := os.LookupEnv("BEADS_DIR"); got != tc.value || present != tc.present { + t.Fatalf("after fixture cleanup BEADS_DIR = (%q, %v), want (%q, %v)", + got, present, tc.value, tc.present) + } + }) + } +} + // runVersionDispatch executes `bd --version` through the shared root command with // stdout discarded. Cobra's command tree and os.Stdout are process-global, so this // holds stdioMutex per the policy documented on that mutex. diff --git a/cmd/bd/test_helpers_pure_test.go b/cmd/bd/test_helpers_pure_test.go index 8c7acf479..8375edf1d 100644 --- a/cmd/bd/test_helpers_pure_test.go +++ b/cmd/bd/test_helpers_pure_test.go @@ -80,6 +80,17 @@ func generateUniqueTestID(t *testing.T, prefix string, index int) string { return prefix + "-" + hex.EncodeToString(hash[:])[:8] } +// isolateBeadsDirForTest starts a fresh-workspace fixture without an inherited +// selection and restores BEADS_DIR exactly after command dispatch, even +// when dispatch changes BEADS_DIR with raw os.Setenv. +// Call before fixture setup or dispatch; like t.Setenv, it is not parallel-safe. +// Tests that intentionally select a workspace should set BEADS_DIR explicitly +// instead; initConfigForTest and ensureCleanGlobalState preserve that selection. +func isolateBeadsDirForTest(t *testing.T) { + t.Helper() + t.Setenv("BEADS_DIR", "") +} + // initConfigForTest initializes viper config for a test and ensures cleanup. // main.go's init() calls config.Initialize() which picks up the real .beads/config.yaml. // TestMain resets viper, but any test calling config.Initialize() re-loads the real config. diff --git a/cmd/bd/test_repo_beads_guard_test.go b/cmd/bd/test_repo_beads_guard_test.go index b43c58221..e129d0b55 100644 --- a/cmd/bd/test_repo_beads_guard_test.go +++ b/cmd/bd/test_repo_beads_guard_test.go @@ -196,6 +196,8 @@ func testMainInner(m *testing.M) int { // Clear BEADS_DIR to prevent tests from accidentally picking up the project's // .beads directory via git repo detection when there's a redirect file. // Each test that needs a .beads directory should set BEADS_DIR explicitly. + // This is startup isolation only: fresh in-process command fixtures should + // use isolateBeadsDirForTest before setup to contain later dispatch mutations. origBeadsDir := os.Getenv("BEADS_DIR") os.Unsetenv("BEADS_DIR") defer func() { diff --git a/engdocs/TESTING.md b/engdocs/TESTING.md index a73949e03..d26675b14 100644 --- a/engdocs/TESTING.md +++ b/engdocs/TESTING.md @@ -84,6 +84,14 @@ Tests that need a temporary repository or store should use `t.TempDir()` and `t.Cleanup()`. Temporary repositories must set a repository-local hooks path; do not inherit the developer's global hooks configuration. +In `cmd/bd`, fresh-workspace command fixtures should call +`isolateBeadsDirForTest(t)` before setup or dispatch. It clears inherited +`BEADS_DIR` and restores that variable exactly at cleanup, even after raw +command-dispatch mutations. The `TestMain` reset only isolates startup. +These fixtures must not use `t.Parallel()`. Tests intentionally selecting a +workspace should use `t.Setenv("BEADS_DIR", ...)`; `initConfigForTest` and +`ensureCleanGlobalState` preserve that selection. + For manual CLI experiments, run both initialization and subsequent commands from a disposable working directory: