-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache_test.go
More file actions
276 lines (254 loc) · 8.37 KB
/
Copy pathcache_test.go
File metadata and controls
276 lines (254 loc) · 8.37 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
package clone
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
func TestCacheDirIsStableAndSeparatedByURL(t *testing.T) {
cache := Cache{Root: "/data/cache"}
first := cache.Dir("https://example.com/a")
if first != cache.Dir("https://example.com/a") {
t.Fatal("same URL produced different cache directories")
}
if first == cache.Dir("https://example.com/b") {
t.Fatal("different URLs produced the same cache directory")
}
prefix := filepath.Clean(cache.Root) + string(filepath.Separator)
if !strings.HasPrefix(first, prefix) {
t.Errorf("cache path %q is not under %q", first, cache.Root)
}
}
func TestCachePrepareUpdatesAndReplacesDestination(t *testing.T) {
origin := newOriginFixture(t)
cache := Cache{Root: t.TempDir()}
dst := filepath.Join(t.TempDir(), "workspace", "src")
commit, err := cache.Prepare(context.Background(), origin.url, "", dst)
if err != nil {
t.Fatalf("Prepare: %v", err)
}
if commit != origin.mainSHA {
t.Errorf("commit = %q, want %q", commit, origin.mainSHA)
}
content, err := os.ReadFile(filepath.Join(dst, "file.txt"))
if err != nil {
t.Fatal(err)
}
if string(content) != "main\n" {
t.Errorf("content = %q", content)
}
if err := os.WriteFile(filepath.Join(dst, "generated"), []byte("remove me"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(origin.dir, "file.txt"), []byte("updated\n"), 0o644); err != nil {
t.Fatal(err)
}
runGitTest(t, origin.dir, "commit", "--quiet", "-am", "updated")
wantCommit := runGitTest(t, origin.dir, "rev-parse", "HEAD")
commit, err = cache.Prepare(context.Background(), origin.url, "", dst)
if err != nil {
t.Fatalf("second Prepare: %v", err)
}
if commit != wantCommit {
t.Errorf("second commit = %q, want %q", commit, wantCommit)
}
if _, err := os.Stat(filepath.Join(dst, "generated")); !os.IsNotExist(err) {
t.Errorf("stale destination file remains: %v", err)
}
content, err = os.ReadFile(filepath.Join(dst, "file.txt"))
if err != nil {
t.Fatal(err)
}
if string(content) != "updated\n" {
t.Errorf("updated content = %q", content)
}
if cache.DiskBytes(origin.url) == 0 {
t.Error("DiskBytes = 0 after Prepare")
}
}
func TestCachePrepareIncludesSubmodules(t *testing.T) {
fixture := newSubmoduleOriginFixture(t)
cache := Cache{Root: t.TempDir(), RecurseSubmodules: true}
dst := filepath.Join(t.TempDir(), "workspace", "src")
if _, err := cache.Prepare(context.Background(), fixture.origin.url, "", dst); err != nil {
t.Fatalf("Prepare: %v", err)
}
content, err := os.ReadFile(filepath.Join(dst, "vendor", "library", "vendor.c"))
if err != nil {
t.Fatal(err)
}
if string(content) != "first\n" {
t.Errorf("submodule content = %q, want first revision", content)
}
}
func TestCachePrepareRejectsDestinationOverlappingCache(t *testing.T) {
rootParent := t.TempDir()
cache := Cache{Root: filepath.Join(rootParent, "cache")}
url := "https://example.invalid/repo"
for _, dst := range []string{
rootParent,
cache.Root,
cache.Dir(url),
filepath.Join(cache.Dir(url), "workspace"),
cache.Dir("https://example.invalid/other"),
} {
_, err := cache.Prepare(context.Background(), url, "", dst)
if err == nil || !strings.Contains(err.Error(), "overlaps") {
t.Errorf("Prepare destination %q error = %v, want overlap error", dst, err)
}
}
}
func TestCacheEnsureCommitUnshallowsHistory(t *testing.T) {
origin := newOriginFixture(t)
cache := Cache{Root: t.TempDir()}
if _, err := cache.Prepare(context.Background(), origin.url, "", filepath.Join(t.TempDir(), "src")); err != nil {
t.Fatalf("Prepare: %v", err)
}
cacheSrc := filepath.Join(cache.Dir(origin.url), "src")
if got := runGitTest(t, cacheSrc, "rev-parse", "--is-shallow-repository"); got != "true" {
t.Fatalf("cache shallow = %q, want true", got)
}
cmdErr := func() error {
_, err := Run(context.Background(), cacheSrc, nil, "cat-file", "-e", origin.firstSHA+"^{commit}")
return err
}()
if cmdErr == nil {
t.Fatal("historical commit is already reachable in shallow cache")
}
if err := cache.EnsureCommit(context.Background(), origin.url, origin.firstSHA); err != nil {
t.Fatalf("EnsureCommit: %v", err)
}
if got := runGitTest(t, cacheSrc, "rev-parse", "--is-shallow-repository"); got != "false" {
t.Errorf("cache shallow = %q, want false", got)
}
runGitTest(t, cacheSrc, "cat-file", "-e", origin.firstSHA+"^{commit}")
}
func TestCacheEnsureCommitNoCacheIsNoOp(t *testing.T) {
cache := Cache{Root: t.TempDir()}
if err := cache.EnsureCommit(context.Background(), "https://example.com/repo", "deadbeef"); err != nil {
t.Fatalf("EnsureCommit: %v", err)
}
}
func TestCacheEnsureCommitRejectsInvalidCommit(t *testing.T) {
cache := Cache{Root: t.TempDir()}
err := cache.EnsureCommit(context.Background(), "https://example.com/repo", "--help")
if err == nil || !strings.Contains(err.Error(), "invalid commit") {
t.Fatalf("error = %v", err)
}
}
func TestCacheEnsureCommitRetriesUnshallowFetch(t *testing.T) {
cache := Cache{Root: t.TempDir()}
url := "https://example.invalid/repo"
cacheSrc := filepath.Join(cache.Dir(url), "src")
if err := os.MkdirAll(filepath.Join(cacheSrc, ".git"), 0o755); err != nil {
t.Fatal(err)
}
fetchCalls := 0
cache.Retry = Retry{
Run: func(_ context.Context, _ string, _ []string, args ...string) (string, error) {
switch subcommand(args) {
case "cat-file":
return "", errors.New("missing object")
case "rev-parse":
return "true\n", nil
case "fetch":
fetchCalls++
if fetchCalls == 1 {
return "fatal: Connection reset by peer", errGitExit
}
return "", nil
default:
return "", errors.New("unexpected Git command")
}
},
Sleep: func(context.Context, time.Duration) error { return nil },
}
if err := cache.EnsureCommit(context.Background(), url, "deadbeef"); err != nil {
t.Fatalf("EnsureCommit: %v", err)
}
if fetchCalls != 2 {
t.Errorf("fetch calls = %d, want 2", fetchCalls)
}
}
func TestCacheEnsureCommitConfiguresLongPathsForFetch(t *testing.T) {
cache := Cache{Root: t.TempDir()}
url := "https://example.invalid/repo"
cacheSrc := filepath.Join(cache.Dir(url), "src")
if err := os.MkdirAll(filepath.Join(cacheSrc, ".git"), 0o755); err != nil {
t.Fatal(err)
}
var fetchArgs []string
cache.Retry = Retry{
Run: func(_ context.Context, _ string, _ []string, args ...string) (string, error) {
switch subcommand(args) {
case "cat-file":
return "", errors.New("missing object")
case "rev-parse":
return "true\n", nil
case "fetch":
fetchArgs = append([]string(nil), args...)
return "", nil
default:
return "", errors.New("unexpected Git command")
}
},
}
if err := cache.EnsureCommit(context.Background(), url, "deadbeef"); err != nil {
t.Fatalf("EnsureCommit: %v", err)
}
want := []string{"-c", "core.longpaths=true"}
if len(fetchArgs) < len(want) || !slices.Equal(fetchArgs[:len(want)], want) {
t.Errorf("fetch args = %v, want prefix %v", fetchArgs, want)
}
}
func TestCacheEnsureCommitSkipsUnreachableCommitInFullClone(t *testing.T) {
cache := Cache{Root: t.TempDir()}
url := "https://example.invalid/repo"
cacheSrc := filepath.Join(cache.Dir(url), "src")
if err := os.MkdirAll(filepath.Join(cacheSrc, ".git"), 0o755); err != nil {
t.Fatal(err)
}
cache.Retry = Retry{
Run: func(_ context.Context, _ string, _ []string, args ...string) (string, error) {
switch args[0] {
case "cat-file":
return "", errors.New("missing object")
case "rev-parse":
return "false\n", nil
case "fetch":
t.Fatal("full clone must not fetch")
}
return "", nil
},
}
if err := cache.EnsureCommit(context.Background(), url, "deadbeef"); err != nil {
t.Fatalf("EnsureCommit: %v", err)
}
}
func TestCacheDiskBytesCountsRegularFiles(t *testing.T) {
cache := Cache{Root: t.TempDir()}
url := "https://example.com/repo"
dir := cache.Dir(url)
if err := os.MkdirAll(filepath.Join(dir, "nested"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "one"), make([]byte, 10), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "nested", "two"), make([]byte, 20), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink("one", filepath.Join(dir, "link")); err != nil {
t.Fatal(err)
}
if got := cache.DiskBytes(url); got != 30 {
t.Errorf("DiskBytes = %d, want 30", got)
}
if got := cache.DiskBytes("https://example.com/missing"); got != 0 {
t.Errorf("missing DiskBytes = %d, want 0", got)
}
}