|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDoUninstallHooks_AppendedLines(t *testing.T) { |
| 13 | + // Simulate a hook file where git-pkgs lines were appended to an existing hook |
| 14 | + existingContent := `#!/bin/bash |
| 15 | +echo "my custom hook" |
| 16 | +do_something |
| 17 | +
|
| 18 | +# git-pkgs hook |
| 19 | +git pkgs reindex --quiet 2>/dev/null || true |
| 20 | +` |
| 21 | + |
| 22 | + dir := t.TempDir() |
| 23 | + hooksDir := filepath.Join(dir, "hooks") |
| 24 | + if err := os.MkdirAll(hooksDir, 0755); err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + |
| 28 | + hookPath := filepath.Join(hooksDir, "post-commit") |
| 29 | + if err := os.WriteFile(hookPath, []byte(existingContent), 0755); err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + // Save original hookNames and override for test |
| 34 | + origHookNames := hookNames |
| 35 | + hookNames = []string{"post-commit"} |
| 36 | + defer func() { hookNames = origHookNames }() |
| 37 | + |
| 38 | + rootCmd := newTestCommand() |
| 39 | + if err := doUninstallHooks(rootCmd, hooksDir); err != nil { |
| 40 | + t.Fatalf("unexpected error: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + content, err := os.ReadFile(hookPath) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("reading hook: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + result := string(content) |
| 49 | + if strings.Contains(result, "git-pkgs") { |
| 50 | + t.Errorf("expected git-pkgs lines to be removed, got:\n%s", result) |
| 51 | + } |
| 52 | + if strings.Contains(result, "git pkgs reindex") { |
| 53 | + t.Errorf("expected reindex line to be removed, got:\n%s", result) |
| 54 | + } |
| 55 | + if !strings.Contains(result, "my custom hook") { |
| 56 | + t.Error("expected original hook content to be preserved") |
| 57 | + } |
| 58 | + if !strings.Contains(result, "do_something") { |
| 59 | + t.Error("expected original hook content to be preserved") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestDoUninstallHooks_BlankLineBetweenMarkers(t *testing.T) { |
| 64 | + // Edge case: blank line between comment and command should still remove both |
| 65 | + existingContent := `#!/bin/bash |
| 66 | +echo "my custom hook" |
| 67 | +
|
| 68 | +# git-pkgs hook |
| 69 | +
|
| 70 | +git pkgs reindex --quiet 2>/dev/null || true |
| 71 | +` |
| 72 | + |
| 73 | + dir := t.TempDir() |
| 74 | + hooksDir := filepath.Join(dir, "hooks") |
| 75 | + if err := os.MkdirAll(hooksDir, 0755); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + hookPath := filepath.Join(hooksDir, "post-commit") |
| 80 | + if err := os.WriteFile(hookPath, []byte(existingContent), 0755); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + origHookNames := hookNames |
| 85 | + hookNames = []string{"post-commit"} |
| 86 | + defer func() { hookNames = origHookNames }() |
| 87 | + |
| 88 | + rootCmd := newTestCommand() |
| 89 | + if err := doUninstallHooks(rootCmd, hooksDir); err != nil { |
| 90 | + t.Fatalf("unexpected error: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + content, err := os.ReadFile(hookPath) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("reading hook: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + result := string(content) |
| 99 | + if strings.Contains(result, "git-pkgs") { |
| 100 | + t.Errorf("expected git-pkgs lines to be removed, got:\n%s", result) |
| 101 | + } |
| 102 | + if strings.Contains(result, "git pkgs reindex") { |
| 103 | + t.Errorf("expected reindex line to be removed, got:\n%s", result) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func newTestCommand() *cobra.Command { |
| 108 | + return &cobra.Command{Use: "test"} |
| 109 | +} |
0 commit comments