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>
This commit is contained in:
ecuthiell 2026-10-01 06:15:56 -06:00 • committed by GitHub
commit 150a58acbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 137 additions and 26 deletions

View file

@ -765,6 +765,7 @@ go_test(
"gc_embedded_test.go",
"gc_prune_test.go",
"git_repo_template_test.go",
"git_test_helpers_scope_test.go",
"github_link_scope_test.go",
"gitlab_integration_test.go",
"gitlab_test.go",

View file

@ -11,7 +11,6 @@ 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/storage/contextinfo"
@ -84,13 +83,7 @@ func TestContextRoutesNameOneWorkspaceTheSameWay(t *testing.T) {
}
t.Chdir(dir)
t.Setenv("BEADS_DIR", beadsDir)
// git caches the cwd's repository context process-wide; a test
// that ran earlier from a non-repo directory would otherwise make
// this fresh repo look like "not a git repository".
git.ResetCaches()
beads.ResetCaches()
t.Cleanup(beads.ResetCaches)
t.Cleanup(git.ResetCaches)
resetRepoCachesForTest(t)
provider, err := contextinfo.NewContextProvider(dir, Version).ContextUseCase().GetContextInfo(t.Context())
if err != nil {

View file

@ -8,11 +8,26 @@ import (
"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.
// 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()
@ -22,14 +37,12 @@ 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()
resetRepoCaches()
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Fatalf("failed to restore working directory: %v", err)
}
beads.ResetCaches()
git.ResetCaches()
resetRepoCaches()
}()
fn()
}

View file

@ -0,0 +1,111 @@
package main
import (
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
func TestResetRepoCachesForTestScopesDiscovery(t *testing.T) {
t.Setenv("BEADS_DIR", "")
repo := t.TempDir()
initGitRepoAt(t, repo)
beadsDir := filepath.Join(repo, ".beads")
if err := os.Mkdir(beadsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o600); err != nil {
t.Fatal(err)
}
t.Chdir(t.TempDir())
resetRepoCachesForTest(t)
assertNoWorkspace := func() {
t.Helper()
if got := git.GetRepoRoot(); got != "" {
t.Fatalf("non-repository directory resolved Git root %q", got)
}
if ctx, err := beads.GetRepoContext(); err == nil || ctx != nil {
t.Fatalf("non-workspace directory resolved context %+v, err=%v", ctx, err)
}
}
// Prime both cached failures before entering a different fixture.
assertNoWorkspace()
t.Run("repository", func(t *testing.T) {
t.Chdir(repo)
resetRepoCachesForTest(t)
ctx, err := beads.GetRepoContext()
if err != nil {
t.Fatal(err)
}
for _, paths := range []struct{ got, want string }{
{git.GetRepoRoot(), repo},
{ctx.RepoRoot, repo},
{ctx.BeadsDir, beadsDir},
} {
gotInfo, err := os.Stat(paths.got)
if err != nil {
t.Fatal(err)
}
wantInfo, err := os.Stat(paths.want)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(gotInfo, wantInfo) {
t.Errorf("discovered %q, want filesystem identity of %q", paths.got, paths.want)
}
}
})
// The child primed successful answers; its cleanup must discard both.
assertNoWorkspace()
}
// runInDir shares the invariant above, so it owes the same guarantee: workspace
// discovery must follow it into dir and must not survive the restore.
func TestRunInDirScopesWorkspaceDiscovery(t *testing.T) {
t.Setenv("BEADS_DIR", "")
repo := t.TempDir()
initGitRepoAt(t, repo)
beadsDir := filepath.Join(repo, ".beads")
if err := os.Mkdir(beadsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o600); err != nil {
t.Fatal(err)
}
t.Chdir(t.TempDir())
resetRepoCachesForTest(t)
assertNoWorkspaceContext := func() {
t.Helper()
if ctx, err := beads.GetRepoContext(); err == nil || ctx != nil {
t.Fatalf("non-workspace directory resolved context %+v, err=%v", ctx, err)
}
}
// Prime the cached failure that a Git-only reset would leave in place.
assertNoWorkspaceContext()
runInDir(t, repo, func() {
ctx, err := beads.GetRepoContext()
if err != nil {
t.Fatalf("runInDir left workspace discovery scoped to the previous directory: %v", err)
}
gotInfo, err := os.Stat(ctx.BeadsDir)
if err != nil {
t.Fatal(err)
}
wantInfo, err := os.Stat(beadsDir)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(gotInfo, wantInfo) {
t.Errorf("discovered %q, want filesystem identity of %q", ctx.BeadsDir, beadsDir)
}
})
// runInDir primed a successful answer; its restore must discard it too.
assertNoWorkspaceContext()
}

View file

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

View file

@ -16,7 +16,6 @@ import (
"github.com/spf13/pflag"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/httpapi"
"github.com/steveyegge/beads/internal/storage"
@ -484,8 +483,7 @@ func runServeUnderReadonly(t *testing.T, dir string) (string, error) {
// without this a directory with no workspace would still resolve to
// whichever one an earlier test in this binary left behind — and "no
// workspace" is the whole premise of the ordering assertion above.
beads.ResetCaches()
t.Cleanup(beads.ResetCaches)
resetRepoCachesForTest(t)
var err error
stderr := captureBootstrapStderr(t, func() { err = runServe() })