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

@@ -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)