-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalls_cache_key_test.go
More file actions
62 lines (48 loc) · 2.38 KB
/
Copy pathcalls_cache_key_test.go
File metadata and controls
62 lines (48 loc) · 2.38 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
package repomap
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestCallsCacheKey_ContentHashInvalidation verifies that the key changes when
// a tracked source file's CONTENT changes, even though its path and flags are
// identical. (The old mtime-based key could collide when mtime was preserved.)
func TestCallsCacheKey_ContentHashInvalidation(t *testing.T) {
t.Parallel()
root := t.TempDir()
rel := "foo.go"
abs := filepath.Join(root, rel)
require.NoError(t, os.WriteFile(abs, []byte("package foo\n\nfunc A() {}\n"), 0o644))
sym := Symbol{Name: "A", Kind: "function", Exported: true, Line: 3}
ranked := []RankedFile{makeTestRankedFile(rel, 2, []Symbol{sym})}
cfg := CallsConfig{Threshold: 2, Limit: 10}
k1 := CallsCacheKey(root, ranked, cfg)
// Rewrite with different content but force the SAME mtime, so an
// mtime-based key would (wrongly) collide.
info, err := os.Stat(abs)
require.NoError(t, err)
require.NoError(t, os.WriteFile(abs, []byte("package foo\n\nfunc A() { _ = 1 }\n"), 0o644))
require.NoError(t, os.Chtimes(abs, info.ModTime(), info.ModTime()))
k2 := CallsCacheKey(root, ranked, cfg)
assert.NotEqual(t, k1, k2, "content change must change the cache key even with identical mtime")
}
// TestCallsCacheKey_FlagInvalidation verifies that the key changes when any of
// Threshold, Limit, or IncludeTests changes (same path + content).
func TestCallsCacheKey_FlagInvalidation(t *testing.T) {
t.Parallel()
root := t.TempDir()
rel := "bar.go"
require.NoError(t, os.WriteFile(filepath.Join(root, rel), []byte("package bar\n\nfunc B() {}\n"), 0o644))
sym := Symbol{Name: "B", Kind: "function", Exported: true, Line: 3}
ranked := []RankedFile{makeTestRankedFile(rel, 2, []Symbol{sym})}
base := CallsConfig{Threshold: 2, Limit: 10, IncludeTests: false}
kBase := CallsCacheKey(root, ranked, base)
kThreshold := CallsCacheKey(root, ranked, CallsConfig{Threshold: 3, Limit: 10, IncludeTests: false})
kLimit := CallsCacheKey(root, ranked, CallsConfig{Threshold: 2, Limit: 5, IncludeTests: false})
kTests := CallsCacheKey(root, ranked, CallsConfig{Threshold: 2, Limit: 10, IncludeTests: true})
assert.NotEqual(t, kBase, kThreshold, "Threshold change must change key")
assert.NotEqual(t, kBase, kLimit, "Limit change must change key")
assert.NotEqual(t, kBase, kTests, "IncludeTests change must change key")
}