fix: make tests resilient to project .beads/redirect

Tests were failing because beads.FindDatabasePath() follows the
project's .beads/redirect file, causing tests to find unexpected
databases. Fixed by:

- Setting BEADS_DIR in tests that need isolation from git repo detection
- Clearing BEADS_DIR in TestMain to prevent global contamination
- Updating migration test schema to include owner column

This ensures tests work correctly in crew directories that have
redirect files pointing to shared .beads directories.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Executed-By: beads/crew/dave
Rig: beads
Role: crew
This commit is contained in:
beads/crew/dave
2026-01-10 20:39:55 -08:00
committed by Steve Yegge
parent f79e636000
commit ac24a63187
6 changed files with 92 additions and 16 deletions

View File

@@ -466,16 +466,27 @@ func TestInitNoDbMode(t *testing.T) {
// Reset global state
origDBPath := dbPath
origNoDb := noDb
defer func() {
defer func() {
dbPath = origDBPath
noDb = origNoDb
}()
dbPath = ""
noDb = false
tmpDir := t.TempDir()
t.Chdir(tmpDir)
// Set BEADS_DIR to prevent git repo detection from finding project's .beads
origBeadsDir := os.Getenv("BEADS_DIR")
os.Setenv("BEADS_DIR", filepath.Join(tmpDir, ".beads"))
defer func() {
if origBeadsDir != "" {
os.Setenv("BEADS_DIR", origBeadsDir)
} else {
os.Unsetenv("BEADS_DIR")
}
}()
// Initialize with --no-db flag
rootCmd.SetArgs([]string{"init", "--no-db", "--no-daemon", "--prefix", "test", "--quiet"})

View File

@@ -29,6 +29,17 @@ func TestMain(m *testing.M) {
}
}()
// 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.
origBeadsDir := os.Getenv("BEADS_DIR")
os.Unsetenv("BEADS_DIR")
defer func() {
if origBeadsDir != "" {
os.Setenv("BEADS_DIR", origBeadsDir)
}
}()
if os.Getenv("BEADS_TEST_GUARD_DISABLE") != "" {
os.Exit(m.Run())
}

View File

@@ -86,6 +86,18 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) {
// Reset git caches after changing directory (required for IsWorktree to re-detect)
git.ResetCaches()
// Set BEADS_DIR to the test's .beads directory to prevent
// git repo detection from finding the project's .beads
origBeadsDir := os.Getenv("BEADS_DIR")
os.Setenv("BEADS_DIR", mainDir+"/.beads")
defer func() {
if origBeadsDir != "" {
os.Setenv("BEADS_DIR", origBeadsDir)
} else {
os.Unsetenv("BEADS_DIR")
}
}()
// No sync-branch configured
os.Unsetenv("BEADS_SYNC_BRANCH")
@@ -217,6 +229,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) {
// Reset git caches after changing directory
git.ResetCaches()
// Set BEADS_DIR to the test's .beads directory to prevent
// git repo detection from finding the project's .beads
origBeadsDir := os.Getenv("BEADS_DIR")
os.Setenv("BEADS_DIR", mainDir+"/.beads")
defer func() {
if origBeadsDir != "" {
os.Setenv("BEADS_DIR", origBeadsDir)
} else {
os.Unsetenv("BEADS_DIR")
}
}()
// Clear all daemon-related env vars
os.Unsetenv("BEADS_NO_DAEMON")
os.Unsetenv("BEADS_AUTO_START_DAEMON")