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>
133 lines
4.7 KiB
Go
133 lines
4.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/gitenv"
|
|
)
|
|
|
|
// TestBeadsRoleWriterIgnoresInheritedGitRouting pins the init role pair to the
|
|
// same boundary as every beads.role reader. The reader half of the stack was
|
|
// hardened first, which is what makes the writer half load-bearing: with the
|
|
// writer still inherited, `GIT_DIR=<other repo>/.git bd init` writes the role
|
|
// into that other repository, then reports success while every scrubbed reader
|
|
// looks at the repository the user is actually standing in and finds nothing.
|
|
//
|
|
// Both subtests must assert *where* the value landed. A roundtrip assertion is
|
|
// vacuous here: before the fix the write and the read followed the same
|
|
// redirect, so they agreed with each other about the wrong repository.
|
|
func TestBeadsRoleWriterIgnoresInheritedGitRouting(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("role routing fixture requires Git")
|
|
}
|
|
for _, entry := range os.Environ() {
|
|
key := gitenv.EntryKey(entry)
|
|
if gitenv.IsRoutingKeyForOS(key, runtime.GOOS) {
|
|
t.Setenv(key, "")
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// localRole reads repo's own config file directly, so it reports where a
|
|
// value physically landed rather than what discovery would resolve to.
|
|
localRole := func(t *testing.T, repo string) string {
|
|
t.Helper()
|
|
cmd := gitCommand("config", "--local", "--get", "beads.role")
|
|
cmd.Dir = repo
|
|
cmd.Env = gitenv.ScrubRoutingAndSuppression(os.Environ())
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
setLocalRole := func(t *testing.T, repo, role string) {
|
|
t.Helper()
|
|
cmd := gitCommand("config", "--local", "beads.role", role)
|
|
cmd.Dir = repo
|
|
cmd.Env = gitenv.ScrubRoutingAndSuppression(os.Environ())
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("fixture git config: %v: %s", err, out)
|
|
}
|
|
}
|
|
poison := func(t *testing.T, decoy string) {
|
|
t.Helper()
|
|
t.Setenv("GIT_DIR", filepath.Join(decoy, ".git"))
|
|
t.Setenv("GIT_WORK_TREE", decoy)
|
|
}
|
|
|
|
t.Run("write stays in the selected repository", func(t *testing.T) {
|
|
target, decoy := newGitRepo(t), newGitRepo(t)
|
|
t.Chdir(target)
|
|
poison(t, decoy)
|
|
|
|
if err := setBeadsRole("contributor"); err != nil {
|
|
t.Fatalf("setBeadsRole: %v", err)
|
|
}
|
|
if got := localRole(t, target); got != "contributor" {
|
|
t.Errorf("target beads.role = %q, want %q: the write did not land where bd is standing", got, "contributor")
|
|
}
|
|
if got := localRole(t, decoy); got != "" {
|
|
t.Errorf("decoy beads.role = %q, want unset: an inherited GIT_DIR captured the write", got)
|
|
}
|
|
})
|
|
|
|
t.Run("read ignores a redirected repository", func(t *testing.T) {
|
|
target, decoy := newGitRepo(t), newGitRepo(t)
|
|
setLocalRole(t, decoy, "maintainer")
|
|
t.Chdir(target)
|
|
poison(t, decoy)
|
|
|
|
// Fixture guard: the decoy really does hold a readable role, so a
|
|
// "not maintainer" result below can only come from ignoring it.
|
|
if got := localRole(t, decoy); got != "maintainer" {
|
|
t.Fatalf("decoy fixture beads.role = %q, want %q", got, "maintainer")
|
|
}
|
|
role, hasRole := getBeadsRole()
|
|
if hasRole {
|
|
t.Errorf("getBeadsRole() = %q, true; want unset: the read followed an inherited GIT_DIR", role)
|
|
}
|
|
})
|
|
|
|
// A redirect is not the only way to defeat the pair, and the two subtests
|
|
// above cannot tell the two vectors apart: they pass on the GIT_DIR half
|
|
// alone, so downgrading getBeadsRole to gitenv.ScrubRouting leaves them
|
|
// green. Suppression is the load-bearing half here — init.go treats a
|
|
// getBeadsRole miss as permission to write a default role — so it needs a
|
|
// pin of its own.
|
|
t.Run("read ignores inherited config suppression", func(t *testing.T) {
|
|
home, target := t.TempDir(), newGitRepo(t)
|
|
for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME"} {
|
|
t.Setenv(key, home)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[beads]\n\trole = contributor\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Chdir(target)
|
|
|
|
for _, tc := range []struct{ name, blind string }{
|
|
// Fixture guard: the global role must be readable at all, and must
|
|
// not be shadowed by a local one, or the blinded case below would
|
|
// pass for the wrong reason.
|
|
{"unblinded", ""},
|
|
{"global_null", os.DevNull},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if tc.blind != "" {
|
|
t.Setenv("GIT_CONFIG_GLOBAL", tc.blind)
|
|
}
|
|
role, hasRole := getBeadsRole()
|
|
if !hasRole || role != "contributor" {
|
|
t.Errorf("getBeadsRole() = %q, %t; want %q, true: a blinded read is what makes init write a default role", role, hasRole, "contributor")
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|