-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
142 lines (116 loc) · 3.93 KB
/
Copy pathcache_test.go
File metadata and controls
142 lines (116 loc) · 3.93 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
package repomap
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCacheVersion(t *testing.T) {
assert.Equal(t, 15, cacheVersion)
}
func TestCacheExactHitDoesNotRewriteCache(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, repo, cacheDir)
cacheFile := cachePath(cacheDir, repo)
fixedTime := time.Unix(1, 0)
require.NoError(t, os.Chtimes(cacheFile, fixedTime, fixedTime))
before, err := os.Stat(cacheFile)
require.NoError(t, err)
beforeData, err := os.ReadFile(cacheFile)
require.NoError(t, err)
buildWithCache(t, repo, cacheDir)
after, err := os.Stat(cacheFile)
require.NoError(t, err)
afterData, err := os.ReadFile(cacheFile)
require.NoError(t, err)
assert.Equal(t, before.ModTime(), after.ModTime())
assert.Equal(t, before.Size(), after.Size())
assert.Equal(t, beforeData, afterData)
}
func TestCacheDirtyDigestIgnoresStagingAndSupportsRevert(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, repo, cacheDir)
path := filepath.Join(repo, "feature.py")
first := []byte("def feature():\n return 'first'\n")
require.NoError(t, os.WriteFile(path, first, 0o644))
buildWithCache(t, repo, cacheDir) // save a dirty cache for first.
cacheFile := cachePath(cacheDir, repo)
before, err := os.Stat(cacheFile)
require.NoError(t, err)
gitRun(t, repo, "add", "feature.py")
buildWithCache(t, repo, cacheDir) // index-only change: exact digest hit.
after, err := os.Stat(cacheFile)
require.NoError(t, err)
assert.Equal(t, before.ModTime(), after.ModTime())
require.NoError(t, os.WriteFile(path, []byte("def feature():\n return 'second'\n"), 0o644))
mChanged := New(repo, DefaultConfig())
_, ok := mChanged.cacheLoadPlan(context.Background(), cacheDir)
assert.False(t, ok, "a changed dirty baseline must not be incrementally reused")
require.NoError(t, os.WriteFile(path, first, 0o644))
mReverted := New(repo, DefaultConfig())
plan, ok := mReverted.cacheLoadPlan(context.Background(), cacheDir)
require.True(t, ok)
assert.True(t, plan.exactHit, "reverting to saved dirty contents restores the exact hit")
}
func TestCacheV14IsRejected(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
buildWithCache(t, repo, cacheDir)
cacheFile := cachePath(cacheDir, repo)
data, err := os.ReadFile(cacheFile)
require.NoError(t, err)
var entry diskCache
require.NoError(t, json.Unmarshal(data, &entry))
entry.Version = 14
data, err = json.Marshal(entry)
require.NoError(t, err)
require.NoError(t, os.WriteFile(cacheFile, data, 0o644))
m := New(repo, DefaultConfig())
assert.False(t, m.LoadCache(cacheDir))
ok, _ := m.LoadCacheIncremental(context.Background(), cacheDir)
assert.False(t, ok)
}
func TestCacheConfigMismatchMiss(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
ctx := context.Background()
cfgA := DefaultConfig()
cfgA.MaxTokens = 512
m1 := New(repo, cfgA)
m1.SetCacheDir(cacheDir)
require.NoError(t, m1.Build(ctx))
// Different config → both load paths must miss.
cfgB := DefaultConfig()
cfgB.MaxTokens = 4096
m2 := New(repo, cfgB)
require.False(t, m2.LoadCache(cacheDir))
ok, _ := m2.LoadCacheIncremental(ctx, cacheDir)
require.False(t, ok)
// Same config as original → must hit.
m3 := New(repo, cfgA)
require.True(t, m3.LoadCache(cacheDir))
}
func TestCacheBlocklistChangeMiss(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
cfg := DefaultConfig()
m1 := New(repo, cfg)
m1.SetCacheDir(cacheDir)
require.NoError(t, m1.Build(context.Background()))
blYAML := "method_blocklist:\n - \"Zzz*\"\n"
require.NoError(t, os.WriteFile(filepath.Join(repo, ".repomap.yaml"), []byte(blYAML), 0o644))
// New reads the .repomap.yaml → different blocklist → different config hash.
m2 := New(repo, cfg)
require.False(t, m2.LoadCache(cacheDir))
}