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
This commit is contained in:
parent
566c84ba8e
commit
fd8da7b13d
4 changed files with 86 additions and 0 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue