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:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user