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:
Steve Yegge
2025-12-13 08:42:02 +11:00
parent 6985ea94e5
commit 4f44265f1d
2 changed files with 55 additions and 11 deletions

View File

@@ -43,7 +43,11 @@ func TestInstallHooks(t *testing.T) {
t.Skipf("Skipping test: git init failed: %v", err)
}
gitDir := filepath.Join(tmpDir, ".git", "hooks")
gitDirPath, err := getGitDir()
if err != nil {
t.Fatalf("getGitDir() failed: %v", err)
}
gitDir := filepath.Join(gitDirPath, "hooks")
// Get embedded hooks
hooks, err := getEmbeddedHooks()
@@ -90,7 +94,16 @@ func TestInstallHooksBackup(t *testing.T) {
t.Skipf("Skipping test: git init failed: %v", err)
}
gitDir := filepath.Join(tmpDir, ".git", "hooks")
gitDirPath, err := getGitDir()
if err != nil {
t.Fatalf("getGitDir() failed: %v", err)
}
gitDir := filepath.Join(gitDirPath, "hooks")
// Ensure hooks directory exists
if err := os.MkdirAll(gitDir, 0750); err != nil {
t.Fatalf("Failed to create hooks directory: %v", err)
}
// Create an existing hook
existingHook := filepath.Join(gitDir, "pre-commit")
@@ -138,7 +151,16 @@ func TestInstallHooksForce(t *testing.T) {
t.Skipf("Skipping test: git init failed: %v", err)
}
gitDir := filepath.Join(tmpDir, ".git", "hooks")
gitDirPath, err := getGitDir()
if err != nil {
t.Fatalf("getGitDir() failed: %v", err)
}
gitDir := filepath.Join(gitDirPath, "hooks")
// Ensure hooks directory exists
if err := os.MkdirAll(gitDir, 0750); err != nil {
t.Fatalf("Failed to create hooks directory: %v", err)
}
// Create an existing hook
existingHook := filepath.Join(gitDir, "pre-commit")
@@ -176,7 +198,11 @@ func TestUninstallHooks(t *testing.T) {
t.Skipf("Skipping test: git init failed: %v", err)
}
gitDir := filepath.Join(tmpDir, ".git", "hooks")
gitDirPath, err := getGitDir()
if err != nil {
t.Fatalf("getGitDir() failed: %v", err)
}
gitDir := filepath.Join(gitDirPath, "hooks")
// Get embedded hooks and install them
hooks, err := getEmbeddedHooks()
@@ -294,7 +320,11 @@ func TestInstallHooksShared(t *testing.T) {
}
// Verify hooks were NOT installed to .git/hooks/
standardHooksDir := filepath.Join(".git", "hooks")
gitDirPath, err := getGitDir()
if err != nil {
t.Fatalf("getGitDir() failed: %v", err)
}
standardHooksDir := filepath.Join(gitDirPath, "hooks")
for hookName := range hooks {
hookPath := filepath.Join(standardHooksDir, hookName)
if _, err := os.Stat(hookPath); !os.IsNotExist(err) {