* 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>
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
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()
|
|
}
|