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>
226 lines
6.7 KiB
Go
226 lines
6.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/git"
|
|
)
|
|
|
|
func TestBootstrap_WorktreeFallbackDir(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "beads-bootstrap-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(dir string, args ...string) {
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
|
|
run(mainRepoDir, "init")
|
|
run(mainRepoDir, "config", "user.email", "test@example.com")
|
|
run(mainRepoDir, "config", "user.name", "Test User")
|
|
|
|
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run(mainRepoDir, "add", "README.md")
|
|
run(mainRepoDir, "commit", "-m", "Initial commit")
|
|
|
|
worktreeDir := filepath.Join(tmpDir, "worktree")
|
|
cmd := gitCommand("worktree", "add", worktreeDir, "HEAD")
|
|
cmd.Dir = mainRepoDir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git worktree add failed: %v\n%s", err, out)
|
|
}
|
|
t.Cleanup(func() {
|
|
cleanupCmd := gitCommand("worktree", "remove", "--force", worktreeDir)
|
|
cleanupCmd.Dir = mainRepoDir
|
|
_ = cleanupCmd.Run()
|
|
})
|
|
|
|
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
|
|
if err := os.MkdirAll(filepath.Join(mainBeadsDir, "dolt"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
worktreeBeadsDir := filepath.Join(worktreeDir, ".beads")
|
|
_ = os.RemoveAll(worktreeBeadsDir)
|
|
|
|
t.Chdir(worktreeDir)
|
|
git.ResetCaches()
|
|
|
|
if !git.IsWorktree() {
|
|
t.Fatal("expected git.IsWorktree() to return true in worktree")
|
|
}
|
|
|
|
got := beads.GetWorktreeFallbackBeadsDir()
|
|
if got == "" {
|
|
t.Fatal("GetWorktreeFallbackBeadsDir() returned empty string")
|
|
}
|
|
|
|
gotClean := filepath.Clean(strings.TrimSpace(got))
|
|
wantClean := filepath.Clean(strings.TrimSpace(mainBeadsDir))
|
|
|
|
gotResolved, err := filepath.EvalSymlinks(gotClean)
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", gotClean, err)
|
|
}
|
|
wantResolved, err := filepath.EvalSymlinks(wantClean)
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q) failed: %v", wantClean, err)
|
|
}
|
|
|
|
if gotResolved != wantResolved {
|
|
t.Errorf("GetWorktreeFallbackBeadsDir() = %q (resolved %q), want %q (resolved %q)",
|
|
gotClean, gotResolved, wantClean, wantResolved)
|
|
}
|
|
|
|
cwd, _ := os.Getwd()
|
|
cwdBeads := filepath.Join(cwd, ".beads")
|
|
cwdResolved, _ := filepath.EvalSymlinks(filepath.Clean(cwdBeads))
|
|
if gotResolved == cwdResolved {
|
|
t.Errorf("GetWorktreeFallbackBeadsDir() returned CWD-based path %q, expected main repo path", gotResolved)
|
|
}
|
|
}
|
|
|
|
func TestBootstrap_WorktreeLocalBeadsPreferLocal(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "beads-bootstrap-local-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(dir string, args ...string) {
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
run(mainRepoDir, "init")
|
|
run(mainRepoDir, "config", "user.email", "test@example.com")
|
|
run(mainRepoDir, "config", "user.name", "Test User")
|
|
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run(mainRepoDir, "add", "README.md")
|
|
run(mainRepoDir, "commit", "-m", "Initial commit")
|
|
|
|
worktreeDir := filepath.Join(tmpDir, "worktree")
|
|
cmd := gitCommand("worktree", "add", worktreeDir, "HEAD")
|
|
cmd.Dir = mainRepoDir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git worktree add failed: %v\n%s", err, out)
|
|
}
|
|
t.Cleanup(func() {
|
|
cleanupCmd := gitCommand("worktree", "remove", "--force", worktreeDir)
|
|
cleanupCmd.Dir = mainRepoDir
|
|
_ = cleanupCmd.Run()
|
|
})
|
|
|
|
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
|
|
worktreeBeadsDir := filepath.Join(worktreeDir, ".beads")
|
|
if err := os.MkdirAll(filepath.Join(mainBeadsDir, "dolt"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(worktreeBeadsDir, "dolt"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
foundResolved, _ := filepath.EvalSymlinks(filepath.Clean(found))
|
|
worktreeBeadsResolved, _ := filepath.EvalSymlinks(filepath.Clean(worktreeBeadsDir))
|
|
|
|
if foundResolved != worktreeBeadsResolved {
|
|
t.Errorf("FindBeadsDir() = %q, want %q (local worktree .beads should take precedence over main repo fallback)",
|
|
foundResolved, worktreeBeadsResolved)
|
|
}
|
|
}
|
|
|
|
func TestBootstrap_WorktreeNoBeadsAnywhereStillPointsToMainRepo(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "beads-bootstrap-empty-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(dir string, args ...string) {
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
run(mainRepoDir, "init")
|
|
run(mainRepoDir, "config", "user.email", "test@example.com")
|
|
run(mainRepoDir, "config", "user.name", "Test User")
|
|
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run(mainRepoDir, "add", "README.md")
|
|
run(mainRepoDir, "commit", "-m", "Initial commit")
|
|
|
|
worktreeDir := filepath.Join(tmpDir, "worktree")
|
|
cmd := gitCommand("worktree", "add", worktreeDir, "HEAD")
|
|
cmd.Dir = mainRepoDir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git worktree add failed: %v\n%s", err, out)
|
|
}
|
|
t.Cleanup(func() {
|
|
cleanupCmd := gitCommand("worktree", "remove", "--force", worktreeDir)
|
|
cleanupCmd.Dir = mainRepoDir
|
|
_ = cleanupCmd.Run()
|
|
})
|
|
|
|
os.RemoveAll(filepath.Join(worktreeDir, ".beads"))
|
|
|
|
t.Chdir(worktreeDir)
|
|
git.ResetCaches()
|
|
|
|
fallback := beads.GetWorktreeFallbackBeadsDir()
|
|
if fallback == "" {
|
|
t.Fatal("GetWorktreeFallbackBeadsDir() returned empty")
|
|
}
|
|
|
|
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
|
|
fallbackResolved, _ := filepath.EvalSymlinks(filepath.Clean(fallback))
|
|
mainResolved, _ := filepath.EvalSymlinks(filepath.Clean(mainBeadsDir))
|
|
if fallbackResolved != mainResolved {
|
|
t.Errorf("GetWorktreeFallbackBeadsDir() = %q, want %q (should resolve to main repo even without .beads)", fallbackResolved, mainResolved)
|
|
}
|
|
}
|