-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincremental_test.go
More file actions
492 lines (396 loc) · 15.4 KB
/
Copy pathincremental_test.go
File metadata and controls
492 lines (396 loc) · 15.4 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package repomap
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newGitRepo initializes a temp git repo with one Go file and an initial commit.
// Returns the root path. Git subcommands use -c user.email/user.name to avoid
// environment dependencies.
func newGitRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
mainSrc := `package main
func Hello() string { return "hello" }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(mainSrc), 0o644))
gitRun(t, dir, "init")
gitRun(t, dir, "add", ".")
gitCommitAll(t, dir, "init")
return dir
}
// gitRun executes a git subcommand in dir. Fatals on non-zero exit.
func gitRun(t *testing.T, dir string, args ...string) {
t.Helper()
base := []string{"-c", "user.email=test@example.com", "-c", "user.name=Test"}
all := append(base, args...)
cmd := exec.Command("git", all...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
}
// gitCommitAll stages all changes and commits with the given message.
func gitCommitAll(t *testing.T, dir, msg string) {
t.Helper()
gitRun(t, dir, "add", "-A")
gitRun(t, dir, "commit", "-m", msg)
}
// buildWithCache runs Build() with cacheDir set and returns the built Map.
func buildWithCache(t *testing.T, dir, cacheDir string) *Map {
t.Helper()
m := New(dir, DefaultConfig())
m.SetCacheDir(cacheDir)
err := m.Build(context.Background())
require.NoError(t, err)
return m
}
// TestIncrementalUnchanged verifies the fast path when HEAD == LastSHA and no
// worktree changes. The second build must update builtAt and return the same output.
func TestIncrementalUnchanged(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
m1 := buildWithCache(t, dir, cacheDir)
out1 := m1.String()
builtAt1 := m1.BuiltAt()
// Small sleep so builtAt can advance.
time.Sleep(5 * time.Millisecond)
m2 := buildWithCache(t, dir, cacheDir)
out2 := m2.String()
assert.Equal(t, out1, out2, "output must be identical on unchanged repo")
assert.True(t, m2.BuiltAt().After(builtAt1) || m2.BuiltAt().Equal(builtAt1),
"builtAt must be updated on second build")
}
// TestIncrementalModifiedFile verifies that modifying a committed file causes
// the new symbol to appear in the next build's output.
func TestIncrementalModifiedFile(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
// Modify the existing file with a new exported function.
updated := `package main
func Hello() string { return "hello" }
func Added() int { return 42 }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(updated), 0o644))
gitCommitAll(t, dir, "add Added func")
m2 := buildWithCache(t, dir, cacheDir)
assert.Contains(t, m2.String(), "Added", "new symbol must appear after incremental rebuild")
assert.Contains(t, m2.String(), "Hello", "original symbol must still appear")
}
// TestIncrementalAddedFile verifies that adding a new file causes the new symbol
// to appear and the ranked count to increase by exactly 1.
func TestIncrementalAddedFile(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
m1 := buildWithCache(t, dir, cacheDir)
count1 := len(m1.Ranked())
helperSrc := `package main
func HelperFn() bool { return true }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "helper.go"), []byte(helperSrc), 0o644))
gitCommitAll(t, dir, "add helper.go")
m2 := buildWithCache(t, dir, cacheDir)
assert.Contains(t, m2.String(), "HelperFn", "new symbol from added file must appear")
assert.Equal(t, count1+1, len(m2.Ranked()), "ranked count must increase by exactly 1")
}
// TestIncrementalDeletedFile verifies that deleting a committed file removes it
// from the ranked output.
func TestIncrementalDeletedFile(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
// Add a second file so there are 2 tracked files.
extraSrc := `package main
func Extra() string { return "extra" }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "extra.go"), []byte(extraSrc), 0o644))
gitCommitAll(t, dir, "add extra.go")
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
// Delete extra.go and commit.
require.NoError(t, os.Remove(filepath.Join(dir, "extra.go")))
gitCommitAll(t, dir, "delete extra.go")
m2 := buildWithCache(t, dir, cacheDir)
for _, rf := range m2.Ranked() {
assert.NotEqual(t, "extra.go", rf.Path, "deleted file must not appear in ranked output")
}
}
func TestIncrementalRenamedFile(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
gitRun(t, dir, "mv", "main.go", "renamed.go")
gitCommitAll(t, dir, "rename main source")
m2 := buildWithCache(t, dir, cacheDir)
found := false
for _, rf := range m2.Ranked() {
assert.NotEqual(t, "main.go", rf.Path)
if rf.Path == "renamed.go" {
found = true
}
}
assert.True(t, found, "renamed source must replace its old cached path")
}
func TestPrepareIncrementalRejectsGoSemanticInputsBeforeHydration(t *testing.T) {
t.Parallel()
for _, path := range []string{"changed.go", "go.mod", "go.sum", "go.work"} {
for _, change := range []string{"added", "modified", "deleted"} {
t.Run(change+"_"+path, func(t *testing.T) {
m := New(t.TempDir(), DefaultConfig())
entry := diskCache{
Ranked: []RankedFile{{FileSymbols: &FileSymbols{Path: "cached.go"}}},
SemanticCallers: SymbolCallers{callsKey("cached.go", "Target"): {{File: "caller.go", Line: 3}}},
}
var added, modified, deleted []string
switch change {
case "added":
added = []string{path}
case "modified":
modified = []string{path}
case "deleted":
deleted = []string{path}
}
ok, changed := m.prepareIncremental(entry, added, modified, deleted)
assert.False(t, ok)
assert.Nil(t, changed)
assert.Empty(t, m.Ranked(), "semantic changes must reject before cache hydration")
assert.Empty(t, m.SemanticCallers(), "semantic callers must not be restored from an invalid cache")
})
}
}
}
func TestIncrementalDeletedGoCallerRebuildsSemanticCallers(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/incremental\n\ngo 1.26\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "target.go"), []byte("package main\n\nfunc Target() {}\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "caller.go"), []byte("package main\n\nfunc Call() { Target() }\n"), 0o644))
gitCommitAll(t, dir, "add semantic caller")
cacheDir := t.TempDir()
cfg := DefaultConfig()
cfg.GoAnalysisCalls = true
m1 := New(dir, cfg)
m1.SetCacheDir(cacheDir)
require.NoError(t, m1.Build(context.Background()))
require.NotEmpty(t, m1.SemanticCallers())
require.NoError(t, os.Remove(filepath.Join(dir, "caller.go")))
gitCommitAll(t, dir, "delete semantic caller")
m2 := New(dir, cfg)
m2.SetCacheDir(cacheDir)
ok, changed := m2.LoadCacheIncremental(context.Background(), cacheDir)
assert.False(t, ok, "deleted Go callers require a full semantic rebuild")
assert.Nil(t, changed)
require.NoError(t, m2.Build(context.Background()))
assert.Empty(t, m2.SemanticCallers(), "the deleted caller must disappear in the same build")
}
// TestIncrementalUntrackedFile verifies that an untracked file (not yet committed)
// is picked up by the incremental path via git ls-files --others.
func TestIncrementalUntrackedFile(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
// Add a new file WITHOUT committing it.
untrackedSrc := `package main
func UntrackedFn() float64 { return 3.14 }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "untracked.go"), []byte(untrackedSrc), 0o644))
m2 := buildWithCache(t, dir, cacheDir)
assert.Contains(t, m2.String(), "UntrackedFn", "untracked file symbol must appear in incremental output")
}
// TestIncrementalThresholdFallback verifies that when >30% of files change, the
// incremental path falls back to a full rebuild. The output must still be correct.
func TestIncrementalThresholdFallback(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
// Create 9 additional Go files so we have 10 total tracked files.
for i := range 9 {
src := fmt.Sprintf("package main\n\nfunc Fn%d() int { return %d }\n", i, i)
require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("file%d.go", i)), []byte(src), 0o644))
}
gitCommitAll(t, dir, "add 9 more files")
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
// Modify 5 out of 10 files (50% > 30% threshold) — triggers full rebuild.
for i := range 5 {
src := fmt.Sprintf("package main\n\nfunc Fn%dModified() int { return %d }\n", i, i+100)
require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("file%d.go", i)), []byte(src), 0o644))
}
gitCommitAll(t, dir, "modify 5 files")
m2 := buildWithCache(t, dir, cacheDir)
// All 10 files must still be present — full rebuild produces correct output.
assert.Equal(t, 10, len(m2.Ranked()), "full rebuild must include all 10 files")
// Modified symbols must appear.
assert.Contains(t, m2.String(), "Fn0Modified", "modified symbol must appear after full rebuild")
}
// TestIncrementalNonGitFallback verifies that a directory without git init falls
// through to full rebuild on every call (no panic, no stale state).
func TestIncrementalNonGitFallback(t *testing.T) {
t.Parallel()
// Plain tmpdir — no git init.
dir := t.TempDir()
mainSrc := `package main
func Plain() string { return "plain" }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(mainSrc), 0o644))
cacheDir := t.TempDir()
// First build must succeed (falls through to full build via walk).
m1 := New(dir, DefaultConfig())
m1.SetCacheDir(cacheDir)
// ScanFiles returns nil for non-git dirs, so Build returns ErrNotCodeProject.
// That is the correct behaviour — non-git dir produces no output.
err := m1.Build(context.Background())
// Either no error with output, or ErrNotCodeProject. Both are valid.
if err != nil {
assert.ErrorIs(t, err, ErrNotCodeProject)
return
}
// Second build — no panic even if first returned no output.
m2 := New(dir, DefaultConfig())
m2.SetCacheDir(cacheDir)
_ = m2.Build(context.Background())
}
// TestIncrementalBlocklistApplied verifies that blocklist filtering applies to
// newly-parsed files on the incremental path.
func TestIncrementalBlocklistApplied(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
// Write a .repomap.yaml that blocks Test* symbols.
blocklistYAML := "method_blocklist:\n - \"Test*\"\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, ".repomap.yaml"), []byte(blocklistYAML), 0o644))
gitCommitAll(t, dir, "add blocklist")
cacheDir := t.TempDir()
buildWithCache(t, dir, cacheDir)
// Add a new file with a blocked symbol and a real symbol.
newSrc := `package main
func TestFoo() {}
func RealFn() int { return 1 }
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "newthing.go"), []byte(newSrc), 0o644))
gitCommitAll(t, dir, "add newthing.go")
m2 := buildWithCache(t, dir, cacheDir)
out := m2.String()
assert.NotContains(t, out, "TestFoo", "blocked symbol must not appear in incremental output")
assert.Contains(t, out, "RealFn", "non-blocked symbol must appear in incremental output")
}
// TestIncrementalCacheVersionBump verifies that a v5 cache file on disk is
// rejected by LoadCacheIncremental and Build falls through to a full rebuild.
func TestIncrementalCacheVersionBump(t *testing.T) {
t.Parallel()
dir := newGitRepo(t)
cacheDir := t.TempDir()
// Write a minimal v5 cache file manually.
m := New(dir, DefaultConfig())
m.SetCacheDir(cacheDir)
v5Cache := map[string]any{
"version": 5,
"root": dir,
"built_at": time.Now(),
"mtimes": map[string]time.Time{},
"output": "stale output",
"ranked": []any{},
}
data, err := json.Marshal(v5Cache)
require.NoError(t, err)
require.NoError(t, os.WriteFile(cachePath(cacheDir, dir), data, 0o644))
// LoadCacheIncremental must return (false, nil) for v5 cache.
ok, changed := m.LoadCacheIncremental(context.Background(), cacheDir)
assert.False(t, ok, "v5 cache must be rejected")
assert.Nil(t, changed, "no changed files must be returned for rejected cache")
// Full Build must still succeed.
require.NoError(t, m.Build(context.Background()))
assert.Contains(t, m.String(), "Hello", "full rebuild must produce correct output after v5 cache rejection")
}
// TestIncrementalEquivalence verifies that an incremental rebuild produces the
// same ranked output as a cold full build for an identical tree+config. Before
// the applyRankPasses fix, the incremental path skipped test demotion and
// reference bonuses, diverging from cold-build output.
func TestIncrementalEquivalence(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
// Add util.go with two exported functions.
utilSrc := `package main
func UtilA() string { return "a" }
func UtilB() int { return 1 }
`
require.NoError(t, os.WriteFile(filepath.Join(repo, "util.go"), []byte(utilSrc), 0o644))
// Add util_test.go with a trivial test referencing util.go.
testSrc := `package main
import "testing"
func TestUtil(t *testing.T) {
if UtilA() != "a" {
t.Fatal("unexpected UtilA")
}
}
`
require.NoError(t, os.WriteFile(filepath.Join(repo, "util_test.go"), []byte(testSrc), 0o644))
gitRun(t, repo, "add", "util.go", "util_test.go")
gitCommitAll(t, repo, "add util files")
cfg := DefaultConfig() // IncludeTests stays false — test demotion must apply
// Cold build with cache.
cacheDir := t.TempDir()
m1 := New(repo, cfg)
m1.SetCacheDir(cacheDir)
require.NoError(t, m1.Build(context.Background()))
// Mutate util.go: append a new exported function.
mutatedSrc := `package main
func UtilA() string { return "a" }
func UtilB() int { return 1 }
func UtilC() bool { return true }
`
require.NoError(t, os.WriteFile(filepath.Join(repo, "util.go"), []byte(mutatedSrc), 0o644))
gitRun(t, repo, "add", "util.go")
gitCommitAll(t, repo, "add UtilC to util.go")
// Incremental rebuild from cache.
m2 := New(repo, cfg)
m2.SetCacheDir(cacheDir)
require.NoError(t, m2.Build(context.Background()))
// Cold full rebuild as reference (no cache).
m3 := New(repo, cfg)
require.NoError(t, m3.Build(context.Background()))
// Compare m2 (incremental) vs m3 (cold reference).
r2 := m2.Ranked()
r3 := m3.Ranked()
require.Equal(t, len(r3), len(r2), "ranked lengths must match")
scores2 := make(map[string]int, len(r2))
detail2 := make(map[string]int, len(r2))
paths2 := make([]string, 0, len(r2))
for _, rf := range r2 {
scores2[rf.Path] = rf.Score
detail2[rf.Path] = rf.DetailLevel
paths2 = append(paths2, rf.Path)
}
scores3 := make(map[string]int, len(r3))
detail3 := make(map[string]int, len(r3))
paths3 := make([]string, 0, len(r3))
for _, rf := range r3 {
scores3[rf.Path] = rf.Score
detail3[rf.Path] = rf.DetailLevel
paths3 = append(paths3, rf.Path)
}
// Compare path sets.
slices.Sort(paths2)
slices.Sort(paths3)
require.Equal(t, paths3, paths2, "sorted path sets must match")
// Compare scores and detail levels per path.
for path, s3 := range scores3 {
s2, ok := scores2[path]
require.True(t, ok, "path %s missing in incremental output", path)
require.InDelta(t, float64(s3), float64(s2), 1e-9, "score mismatch for %s", path)
require.Equal(t, detail3[path], detail2[path], "detail level mismatch for %s", path)
}
}