forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnapshot.js
314 lines (268 loc) · 8.11 KB
/
snapshot.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
'use strict';
const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypeSlice,
ArrayPrototypeSort,
JSONStringify,
ObjectKeys,
SafeMap,
String,
StringPrototypeReplaceAll,
} = primordials;
const {
codes: {
ERR_INVALID_STATE,
},
} = require('internal/errors');
const { kEmptyObject } = require('internal/util');
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
debug = fn;
});
const {
validateArray,
validateFunction,
validateObject,
validateString,
} = require('internal/validators');
const { strictEqual } = require('assert');
const { mkdirSync, readFileSync, writeFileSync } = require('fs');
const { dirname } = require('path');
const { createContext, runInContext } = require('vm');
const kMissingSnapshotTip = 'Missing snapshots can be generated by rerunning ' +
'the command with the --test-update-snapshots flag.';
const defaultSerializers = [
(value) => { return JSONStringify(value, null, 2); },
];
function defaultResolveSnapshotPath(testPath) {
if (typeof testPath !== 'string') {
return testPath;
}
return `${testPath}.snapshot`;
}
let resolveSnapshotPathFn = defaultResolveSnapshotPath;
let serializerFns = defaultSerializers;
function setResolveSnapshotPath(fn) {
validateFunction(fn, 'fn');
resolveSnapshotPathFn = fn;
}
function setDefaultSnapshotSerializers(serializers) {
validateFunctionArray(serializers, 'serializers');
serializerFns = ArrayPrototypeSlice(serializers);
}
class SnapshotFile {
constructor(snapshotFile) {
this.snapshotFile = snapshotFile;
this.snapshots = { __proto__: null };
this.nameCounts = new SafeMap();
this.loaded = false;
}
getSnapshot(id) {
if (!(id in this.snapshots)) {
const err = new ERR_INVALID_STATE(`Snapshot '${id}' not found in ` +
`'${this.snapshotFile}.' ${kMissingSnapshotTip}`);
err.snapshot = id;
err.filename = this.snapshotFile;
throw err;
}
return this.snapshots[id];
}
setSnapshot(id, value) {
this.snapshots[escapeSnapshotKey(id)] = value;
}
nextId(name) {
const count = this.nameCounts.get(name) ?? 1;
this.nameCounts.set(name, count + 1);
return `${name} ${count}`;
}
readFile() {
if (this.loaded) {
debug('skipping read of snapshot file');
return;
}
try {
const source = readFileSync(this.snapshotFile, 'utf8');
const context = { __proto__: null, exports: { __proto__: null } };
createContext(context);
runInContext(source, context);
if (context.exports === null || typeof context.exports !== 'object') {
throw new ERR_INVALID_STATE(
`Malformed snapshot file '${this.snapshotFile}'.`,
);
}
for (const key in context.exports) {
this.snapshots[key] = templateEscape(context.exports[key]);
}
this.loaded = true;
} catch (err) {
throwReadError(err, this.snapshotFile);
}
}
writeFile() {
try {
const keys = ArrayPrototypeSort(ObjectKeys(this.snapshots));
const snapshotStrings = ArrayPrototypeMap(keys, (key) => {
return `exports[\`${key}\`] = \`${this.snapshots[key]}\`;\n`;
});
const output = ArrayPrototypeJoin(snapshotStrings, '\n');
mkdirSync(dirname(this.snapshotFile), { __proto__: null, recursive: true });
writeFileSync(this.snapshotFile, output, 'utf8');
} catch (err) {
throwWriteError(err, this.snapshotFile);
}
}
}
class SnapshotManager {
constructor(updateSnapshots) {
// A manager instance will only read or write snapshot files based on the
// updateSnapshots argument.
this.updateSnapshots = updateSnapshots;
this.cache = new SafeMap();
}
resolveSnapshotFile(entryFile) {
let snapshotFile = this.cache.get(entryFile);
if (snapshotFile === undefined) {
const resolved = resolveSnapshotPathFn(entryFile);
if (typeof resolved !== 'string') {
const err = new ERR_INVALID_STATE('Invalid snapshot filename.');
err.filename = resolved;
throw err;
}
snapshotFile = new SnapshotFile(resolved);
snapshotFile.loaded = this.updateSnapshots;
this.cache.set(entryFile, snapshotFile);
}
return snapshotFile;
}
serialize(input, serializers = serializerFns) {
try {
const value = serializeValue(input, serializers);
return `\n${templateEscape(value)}\n`;
} catch (err) {
throwSerializationError(input, err);
}
}
serializeWithoutEscape(input, serializers = serializerFns) {
try {
return serializeValue(input, serializers);
} catch (err) {
throwSerializationError(input, err);
}
}
writeSnapshotFiles() {
if (!this.updateSnapshots) {
debug('skipping write of snapshot files');
return;
}
this.cache.forEach((snapshotFile) => {
snapshotFile.writeFile();
});
}
createAssert() {
const manager = this;
return function snapshotAssertion(actual, options = kEmptyObject) {
validateObject(options, 'options');
const {
serializers = serializerFns,
} = options;
validateFunctionArray(serializers, 'options.serializers');
const { filePath, fullName } = this;
const snapshotFile = manager.resolveSnapshotFile(filePath);
const value = manager.serialize(actual, serializers);
const id = snapshotFile.nextId(fullName);
if (manager.updateSnapshots) {
snapshotFile.setSnapshot(id, value);
} else {
snapshotFile.readFile();
strictEqual(value, snapshotFile.getSnapshot(id));
}
};
}
createFileAssert() {
const manager = this;
return function fileSnapshotAssertion(actual, path, options = kEmptyObject) {
validateString(path, 'path');
validateObject(options, 'options');
const {
serializers = serializerFns,
} = options;
validateFunctionArray(serializers, 'options.serializers');
const value = manager.serializeWithoutEscape(actual, serializers);
if (manager.updateSnapshots) {
try {
mkdirSync(dirname(path), { __proto__: null, recursive: true });
writeFileSync(path, value, 'utf8');
} catch (err) {
throwWriteError(err, path);
}
} else {
let expected;
try {
expected = readFileSync(path, 'utf8');
} catch (err) {
throwReadError(err, path);
}
strictEqual(value, expected);
}
};
}
}
function throwReadError(err, filename) {
let msg = `Cannot read snapshot file '${filename}.'`;
if (err?.code === 'ENOENT') {
msg += ` ${kMissingSnapshotTip}`;
}
const error = new ERR_INVALID_STATE(msg);
error.cause = err;
error.filename = filename;
throw error;
}
function throwWriteError(err, filename) {
const msg = `Cannot write snapshot file '${filename}.'`;
const error = new ERR_INVALID_STATE(msg);
error.cause = err;
error.filename = filename;
throw error;
}
function throwSerializationError(input, err) {
const error = new ERR_INVALID_STATE(
'The provided serializers did not generate a string.',
);
error.input = input;
error.cause = err;
throw error;
}
function serializeValue(value, serializers) {
let v = value;
for (let i = 0; i < serializers.length; ++i) {
const fn = serializers[i];
v = fn(v);
}
return v;
}
function validateFunctionArray(fns, name) {
validateArray(fns, name);
for (let i = 0; i < fns.length; ++i) {
validateFunction(fns[i], `${name}[${i}]`);
}
}
function escapeSnapshotKey(str) {
let result = JSONStringify(str, null, 2).slice(1, -1);
result = StringPrototypeReplaceAll(result, '`', '\\`');
result = StringPrototypeReplaceAll(result, '${', '\\${');
return result;
}
function templateEscape(str) {
let result = String(str);
result = StringPrototypeReplaceAll(result, '\\', '\\\\');
result = StringPrototypeReplaceAll(result, '`', '\\`');
result = StringPrototypeReplaceAll(result, '${', '\\${');
return result;
}
module.exports = {
SnapshotManager,
defaultResolveSnapshotPath, // Exported for testing only.
defaultSerializers, // Exported for testing only.
setDefaultSnapshotSerializers,
setResolveSnapshotPath,
};