* wy-qlvlmf: test(cmd/bd): stop tests from mutating the host checkout's .git; guard it in TestMain On 2026-10-16 a 'go test ./cmd/bd' run from a linked worktree rewrote the host rig's shared .git/config (core.bare=true, extensions.worktreeConfig, remote.origin.url replaced by metadata_test's fixture URL, user identity, core.hooksPath) and its hooks dir, leaving every worktree of that clone answering 'fatal: this operation must be run in a work tree' (r880). Root causes, all one class — fixture git commands routed at the repository the test process itself lives in instead of a fixture the test owns: - TestRunApplyAllOK ran runApply(false) (a LIVE apply: hook reinstall, hooksPath mkdir) with no fixture, against the package cwd's repo. - runGitForBootstrapTest treated dir=="" as 'inherit package cwd'. - doctor/fix setupGitRepoInDir swallowed git config failures non-fatally and ran with whatever git routing env (GIT_DIR, GIT_WORK_TREE) leaked in. Fixes: - TestRunApplyAllOK / TestRunApplyDryRun now chdir into an isolated git+.beads fixture (isolatedApplyFixture + runInDir). - runGitForBootstrapTest: empty dir gets a neutral t.TempDir() cwd, never the package cwd; env scrubbed of git routing vars. - initGitRepoAt / doctor setupGitRepoInDir / doctor-fix setupGitRepoInDir: refuse an empty dir, fail on ANY git error, scrub routing env. - testMainInner scrubs git routing env process-wide at startup. - TestMain's repo guard now also snapshots the checkout's own git state (common .git/config, config.worktree, common hooks dir) and reddens the run on any diff, naming the mutated file. Guard vacuity check: a probe test writing one key into the checkout's .git/config passes individually and reddens the suite via the guard. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * build(bazel): auto-sync BUILD files Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/39013861275 (`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy). Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see scripts/bazel-autofix-push.sh. * test(cmd/bd): scrub doctor suites, keep git fences, key-level config guard (wy-qlvlmf) Maintainer fixup on top of #7650 by cat, for the first review's three majors, minor and nit, and three of the second review's minors. - Doctor suites: the leaked-GIT_DIR vector was still open in cmd/bd/doctor and cmd/bd/doctor/fix. Their four TestMains now unset the repository-location env first (testutil.UnsetGitRepoLocationEnv), and the three initGitTemplate copies plus testutil.ForceRepoLocalHooksPath run git with a filtered env. With GIT_DIR/GIT_WORK_TREE leaked at a decoy repo, the PR head wrote core.hooksPath into the decoy and failed 120 doctor tests; with this commit all three packages pass and the decoy stays byte-identical. - Narrower scrub: the process-wide scrub also removed GIT_CEILING_DIRECTORIES and the GIT_CONFIG_* fences that tools/bazel/test_env.sh and scripts/ci/lib/test-env.sh set on purpose. testutil.WithoutGitRepoLocationEnv removes only GIT_DIR, GIT_WORK_TREE, GIT_COMMON_DIR, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_NAMESPACE and the legacy GIT_CONFIG (which sends `git config` reads and writes to the file it names, even under -C). Keys match exactly, so GIT_CEILING_DIRECTORIES, GIT_CONFIG_GLOBAL and GIT_CONFIG_NOSYSTEM stay. Every helper that built its own scrubbed env now uses it. - Guard false reds: the TestMain guard compared the shared common config byte for byte, so a sibling worktree's routine branch write failed a passing run. It now diffs `git config --list -z` key by key. branch.<name>.* keys, and a remote other than origin added or removed whole, print a NOTE and leave the exit code alone. Any other key (including origin appearing or disappearing, and a changed key of a remote that exists before and after), any config.worktree change, any hook file change or an unparseable config still fails the run. - config.worktree: the guard also snapshots a main checkout's own config.worktree, which sits in the common dir. With extensions.worktreeConfig set its keys override the shared config, and before this an escaped `git config --worktree` write there went unreported. - Report: changed keys are named with values redacted, deleted and new files are reported as such, pre-run copies of the changed files are saved (0700 dir, 0600 files) and their path printed, and a failure to locate the checkout's git dirs prints a warning instead of disarming the guard silently. - doctor setupGitRepoInDir stat-checks its dir like the doctor/fix copy. New tests: internal/testutil/git_env_test.go, and cmd/bd/test_repo_git_guard_test.go (real-git fixtures for locating the git dirs, classifying writes, config parsing and the report). Validation: ./scripts/test.sh ./cmd/bd in a scratch clone with GIT_DIR leaked at a decoy (ok; decoy and the clone's .git unchanged; guard quiet); bazel test --config=ci for testutil, doctor, doctor/fix, fmt_test and the new bd_test guard tests; make ci-pr-lint; tools/bazel/go_srcs.py --check. Second review: leaving GIT_CONFIG out of the scrub fails TestWithoutGitRepoLocationEnvKeepsWrapperGitControls and TestForceRepoLocalHooksPathIgnoresLeakedGitDir; the old main-checkout config.worktree path fails TestLocateRepoGitDirs/main_checkout and the new config.worktree case; treating origin like any other remote fails the new origin added and removed cases. In a scratch main-checkout clone with extensions.worktreeConfig set, a test that wrote `git config --worktree user.email` into the checkout now fails the suite (exit 1, an ERROR naming .git/config.worktree). bazel test --config=ci passes for testutil, fmt_test and the bd_test guard tests. Not in this commit: the same scrub in beads_test_env_enter (scripts/ci/lib/test-env.sh) and tools/bazel/test_env.sh, which would cover the packages outside cmd/bd that still write to the clone under a hook-leaked GIT_DIR. That predates this PR and is left to a follow-up. Co-authored-by: cat <cino@ghosttrack.com> Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: cat <cino@ghosttrack.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
114 lines
3.6 KiB
Go
114 lines
3.6 KiB
Go
package testutil
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// wrapperGitEnv is what tools/bazel/test_env.sh and scripts/ci/lib/test-env.sh
|
|
// export for every test. The location scrub must leave all of it in place.
|
|
var wrapperGitEnv = []string{
|
|
"GIT_CEILING_DIRECTORIES=/sandbox/srcdir:/sandbox/tmp",
|
|
"GIT_CONFIG_GLOBAL=/sandbox/gitconfig",
|
|
"GIT_CONFIG_NOSYSTEM=1",
|
|
}
|
|
|
|
func TestWithoutGitRepoLocationEnvKeepsWrapperGitControls(t *testing.T) {
|
|
env := append([]string{
|
|
"GIT_DIR=/host/.git/worktrees/wt",
|
|
"GIT_WORK_TREE=/host",
|
|
"GIT_COMMON_DIR=/host/.git",
|
|
"GIT_INDEX_FILE=/host/.git/worktrees/wt/index",
|
|
"GIT_OBJECT_DIRECTORY=/host/.git/objects",
|
|
"GIT_ALTERNATE_OBJECT_DIRECTORIES=/host/.git/objects",
|
|
"GIT_NAMESPACE=host",
|
|
"GIT_CONFIG=/host/.git/config",
|
|
"HOME=/sandbox/home",
|
|
}, wrapperGitEnv...)
|
|
|
|
got := WithoutGitRepoLocationEnv(env)
|
|
want := append([]string{"HOME=/sandbox/home"}, wrapperGitEnv...)
|
|
if !slices.Equal(got, want) {
|
|
t.Fatalf("WithoutGitRepoLocationEnv:\n got %q\nwant %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestUnsetGitRepoLocationEnvKeepsWrapperGitControls(t *testing.T) {
|
|
for _, key := range gitRepoLocationKeys {
|
|
t.Setenv(key, "/host/"+key)
|
|
}
|
|
for _, entry := range wrapperGitEnv {
|
|
key, value, _ := strings.Cut(entry, "=")
|
|
t.Setenv(key, value)
|
|
}
|
|
|
|
if err := UnsetGitRepoLocationEnv(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, key := range gitRepoLocationKeys {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
t.Errorf("%s=%q survived UnsetGitRepoLocationEnv", key, value)
|
|
}
|
|
}
|
|
for _, entry := range wrapperGitEnv {
|
|
key, want, _ := strings.Cut(entry, "=")
|
|
if got := os.Getenv(key); got != want {
|
|
t.Errorf("%s=%q after UnsetGitRepoLocationEnv, want %q kept", key, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestForceRepoLocalHooksPathIgnoresLeakedGitDir pins the write target: with
|
|
// GIT_DIR and GIT_WORK_TREE naming another repository, or GIT_CONFIG naming
|
|
// another config file, the hooksPath write must still land in repoDir and leave
|
|
// the other config untouched.
|
|
func TestForceRepoLocalHooksPathIgnoresLeakedGitDir(t *testing.T) {
|
|
decoy, target := t.TempDir(), t.TempDir()
|
|
for _, dir := range []string{decoy, target} {
|
|
cmd := exec.Command("git", "init", "--quiet", dir)
|
|
cmd.Env = WithoutGitRepoLocationEnv(os.Environ())
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git init %s: %v\n%s", dir, err, out)
|
|
}
|
|
}
|
|
decoyConfig := filepath.Join(decoy, ".git", "config")
|
|
before, err := os.ReadFile(decoyConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
decoyFile := filepath.Join(t.TempDir(), "leaked.gitconfig")
|
|
const decoyFileBefore = "[user]\n\tname = decoy\n"
|
|
if err := os.WriteFile(decoyFile, []byte(decoyFileBefore), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Setenv("GIT_DIR", filepath.Join(decoy, ".git"))
|
|
t.Setenv("GIT_WORK_TREE", decoy)
|
|
t.Setenv("GIT_CONFIG", decoyFile)
|
|
if err := ForceRepoLocalHooksPath(target); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
after, err := os.ReadFile(decoyConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(after) != string(before) {
|
|
t.Fatalf("leaked GIT_DIR routed the hooksPath write into %s:\n%s", decoyConfig, after)
|
|
}
|
|
if got, err := os.ReadFile(decoyFile); err != nil || string(got) != decoyFileBefore {
|
|
t.Fatalf("leaked GIT_CONFIG routed the hooksPath write into %s (err %v):\n%s", decoyFile, err, got)
|
|
}
|
|
get := exec.Command("git", "config", "--file", filepath.Join(target, ".git", "config"), "--get", "core.hooksPath")
|
|
out, err := get.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("read target core.hooksPath: %v\n%s", err, out)
|
|
}
|
|
if got := strings.TrimSpace(string(out)); got != ".git/hooks" {
|
|
t.Fatalf("target core.hooksPath=%q, want %q", got, ".git/hooks")
|
|
}
|
|
}
|