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:
@@ -43,7 +43,11 @@ func TestInstallHooks(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
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
|
// Get embedded hooks
|
||||||
hooks, err := getEmbeddedHooks()
|
hooks, err := getEmbeddedHooks()
|
||||||
@@ -90,7 +94,16 @@ func TestInstallHooksBackup(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
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
|
// Create an existing hook
|
||||||
existingHook := filepath.Join(gitDir, "pre-commit")
|
existingHook := filepath.Join(gitDir, "pre-commit")
|
||||||
@@ -138,7 +151,16 @@ func TestInstallHooksForce(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
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
|
// Create an existing hook
|
||||||
existingHook := filepath.Join(gitDir, "pre-commit")
|
existingHook := filepath.Join(gitDir, "pre-commit")
|
||||||
@@ -176,7 +198,11 @@ func TestUninstallHooks(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
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
|
// Get embedded hooks and install them
|
||||||
hooks, err := getEmbeddedHooks()
|
hooks, err := getEmbeddedHooks()
|
||||||
@@ -294,7 +320,11 @@ func TestInstallHooksShared(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify hooks were NOT installed to .git/hooks/
|
// 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 {
|
for hookName := range hooks {
|
||||||
hookPath := filepath.Join(standardHooksDir, hookName)
|
hookPath := filepath.Join(standardHooksDir, hookName)
|
||||||
if _, err := os.Stat(hookPath); !os.IsNotExist(err) {
|
if _, err := os.Stat(hookPath); !os.IsNotExist(err) {
|
||||||
|
|||||||
@@ -18,8 +18,11 @@ func TestDetectExistingHooks(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
t.Skipf("Skipping test: git init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gitDir := filepath.Join(tmpDir, ".git")
|
gitDirPath, err := getGitDir()
|
||||||
hooksDir := filepath.Join(gitDir, "hooks")
|
if err != nil {
|
||||||
|
t.Fatalf("getGitDir() failed: %v", err)
|
||||||
|
}
|
||||||
|
hooksDir := filepath.Join(gitDirPath, "hooks")
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -109,8 +112,11 @@ func TestInstallGitHooks_NoExistingHooks(t *testing.T) {
|
|||||||
t.Skipf("Skipping test: git init failed: %v", err)
|
t.Skipf("Skipping test: git init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gitDir := filepath.Join(tmpDir, ".git")
|
gitDirPath, err := getGitDir()
|
||||||
hooksDir := filepath.Join(gitDir, "hooks")
|
if err != nil {
|
||||||
|
t.Fatalf("getGitDir() failed: %v", err)
|
||||||
|
}
|
||||||
|
hooksDir := filepath.Join(gitDirPath, "hooks")
|
||||||
|
|
||||||
// Note: Can't fully test interactive prompt in automated tests
|
// Note: Can't fully test interactive prompt in automated tests
|
||||||
// This test verifies the logic works when no existing hooks present
|
// 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)
|
t.Skipf("Skipping test: git init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gitDir := filepath.Join(tmpDir, ".git")
|
gitDirPath, err := getGitDir()
|
||||||
hooksDir := filepath.Join(gitDir, "hooks")
|
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
|
// Create an existing pre-commit hook
|
||||||
preCommitPath := filepath.Join(hooksDir, "pre-commit")
|
preCommitPath := filepath.Join(hooksDir, "pre-commit")
|
||||||
|
|||||||
Reference in New Issue
Block a user