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>
384 lines
14 KiB
Go
384 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/git"
|
|
"github.com/steveyegge/beads/internal/gitenv"
|
|
"github.com/steveyegge/beads/internal/utils"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestStandaloneHooksAbsoluteFallbackHasNoConfigAuthority(t *testing.T) {
|
|
oldJSON := jsonOutput
|
|
jsonOutput = true // Reproduce the prior-command state observed in the full CI suite.
|
|
t.Cleanup(func() { jsonOutput = oldJSON })
|
|
for _, name := range []string{"nonrepo", "bare"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, decoy, _, _ := newInitHooksFixture(t)
|
|
require.True(t, utils.PathsEqual(decoy, git.GetRepoRoot()), "seed stale config authority")
|
|
workDir, hooksDir := t.TempDir(), t.TempDir()
|
|
if name == "bare" {
|
|
initExcludeGit(t, workDir, "init", "--bare")
|
|
}
|
|
for _, key := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE"} {
|
|
t.Setenv(key, "")
|
|
require.NoError(t, os.Unsetenv(key))
|
|
}
|
|
initExcludeGit(t, decoy, "config", "--global", "core.hooksPath", hooksDir)
|
|
t.Chdir(workDir)
|
|
preserveStandaloneHookInputs(t, filepath.Join(decoy, ".git", "config"), filepath.Join(os.Getenv("HOME"), ".gitconfig"))
|
|
for _, mode := range []string{"shared", "beads"} {
|
|
setStandaloneHookMode(t, mode)
|
|
var err error
|
|
stderr := captureStderr(t, func() { err = hooksInstallCmd.RunE(hooksInstallCmd, nil) })
|
|
require.Equal(t, &exitError{Code: 1}, err)
|
|
require.Contains(t, stderr, "require a working Git repository")
|
|
require.NoFileExists(t, filepath.Join(hooksDir, "pre-commit"))
|
|
}
|
|
setStandaloneHookMode(t, "")
|
|
require.NoError(t, hooksInstallCmd.RunE(hooksInstallCmd, nil))
|
|
require.Contains(t, string(readInitHooksFile(t, filepath.Join(hooksDir, "pre-commit"))), hookSectionBeginPrefix)
|
|
require.NoError(t, hooksUninstallCmd.RunE(hooksUninstallCmd, nil))
|
|
require.NoFileExists(t, filepath.Join(hooksDir, "pre-commit"))
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestUninstallHooksUnsetsBeadsRole verifies AC2: bd hooks uninstall clears
|
|
// beads.role in addition to core.hooksPath, so a manual `rm -rf .beads/`
|
|
// followed by a proper uninstall doesn't leave stale beads-managed git
|
|
// config behind (GH#4440).
|
|
func TestUninstallHooksUnsetsBeadsRole(t *testing.T) {
|
|
tmpDir := newGitRepo(t)
|
|
runInDir(t, tmpDir, func() {
|
|
cmd := gitCommand("config", "beads.role", "primary")
|
|
cmd.Dir = tmpDir
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("failed to set beads.role: %v", err)
|
|
}
|
|
|
|
if err := uninstallHooks(); err != nil {
|
|
t.Fatalf("uninstallHooks() failed: %v", err)
|
|
}
|
|
|
|
getCmd := gitCommand("config", "--local", "--get", "beads.role")
|
|
getCmd.Dir = tmpDir
|
|
out, err := getCmd.Output()
|
|
if err == nil {
|
|
t.Errorf("expected beads.role to be unset after uninstallHooks(), got %q", strings.TrimSpace(string(out)))
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestUninstallHooksNoBeadsRoleIsNotAnError verifies that an already-absent
|
|
// beads.role is treated as success, not surfaced as a failure.
|
|
func TestUninstallHooksNoBeadsRoleIsNotAnError(t *testing.T) {
|
|
tmpDir := newGitRepo(t)
|
|
runInDir(t, tmpDir, func() {
|
|
if err := uninstallHooks(); err != nil {
|
|
t.Fatalf("uninstallHooks() failed when beads.role was never set: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A duplicated beads.role makes git refuse the unset as ambiguous — and it
|
|
// exits 5 to say so, the same code it uses for "key not set". Reading the key
|
|
// before unsetting is what keeps the two apart; treating exit 5 as success
|
|
// would report a clean uninstall with the key still set, which is precisely
|
|
// the failure AC2 exists to prevent.
|
|
func TestUninstallHooksReportsAmbiguousBeadsRole(t *testing.T) {
|
|
tmpDir := newGitRepo(t)
|
|
runInDir(t, tmpDir, func() {
|
|
for _, value := range []string{"primary", "secondary"} {
|
|
cmd := gitCommand("config", "--add", "beads.role", value)
|
|
cmd.Dir = tmpDir
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("failed to add beads.role=%s: %v", value, err)
|
|
}
|
|
}
|
|
|
|
err := uninstallHooks()
|
|
if err == nil {
|
|
t.Fatal("uninstallHooks() = nil, want an error: git cannot unset a multi-valued beads.role")
|
|
}
|
|
if !strings.Contains(err.Error(), "beads.role") {
|
|
t.Errorf("error %q does not name beads.role", err)
|
|
}
|
|
|
|
// And the key really is still set — the error was not spurious.
|
|
getCmd := gitCommand("config", "--local", "--get-all", "beads.role")
|
|
getCmd.Dir = tmpDir
|
|
out, getErr := getCmd.Output()
|
|
if getErr != nil {
|
|
t.Fatalf("expected beads.role to still be set, --get-all failed: %v", getErr)
|
|
}
|
|
if len(strings.Fields(string(out))) != 2 {
|
|
t.Errorf("beads.role values = %q, want both still present", strings.TrimSpace(string(out)))
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestResetHooksPathIfBeadsManagedReportsFailureLoudly verifies AC2: when
|
|
// the underlying git config command genuinely fails, resetHooksPathIfBeadsManaged
|
|
// returns a non-nil error (and thus uninstallHooks propagates it) instead of
|
|
// silently printing a scrolling stderr warning and reporting success.
|
|
func TestResetHooksPathIfBeadsManagedReportsFailureLoudly(t *testing.T) {
|
|
tmpDir := newGitRepo(t)
|
|
runInDir(t, tmpDir, func() {
|
|
gitDir := filepath.Join(tmpDir, ".git")
|
|
info, err := os.Stat(gitDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to stat .git: %v", err)
|
|
}
|
|
orig := info.Mode()
|
|
|
|
// There has to be something to unset, or there is no git invocation to
|
|
// fail: the reset reads beads.role first and only unsets it when it is
|
|
// actually present.
|
|
setCmd := gitCommand("config", "beads.role", "primary")
|
|
setCmd.Dir = tmpDir
|
|
if err := setCmd.Run(); err != nil {
|
|
t.Fatalf("failed to set beads.role: %v", err)
|
|
}
|
|
|
|
// Make .git/ read+execute only (no write), so `git rev-parse` (used to
|
|
// resolve repoRoot) and `git config --get` still succeed but
|
|
// `git config --unset` cannot create its lockfile — a genuine failure,
|
|
// which resetHooksPathIfBeadsManaged must not swallow.
|
|
if err := os.Chmod(gitDir, 0555); err != nil {
|
|
t.Fatalf("failed to chmod .git: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = os.Chmod(gitDir, orig)
|
|
})
|
|
|
|
err = resetHooksPathIfBeadsManaged()
|
|
if err == nil {
|
|
t.Fatal("expected resetHooksPathIfBeadsManaged to return an error when the config lockfile cannot be created")
|
|
}
|
|
if !strings.Contains(err.Error(), "beads.role") {
|
|
t.Errorf("error %q does not name the key it failed to unset", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestResetConfigReportsReadFailure(t *testing.T) {
|
|
repo := newGitRepo(t)
|
|
t.Chdir(repo)
|
|
git.ResetCaches()
|
|
t.Cleanup(git.ResetCaches)
|
|
common, err := git.GetGitCommonDir()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Corrupt the selected config after context capture so the reads, not discovery, fail.
|
|
path := filepath.Join(common, "config")
|
|
data := []byte("[invalid\n")
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = resetHooksPathIfBeadsManaged()
|
|
for _, fragment := range []string{"read core.hooksPath", "read beads.role", "bad config"} {
|
|
if err == nil || !strings.Contains(err.Error(), fragment) {
|
|
t.Errorf("reset error = %v, want %q", err, fragment)
|
|
}
|
|
}
|
|
if after, err := os.ReadFile(path); err != nil || string(after) != string(data) {
|
|
t.Errorf("failed read changed config: %q (%v)", after, err)
|
|
}
|
|
}
|
|
|
|
func TestResetRolePreservesSelectedGitContext(t *testing.T) {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
for _, name := range []string{"ordinary", "selected_repository", "bare_external", "invalid", "inline_absent", "selected_config", "global_role", "config_lock", "global_only", "hooks_inline_only", "hooks_global_only", "hooks_foreign_absolute"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME"} {
|
|
t.Setenv(key, home)
|
|
}
|
|
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
|
global := filepath.Join(home, ".gitconfig")
|
|
globalData := "[user]\n\tname = fixture global\n"
|
|
if name == "global_role" || name == "global_only" {
|
|
globalData += "[beads]\n\trole = global-default\n"
|
|
}
|
|
if name == "hooks_global_only" {
|
|
globalData += "[core]\n\thooksPath = .beads/hooks\n"
|
|
}
|
|
if err := os.WriteFile(global, []byte(globalData), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
query := func(dir string, args ...string) ([]byte, error) {
|
|
cmd := gitCommand(args...)
|
|
cmd.Dir, cmd.Env = dir, gitenv.ScrubRouting(os.Environ())
|
|
return cmd.CombinedOutput()
|
|
}
|
|
must := func(dir string, args ...string) string {
|
|
out, err := query(dir, args...)
|
|
if err != nil {
|
|
t.Fatalf("git %v: %v\n%s", args, err, out)
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
cwd, decoy := newGitRepo(t), newGitRepo(t)
|
|
selected := filepath.Join(cwd, ".git")
|
|
for _, repo := range []string{cwd, decoy} {
|
|
must(repo, "config", "beads.role", "primary")
|
|
must(repo, "read-tree", "--empty")
|
|
if err := os.WriteFile(filepath.Join(repo, ".git", "hooks", "pre-commit"), []byte("#!/bin/sh\necho foreign\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
must(decoy, "config", "beads.role", "decoy")
|
|
switch name {
|
|
case "selected_repository":
|
|
selected = filepath.Join(decoy, ".git")
|
|
t.Setenv("GIT_DIR", selected)
|
|
t.Setenv("GIT_WORK_TREE", decoy)
|
|
case "bare_external":
|
|
root := t.TempDir()
|
|
selected, cwd = filepath.Join(root, "selected bare.git"), filepath.Join(root, "external tree")
|
|
must(root, "init", "--bare", selected)
|
|
if err := os.Mkdir(cwd, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
must(root, "--git-dir", selected, "config", "beads.role", "primary")
|
|
t.Setenv("GIT_DIR", filepath.Join("..", "selected bare.git"))
|
|
t.Setenv("GIT_WORK_TREE", cwd)
|
|
case "invalid":
|
|
t.Setenv("GIT_DIR", filepath.Join(t.TempDir(), "missing.git"))
|
|
case "inline_absent":
|
|
must(cwd, "config", "--unset", "beads.role")
|
|
t.Setenv("GIT_CONFIG_COUNT", "1")
|
|
t.Setenv("GIT_CONFIG_KEY_0", "beads.role")
|
|
t.Setenv("GIT_CONFIG_VALUE_0", "forged")
|
|
case "selected_config":
|
|
t.Setenv("GIT_CONFIG", filepath.Join(decoy, ".git", "config"))
|
|
probe := gitCommand("--git-dir", selected, "config", "beads.routing-test", "selected-file")
|
|
probe.Dir = cwd // This fixture precondition deliberately inherits GIT_CONFIG.
|
|
if out, err := probe.CombinedOutput(); err != nil {
|
|
t.Fatalf("selected-file probe: %v\n%s", err, out)
|
|
}
|
|
if got := must(decoy, "config", "--get", "beads.routing-test"); got != "selected-file" {
|
|
t.Fatalf("GIT_CONFIG did not select decoy: %q", got)
|
|
}
|
|
must(decoy, "config", "--unset", "beads.routing-test")
|
|
case "global_only":
|
|
must(cwd, "config", "--local", "--unset", "beads.role")
|
|
if got := must(cwd, "config", "--get", "beads.role"); got != "global-default" {
|
|
t.Fatalf("global-only role precondition = %q", got)
|
|
}
|
|
case "config_lock":
|
|
if err := os.WriteFile(filepath.Join(selected, "config.lock"), []byte("owned lock"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
wantHooksPath, checkHooksPath := "", true
|
|
switch name {
|
|
case "ordinary", "selected_repository", "bare_external", "selected_config":
|
|
must(cwd, "--git-dir", selected, "config", "--local", "core.hooksPath", ".beads/hooks")
|
|
if name == "selected_config" {
|
|
must(decoy, "config", "--local", "core.hooksPath", ".beads/hooks")
|
|
}
|
|
case "hooks_inline_only", "hooks_global_only":
|
|
must(cwd, "config", "--local", "--unset", "core.hooksPath")
|
|
if name == "hooks_inline_only" {
|
|
t.Setenv("GIT_CONFIG_COUNT", "1")
|
|
t.Setenv("GIT_CONFIG_KEY_0", "core.hooksPath")
|
|
t.Setenv("GIT_CONFIG_VALUE_0", ".beads/hooks")
|
|
}
|
|
case "hooks_foreign_absolute":
|
|
wantHooksPath = filepath.Join(t.TempDir(), "foreign-hooks")
|
|
must(cwd, "config", "--local", "core.hooksPath", wantHooksPath)
|
|
default:
|
|
checkHooksPath = false
|
|
}
|
|
saved := map[string][]byte{global: []byte(globalData)}
|
|
if checkHooksPath {
|
|
for _, hooksDir := range []string{filepath.Join(cwd, ".beads", "hooks"), filepath.Join(decoy, ".beads", "hooks"), wantHooksPath} {
|
|
if hooksDir == "" {
|
|
continue
|
|
}
|
|
if err := os.MkdirAll(hooksDir, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(hooksDir, "pre-commit")
|
|
data := []byte("#!/bin/sh\necho foreign\n")
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved[path] = data
|
|
}
|
|
}
|
|
for _, repo := range []string{cwd, decoy} {
|
|
for _, path := range []string{filepath.Join(repo, ".git", "config"), filepath.Join(repo, ".git", "index"), filepath.Join(repo, ".git", "hooks", "pre-commit")} {
|
|
if path == filepath.Join(selected, "config") && name != "invalid" && name != "config_lock" && name != "global_only" {
|
|
continue
|
|
}
|
|
if data, err := os.ReadFile(path); err == nil {
|
|
saved[path] = data
|
|
} else if !os.IsNotExist(err) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
t.Chdir(cwd)
|
|
before := strings.Join(os.Environ(), "\x00")
|
|
git.ResetCaches() // Poison is present before either actual entrypoint resolves its context.
|
|
t.Cleanup(git.ResetCaches)
|
|
var err error
|
|
if name == "invalid" || name == "inline_absent" {
|
|
err = resetHooksPathIfBeadsManaged()
|
|
} else {
|
|
err = uninstallHooks()
|
|
}
|
|
if name == "config_lock" {
|
|
if err == nil || !strings.Contains(err.Error(), "beads.role") {
|
|
t.Errorf("locked uninstall = %v, want beads.role failure", err)
|
|
}
|
|
} else if err != nil {
|
|
t.Errorf("reset/uninstall failed: %v", err)
|
|
}
|
|
out, readErr := query(cwd, "--git-dir", selected, "config", "--local", "--get", "beads.role")
|
|
if name == "invalid" || name == "config_lock" {
|
|
if readErr != nil || strings.TrimSpace(string(out)) != "primary" {
|
|
t.Errorf("retained selected role = %q, %v", out, readErr)
|
|
}
|
|
} else if exit, ok := readErr.(*exec.ExitError); !ok || exit.ExitCode() != 1 {
|
|
t.Errorf("selected local role remains: %q, %v", out, readErr)
|
|
}
|
|
if checkHooksPath {
|
|
out, readErr := query(cwd, "--git-dir", selected, "config", "--local", "--get", "core.hooksPath")
|
|
if wantHooksPath != "" {
|
|
if readErr != nil || strings.TrimSpace(string(out)) != wantHooksPath {
|
|
t.Errorf("foreign hooksPath changed: %q, %v", out, readErr)
|
|
}
|
|
} else if exit, ok := readErr.(*exec.ExitError); !ok || exit.ExitCode() != 1 {
|
|
t.Errorf("selected local hooksPath remains: %q, %v", out, readErr)
|
|
}
|
|
}
|
|
for path, before := range saved {
|
|
if after, err := os.ReadFile(path); err != nil || string(after) != string(before) {
|
|
t.Errorf("unrelated file changed: %s (%v)", path, err)
|
|
}
|
|
}
|
|
if strings.Join(os.Environ(), "\x00") != before {
|
|
t.Error("reset changed inherited environment")
|
|
}
|
|
})
|
|
}
|
|
}
|