-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_analyze_test.go
More file actions
343 lines (327 loc) · 10.6 KB
/
commit_analyze_test.go
File metadata and controls
343 lines (327 loc) · 10.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package repomap
import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// initTestRepo builds a disposable git repo with initial committed content,
// then applies `mutations` (file -> new content) to the working tree without
// staging. Returns the repo root path.
func initTestRepo(t *testing.T, initial, mutations map[string]string) string {
t.Helper()
root := t.TempDir()
runGitT(t, root, "init", "-q", "-b", "main")
runGitT(t, root, "config", "user.email", "test@example.com")
runGitT(t, root, "config", "user.name", "Test")
runGitT(t, root, "config", "commit.gpgsign", "false")
for path, content := range initial {
writeFixture(t, root, path, content)
}
if len(initial) > 0 {
runGitT(t, root, "add", "-A")
runGitT(t, root, "commit", "-q", "-m", "initial")
}
for path, content := range mutations {
writeFixture(t, root, path, content)
}
return root
}
func writeFixture(t *testing.T, root, rel, content string) {
t.Helper()
abs := filepath.Join(root, rel)
if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", abs, err)
}
if err := os.WriteFile(abs, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", abs, err)
}
}
func runGitT(t *testing.T, root string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = root
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
}
// Test_Analyze_HappyPath: a test-pair + a config change form two groups,
// both high confidence, messages non-generic.
func Test_Analyze_HappyPath(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{
"go.mod": "module fixture\ngo 1.22\n",
"pkg/foo.go": "package pkg\nfunc Foo() int { return 1 }\n",
"pkg/foo_test.go": "package pkg\nimport \"testing\"\nfunc TestFoo(t *testing.T) { _ = Foo() }\n",
"README.md": "# Fixture\n",
},
map[string]string{
"pkg/foo.go": "package pkg\nfunc Foo() int { return 2 }\nfunc Bar() int { return 3 }\n",
"pkg/foo_test.go": "package pkg\nimport \"testing\"\nfunc TestFoo(t *testing.T) { _ = Foo() }\nfunc TestBar(t *testing.T) { _ = Bar() }\n",
"README.md": "# Fixture\n\nUpdated.\n",
},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.EarlyExit {
t.Fatalf("early exit unexpected")
}
if got.Counts.Total != 3 {
t.Errorf("total files = %d, want 3", got.Counts.Total)
}
if len(got.Groups) < 1 {
t.Fatalf("no groups produced")
}
// The test-pair edge (weight 1.0) must bind foo.go + foo_test.go.
foundPair := false
for _, g := range got.Groups {
if containsAll(g.Files, "pkg/foo.go", "pkg/foo_test.go") {
foundPair = true
if g.Confidence < 0.75 {
t.Errorf("test-pair group confidence %.2f < 0.75", g.Confidence)
}
}
}
if !foundPair {
t.Errorf("test-pair group not found; groups=%+v", got.Groups)
}
}
// Test_Analyze_CleanRepo: no dirty files => early_exit.
func Test_Analyze_CleanRepo(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"README.md": "# clean\n"},
nil,
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if !got.EarlyExit {
t.Errorf("expected early_exit=true on clean repo")
}
}
// Test_Analyze_Secrets: a live AWS access key in a config file must FLAG.
func Test_Analyze_Secrets(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"config.yaml": "key: value\n"},
map[string]string{"config.yaml": "key: value\naws_access_key: AKIAIOSFODNN7EXAMPLE\n"},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.Secrets.Clean {
t.Errorf("expected secrets.clean=false when AKIA key present")
}
if got.Secrets.FlagCount < 1 {
t.Errorf("expected at least 1 FLAG finding, got %d", got.Secrets.FlagCount)
}
}
// Test_Analyze_PlaceholderPaths: /Users/you/ should NOT flag as PII.
func Test_Analyze_PlaceholderPaths(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"docs.md": "# Docs\n"},
map[string]string{"docs.md": "# Docs\n\nInstall at /Users/you/bin/tool.\n"},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
for _, f := range readFindings(t, got.Refs.Findings) {
if f.Kind == "pii" && strings.Contains(f.Snippet, "/Users/you/") {
t.Errorf("placeholder path leaked into findings: %+v", f)
}
}
}
// Test_Analyze_DepBump: go.mod version bump is detected.
func Test_Analyze_DepBump(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{
"go.mod": "module fixture\n\ngo 1.22\n\nrequire github.com/pkg/errors v0.9.0\n",
},
map[string]string{
"go.mod": "module fixture\n\ngo 1.22\n\nrequire github.com/pkg/errors v0.9.1\n",
},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if len(got.DepBumps) == 0 {
t.Fatalf("expected dep_bumps entry for go.mod")
}
if got.DepBumps[0].Manager != "go" {
t.Errorf("manager = %q, want %q", got.DepBumps[0].Manager, "go")
}
}
// Test_Analyze_TagAndSubjects: latest_tag + recent_subjects are emitted so the
// agent never re-runs `git log` / `git tag` for style or version lookup.
func Test_Analyze_TagAndSubjects(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"README.md": "# Fixture\n"},
nil,
)
// Two more commits with conventional-style subjects; tag the last one.
writeFixture(t, root, "a.txt", "a\n")
runGitT(t, root, "add", "-A")
runGitT(t, root, "commit", "-q", "-m", "feat(core): add a")
writeFixture(t, root, "b.txt", "b\n")
runGitT(t, root, "add", "-A")
runGitT(t, root, "commit", "-q", "-m", "fix(core): patch b")
runGitT(t, root, "tag", "v0.2.0")
// Dirty the tree so analyze doesn't early-exit.
writeFixture(t, root, "README.md", "# Fixture\n\nchanged\n")
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.LatestTag != "v0.2.0" {
t.Errorf("LatestTag = %q, want %q", got.LatestTag, "v0.2.0")
}
if len(got.RecentSubjects) == 0 {
t.Fatalf("RecentSubjects empty; expected recent commit subjects")
}
if len(got.RecentSubjects) > 5 {
t.Errorf("RecentSubjects = %d entries, want ≤5", len(got.RecentSubjects))
}
if !strings.HasPrefix(got.RecentSubjects[0], "fix(core)") {
t.Errorf("RecentSubjects[0] = %q, want newest-first conventional subject", got.RecentSubjects[0])
}
}
// Test_Analyze_NoTag_NoHistory: fresh repo with one commit, no tags — LatestTag
// must be empty (agent signal: propose initial v0.1.0).
func Test_Analyze_NoTag_NoHistory(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"README.md": "# fresh\n"},
map[string]string{"README.md": "# fresh\n\nmodified\n"},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.LatestTag != "" {
t.Errorf("LatestTag = %q, want empty on untagged repo", got.LatestTag)
}
}
// Test_Analyze_Visibility_None_PIISafe: a personal repo with no origin should
// get "safe" default_action on PII REVIEW findings (emails/localhost), so the
// agent can skip per-finding adjudication.
func Test_Analyze_Visibility_None_PIISafe(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"README.md": "# readme\n"},
map[string]string{"README.md": "# readme\n\nping me at dev@example.com on localhost:8080\n"},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.Remote.Visibility != "none" {
t.Fatalf("Remote.Visibility = %q, want %q (no origin remote)", got.Remote.Visibility, "none")
}
if got.Secrets.AmbiguousCount != 0 {
t.Errorf("AmbiguousCount = %d, want 0 (personal repo: PII REVIEWs should be safe)", got.Secrets.AmbiguousCount)
}
findings := readFindings(t, got.Refs.Findings)
if len(findings) == 0 {
t.Fatalf("expected PII REVIEW findings for email/localhost")
}
for _, f := range findings {
if f.Kind == "pii" && f.Class == "REVIEW" && f.DefaultAction != "safe" {
t.Errorf("PII REVIEW finding %q default_action = %q, want %q", f.Snippet, f.DefaultAction, "safe")
}
}
}
// Test_Analyze_Visibility_FlagAlwaysFix: FLAG findings always get default_action=fix
// regardless of visibility — live secrets are never "safe".
func Test_Analyze_Visibility_FlagAlwaysFix(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
root := initTestRepo(t,
map[string]string{"config.yaml": "key: value\n"},
map[string]string{"config.yaml": "key: value\naws_access_key: AKIAIOSFODNN7EXAMPLE\n"},
)
got, err := AnalyzeCommit(context.Background(), AnalyzeOptions{Root: root})
if err != nil {
t.Fatalf("AnalyzeCommit: %v", err)
}
if got.Secrets.FixCount < 1 {
t.Errorf("FixCount = %d, want >=1 (AKIA key must be fix)", got.Secrets.FixCount)
}
findings := readFindings(t, got.Refs.Findings)
for _, f := range findings {
if f.Class == "FLAG" && f.DefaultAction != "fix" {
t.Errorf("FLAG finding %q default_action = %q, want %q", f.Snippet, f.DefaultAction, "fix")
}
}
}
func containsAll(haystack []string, needles ...string) bool {
set := make(map[string]bool, len(haystack))
for _, h := range haystack {
set[h] = true
}
for _, n := range needles {
if !set[n] {
return false
}
}
return true
}
func readFindings(t *testing.T, path string) []Finding {
t.Helper()
if path == "" {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
t.Fatalf("read findings: %v", err)
}
// Reuse the existing JSON schema.
var out []Finding
if err := json.Unmarshal(data, &out); err != nil {
t.Fatalf("unmarshal findings: %v", err)
}
return out
}