|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestHooksPreCommitInstallAndUninstallPreCommitHook(t *testing.T) { |
| 16 | + tmpDir, cleanup := setupTempDir(t) |
| 17 | + defer cleanup() |
| 18 | + |
| 19 | + // Initialize Git repository |
| 20 | + execCmd(t, tmpDir, "git", "init") |
| 21 | + |
| 22 | + // Install pre-commit hook locally |
| 23 | + _ = executeCmdNilAssertion(t, "Installing pre-commit hook", "hooks", "pre-commit", "secrets-install-git-hook") |
| 24 | + |
| 25 | + // Verify hook installation |
| 26 | + hookPath := filepath.Join(tmpDir, ".git", "hooks", "pre-commit") |
| 27 | + assert.FileExists(t, hookPath, "Hook should be installed") |
| 28 | + data, err := os.ReadFile(hookPath) |
| 29 | + assert.NoError(t, err) |
| 30 | + assert.Contains(t, string(data), "cx-secret-detection", "Hook should contain cx-secret-detection") |
| 31 | + |
| 32 | + // Uninstall pre-commit hook |
| 33 | + _ = executeCmdNilAssertion(t, "Uninstalling cx-secret-detection hook", "hooks", "pre-commit", "secrets-uninstall-git-hook") |
| 34 | + |
| 35 | + // Verify hook removal |
| 36 | + hookPath = filepath.Join(tmpDir, ".git", "hooks", "pre-commit") |
| 37 | + data, err = os.ReadFile(hookPath) |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.NotContains(t, string(data), "cx-secret-detection", "Hook content should be cleared after uninstall") |
| 40 | +} |
| 41 | + |
| 42 | +func TestHooksPreCommitUpdatePreCommitHook(t *testing.T) { |
| 43 | + tmpDir, cleanup := setupTempDir(t) |
| 44 | + defer cleanup() |
| 45 | + |
| 46 | + // Initialize Git repository |
| 47 | + execCmd(t, tmpDir, "git", "init") |
| 48 | + |
| 49 | + // Install pre-commit hook locally |
| 50 | + _ = executeCmdNilAssertion(t, "Installing pre-commit hook", "hooks", "pre-commit", "secrets-install-git-hook") |
| 51 | + |
| 52 | + // Update pre-commit hook |
| 53 | + err := executeCmdNilAssertion(t, "Updating pre-commit hook", "hooks", "pre-commit", "secrets-update-git-hook") |
| 54 | + assert.NoError(t, err, "Hook should update successfully") |
| 55 | +} |
| 56 | + |
| 57 | +// Helper functions |
| 58 | +func execCmd(t *testing.T, dir string, name string, args ...string) { |
| 59 | + cmd := exec.Command(name, args...) |
| 60 | + cmd.Dir = dir |
| 61 | + output, err := cmd.CombinedOutput() |
| 62 | + assert.NoError(t, err, "Failed command %s: %s", strings.Join(cmd.Args, " "), string(output)) |
| 63 | +} |
| 64 | + |
| 65 | +func setupTempDir(t *testing.T) (string, func()) { |
| 66 | + originalWD, err := os.Getwd() |
| 67 | + assert.NoError(t, err) |
| 68 | + |
| 69 | + tmpDir := t.TempDir() |
| 70 | + assert.NoError(t, os.Chdir(tmpDir)) |
| 71 | + |
| 72 | + cleanup := func() { |
| 73 | + assert.NoError(t, os.Chdir(originalWD)) |
| 74 | + } |
| 75 | + |
| 76 | + return tmpDir, cleanup |
| 77 | +} |
0 commit comments