test: add git helper and guard annotations

This commit is contained in:
Jordan Hubbard
2025-12-28 21:44:35 -04:00
committed by Steve Yegge
parent f3dcafca66
commit 713c569e6e
10 changed files with 492 additions and 507 deletions

View File

@@ -0,0 +1,29 @@
package main
import (
"os"
"testing"
"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.
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)
}
git.ResetCaches()
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Fatalf("failed to restore working directory: %v", err)
}
git.ResetCaches()
}()
fn()
}