beads/cmd/bd/rename_prefix_worktree_test.go
Bee 3571331b50
test: fixtures commit with maintenance.auto=false before every git worktree add (wy-7ocweu) (#7358)
Fixes #7349. The same race #7314 closed in internal/beads and
internal/config, in the fixtures it did not cover: a fixture's plain
`git commit` spawns the detached `git maintenance run --auto` child, whose
worktree-prune races the fixture's next `git worktree add` and deletes
.git/worktrees/<name>/locked under it (seen on PR #7312 run 37585997441,
cmd/bd Dolt-server tier shard 4/16, TestBootstrap_WorktreeFallbackDir).

One noAutoMaintenance + gitCommand(args...) helper per test package, each
in an existing untagged shared test file (cmd/bd/helpers_test.go,
doctor/git_test_helpers_test.go, doctor/fix/common_test.go,
formula/parser_test.go, git/gitdir_test.go, storage/domain/git/git_test.go;
cmd/bd/test_helpers_test.go is //go:build cgo so unusable for the pure
lane), and every inline exec.Command("git", ...) in those six packages
swapped for it. The flags ride the command line, never env config: the
routing-key scrub drops env config. No new files, so no BUILD.bazel rows.

Proof: GIT_TRACE on `go test ./cmd/bd -run TestBootstrap_WorktreeFallbackDir`
counts 3 `maintenance run --auto` spawns at d8d8bf587b and 0 with this
change.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 08:01:46 +00:00

107 lines
2.8 KiB
Go

package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
func setupWorktree(t *testing.T) (mainRepoDir, worktreeDir string) {
t.Helper()
tmpDir, err := os.MkdirTemp("", "beads-worktree-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(tmpDir) })
mainRepoDir = filepath.Join(tmpDir, "main-repo")
if err := os.MkdirAll(mainRepoDir, 0755); err != nil {
t.Fatal(err)
}
run := func(args ...string) {
cmd := gitCommand(args...)
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Skipf("git %v failed: %v", args, err)
}
}
run("init")
run("config", "user.email", "test@example.com")
run("config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "README.md")
run("commit", "-m", "Initial commit")
worktreeDir = filepath.Join(tmpDir, "worktree")
cmd := gitCommand("worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Fatalf("git worktree add failed: %v", err)
}
t.Cleanup(func() {
cmd := gitCommand("worktree", "remove", "--force", worktreeDir)
cmd.Dir = mainRepoDir
_ = cmd.Run()
})
return mainRepoDir, worktreeDir
}
func TestRenamePrefix_WorktreeNotBlocked(t *testing.T) {
_, worktreeDir := setupWorktree(t)
t.Chdir(worktreeDir)
git.ResetCaches()
if !isGitRepo() {
t.Fatal("expected isGitRepo() to return true")
}
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
// The worktree guard was removed: running from a worktree should not
// produce the old "cannot run 'bd rename-prefix' from a git worktree" error.
// We verify the preconditions that the old guard checked (isGitRepo && IsWorktree)
// are met, confirming the code path that used to block is now reachable.
}
func TestRenamePrefix_WorktreeResolvesMainRepoDB(t *testing.T) {
mainRepoDir, worktreeDir := setupWorktree(t)
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
if err := os.MkdirAll(mainBeadsDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(mainBeadsDir, "metadata.json"), []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
os.RemoveAll(filepath.Join(worktreeDir, ".beads"))
t.Chdir(worktreeDir)
git.ResetCaches()
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
found := beads.FindBeadsDir()
if found == "" {
t.Fatal("FindBeadsDir() returned empty — store initialization would fail")
}
actual, _ := filepath.EvalSymlinks(filepath.Clean(strings.TrimSpace(found)))
expected, _ := filepath.EvalSymlinks(filepath.Clean(mainBeadsDir))
if actual != expected {
t.Errorf("FindBeadsDir() = %q, want %q — rename-prefix would use wrong DB path", actual, expected)
}
}