/
runFixtureTests.js
274 lines (239 loc) 路 7.36 KB
/
runFixtureTests.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
/* global BigInt */
import { multiple as getFixtures } from "@babel/helper-fixtures";
import { codeFrameColumns } from "@babel/code-frame";
import fs from "fs";
import path from "path";
const rootPath = path.join(__dirname, "../../../..");
class FixtureError extends Error {
constructor(previousError, fixturePath, code) {
super(previousError.message);
const messageLines = (previousError.message.match(/\n/g) || []).length + 1;
let fixtureStackFrame = "";
if (previousError.loc) {
fixtureStackFrame =
codeFrameColumns(
code,
{
start: {
line: previousError.loc.line,
column: previousError.loc.column + 1,
},
},
{ highlightCode: true },
) +
"\n" +
`at fixture (${fixturePath}:${previousError.loc.line}:${previousError
.loc.column + 1})\n`;
}
this.stack =
previousError.constructor.name +
": " +
previousError.message +
"\n" +
fixtureStackFrame +
previousError.stack
.split("\n")
.slice(messageLines)
.join("\n");
}
}
export function runFixtureTests(fixturesPath, parseFunction) {
const fixtures = getFixtures(fixturesPath);
Object.keys(fixtures).forEach(function(name) {
fixtures[name].forEach(function(testSuite) {
testSuite.tests.forEach(function(task) {
const testFn = task.disabled ? it.skip : it;
testFn(name + "/" + testSuite.title + "/" + task.title, function() {
try {
runTest(task, parseFunction);
} catch (err) {
if (!task.expect.code && !process.env.CI) {
const fn = path.dirname(task.expect.loc) + "/options.json";
if (!fs.existsSync(fn)) {
task.options = task.options || {};
task.options.throws = err.message.replace(
/^.*Got error message: /,
"",
);
fs.writeFileSync(fn, JSON.stringify(task.options, null, 2));
}
}
const fixturePath = `${path.relative(
rootPath,
fixturesPath,
)}/${name}/${task.actual.filename}`;
throw new FixtureError(err, fixturePath, task.actual.code);
}
});
});
});
});
}
export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
const fixtures = getFixtures(fixturesPath);
Object.keys(fixtures).forEach(function(name) {
fixtures[name].forEach(function(testSuite) {
testSuite.tests.forEach(function(task) {
if (!task.options.throws) return;
task.options.plugins = task.options.plugins || [];
task.options.plugins.push("estree");
const testFn = task.disabled ? it.skip : it;
testFn(name + "/" + testSuite.title + "/" + task.title, function() {
try {
runTest(task, parseFunction);
} catch (err) {
const fixturePath = `${path.relative(
rootPath,
fixturesPath,
)}/${name}/${task.actual.filename}`;
throw new FixtureError(err, fixturePath, task.actual.code);
}
});
});
});
});
}
function save(test, ast) {
forceToString(() =>
fs.writeFileSync(test.expect.loc, JSON.stringify(ast, null, 2)),
);
}
// Ensure that RegExp, BigInt, and Errors are serialized as strings
function forceToString(cb) {
const originalToJSONMap = new Map();
const notJSONparseableObj = [RegExp, Error];
if (typeof BigInt !== "undefined") {
notJSONparseableObj.push(BigInt);
}
for (const obj of notJSONparseableObj) {
const { toJSON } = obj.prototype;
originalToJSONMap.set(obj, toJSON);
obj.prototype.toJSON = function() {
return this.toString();
};
}
cb();
for (const obj of notJSONparseableObj) {
obj.prototype.toJSON = originalToJSONMap.get(obj);
}
}
function runTest(test, parseFunction) {
const opts = test.options;
if (opts.throws && test.expect.code) {
throw new Error(
"File expected.json exists although options specify throws. Remove expected.json.",
);
}
let ast;
try {
ast = parseFunction(test.actual.code, { errorRecovery: true, ...opts });
} catch (err) {
if (opts.throws) {
if (err.message === opts.throws) {
return;
} else {
if (process.env.OVERWRITE) {
const fn = path.dirname(test.expect.loc) + "/options.json";
test.options = test.options || {};
test.options.throws = err.message;
fs.writeFileSync(fn, JSON.stringify(test.options, null, 2));
return;
}
err.message =
"Expected error message: " +
opts.throws +
". Got error message: " +
err.message;
throw err;
}
}
throw err;
}
if (ast.comments && !ast.comments.length) delete ast.comments;
if (ast.errors && !ast.errors.length) delete ast.errors;
if (!test.expect.code && !opts.throws && !process.env.CI) {
test.expect.loc += "on";
return save(test, ast);
}
if (opts.throws) {
if (process.env.OVERWRITE) {
const fn = path.dirname(test.expect.loc) + "/options.json";
test.options = test.options || {};
delete test.options.throws;
const contents = JSON.stringify(test.options, null, 2);
if (contents === "{}") {
fs.unlinkSync(fn);
} else {
fs.writeFileSync(fn, JSON.stringify(test.options, null, 2));
}
test.expect.loc += "on";
return save(test, ast);
}
throw new Error(
"Expected error message: " + opts.throws + ". But parsing succeeded.",
);
} else {
const mis = misMatch(JSON.parse(test.expect.code), ast);
if (mis) {
if (process.env.OVERWRITE) {
return save(test, ast);
}
throw new Error(mis);
}
}
}
function ppJSON(v) {
return JSON.stringify(v, null, 2);
}
function addPath(str, pt) {
if (str.charAt(str.length - 1) === ")") {
return str.slice(0, str.length - 1) + "/" + pt + ")";
} else {
return str + " (" + pt + ")";
}
}
function misMatch(exp, act) {
forceToString(() => {
if (
exp instanceof RegExp ||
act instanceof RegExp ||
exp instanceof Error ||
act instanceof Error
) {
const left = ppJSON(exp);
const right = ppJSON(act);
if (left !== right) return left + " !== " + right;
} else if (Array.isArray(exp)) {
if (!Array.isArray(act)) return ppJSON(exp) + " != " + ppJSON(act);
if (act.length != exp.length) {
return "array length mismatch " + exp.length + " != " + act.length;
}
for (let i = 0; i < act.length; ++i) {
const mis = misMatch(exp[i], act[i]);
if (mis) return addPath(mis, i);
}
} else if (
!exp ||
!act ||
typeof exp != "object" ||
typeof act != "object"
) {
if (exp !== act && typeof exp != "function") {
return ppJSON(exp) + " !== " + ppJSON(act);
}
} else {
for (const prop of Object.keys(exp)) {
const mis = misMatch(exp[prop], act[prop]);
if (mis) return addPath(mis, prop);
}
for (const prop of Object.keys(act)) {
if (typeof act[prop] === "function") {
continue;
}
if (!(prop in exp) && act[prop] !== undefined) {
return `Did not expect a property '${prop}'`;
}
}
}
});
}