beads/cmd/bd/git_test_helpers.go
ecuthiell a8003cf6ec
test(cmd/bd): reset repository caches around CWD fixtures (#5747)
* test(cmd/bd): reset repository caches around CWD fixtures (#5746)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* test(cmd/bd): pair the beads/git cache resets at the two remaining chdir fixtures

Maintainer review fixup for the adopt-pr review of #5747 (iteration 1,
synthesis approve, 0 blocker / 0 major).

Takes the single minor finding (Change Impact / Blast Radius). The PR
establishes that a one-sided cache reset around a chdir fixture is a
structural half-fix, but two sites in the same package still encoded
exactly one half each, failing in opposite directions:

- serve_registered_backend_test.go: had the beads-only pattern the PR
  just deleted elsewhere (beads.ResetCaches + bare
  t.Cleanup(beads.ResetCaches)). Converted to the paired closure the PR
  uses, adding git.ResetCaches on both edges. The file was already cited
  as the exemplar for the paired idiom at two other anchors; this was the
  third, unfixed site.
- git_test_helpers.go: runInDir is the package's single helper for this
  contract across 38 call sites, and it reset git only, never beads.
  Added beads.ResetCaches beside both git.ResetCaches calls, and updated
  the doc comment to state why the pair is required. No call-site churn.

Both caches key on the working directory, so clearing one leaves the
other answering for the previous directory.

Validation: go vet ./cmd/bd/ clean at CGO_ENABLED=1; the 22 test
functions that call runInDir plus TestServeRefusesStrictReadonlyOnARegisteredBackend
run 47/47 PASS, 0 SKIP.

Not taken: the optional shared chdirFixture(t, dir) helper the finding
suggests. It would churn all 38 call sites for no behavior change, which
is beyond what a maintainer fixup on an approved PR should carry.

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-09-30 18:32:26 -07:00

35 lines
993 B
Go

package main
import (
"os"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
// runInDir changes into dir, resets the beads and git caches before/after, and
// executes fn. It ensures tests that mutate git repositories don't leak state
// across cases. Both caches are keyed on the working directory, so a chdir
// fixture has to clear the pair going in and coming out — clearing only one
// leaves the other answering for the previous directory.
func runInDir(t *testing.T, dir string, fn func()) {
t.Helper()
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("failed to change to temp directory: %v", err)
}
beads.ResetCaches()
git.ResetCaches()
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Fatalf("failed to restore working directory: %v", err)
}
beads.ResetCaches()
git.ResetCaches()
}()
fn()
}