Skip to content

Commit 902eaf8

Browse files
author
elchananarb
committed
Add integration test for pre-commit flag (AST-89008)
1 parent 4ffb9f0 commit 902eaf8

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ jobs:
4545
run: go build -o ./bin/cx ./cmd
4646
- name: Install gocovmerge
4747
run: go install github.com/wadey/gocovmerge@latest
48+
- name: Install pre-commit
49+
run: |
50+
pip install pre-commit
51+
pre-commit install
4852
- name: Go Integration test
4953
shell: bash
5054
env:

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
repos:
2+
- repo: local
3+
hooks: []
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)