beads/cmd/bd/test_repo_git_guard_test.go
Steve Yegge 46aa59cc65
test(cmd/bd): stop tests from mutating the host checkout's .git; guard it in TestMain (wy-qlvlmf) (#7650)
* 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>
2026-10-17 12:36:48 +00:00

508 lines
18 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// These tests drive the TestMain git-state guard in
// test_repo_beads_guard_test.go against throwaway clones, never this checkout.
// Each fixture is a main checkout plus one linked worktree, the layout the
// r880 clobber broke (wy-qlvlmf): the guard reads the clone's shared common
// dir through the linked worktree, as it does when the suite runs from one.
// guardFixtureHost appears in every remote URL the fixtures write. No guard
// report may contain it: configs carry remote URLs and can carry credentials.
const guardFixtureHost = "guard-fixture.invalid"
// newGitGuardFixture returns a main checkout with an origin remote and an
// origin/main tracking ref, and a detached linked worktree of it.
func newGitGuardFixture(t *testing.T) (mainDir, linkedDir string) {
t.Helper()
root := t.TempDir()
mainDir = filepath.Join(root, "main")
if err := os.Mkdir(mainDir, 0o755); err != nil {
t.Fatal(err)
}
initGitRepoAt(t, mainDir)
runGitForBootstrapTest(t, mainDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, mainDir, "remote", "add", "origin", "https://"+guardFixtureHost+"/upstream.git")
runGitForBootstrapTest(t, mainDir, "update-ref", "refs/remotes/origin/main", "HEAD")
linkedDir = filepath.Join(root, "linked")
runGitForBootstrapTest(t, mainDir, "worktree", "add", "--detach", linkedDir)
return mainDir, linkedDir
}
func locateGitGuardFixture(t *testing.T, dir string) repoGitDirs {
t.Helper()
dirs, err := locateRepoGitDirs(dir)
if err != nil {
t.Fatalf("locateRepoGitDirs(%s): %v", dir, err)
}
if dirs.common == "" {
t.Fatalf("locateRepoGitDirs(%s) found no common git dir", dir)
}
return dirs
}
func evalRealPath(t *testing.T, p string) string {
t.Helper()
real, err := filepath.EvalSymlinks(p)
if err != nil {
t.Fatalf("EvalSymlinks(%s): %v", p, err)
}
return real
}
func TestLocateRepoGitDirs(t *testing.T) {
mainDir, linkedDir := newGitGuardFixture(t)
wantCommon := evalRealPath(t, filepath.Join(mainDir, ".git"))
t.Run("main checkout", func(t *testing.T) {
dirs := locateGitGuardFixture(t, mainDir)
if got := evalRealPath(t, dirs.common); got != wantCommon {
t.Errorf("common = %s, want %s", got, wantCommon)
}
if got := evalRealPath(t, dirs.gitDir); got != wantCommon {
t.Errorf("gitDir = %s, want the common dir %s", got, wantCommon)
}
// A main checkout has a config.worktree too, beside the shared config.
if got, want := dirs.worktreeConfig(), filepath.Join(dirs.gitDir, "config.worktree"); got != want {
t.Errorf("worktreeConfig() = %q, want %q", got, want)
}
})
t.Run("linked worktree", func(t *testing.T) {
dirs := locateGitGuardFixture(t, linkedDir)
if got := evalRealPath(t, dirs.common); got != wantCommon {
t.Errorf("common = %s, want %s", got, wantCommon)
}
if got, want := evalRealPath(t, dirs.gitDir), filepath.Join(wantCommon, "worktrees", "linked"); got != want {
t.Errorf("gitDir = %s, want %s", got, want)
}
if got, want := dirs.worktreeConfig(), filepath.Join(dirs.gitDir, "config.worktree"); got != want {
t.Errorf("worktreeConfig() = %q, want %q", got, want)
}
})
t.Run("not a checkout", func(t *testing.T) {
dir := t.TempDir()
// Keep git from walking up to a repository above the temp dir.
t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(dir))
dirs, err := locateRepoGitDirs(dir)
if err != nil || dirs != (repoGitDirs{}) {
t.Fatalf("got (%+v, %v); a tree with no .git (Bazel runfiles, a tarball) has nothing to guard and is not an error", dirs, err)
}
})
t.Run("broken checkout", func(t *testing.T) {
dir := t.TempDir()
gitFile := "gitdir: " + filepath.Join(dir, "missing") + "\n"
if err := os.WriteFile(filepath.Join(dir, ".git"), []byte(gitFile), 0o644); err != nil {
t.Fatal(err)
}
if _, err := locateRepoGitDirs(dir); err == nil {
t.Fatal("no error for a .git that points nowhere; the guard would drop its git check without a warning")
}
})
}
// TestDiffRepoGitStateClassifiesGitWrites runs the git commands that other
// worktrees of a shared clone run routinely, and the writes the r880 clobber
// made, and checks which ones the guard lets through.
func TestDiffRepoGitStateClassifiesGitWrites(t *testing.T) {
type fixture struct {
mainDir, linkedDir string
dirs repoGitDirs
}
git := func(dir func(fixture) string, args ...string) func(*testing.T, fixture) {
return func(t *testing.T, f fixture) {
t.Helper()
runGitForBootstrapTest(t, dir(f), args...)
}
}
inMain := func(f fixture) string { return f.mainDir }
inLinked := func(f fixture) string { return f.linkedDir }
writeHook := func(name, content string) func(*testing.T, fixture) {
return func(t *testing.T, f fixture) {
t.Helper()
if err := os.WriteFile(filepath.Join(f.dirs.common, "hooks", name), []byte(content), 0o755); err != nil {
t.Fatal(err)
}
}
}
both := func(steps ...func(*testing.T, fixture)) func(*testing.T, fixture) {
return func(t *testing.T, f fixture) {
t.Helper()
for _, step := range steps {
step(t, f)
}
}
}
forkURL := "https://" + guardFixtureHost + "/fork.git"
tests := []struct {
name string
guardMain bool // guard the main checkout instead of the linked worktree
setup func(*testing.T, fixture) // before the first snapshot
act func(*testing.T, fixture) // between the snapshots
routine bool
want []string // fragments of the one change's detail; nil means no change at all
}{
{
name: "read-only git commands",
act: both(git(inMain, "rev-parse", "HEAD"), git(inLinked, "config", "--get", "user.email")),
},
{
name: "branch tracking set up by another worktree",
act: git(inMain, "branch", "--track", "feature", "origin/main"),
routine: true,
want: []string{"branch.feature.remote added", "branch.feature.merge added"},
},
{
name: "tracked branch with a dotted name deleted",
setup: git(inMain, "branch", "--track", "release/v1.2", "origin/main"),
act: git(inMain, "branch", "-D", "release/v1.2"),
routine: true,
want: []string{"branch.release/v1.2.remote removed", "branch.release/v1.2.merge removed"},
},
{
name: "remote added",
act: git(inMain, "remote", "add", "fork", forkURL),
routine: true,
want: []string{"remote.fork.url added", "remote.fork.fetch added"},
},
{
name: "remote removed",
setup: git(inMain, "remote", "add", "fork", forkURL),
act: git(inMain, "remote", "remove", "fork"),
routine: true,
want: []string{"remote.fork.url removed", "remote.fork.fetch removed"},
},
{
name: "existing remote repointed",
act: git(inLinked, "remote", "set-url", "origin", "https://"+guardFixtureHost+"/test-fixture.git"),
want: []string{"remote.origin.url changed"},
},
{
// Unlike other remotes, the clone's origin is not added or
// removed whole by its worktrees.
name: "origin removed",
act: git(inLinked, "remote", "remove", "origin"),
want: []string{"remote.origin.url removed", "remote.origin.fetch removed"},
},
{
name: "origin added",
setup: git(inMain, "remote", "remove", "origin"),
act: git(inLinked, "remote", "add", "origin", "https://"+guardFixtureHost+"/test-fixture.git"),
want: []string{"remote.origin.url added", "remote.origin.fetch added"},
},
{
name: "core.bare flipped",
act: git(inLinked, "config", "core.bare", "true"),
want: []string{"core.bare changed"},
},
{
name: "worktree config extension enabled",
act: git(inLinked, "config", "extensions.worktreeConfig", "true"),
want: []string{"extensions.worktreeconfig added"},
},
{
name: "identity rewritten",
act: git(inLinked, "config", "user.email", "fixture@"+guardFixtureHost),
want: []string{"user.email changed"},
},
{
name: "hooks path rewritten",
act: git(inLinked, "config", "core.hooksPath", "/dev/null"),
want: []string{"core.hookspath changed"},
},
{
name: "clobber next to routine branch tracking",
act: both(
git(inMain, "branch", "--track", "feature", "origin/main"),
git(inLinked, "config", "core.bare", "true"),
),
want: []string{"core.bare changed", "also 2 branch/remote key change(s)"},
},
{
name: "hook written",
act: writeHook("pre-commit", "#!/bin/sh\nexit 0\n"),
want: []string{"new file (17 bytes)"},
},
{
name: "hook rewritten",
setup: writeHook("pre-push", "#!/bin/sh\n"),
act: writeHook("pre-push", "#!/bin/sh\nexit 1\n"),
want: []string{"changed (10 → 17 bytes)"},
},
{
name: "hook deleted",
setup: writeHook("pre-push", "#!/bin/sh\n"),
act: func(t *testing.T, f fixture) {
if err := os.Remove(filepath.Join(f.dirs.common, "hooks", "pre-push")); err != nil {
t.Fatal(err)
}
},
want: []string{"deleted (was 10 bytes)"},
},
{
// Only the common config gets the shared-clone allowance: nothing
// but this worktree writes its config.worktree.
name: "branch key written to config.worktree",
setup: both(
git(inMain, "config", "extensions.worktreeConfig", "true"),
git(inLinked, "config", "--worktree", "core.sparseCheckout", "false"),
),
act: git(inLinked, "config", "--worktree", "branch.feature.remote", "origin"),
want: []string{"branch.feature.remote added"},
},
{
// A main checkout's config.worktree lives in the common dir, and
// with the extension on its keys override the shared config.
name: "main checkout's config.worktree written",
guardMain: true,
setup: both(
git(inMain, "config", "extensions.worktreeConfig", "true"),
git(inMain, "config", "--worktree", "core.sparseCheckout", "false"),
),
act: git(inMain, "config", "--worktree", "core.hooksPath", "/dev/null"),
want: []string{"core.hookspath added"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mainDir, linkedDir := newGitGuardFixture(t)
guarded := linkedDir
if tt.guardMain {
guarded = mainDir
}
f := fixture{mainDir: mainDir, linkedDir: linkedDir, dirs: locateGitGuardFixture(t, guarded)}
if tt.setup != nil {
tt.setup(t, f)
}
before := readRepoGitState(f.dirs)
tt.act(t, f)
changes := diffRepoGitState(before, readRepoGitState(f.dirs))
if tt.want == nil {
if len(changes) != 0 {
t.Fatalf("got changes %+v, want none", changes)
}
return
}
if len(changes) != 1 {
t.Fatalf("got %d changes %+v, want 1", len(changes), changes)
}
c := changes[0]
if c.routine != tt.routine {
t.Errorf("routine = %v, want %v (detail %q)", c.routine, tt.routine, c.detail)
}
for _, w := range tt.want {
if !strings.Contains(c.detail, w) {
t.Errorf("detail %q does not mention %q", c.detail, w)
}
}
if strings.Contains(c.detail, guardFixtureHost) {
t.Errorf("detail %q echoes a config value", c.detail)
}
})
}
}
func TestDiffGitConfigText(t *testing.T) {
const base = "[core]\n\tbare = false\n" +
"[remote \"origin\"]\n\turl = https://" + guardFixtureHost + "/upstream.git\n" +
"\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
tests := []struct {
name string
after string
shared bool
routine bool
want string
}{
{
name: "comments and layout only",
after: "# reformatted\n[core]\nbare=false\n[remote \"origin\"]\n" +
"url = https://" + guardFixtureHost + "/upstream.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n",
shared: true,
routine: true,
want: "rewritten with no key or value change",
},
{
name: "empty section left behind by a removal",
after: base + "[branch \"gone\"]\n",
shared: true,
routine: true,
want: "rewritten with no key or value change",
},
{
name: "not parseable",
after: "[core\n\tbare = true\n",
shared: true,
want: "not parseable as git config",
},
{
name: "existing remote gains a fetch refspec",
after: base + "[remote \"origin\"]\n\tfetch = +refs/pull/*/head:refs/remotes/origin/pr/*\n",
shared: true,
want: "remote.origin.fetch changed",
},
{
name: "remote with a dotted name added",
after: base + "[remote \"my.fork\"]\n\turl = https://" + guardFixtureHost + "/fork.git\n",
shared: true,
routine: true,
want: "remote.my.fork.url added",
},
{
name: "branch key outside the shared config",
after: base + "[branch \"feature\"]\n\tremote = origin\n",
shared: false,
want: "branch.feature.remote added",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := diffGitConfig("config", base, tt.after, tt.shared)
if c.routine != tt.routine {
t.Errorf("routine = %v, want %v (detail %q)", c.routine, tt.routine, c.detail)
}
if !strings.Contains(c.detail, tt.want) {
t.Errorf("detail %q does not mention %q", c.detail, tt.want)
}
if strings.Contains(c.detail, guardFixtureHost) {
t.Errorf("detail %q echoes a config value", c.detail)
}
})
}
t.Run("long key lists are capped", func(t *testing.T) {
var after strings.Builder
after.WriteString(base)
for i := range 20 {
fmt.Fprintf(&after, "[branch \"b%02d\"]\n\tremote = origin\n", i)
}
c := diffGitConfig("config", base, after.String(), true)
if !c.routine || !strings.HasSuffix(c.detail, ", and 8 more") {
t.Errorf("got routine=%v detail %q, want a routine change listing 12 keys and 8 more", c.routine, c.detail)
}
})
}
func TestSplitConfigKey(t *testing.T) {
for _, tt := range []struct{ key, section, subsection, name string }{
{"core.bare", "core", "", "bare"},
{"remote.origin.url", "remote", "origin", "url"},
{"branch.release/v1.2.merge", "branch", "release/v1.2", "merge"},
{"remote.my.fork.gh-resolved", "remote", "my.fork", "gh-resolved"},
} {
section, subsection, name := splitConfigKey(tt.key)
if section != tt.section || subsection != tt.subsection || name != tt.name {
t.Errorf("splitConfigKey(%q) = (%q, %q, %q), want (%q, %q, %q)",
tt.key, section, subsection, name, tt.section, tt.subsection, tt.name)
}
}
}
func TestReportGitStateChanges(t *testing.T) {
common := t.TempDir()
configPath := filepath.Join(common, "config")
hookPath := filepath.Join(common, "hooks", "pre-commit")
const configBefore = "[core]\n\tbare = false\n"
before := repoGitState{
dirs: repoGitDirs{gitDir: common, common: common},
files: map[string]gitFileSnap{
configPath: {present: true, data: configBefore},
hookPath: {present: false},
},
}
routine := gitStateChange{path: configPath, detail: "branch.feature.remote added", routine: true}
clobber := gitStateChange{path: configPath, detail: "core.bare changed"}
newHook := gitStateChange{path: hookPath, detail: "new file (17 bytes)"}
const savedPrefix = "pre-run copies of the changed files: "
t.Run("no changes", func(t *testing.T) {
if report, failed := reportGitStateChanges(nil, before, t.TempDir()); report != "" || failed {
t.Errorf("got (%q, %v), want an empty passing report", report, failed)
}
})
t.Run("routine changes only draw a note", func(t *testing.T) {
saveDir := t.TempDir()
report, failed := reportGitStateChanges([]gitStateChange{routine}, before, saveDir)
if failed {
t.Error("a routine shared-clone change failed the run")
}
if !strings.HasPrefix(report, "NOTE: ") || !strings.Contains(report, routine.detail) || strings.Contains(report, "ERROR") {
t.Errorf("report %q, want a NOTE naming %q and no ERROR", report, routine.detail)
}
if entries, err := os.ReadDir(saveDir); err != nil || len(entries) != 0 {
t.Errorf("save dir holds %v (err %v), want nothing saved for routine changes", entries, err)
}
})
t.Run("clobber fails and saves the pre-run copies", func(t *testing.T) {
saveDir := t.TempDir()
report, failed := reportGitStateChanges([]gitStateChange{clobber, newHook}, before, saveDir)
if !failed {
t.Error("a clobber did not fail the run")
}
for _, want := range []string{"ERROR: ", "wy-qlvlmf", clobber.detail, newHook.detail} {
if !strings.Contains(report, want) {
t.Errorf("report %q does not mention %q", report, want)
}
}
var savedDir string
for _, line := range strings.Split(report, "\n") {
if dir, ok := strings.CutPrefix(line, savedPrefix); ok {
savedDir = dir
}
}
if filepath.Dir(savedDir) != saveDir {
t.Fatalf("report %q names no save dir under %s", report, saveDir)
}
entries, err := os.ReadDir(savedDir)
if err != nil {
t.Fatal(err)
}
// The hook did not exist before the run, so only the config is saved.
if len(entries) != 1 {
t.Fatalf("saved %v, want exactly the pre-run config", entries)
}
saved := filepath.Join(savedDir, entries[0].Name())
if data, err := os.ReadFile(saved); err != nil || string(data) != configBefore {
t.Errorf("saved copy = %q (err %v), want %q", data, err, configBefore)
}
if runtime.GOOS != "windows" {
for path, want := range map[string]os.FileMode{savedDir: 0o700, saved: 0o600} {
info, err := os.Stat(path)
if err != nil {
t.Error(err)
} else if got := info.Mode().Perm(); got != want {
t.Errorf("%s: mode %v, want %v: a config copy can hold credentials", path, got, want)
}
}
}
})
t.Run("notes and errors together", func(t *testing.T) {
report, failed := reportGitStateChanges([]gitStateChange{routine, newHook}, before, t.TempDir())
note, errPart, found := strings.Cut(report, "ERROR: ")
if !failed || !found || !strings.Contains(note, routine.detail) || !strings.Contains(errPart, newHook.detail) {
t.Errorf("got (%q, %v), want the routine change under NOTE, the hook under ERROR, and a failed run", report, failed)
}
})
t.Run("a failed save is reported and still fails", func(t *testing.T) {
report, failed := reportGitStateChanges([]gitStateChange{clobber}, before, filepath.Join(t.TempDir(), "missing"))
if !failed || !strings.Contains(report, "could not save pre-run copies") {
t.Errorf("got (%q, %v), want a failed run that says the copies were not saved", report, failed)
}
})
}