beads/cmd/bd/git_test_helpers.go
ecuthiell 150a58acbc
test(cli): scope repository discovery caches to fixtures (#6320)
* test(cli): scope workspace and Git caches together

Add resetRepoCachesForTest for serial fixtures that change directories.
Reset both successful and failed discoveries on entry and cleanup, and use
it in the three remaining beads-only context/serve reset sites.

Exercise non-repo to nested repository to non-repo discovery through both
cache owners. Preserve runInDir's callback lifetime and the two original
fixture repairs already owned by PR5747.

Fixes #6207
Reported-by: bee-ghosttrack
Bead: saa-cgh.92
Agent-Signature: unknown-model/unknown-reasoning

* test(cache): leave conventional helper filename available (saa-cgh.92)

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

* test(cli): apply review fixes — one reset invariant, gazelle-synced srcs

Maintainer review fixup for #6320 (iteration 1). Both minors from the review
synthesis; no blocker or major was filed.

Architectural Consistency (minor, both lanes): runInDir reset only the Git
cache, the inverse of the leak this PR fixes — a workspace context primed by an
earlier fixture survived its chdir. Extract the pair into resetRepoCaches() so
one definition owns the invariant, and route both resetRepoCachesForTest and
runInDir through it. Deviation from the synthesis's parenthetical "(or
resetRepoCachesForTest)": calling that helper inside runInDir would move the
exit reset from runInDir's own defer to a t.Cleanup at end of test, dropping
the before/after guarantee its doc comment states, so the shared closure is
extracted instead. The doc comment is updated to match the widened behaviour.

TestRunInDirScopesWorkspaceDiscovery pins both halves. Restoring the git-only
entry reset reds it at :94 ("no .beads directory found" — the finding's own
observable); restoring the git-only exit reset reds it at :110 with the leaked
context. In both arms the existing TestResetRepoCachesForTestScopesDiscovery
stays green, so the new test covers an uncovered site rather than duplicating
it.

Test Evidence Quality (minor, codex): the new test file was absent from the
gazelle-managed go_test srcs list, so bazel test never ran it. Ran make
bazel-sync (gazelle, go_srcs.py, mod tidy) rather than hand-editing. Its entire
output was three srcs lines: ours, plus two base-inherited absentees that
landed in this PR's own base commit. Only the line for the file this PR adds is
carried here, because shipping all three conflicts with origin/main. Measured,
not assumed: git merge-tree --write-tree against origin/main puts BUILD.bazel
in the conflict set with all three lines and merges it cleanly with one, while
the merged tree's bd_test srcs holds the same 648 entries — including all three
files — either way, since origin/main has already gazelle-synced the two
base-inherited ones. The one-line form is therefore what gazelle emits at the
merge target, and it leaves the base's own drift as found rather than fixing it
in this PR.

Validation: 32 tests covering all 22 package-main runInDir call sites pass
before and after with an identical name-level PASS set; the PR's four affected
tests plus the new pin are 5/5 anchored PASS; go vet, gofmt, git diff --check,
golangci-lint (0 issues) and go_srcs.py --check all clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-10-01 05:15:56 -07:00

48 lines
1.3 KiB
Go

package main
import (
"os"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
// resetRepoCaches is the single definition of this file's fixture invariant:
// the workspace and Git discovery caches are reset together, because workspace
// discovery resolves through the Git cache and would otherwise reprime from a
// previous fixture's answer.
func resetRepoCaches() {
beads.ResetCaches()
git.ResetCaches()
}
// resetRepoCachesForTest gives a serial fixture fresh workspace and Git
// discovery, including cached failures. Call after changing its directory.
func resetRepoCachesForTest(t *testing.T) {
t.Helper()
resetRepoCaches()
t.Cleanup(resetRepoCaches)
}
// runInDir changes into dir, resets the repository discovery caches
// before/after, and executes fn. It ensures tests that mutate git repositories
// don't leak state across cases.
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)
}
resetRepoCaches()
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Fatalf("failed to restore working directory: %v", err)
}
resetRepoCaches()
}()
fn()
}