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

@@ -23,8 +23,8 @@ func setupGitRepo(t *testing.T) string {
t.Fatalf("failed to create .beads directory: %v", err)
}
// Initialize git repo
cmd := exec.Command("git", "init")
// Initialize git repo with 'main' as default branch (modern git convention)
cmd := exec.Command("git", "init", "--initial-branch=main")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to init git repo: %v", err)
@@ -278,8 +278,8 @@ func setupGitRepoInDir(t *testing.T, dir string) {
t.Fatalf("failed to create .beads directory: %v", err)
}
// Initialize git repo
cmd := exec.Command("git", "init")
// Initialize git repo with 'main' as default branch (modern git convention)
cmd := exec.Command("git", "init", "--initial-branch=main")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to init git repo: %v", err)

View File

@@ -9,6 +9,7 @@ import (
"runtime"
"testing"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
)
@@ -90,6 +91,7 @@ func testFreshCloneAutoImport(t *testing.T) {
// Test checkGitForIssues detects issues.jsonl
t.Chdir(dir)
git.ResetCaches() // Reset git caches after changing directory
count, path, gitRef := checkGitForIssues()
if count != 1 {
@@ -169,6 +171,7 @@ func testDatabaseRemovalScenario(t *testing.T) {
// Change to test directory
t.Chdir(dir)
git.ResetCaches() // Reset git caches after changing directory
// Test checkGitForIssues finds issues.jsonl (canonical name)
count, path, gitRef := checkGitForIssues()
@@ -247,6 +250,7 @@ func testLegacyFilenameSupport(t *testing.T) {
// Change to test directory
t.Chdir(dir)
git.ResetCaches() // Reset git caches after changing directory
// Test checkGitForIssues finds issues.jsonl
count, path, gitRef := checkGitForIssues()
@@ -323,6 +327,7 @@ func testPrecedenceTest(t *testing.T) {
// Change to test directory
t.Chdir(dir)
git.ResetCaches() // Reset git caches after changing directory
// Test checkGitForIssues prefers issues.jsonl
count, path, _ := checkGitForIssues()
@@ -369,6 +374,7 @@ func testInitSafetyCheck(t *testing.T) {
// Change to test directory
t.Chdir(dir)
git.ResetCaches() // Reset git caches after changing directory
// Create empty database (simulating failed import)
dbPath := filepath.Join(beadsDir, "test.db")
@@ -409,8 +415,14 @@ func testInitSafetyCheck(t *testing.T) {
// Helper functions
// runCmd runs a command and fails the test if it returns an error
// If the command is "git init", it automatically adds --initial-branch=main
// for modern git compatibility.
func runCmd(t *testing.T, dir string, name string, args ...string) {
t.Helper()
// Add --initial-branch=main to git init for modern git compatibility
if name == "git" && len(args) > 0 && args[0] == "init" {
args = append(args, "--initial-branch=main")
}
cmd := exec.Command(name, args...)
cmd.Dir = dir
if output, err := cmd.CombinedOutput(); err != nil {

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

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/git"
// Import SQLite driver for test database creation
_ "github.com/ncruces/go-sqlite3/driver"
@@ -70,8 +71,10 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
// Reset git caches after changing directory
git.ResetCaches()
// Reinitialize config to restore original state
_ = config.Initialize()
}()
@@ -79,6 +82,9 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory (required for IsWorktree to re-detect)
git.ResetCaches()
// No sync-branch configured
os.Unsetenv("BEADS_SYNC_BRANCH")
@@ -106,14 +112,18 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
git.ResetCaches()
_ = config.Initialize()
}()
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Reinitialize config to pick up the new directory's config.yaml
if err := config.Initialize(); err != nil {
t.Fatalf("Failed to reinitialize config: %v", err)
@@ -137,14 +147,18 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
git.ResetCaches()
_ = config.Initialize()
}()
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Reinitialize config to pick up the new directory's config.yaml
if err := config.Initialize(); err != nil {
t.Fatalf("Failed to reinitialize config: %v", err)
@@ -187,14 +201,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
git.ResetCaches()
_ = config.Initialize()
}()
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Clear all daemon-related env vars
os.Unsetenv("BEADS_NO_DAEMON")
os.Unsetenv("BEADS_AUTO_START_DAEMON")
@@ -220,14 +238,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
git.ResetCaches()
_ = config.Initialize()
}()
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Reinitialize config to pick up the new directory's config.yaml
if err := config.Initialize(); err != nil {
t.Fatalf("Failed to reinitialize config: %v", err)
@@ -253,14 +275,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) {
// Change to the worktree directory
origDir, _ := os.Getwd()
defer func() {
defer func() {
_ = os.Chdir(origDir)
git.ResetCaches()
_ = config.Initialize()
}()
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("Failed to change to worktree dir: %v", err)
}
// Reset git caches after changing directory
git.ResetCaches()
// Reinitialize config to pick up the new directory's config.yaml
if err := config.Initialize(); err != nil {
t.Fatalf("Failed to reinitialize config: %v", err)
@@ -302,8 +328,8 @@ func setupWorktreeTestRepo(t *testing.T) (mainDir, worktreeDir string) {
// Create main repo directory
mainDir = t.TempDir()
// Initialize git repo
cmd := exec.Command("git", "init")
// Initialize git repo with 'main' as default branch (modern git convention)
cmd := exec.Command("git", "init", "--initial-branch=main")
cmd.Dir = mainDir
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to init git repo: %v\n%s", err, output)

View File

@@ -31,7 +31,7 @@ func TestCommitToSyncBranch(t *testing.T) {
writeFile(t, jsonlPath, `{"id":"test-1"}`)
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "initial sync branch commit")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Write new content to commit
writeFile(t, jsonlPath, `{"id":"test-1"}`+"\n"+`{"id":"test-2"}`)
@@ -64,7 +64,7 @@ func TestCommitToSyncBranch(t *testing.T) {
writeFile(t, jsonlPath, `{"id":"test-1"}`)
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "initial")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Write the same content that's in the sync branch
writeFile(t, jsonlPath, `{"id":"test-1"}`)
@@ -101,7 +101,7 @@ func TestPullFromSyncBranch(t *testing.T) {
writeFile(t, jsonlPath, `{"id":"test-1"}`)
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "local sync")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Pull should handle the case where remote doesn't have the branch
result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false)
@@ -131,7 +131,7 @@ func TestPullFromSyncBranch(t *testing.T) {
runGit(t, repoDir, "commit", "-m", "sync commit")
// Set up a fake remote ref at the same commit
runGit(t, repoDir, "update-ref", "refs/remotes/origin/"+syncBranch, "HEAD")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Pull when already at remote HEAD
result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false)
@@ -158,7 +158,7 @@ func TestPullFromSyncBranch(t *testing.T) {
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "sync commit")
runGit(t, repoDir, "update-ref", "refs/remotes/origin/"+syncBranch, "HEAD")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Remove local JSONL to verify it gets copied back
os.Remove(jsonlPath)
@@ -198,7 +198,7 @@ func TestPullFromSyncBranch(t *testing.T) {
// Reset back to base (so remote is ahead)
runGit(t, repoDir, "reset", "--hard", baseCommit)
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// Pull should fast-forward
result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false)
@@ -233,7 +233,7 @@ func TestResetToRemote(t *testing.T) {
writeFile(t, jsonlPath, `{"id":"local-1"}`)
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "local commit")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// ResetToRemote should fail since remote branch doesn't exist
err := ResetToRemote(ctx, repoDir, syncBranch, jsonlPath)
@@ -264,7 +264,7 @@ func TestPushSyncBranch(t *testing.T) {
writeFile(t, filepath.Join(repoDir, ".beads", "issues.jsonl"), `{"id":"test-1"}`)
runGit(t, repoDir, "add", ".")
runGit(t, repoDir, "commit", "-m", "initial")
runGit(t, repoDir, "checkout", "master")
runGit(t, repoDir, "checkout", "main")
// PushSyncBranch should handle the worktree creation
err := PushSyncBranch(ctx, repoDir, syncBranch)
@@ -391,8 +391,8 @@ func setupTestRepoWithRemote(t *testing.T) string {
t.Fatalf("Failed to create temp dir: %v", err)
}
// Initialize git repo
runGit(t, tmpDir, "init")
// Initialize git repo with 'main' as default branch (modern git convention)
runGit(t, tmpDir, "init", "--initial-branch=main")
runGit(t, tmpDir, "config", "user.email", "test@test.com")
runGit(t, tmpDir, "config", "user.name", "Test User")