- Embed git hooks in binary using go:embed and templates directory - Add bd hooks install/uninstall/list subcommands - Install hooks with automatic backup of existing hooks - Update AGENTS.md to reference 'bd hooks install' instead of install.sh - Add comprehensive tests for hooks commands - Update hook version to 0.22.1 Closes bd-908z
247 lines
6.3 KiB
Go
247 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetEmbeddedHooks(t *testing.T) {
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
|
|
expectedHooks := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
|
for _, hookName := range expectedHooks {
|
|
content, ok := hooks[hookName]
|
|
if !ok {
|
|
t.Errorf("Missing hook: %s", hookName)
|
|
continue
|
|
}
|
|
if len(content) == 0 {
|
|
t.Errorf("Hook %s has empty content", hookName)
|
|
}
|
|
// Verify it's a shell script
|
|
if content[:2] != "#!" {
|
|
t.Errorf("Hook %s doesn't start with shebang: %s", hookName, content[:50])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInstallHooks(t *testing.T) {
|
|
// Create temp directory with fake .git
|
|
tmpDir := t.TempDir()
|
|
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
|
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create test git dir: %v", err)
|
|
}
|
|
|
|
// Change to temp directory
|
|
oldWd, _ := os.Getwd()
|
|
defer os.Chdir(oldWd)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Get embedded hooks
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
|
|
// Install hooks
|
|
if err := installHooks(hooks, false); err != nil {
|
|
t.Fatalf("installHooks() failed: %v", err)
|
|
}
|
|
|
|
// Verify hooks were installed
|
|
for hookName := range hooks {
|
|
hookPath := filepath.Join(gitDir, hookName)
|
|
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
|
|
t.Errorf("Hook %s was not installed", hookName)
|
|
}
|
|
// Check it's executable
|
|
info, err := os.Stat(hookPath)
|
|
if err != nil {
|
|
t.Errorf("Failed to stat %s: %v", hookName, err)
|
|
continue
|
|
}
|
|
if info.Mode()&0111 == 0 {
|
|
t.Errorf("Hook %s is not executable", hookName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInstallHooksBackup(t *testing.T) {
|
|
// Create temp directory with fake .git
|
|
tmpDir := t.TempDir()
|
|
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
|
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create test git dir: %v", err)
|
|
}
|
|
|
|
// Change to temp directory
|
|
oldWd, _ := os.Getwd()
|
|
defer os.Chdir(oldWd)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Create an existing hook
|
|
existingHook := filepath.Join(gitDir, "pre-commit")
|
|
existingContent := "#!/bin/sh\necho old hook\n"
|
|
if err := os.WriteFile(existingHook, []byte(existingContent), 0755); err != nil {
|
|
t.Fatalf("Failed to create existing hook: %v", err)
|
|
}
|
|
|
|
// Get embedded hooks
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
|
|
// Install hooks (should backup existing)
|
|
if err := installHooks(hooks, false); err != nil {
|
|
t.Fatalf("installHooks() failed: %v", err)
|
|
}
|
|
|
|
// Verify backup was created
|
|
backupPath := existingHook + ".backup"
|
|
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
|
|
t.Errorf("Backup was not created")
|
|
}
|
|
|
|
// Verify backup has original content
|
|
backupContent, err := os.ReadFile(backupPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read backup: %v", err)
|
|
}
|
|
if string(backupContent) != existingContent {
|
|
t.Errorf("Backup content mismatch: got %q, want %q", string(backupContent), existingContent)
|
|
}
|
|
}
|
|
|
|
func TestInstallHooksForce(t *testing.T) {
|
|
// Create temp directory with fake .git
|
|
tmpDir := t.TempDir()
|
|
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
|
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create test git dir: %v", err)
|
|
}
|
|
|
|
// Change to temp directory
|
|
oldWd, _ := os.Getwd()
|
|
defer os.Chdir(oldWd)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Create an existing hook
|
|
existingHook := filepath.Join(gitDir, "pre-commit")
|
|
if err := os.WriteFile(existingHook, []byte("old"), 0755); err != nil {
|
|
t.Fatalf("Failed to create existing hook: %v", err)
|
|
}
|
|
|
|
// Get embedded hooks
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
|
|
// Install hooks with force (should not create backup)
|
|
if err := installHooks(hooks, true); err != nil {
|
|
t.Fatalf("installHooks() failed: %v", err)
|
|
}
|
|
|
|
// Verify no backup was created
|
|
backupPath := existingHook + ".backup"
|
|
if _, err := os.Stat(backupPath); !os.IsNotExist(err) {
|
|
t.Errorf("Backup should not have been created with --force")
|
|
}
|
|
}
|
|
|
|
func TestUninstallHooks(t *testing.T) {
|
|
// Create temp directory with fake .git
|
|
tmpDir := t.TempDir()
|
|
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
|
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create test git dir: %v", err)
|
|
}
|
|
|
|
// Change to temp directory
|
|
oldWd, _ := os.Getwd()
|
|
defer os.Chdir(oldWd)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Get embedded hooks and install them
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
if err := installHooks(hooks, false); err != nil {
|
|
t.Fatalf("installHooks() failed: %v", err)
|
|
}
|
|
|
|
// Uninstall hooks
|
|
if err := uninstallHooks(); err != nil {
|
|
t.Fatalf("uninstallHooks() failed: %v", err)
|
|
}
|
|
|
|
// Verify hooks were removed
|
|
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
|
for _, hookName := range hookNames {
|
|
hookPath := filepath.Join(gitDir, hookName)
|
|
if _, err := os.Stat(hookPath); !os.IsNotExist(err) {
|
|
t.Errorf("Hook %s was not removed", hookName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHooksCheckGitHooks(t *testing.T) {
|
|
// Create temp directory with fake .git
|
|
tmpDir := t.TempDir()
|
|
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
|
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create test git dir: %v", err)
|
|
}
|
|
|
|
// Change to temp directory
|
|
oldWd, _ := os.Getwd()
|
|
defer os.Chdir(oldWd)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Initially no hooks installed
|
|
statuses, err := CheckGitHooks()
|
|
if err != nil {
|
|
t.Fatalf("CheckGitHooks() failed: %v", err)
|
|
}
|
|
|
|
for _, status := range statuses {
|
|
if status.Installed {
|
|
t.Errorf("Hook %s should not be installed initially", status.Name)
|
|
}
|
|
}
|
|
|
|
// Install hooks
|
|
hooks, err := getEmbeddedHooks()
|
|
if err != nil {
|
|
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
|
}
|
|
if err := installHooks(hooks, false); err != nil {
|
|
t.Fatalf("installHooks() failed: %v", err)
|
|
}
|
|
|
|
// Check again
|
|
statuses, err = CheckGitHooks()
|
|
if err != nil {
|
|
t.Fatalf("CheckGitHooks() failed: %v", err)
|
|
}
|
|
|
|
for _, status := range statuses {
|
|
if !status.Installed {
|
|
t.Errorf("Hook %s should be installed", status.Name)
|
|
}
|
|
if status.Version != Version {
|
|
t.Errorf("Hook %s version mismatch: got %s, want %s", status.Name, status.Version, Version)
|
|
}
|
|
if status.Outdated {
|
|
t.Errorf("Hook %s should not be outdated", status.Name)
|
|
}
|
|
}
|
|
}
|