fix(tests): use --initial-branch=main for modern git compatibility

Modern git (2.28+) uses 'main' as default branch, not 'master'.
Tests were failing because they assumed 'master' branch exists.

Changes:
- Use 'git init --initial-branch=main' instead of bare 'git init'
- Change 'git checkout master' to 'git checkout main'
- Add git.ResetCaches() after os.Chdir() to clear cached git state
- Ensures test isolation when changing directories
This commit is contained in:
Ryan Snodgrass
2025-12-26 19:09:41 -05:00
parent 252de1cdba
commit 9c9f2f2ad8
5 changed files with 78 additions and 26 deletions

View File

@@ -5,6 +5,8 @@ import (
"os/exec"
"testing"
"time"
"github.com/steveyegge/beads/internal/git"
)
// waitFor repeatedly evaluates pred until it returns true or timeout expires.
@@ -37,8 +39,11 @@ func setupGitRepo(t *testing.T) (repoPath string, cleanup func()) {
t.Fatalf("failed to change to temp directory: %v", err)
}
// Initialize git repo
if err := exec.Command("git", "init").Run(); err != nil {
// Reset git caches after changing directory
git.ResetCaches()
// Initialize git repo with 'main' as default branch (modern git convention)
if err := exec.Command("git", "init", "--initial-branch=main").Run(); err != nil {
_ = os.Chdir(originalWd)
t.Fatalf("failed to init git repo: %v", err)
}
@@ -60,6 +65,7 @@ func setupGitRepo(t *testing.T) (repoPath string, cleanup func()) {
cleanup = func() {
_ = os.Chdir(originalWd)
git.ResetCaches()
}
return tmpDir, cleanup
@@ -80,6 +86,9 @@ func setupGitRepoWithBranch(t *testing.T, branch string) (repoPath string, clean
t.Fatalf("failed to change to temp directory: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Initialize git repo with specific branch
if err := exec.Command("git", "init", "-b", branch).Run(); err != nil {
_ = os.Chdir(originalWd)
@@ -103,6 +112,7 @@ func setupGitRepoWithBranch(t *testing.T, branch string) (repoPath string, clean
cleanup = func() {
_ = os.Chdir(originalWd)
git.ResetCaches()
}
return tmpDir, cleanup
@@ -123,8 +133,11 @@ func setupMinimalGitRepo(t *testing.T) (repoPath string, cleanup func()) {
t.Fatalf("failed to change to temp directory: %v", err)
}
// Initialize git repo
if err := exec.Command("git", "init").Run(); err != nil {
// Reset git caches after changing directory
git.ResetCaches()
// Initialize git repo with 'main' as default branch (modern git convention)
if err := exec.Command("git", "init", "--initial-branch=main").Run(); err != nil {
_ = os.Chdir(originalWd)
t.Fatalf("failed to init git repo: %v", err)
}
@@ -135,6 +148,7 @@ func setupMinimalGitRepo(t *testing.T) (repoPath string, cleanup func()) {
cleanup = func() {
_ = os.Chdir(originalWd)
git.ResetCaches()
}
return tmpDir, cleanup