-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
FileSystemInfo.unittest.js
412 lines (389 loc) · 12.3 KB
/
FileSystemInfo.unittest.js
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
"use strict";
const { createFsFromVolume, Volume } = require("memfs");
const util = require("util");
const FileSystemInfo = require("../lib/FileSystemInfo");
const { buffersSerializer } = require("../lib/util/serialization");
describe("FileSystemInfo", () => {
const files = [
"/path/file.txt",
"/path/nested/deep/file.txt",
"/path/nested/deep/ignored.txt",
"/path/context+files/file.txt",
"/path/context+files/sub/file.txt",
"/path/context+files/sub/ignored.txt",
"/path/node_modules/package/file.txt",
"/path/cache/package-1234/file.txt",
"/path/circular/circular/file2.txt",
"/path/nested/deep/symlink/file.txt",
"/path/context+files/sub/symlink/file.txt",
"/path/context/sub/symlink/file.txt",
"/path/missing.txt"
];
const directories = [
"/path/context+files",
"/path/context",
"/path/missing",
"/path/node_modules/package",
"/path/node_modules/missing",
"/path/cache/package-1234",
"/path/cache/package-missing"
];
const missing = [
"/path/package.json",
"/path/file2.txt",
"/path/context+files/file2.txt",
"/path/node_modules/package.txt",
"/path/node_modules/package/missing.txt",
"/path/cache/package-2345",
"/path/cache/package-1234/missing.txt",
"/path/ignored.txt"
];
const ignored = [
"/path/nested/deep/ignored.txt",
"/path/context+files/sub/ignored.txt",
"/path/context/sub/ignored.txt",
"/path/ignored.txt",
"/path/node_modules/package/ignored.txt",
"/path/cache/package-1234/ignored.txt"
];
const managedPaths = ["/path/node_modules"];
const immutablePaths = ["/path/cache"];
const createFs = () => {
const fs = createFsFromVolume(new Volume());
fs.mkdirpSync("/path/context+files/sub");
fs.mkdirpSync("/path/context/sub");
fs.mkdirpSync("/path/nested/deep");
fs.mkdirpSync("/path/node_modules/package");
fs.mkdirpSync("/path/cache/package-1234");
fs.mkdirpSync("/path/folder/context");
fs.mkdirpSync("/path/folder/context+files");
fs.mkdirpSync("/path/folder/nested");
fs.writeFileSync("/path/file.txt", "Hello World");
fs.writeFileSync("/path/file2.txt", "Hello World2");
fs.writeFileSync("/path/nested/deep/file.txt", "Hello World");
fs.writeFileSync("/path/nested/deep/ignored.txt", "Ignored");
fs.writeFileSync("/path/context+files/file.txt", "Hello World");
fs.writeFileSync("/path/context+files/file2.txt", "Hello World2");
fs.writeFileSync("/path/context+files/sub/file.txt", "Hello World");
fs.writeFileSync("/path/context+files/sub/file2.txt", "Hello World2");
fs.writeFileSync("/path/context+files/sub/file3.txt", "Hello World3");
fs.writeFileSync("/path/context+files/sub/ignored.txt", "Ignored");
fs.writeFileSync("/path/context/file.txt", "Hello World");
fs.writeFileSync("/path/context/file2.txt", "Hello World2");
fs.writeFileSync("/path/context/sub/file.txt", "Hello World");
fs.writeFileSync("/path/context/sub/file2.txt", "Hello World2");
fs.writeFileSync("/path/context/sub/file3.txt", "Hello World3");
fs.writeFileSync("/path/context/sub/ignored.txt", "Ignored");
fs.writeFileSync(
"/path/node_modules/package/package.json",
JSON.stringify({ name: "package", version: "1.0.0" })
);
fs.writeFileSync("/path/node_modules/package/file.txt", "Hello World");
fs.writeFileSync("/path/node_modules/package/ignored.txt", "Ignored");
fs.writeFileSync(
"/path/cache/package-1234/package.json",
JSON.stringify({ name: "package", version: "1.0.0" })
);
fs.writeFileSync("/path/cache/package-1234/file.txt", "Hello World");
fs.writeFileSync("/path/cache/package-1234/ignored.txt", "Ignored");
fs.symlinkSync("/path", "/path/circular", "dir");
fs.writeFileSync("/path/folder/context/file.txt", "Hello World");
fs.writeFileSync("/path/folder/context+files/file.txt", "Hello World");
fs.writeFileSync("/path/folder/nested/file.txt", "Hello World");
fs.symlinkSync("/path/folder/context", "/path/context/sub/symlink", "dir");
fs.symlinkSync(
"/path/folder/context+files",
"/path/context+files/sub/symlink",
"dir"
);
fs.symlinkSync("/path/folder/nested", "/path/nested/deep/symlink", "dir");
return fs;
};
const createFsInfo = fs => {
const logger = {
error: (...args) => {
throw new Error(util.format(...args));
}
};
const fsInfo = new FileSystemInfo(fs, {
logger,
managedPaths,
immutablePaths,
hashFunction: "sha256"
});
for (const method of ["warn", "info", "log", "debug"]) {
fsInfo.logs = [];
fsInfo[method] = [];
logger[method] = (...args) => {
const msg = util.format(...args);
fsInfo[method].push(msg);
fsInfo.logs.push(`[${method}] ${msg}`);
};
}
fsInfo.addFileTimestamps(new Map(ignored.map(i => [i, "ignore"])));
return fsInfo;
};
const createSnapshot = (fs, options, callback) => {
const fsInfo = createFsInfo(fs);
fsInfo.createSnapshot(
Date.now() + 10000,
files,
directories,
missing,
options,
(err, snapshot) => {
if (err) return callback(err);
snapshot.name = "initial snapshot";
// create another one to test the caching
fsInfo.createSnapshot(
Date.now() + 10000,
files,
directories,
missing,
options,
(err, snapshot2) => {
if (err) return callback(err);
snapshot2.name = "cached snapshot";
callback(null, snapshot, snapshot2);
}
);
}
);
};
const clone = object => {
const serialized = buffersSerializer.serialize(object, {});
return buffersSerializer.deserialize(serialized, {});
};
const expectSnapshotsState = (
fs,
snapshot,
snapshot2,
expected,
callback
) => {
expectSnapshotState(fs, snapshot, expected, err => {
if (err) return callback(err);
if (!snapshot2) return callback();
expectSnapshotState(fs, snapshot2, expected, callback);
});
};
const expectSnapshotState = (fs, snapshot, expected, callback) => {
const fsInfo = createFsInfo(fs);
const details = snapshot => `${fsInfo.logs.join("\n")}
${util.inspect(snapshot, false, Infinity, true)}`;
fsInfo.checkSnapshotValid(snapshot, (err, valid) => {
if (err) return callback(err);
if (valid !== expected) {
return callback(
new Error(`Expected snapshot to be ${
expected ? "valid" : "invalid"
} but it is ${valid ? "valid" : "invalid"}:
${details(snapshot)}`)
);
}
// Another try to check if direct caching works
fsInfo.checkSnapshotValid(snapshot, (err, valid) => {
if (err) return callback(err);
if (valid !== expected) {
return callback(
new Error(`Expected snapshot lead to the same result when directly cached:
${details(snapshot)}`)
);
}
// Another try to check if indirect caching works
fsInfo.checkSnapshotValid(clone(snapshot), (err, valid) => {
if (err) return callback(err);
if (valid !== expected) {
return callback(
new Error(`Expected snapshot lead to the same result when indirectly cached:
${details(snapshot)}`)
);
}
callback();
});
});
});
};
const updateFile = (fs, filename) => {
const oldContent = fs.readFileSync(filename, "utf-8");
if (filename.endsWith(".json")) {
const data = JSON.parse(oldContent);
fs.writeFileSync(
filename,
JSON.stringify({
...data,
version: data.version + ".1"
})
);
} else {
fs.writeFileSync(
filename,
oldContent + "!"
);
}
};
for (const [name, options] of [
["timestamp", { timestamp: true }],
["hash", { hash: true }],
["tsh", { timestamp: true, hash: true }]
]) {
describe(`${name} mode`, () => {
it("should always accept an empty snapshot", done => {
const fs = createFs();
const fsInfo = createFsInfo(fs);
fsInfo.createSnapshot(
Date.now() + 10000,
[],
[],
[],
options,
(err, snapshot) => {
if (err) return done(err);
const fs = createFs();
expectSnapshotState(fs, snapshot, true, done);
}
);
});
it("should accept a snapshot when fs is unchanged", done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
expectSnapshotsState(fs, snapshot, snapshot2, true, done);
});
});
const ignoredFileChanges = [
"/path/nested/deep/ignored.txt",
"/path/context+files/sub/ignored.txt"
];
for (const fileChange of [
"/path/file.txt",
"/path/file2.txt",
"/path/nested/deep/file.txt",
"/path/context+files/file.txt",
"/path/context+files/file2.txt",
"/path/context+files/sub/file.txt",
"/path/context+files/sub/file2.txt",
"/path/context+files/sub/file3.txt",
"/path/context/file.txt",
"/path/context/file2.txt",
"/path/context/sub/file.txt",
"/path/context/sub/file2.txt",
"/path/context/sub/file3.txt",
"/path/node_modules/package/package.json",
"/path/folder/context/file.txt",
"/path/folder/context+files/file.txt",
"/path/folder/nested/file.txt",
...(name !== "timestamp" ? ignoredFileChanges : []),
...(name === "hash" ? ["/path/context/sub/ignored.txt"] : [])
]) {
it(`should invalidate the snapshot when ${fileChange} is changed`, done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
updateFile(fs, fileChange);
expectSnapshotsState(fs, snapshot, snapshot2, false, done);
});
});
}
for (const fileChange of [
"/path/node_modules/package/file.txt",
"/path/node_modules/package/ignored.txt",
"/path/cache/package-1234/package.json",
"/path/cache/package-1234/file.txt",
"/path/cache/package-1234/ignored.txt",
...(name === "timestamp" ? ignoredFileChanges : []),
...(name !== "hash" ? ["/path/context/sub/ignored.txt"] : [])
]) {
it(`should not invalidate the snapshot when ${fileChange} is changed`, done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
updateFile(fs, fileChange);
expectSnapshotsState(fs, snapshot, snapshot2, true, done);
});
});
}
for (const newFile of [
"/path/package.json",
"/path/file2.txt",
"/path/context+files/file2.txt",
"/path/node_modules/package.txt"
]) {
it(`should invalidate the snapshot when ${newFile} is created`, done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
fs.writeFileSync(newFile, "New file");
expectSnapshotsState(fs, snapshot, snapshot2, false, done);
});
});
}
for (const newFile of [
"/path/node_modules/package/missing.txt",
"/path/cache/package-1234/missing.txt",
"/path/cache/package-2345",
"/path/ignored.txt"
]) {
it(`should not invalidate the snapshot when ${newFile} is created`, done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
fs.writeFileSync(newFile, "New file");
expectSnapshotsState(fs, snapshot, snapshot2, true, done);
});
});
}
if (name !== "timestamp") {
it("should not invalidate snapshot when only timestamps have changed", done => {
const fs = createFs();
createSnapshot(fs, options, (err, snapshot, snapshot2) => {
if (err) return done(err);
const fs = createFs();
expectSnapshotsState(fs, snapshot, snapshot2, true, done);
});
});
}
});
}
describe("stable iterables identity", () => {
const options = { timestamp: true };
/**
* @param {function((WebpackError | null)=, (Snapshot | null)=): void} callback callback function
*/
function getSnapshot(callback) {
const fs = createFs();
const fsInfo = createFsInfo(fs);
fsInfo.createSnapshot(
Date.now() + 10000,
files,
directories,
missing,
options,
callback
);
}
it("should return same iterable for getFileIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getFileIterable()).toEqual(snapshot.getFileIterable());
done();
});
});
it("should return same iterable for getContextIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getContextIterable()).toEqual(
snapshot.getContextIterable()
);
done();
});
});
it("should return same iterable for getMissingIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getFileIterable()).toEqual(snapshot.getFileIterable());
done();
});
});
});
});