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>
This commit is contained in:
ecuthiell 2026-09-30 19:32:26 -06:00 • committed by GitHub
commit a8003cf6ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 4 deletions

View file

@ -4,11 +4,15 @@ import (
"os"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
// runInDir changes into dir, resets git caches before/after, and executes fn.
// It ensures tests that mutate git repositories don't leak state across cases.
// 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()
@ -18,11 +22,13 @@ func runInDir(t *testing.T, dir string, fn func()) {
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()

View file

@ -13,8 +13,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
@ -83,6 +85,12 @@ func migrateModeWorkspace(t *testing.T, mode string) string {
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "dolt", ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
writeMetadataConfig(t, beadsDir, mode, "myproj")
t.Chdir(dir)
beads.ResetCaches()
git.ResetCaches()
t.Cleanup(func() {
beads.ResetCaches()
git.ResetCaches()
})
return beadsDir
}

View file

@ -428,7 +428,11 @@ func TestServeRefusesStrictReadonlyOnARegisteredBackend(t *testing.T) {
t.Chdir(dir)
t.Setenv("BEADS_DIR", beadsDir)
beads.ResetCaches()
t.Cleanup(beads.ResetCaches)
git.ResetCaches()
t.Cleanup(func() {
beads.ResetCaches()
git.ResetCaches()
})
origReadonly := readonlyMode
readonlyMode = true

View file

@ -16,6 +16,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/backends"
"github.com/steveyegge/beads/internal/types"
@ -73,7 +74,11 @@ func TestServeAnswersFromTheStoreTheRootCommandOpened(t *testing.T) {
// that chdirs into its own workspace has to clear it going in and coming
// out.
beads.ResetCaches()
t.Cleanup(beads.ResetCaches)
git.ResetCaches()
t.Cleanup(func() {
beads.ResetCaches()
git.ResetCaches()
})
// Stand in for PersistentPreRunE, which opens the workspace through exactly
// this dispatch and leaves it in `store`. This is the store bd already