fix: make hook tests worktree-aware by using getGitDir()
Tests were hardcoding '.git' paths directly, which fails in git worktrees where .git is a file (not a directory) containing a pointer to the actual git directory. Changes: - Replace hardcoded '.git' paths with getGitDir() calls in all hook tests - Add os.MkdirAll() calls to ensure hooks directory exists before writing Fixes test failures in: - TestInstallHooks, TestInstallHooksBackup, TestInstallHooksForce, TestUninstallHooks, TestInstallHooksShared (hooks_test.go) - TestDetectExistingHooks, TestInstallGitHooks_NoExistingHooks, TestInstallGitHooks_ExistingHookBackup (init_hooks_test.go) Cherry-picked from PR #472 Co-Authored-By: matt wilkie <maphew@gmail.com> 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,8 +18,11 @@ func TestDetectExistingHooks(t *testing.T) {
|
||||
t.Skipf("Skipping test: git init failed: %v", err)
|
||||
}
|
||||
|
||||
gitDir := filepath.Join(tmpDir, ".git")
|
||||
hooksDir := filepath.Join(gitDir, "hooks")
|
||||
gitDirPath, err := getGitDir()
|
||||
if err != nil {
|
||||
t.Fatalf("getGitDir() failed: %v", err)
|
||||
}
|
||||
hooksDir := filepath.Join(gitDirPath, "hooks")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -109,8 +112,11 @@ func TestInstallGitHooks_NoExistingHooks(t *testing.T) {
|
||||
t.Skipf("Skipping test: git init failed: %v", err)
|
||||
}
|
||||
|
||||
gitDir := filepath.Join(tmpDir, ".git")
|
||||
hooksDir := filepath.Join(gitDir, "hooks")
|
||||
gitDirPath, err := getGitDir()
|
||||
if err != nil {
|
||||
t.Fatalf("getGitDir() failed: %v", err)
|
||||
}
|
||||
hooksDir := filepath.Join(gitDirPath, "hooks")
|
||||
|
||||
// Note: Can't fully test interactive prompt in automated tests
|
||||
// This test verifies the logic works when no existing hooks present
|
||||
@@ -148,8 +154,16 @@ func TestInstallGitHooks_ExistingHookBackup(t *testing.T) {
|
||||
t.Skipf("Skipping test: git init failed: %v", err)
|
||||
}
|
||||
|
||||
gitDir := filepath.Join(tmpDir, ".git")
|
||||
hooksDir := filepath.Join(gitDir, "hooks")
|
||||
gitDirPath, err := getGitDir()
|
||||
if err != nil {
|
||||
t.Fatalf("getGitDir() failed: %v", err)
|
||||
}
|
||||
hooksDir := filepath.Join(gitDirPath, "hooks")
|
||||
|
||||
// Ensure hooks directory exists
|
||||
if err := os.MkdirAll(hooksDir, 0750); err != nil {
|
||||
t.Fatalf("Failed to create hooks directory: %v", err)
|
||||
}
|
||||
|
||||
// Create an existing pre-commit hook
|
||||
preCommitPath := filepath.Join(hooksDir, "pre-commit")
|
||||
|
||||
Reference in New Issue
Block a user