Repository navigation
Expand file tree
/
Copy pathcache-snapshot.test.ts
More file actions
347 lines (319 loc) · 14.3 KB
/
Copy pathcache-snapshot.test.ts
File metadata and controls
347 lines (319 loc) · 14.3 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
import { createHash } from "node:crypto";
import {
mkdtempSync,
mkdirSync,
writeFileSync,
readFileSync,
rmSync,
existsSync,
lstatSync,
realpathSync,
readlinkSync,
renameSync,
statSync,
symlinkSync,
unlinkSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import { restoreCacheSnapshot } from "./cache-snapshot.js";
import { isWindows } from "./platform.js";
vi.mock("node:fs", async (importOriginal) => {
const fs = await importOriginal<typeof import("node:fs")>();
return { ...fs, statSync: vi.fn(fs.statSync) };
});
const directories: string[] = [];
afterEach(() => {
vi.mocked(statSync).mockReset();
for (const dir of directories.splice(0)) rmSync(dir, { recursive: true, force: true });
});
function fixture() {
const root = realpathSync(mkdtempSync(path.join(tmpdir(), "setup-vp-cache-")));
directories.push(root);
const store = path.join(root, "store");
const cache = path.join(root, "snapshot");
const lockFile = path.join(root, "package-lock.json");
mkdirSync(store);
writeFileSync(lockFile, "first lock");
return {
root,
store,
cache,
lockFile,
metadata: { ready: true, cachePath: store, lockFile, lockType: "npm" },
};
}
describe("GitLab cache snapshots", () => {
it("restores exact hits into the dynamically resolved package-manager store", () => {
const { store, cache, metadata } = fixture();
writeFileSync(path.join(store, "package"), "cached");
const first = restoreCacheSnapshot(metadata, cache);
expect(first.hit).toBe(false);
expect(existsSync(cache)).toBe(false); // Restore-only does not create a cache.
first.save();
rmSync(store, { recursive: true });
expect(restoreCacheSnapshot(metadata, cache).hit).toBe(true);
expect(readFileSync(path.join(store, "package"), "utf8")).toBe("cached");
});
it("restores compatible fallback data but reports a miss for changed lock files", () => {
const { store, cache, lockFile, metadata } = fixture();
writeFileSync(path.join(store, "package"), "cached");
restoreCacheSnapshot(metadata, cache).save();
writeFileSync(lockFile, "second lock");
rmSync(store, { recursive: true });
expect(restoreCacheSnapshot(metadata, cache).hit).toBe(false);
expect(readFileSync(path.join(store, "package"), "utf8")).toBe("cached");
expect(restoreCacheSnapshot({ ...metadata, lockType: "yarn" }, cache).hit).toBe(false);
});
it("post-save includes packages added by later job scripts without restoring stale files", () => {
const { store, cache, metadata } = fixture();
writeFileSync(path.join(store, "package"), "before");
restoreCacheSnapshot(metadata, cache).save();
writeFileSync(path.join(store, "package"), "after");
restoreCacheSnapshot(metadata, cache, undefined, false).save();
rmSync(store, { recursive: true });
restoreCacheSnapshot(metadata, cache);
expect(readFileSync(path.join(store, "package"), "utf8")).toBe("after");
});
it.each(["absolute", "relative"])(
"saves and restores Bun-style %s directory symlinks repeatedly",
(targetKind) => {
const { store, cache, lockFile, metadata } = fixture();
metadata.lockType = "bun";
const packageDir = path.join(store, "sample@1.0.0@@@1");
const index = path.join(store, "sample");
const link = path.join(index, "1.0.0@@@1");
const cached = path.join(cache, process.platform, process.arch, "bun");
const cachedLink = path.join(cached, "packages", "sample", "1.0.0@@@1");
const warn = vi.fn();
mkdirSync(packageDir);
mkdirSync(index);
writeFileSync(path.join(packageDir, "package.json"), "before");
symlinkSync(
targetKind === "absolute" ? packageDir : path.relative(index, packageDir),
link,
isWindows() ? "junction" : "dir",
);
restoreCacheSnapshot(metadata, cache, warn).save();
writeFileSync(path.join(packageDir, "package.json"), "after");
writeFileSync(lockFile, "second lock");
restoreCacheSnapshot(metadata, cache, warn, false).save();
expect(warn).not.toHaveBeenCalled();
expect(readFileSync(path.join(cached, "lock-hash"), "utf8")).toBe(
createHash("sha256").update("second lock").digest("hex"),
);
expect(
readFileSync(path.join(cached, "packages", "sample@1.0.0@@@1", "package.json"), "utf8"),
).toBe("after");
expect(lstatSync(cachedLink).isSymbolicLink()).toBe(true);
expect(realpathSync(cachedLink)).toBe(
realpathSync(path.join(cached, "packages", "sample@1.0.0@@@1")),
);
// A warm store must retain its current entries, including its symlinks.
writeFileSync(path.join(packageDir, "package.json"), "local");
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(readFileSync(path.join(packageDir, "package.json"), "utf8")).toBe("local");
expect(realpathSync(link)).toBe(realpathSync(packageDir));
expect(warn).not.toHaveBeenCalled();
},
);
it.each(["absolute", "relative"])(
"relocates %s links when snapshot and store roots both change",
(targetKind) => {
const { root, store, cache, metadata } = fixture();
const packageDir = path.join(store, "package");
const index = path.join(store, "index");
mkdirSync(packageDir);
mkdirSync(index);
writeFileSync(path.join(packageDir, "value"), "cached package");
symlinkSync(
targetKind === "absolute" ? packageDir : "../package",
path.join(index, "link"),
isWindows() ? "junction" : "dir",
);
const warn = vi.fn();
restoreCacheSnapshot(metadata, cache, warn).save();
const movedCache = path.join(root, "another-runner-snapshot");
const movedStore = path.join(root, "another-runner-store");
renameSync(cache, movedCache);
rmSync(store, { recursive: true });
expect(
restoreCacheSnapshot({ ...metadata, cachePath: movedStore }, movedCache, warn).hit,
).toBe(true);
const restoredLink = path.join(movedStore, "index/link");
expect(readFileSync(path.join(restoredLink, "value"), "utf8")).toBe("cached package");
expect(readlinkSync(restoredLink)).toBe(path.join("..", "package"));
expect(warn).not.toHaveBeenCalled();
},
);
it("hashes the current lockfile when saving the setup-phase snapshot", () => {
const { store, cache, lockFile, metadata } = fixture();
writeFileSync(path.join(store, "package"), "cached");
const snapshot = restoreCacheSnapshot(metadata, cache);
writeFileSync(lockFile, "changed during install");
snapshot.save();
expect(restoreCacheSnapshot(metadata, cache).hit).toBe(true);
});
it("restores links whose target is readable but following the cached link fails", async () => {
const { root, store, cache, metadata } = fixture();
const target = path.join(root, "project");
const link = path.join(store, "project-link");
const cachedLink = path.join(
cache,
process.platform,
process.arch,
"npm/packages/project-link",
);
mkdirSync(target);
writeFileSync(path.join(target, "value"), "project data");
writeFileSync(path.join(store, "package"), "cached package");
symlinkSync(target, link, isWindows() ? "junction" : "dir");
const warn = vi.fn();
restoreCacheSnapshot(metadata, cache, warn).save();
rmSync(store, { recursive: true });
const fs = await vi.importActual<typeof import("node:fs")>("node:fs");
vi.mocked(statSync).mockImplementation((file, options) => {
if (file === cachedLink) {
throw Object.assign(new Error("operation not permitted, stat cached link"), {
code: "EPERM",
});
}
return fs.statSync(file, options);
});
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(readFileSync(path.join(link, "value"), "utf8")).toBe("project data");
expect(readFileSync(path.join(store, "package"), "utf8")).toBe("cached package");
expect(warn).not.toHaveBeenCalled();
});
it.skipIf(!isWindows())(
"repairs a directory link extracted as a file link before its target exists",
() => {
const { root, store, cache, metadata } = fixture();
const target = path.join(root, "project-from-artifacts");
const cachedLink = path.join(
cache,
process.platform,
process.arch,
"npm/packages/project-link",
);
const warn = vi.fn();
writeFileSync(path.join(store, "package"), "cached package");
restoreCacheSnapshot(metadata, cache, warn).save();
rmSync(store, { recursive: true });
// Cache extraction precedes artifacts. A missing directory target can
// cause an extractor to create a file symlink, which Windows cannot follow.
symlinkSync(target, cachedLink, "file");
mkdirSync(target);
writeFileSync(path.join(target, "value"), "project data");
expect(() => statSync(cachedLink)).toThrow(expect.objectContaining({ code: "EPERM" }));
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(readFileSync(path.join(store, "project-link/value"), "utf8")).toBe("project data");
expect(readFileSync(path.join(store, "package"), "utf8")).toBe("cached package");
expect(warn).not.toHaveBeenCalled();
},
);
it.each(["file", "directory"])("replaces a cached %s with a symlink on save", (entryKind) => {
const { store, cache, lockFile, metadata } = fixture();
const entry = path.join(store, "entry");
const target = path.join(store, "target");
const cached = path.join(cache, process.platform, process.arch, "npm");
const warn = vi.fn();
mkdirSync(target);
writeFileSync(path.join(target, "value"), "new value");
if (entryKind === "directory") {
mkdirSync(entry);
writeFileSync(path.join(entry, "old"), "old value");
} else {
writeFileSync(entry, "old value");
}
restoreCacheSnapshot(metadata, cache, warn).save();
rmSync(entry, { recursive: true });
symlinkSync(target, entry, isWindows() ? "junction" : "dir");
writeFileSync(lockFile, "second lock");
restoreCacheSnapshot(metadata, cache, warn, false).save();
expect(warn).not.toHaveBeenCalled();
const cachedEntry = path.join(cached, "packages", "entry");
expect(lstatSync(cachedEntry).isSymbolicLink()).toBe(true);
expect(readFileSync(path.join(cachedEntry, "value"), "utf8")).toBe("new value");
expect(readFileSync(path.join(cached, "lock-hash"), "utf8")).toBe(
createHash("sha256").update("second lock").digest("hex"),
);
rmSync(store, { recursive: true });
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(lstatSync(entry).isSymbolicLink()).toBe(true);
expect(readFileSync(path.join(entry, "value"), "utf8")).toBe("new value");
expect(warn).not.toHaveBeenCalled();
});
it.each(["file", "directory"])(
"replaces a cached symlink with a %s without modifying its external target",
(entryKind) => {
const { root, store, cache, lockFile, metadata } = fixture();
const entry = path.join(store, "entry");
const target = path.join(root, "external-target");
const cached = path.join(cache, process.platform, process.arch, "npm");
const warn = vi.fn();
if (entryKind === "directory") {
mkdirSync(target);
writeFileSync(path.join(target, "value"), "external value");
symlinkSync(target, entry, isWindows() ? "junction" : "dir");
} else {
writeFileSync(target, "external value");
symlinkSync(target, entry, "file");
}
restoreCacheSnapshot(metadata, cache, warn).save();
unlinkSync(entry);
if (entryKind === "directory") mkdirSync(entry);
const valuePath = entryKind === "file" ? entry : path.join(entry, "value");
const externalValuePath = entryKind === "file" ? target : path.join(target, "value");
writeFileSync(valuePath, "new value");
writeFileSync(lockFile, "second lock");
restoreCacheSnapshot(metadata, cache, warn, false).save();
expect(warn).not.toHaveBeenCalled();
expect(readFileSync(externalValuePath, "utf8")).toBe("external value");
const cachedEntry = path.join(cached, "packages", "entry");
expect(lstatSync(cachedEntry).isSymbolicLink()).toBe(false);
expect(readFileSync(path.join(cached, "lock-hash"), "utf8")).toBe(
createHash("sha256").update("second lock").digest("hex"),
);
rmSync(store, { recursive: true });
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(readFileSync(valuePath, "utf8")).toBe("new value");
expect(readFileSync(externalValuePath, "utf8")).toBe("external value");
expect(warn).not.toHaveBeenCalled();
},
);
it("replaces changed symlinks on save without dereferencing their targets", () => {
const { root, store, cache, metadata } = fixture();
const link = path.join(store, "link");
const oldTarget = path.join(root, "old-target");
const newTarget = path.join(root, "new-target");
const cachedLink = path.join(cache, process.platform, process.arch, "npm", "packages", "link");
const warn = vi.fn();
const linkType = isWindows() ? "junction" : "dir";
mkdirSync(oldTarget);
mkdirSync(newTarget);
symlinkSync(oldTarget, link, linkType);
restoreCacheSnapshot(metadata, cache, warn).save();
unlinkSync(link);
rmSync(oldTarget, { recursive: true });
symlinkSync(newTarget, link, linkType);
restoreCacheSnapshot(metadata, cache, warn, false).save();
expect(warn).not.toHaveBeenCalled();
expect(lstatSync(cachedLink).isSymbolicLink()).toBe(true);
expect(realpathSync(cachedLink)).toBe(realpathSync(newTarget));
// Restore must not replace an existing link, even if that link is dangling.
rmSync(newTarget, { recursive: true });
expect(restoreCacheSnapshot(metadata, cache, warn).hit).toBe(true);
expect(lstatSync(link).isSymbolicLink()).toBe(true);
expect(warn).not.toHaveBeenCalled();
});
it("skips missing metadata and overlapping paths safely", () => {
const { root, cache, metadata } = fixture();
const warn = vi.fn();
expect(restoreCacheSnapshot({ ready: false }, cache).hit).toBe(false);
restoreCacheSnapshot({ ...metadata, cachePath: root }, cache, warn).save();
expect(warn).toHaveBeenCalledWith(expect.stringContaining("overlaps"));
expect(existsSync(cache)).toBe(false);
});
});