-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathexternal_hooks_test.go
More file actions
194 lines (175 loc) · 6.15 KB
/
Copy pathexternal_hooks_test.go
File metadata and controls
194 lines (175 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//go:build integration
package integration
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/entireio/cli/cmd/entire/cli/jsonutil"
)
// writeExternalHooksSettings overwrites .entire/settings.json with the
// external git hooks backend pointed at the given external_dir.
// Replaces (not merges) because InitEntire's writer used the wrong shape
// for our discriminated union — full overwrite is the cleanest path.
func writeExternalHooksSettings(t *testing.T, env *TestEnv, externalDir string) {
t.Helper()
settings := map[string]any{
"enabled": true,
"local_dev": true,
"strategy_options": map[string]any{
"filtered_fetches": true,
},
"git_hooks": map[string]any{
"backend": "external",
"external_dir": externalDir,
},
}
data, err := jsonutil.MarshalIndentWithNewline(settings, "", " ")
if err != nil {
t.Fatalf("marshal settings: %v", err)
}
settingsPath := filepath.Join(env.RepoDir, ".entire", "settings.json")
if err := os.WriteFile(settingsPath, data, 0o644); err != nil {
t.Fatalf("write settings: %v", err)
}
}
// snapshotFiles returns a flat map of relpath→contents for every regular
// file under root. Used for byte-identical comparison after `entire enable`
// runs in external mode — the contract says nothing outside marker reads
// should change on disk.
func snapshotFiles(t *testing.T, root string) map[string]string {
t.Helper()
out := make(map[string]string)
err := filepath.Walk(root, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if info.IsDir() {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
out[rel] = string(data)
return nil
})
if err != nil && !os.IsNotExist(err) {
t.Fatalf("snapshot %s: %v", root, err)
}
return out
}
func mapsEqual(a, b map[string]string) (string, bool) {
for k, v := range a {
bv, ok := b[k]
if !ok {
return "missing key " + k, false
}
if bv != v {
return "content changed for " + k, false
}
}
for k := range b {
if _, ok := a[k]; !ok {
return "unexpected new key " + k, false
}
}
return "", true
}
// TestEnable_ExternalBackend_HuskyShape_EndToEnd is the end-to-end tracer for
// the external git hooks backend. It exercises the full `entire enable` path
// in a Husky-shaped repository (.husky/_/* stubs + .husky/<hook> user scripts)
// and asserts:
// 1. exit code 0
// 2. stdout contains the variant-3 hint line
// 3. .git/hooks/ is byte-identical before and after (no install)
// 4. .husky/ is byte-identical before and after (no writes to user dir)
//
// This test is the safety net for the design contract: external mode must
// be detection-only. If any cycle's GREEN implementation accidentally writes
// to .git/hooks/ or .husky/, this test catches it.
func TestEnable_ExternalBackend_HuskyShape_EndToEnd(t *testing.T) {
t.Parallel()
env := NewRepoWithCommit(t)
// Husky shape: stubs in .husky/_/, user scripts in .husky/<hook>.
// Each user script carries the Entire marker and a dispatch call so
// IsGitHookInstalled detects them.
huskyDir := filepath.Join(env.RepoDir, ".husky")
huskyStubsDir := filepath.Join(huskyDir, "_")
if err := os.MkdirAll(huskyStubsDir, 0o755); err != nil {
t.Fatal(err)
}
managedHooks := []string{"prepare-commit-msg", "commit-msg", "post-commit", "post-rewrite", "pre-push"}
for _, h := range managedHooks {
// .husky/_/<hook>: Husky's stub (we don't write or touch these)
stubContent := "#!/bin/sh\n# managed by husky — DO NOT EDIT\n" + h + " \"$@\"\n"
if err := os.WriteFile(filepath.Join(huskyStubsDir, h), []byte(stubContent), 0o755); err != nil {
t.Fatal(err)
}
// .husky/<hook>: user-owned script containing the Entire marker
userContent := "#!/bin/sh\n# Entire CLI hooks\nentire hooks git " + h + " \"$@\"\n"
if err := os.WriteFile(filepath.Join(huskyDir, h), []byte(userContent), 0o755); err != nil {
t.Fatal(err)
}
}
// Configure external backend pointed at .husky/
writeExternalHooksSettings(t, env, ".husky")
hooksDir := filepath.Join(env.RepoDir, ".git", "hooks")
gitHooksBefore := snapshotFiles(t, hooksDir)
huskyBefore := snapshotFiles(t, huskyDir)
output, err := env.RunCLIWithError("enable")
if err != nil {
t.Fatalf("enable failed: %v\noutput:\n%s", err, output)
}
// Variant-3 success hint
wantHint := "Git hooks: external (.husky)"
if !strings.Contains(output, wantHint) {
t.Errorf("expected output to contain %q\nfull output:\n%s", wantHint, output)
}
// .git/hooks/ unchanged (entire never wrote here)
gitHooksAfter := snapshotFiles(t, hooksDir)
if msg, ok := mapsEqual(gitHooksBefore, gitHooksAfter); !ok {
t.Errorf(".git/hooks/ changed in external mode: %s\nbefore: %d files\nafter: %d files",
msg, len(gitHooksBefore), len(gitHooksAfter))
}
// .husky/ unchanged (user-owned directory; we promised not to touch it)
huskyAfter := snapshotFiles(t, huskyDir)
if msg, ok := mapsEqual(huskyBefore, huskyAfter); !ok {
t.Errorf(".husky/ changed in external mode: %s\nbefore: %d files\nafter: %d files",
msg, len(huskyBefore), len(huskyAfter))
}
}
// TestEnable_ExternalBackend_MissingDir_AbortsWithHelp pins down the failure
// path: external + dir absent → exit non-zero + full instructional message
// printed. We don't assert the entire 30+ line block, just the key markers
// that prove it came from FormatExternalDirMissingHelp.
func TestEnable_ExternalBackend_MissingDir_AbortsWithHelp(t *testing.T) {
t.Parallel()
env := NewRepoWithCommit(t)
// Configure external backend pointing to a directory that does NOT exist
writeExternalHooksSettings(t, env, ".husky")
output, err := env.RunCLIWithError("enable")
if err == nil {
t.Fatalf("enable should fail when external_dir is missing\noutput:\n%s", output)
}
// Output must contain the instructional message key phrases
mustContain := []string{
`.husky`,
"Required setup for external git hooks",
"# Entire CLI hooks",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"post-rewrite",
"pre-push",
}
for _, s := range mustContain {
if !strings.Contains(output, s) {
t.Errorf("output missing %q\nfull output:\n%s", s, output)
}
}
}