forked from babel/babel
/
parser-test-runner.js
283 lines (243 loc) · 7.38 KB
/
parser-test-runner.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
import fs from "fs/promises";
import chalk from "chalk";
import { parse as parser } from "../../../packages/babel-parser/lib/index.js";
const dot = chalk.gray(".");
class TestRunner {
constructor({
testDir,
allowlist,
logInterval = 1,
shouldUpdate,
getTests,
parse = this.parse,
}) {
this.testDir = testDir;
this.allowlist = allowlist;
this.logInterval = logInterval;
this.shouldUpdate = shouldUpdate;
this.getTests = getTests;
this.parse = parse;
}
async run() {
const allowlistP = this.getAllowlist();
console.log(`Now running tests...`);
const results = [];
for await (const result of this.runTests()) {
results.push(result);
if (results.length % this.logInterval === 0) process.stdout.write(dot);
}
process.stdout.write("\n");
const summary = this.interpret(results, await allowlistP);
await this.output(summary);
}
async *runTests() {
for await (const test of this.getTests()) {
yield this.runTest(test);
}
}
runTest(test) {
try {
this.parse(test, parser);
test.actualError = false;
} catch (err) {
test.actualError = true;
test.actualErrorObject = err;
}
test.result = test.expectedError !== test.actualError ? "fail" : "pass";
return test;
}
parse(test, parser) {
const tests = typeof test.contents === "string" ? [test] : test.contents;
for (const { contents, ...options } of tests) {
parser(contents, options);
}
}
async getAllowlist() {
const contents = await fs.readFile(this.allowlist, "utf-8");
const table = new Set();
for (const line of contents.split("\n")) {
const filename = line.replace(/#.*$/, "").trim();
if (filename) table.add(filename);
}
return table;
}
async updateAllowlist(summary) {
const contents = await fs.readFile(this.allowlist, "utf-8");
const toRemove = summary.disallowed.success
.concat(summary.disallowed.failure)
.map(test => test.id)
.concat(summary.unrecognized);
const allowedFalsePositiveIds = summary.allowed.falsePositive.map(
test => test.id
);
let invalidWithoutError = [];
let validWithError = [];
for (const line of contents.split("\n")) {
const testId = line.replace(/#.*$/, "").trim();
if (testId && !toRemove.includes(testId)) {
if (allowedFalsePositiveIds.includes(testId)) {
invalidWithoutError.push(line);
} else {
validWithError.push(line);
}
}
}
invalidWithoutError = invalidWithoutError.concat(
summary.disallowed.falsePositive.map(test => test.id)
);
validWithError = validWithError.concat(
summary.disallowed.falseNegative.map(test => test.id)
);
invalidWithoutError.sort();
validWithError.sort();
const errorsMap = new Map();
summary.allowed.falseNegative
.concat(summary.disallowed.falseNegative)
.forEach(test => {
errorsMap.set(test.id, test.actualErrorObject);
});
const updated = [
`# ${invalidWithoutError.length} invalid programs did not produce a parsing error\n`,
"\n",
invalidWithoutError.join("\n"),
"\n",
"\n",
"\n",
`# ${validWithError.length} valid programs produced a parsing error\n`,
"\n",
...validWithError
// .map(
// v =>
// `${v}\n# ${JSON.stringify(errorsMap.get(v), null, 2)
// .trimEnd()
// .replace(/\n/g, "\n#")}\n`
// )
.join("\n"),
];
await fs.writeFile(this.allowlist, updated.join("") + "\n", "utf8");
}
interpret(results, allowlist) {
const summary = {
passed: true,
allowed: {
success: [],
failure: [],
falsePositive: [],
falseNegative: [],
},
disallowed: {
success: [],
failure: [],
falsePositive: [],
falseNegative: [],
},
unrecognized: null,
count: results.length,
};
results.forEach(function (result) {
let classification, isAllowed;
const inAllowlist = allowlist.has(result.id);
allowlist.delete(result.id);
if (!result.expectedError) {
if (!result.actualError) {
classification = "success";
isAllowed = !inAllowlist;
} else {
classification = "falseNegative";
isAllowed = inAllowlist;
}
} else {
if (!result.actualError) {
classification = "falsePositive";
isAllowed = inAllowlist;
} else {
classification = "failure";
isAllowed = !inAllowlist;
}
}
summary.passed &= isAllowed;
summary[isAllowed ? "allowed" : "disallowed"][classification].push(
result
);
});
summary.unrecognized = Array.from(allowlist);
summary.passed = !!summary.passed && summary.unrecognized.length === 0;
return summary;
}
async output(summary) {
const goodnews = [
summary.allowed.success.length + " valid programs parsed without error",
summary.allowed.failure.length +
" invalid programs produced a parsing error",
summary.allowed.falsePositive.length +
" invalid programs did not produce a parsing error" +
" (and allowed by the allowlist file)",
summary.allowed.falseNegative.length +
" valid programs produced a parsing error" +
" (and allowed by the allowlist file)",
];
const badnews = [];
const badnewsDetails = [];
void [
{
tests: summary.disallowed.success,
label:
"valid programs parsed without error" +
" (in violation of the allowlist file)",
},
{
tests: summary.disallowed.failure,
label:
"invalid programs produced a parsing error" +
" (in violation of the allowlist file)",
},
{
tests: summary.disallowed.falsePositive,
label:
"invalid programs did not produce a parsing error" +
" (without a corresponding entry in the allowlist file)",
},
{
tests: summary.disallowed.falseNegative,
label:
"valid programs produced a parsing error" +
" (without a corresponding entry in the allowlist file)",
},
{
tests: summary.unrecognized,
label: "non-existent programs specified in the allowlist file",
},
].forEach(function ({ tests, label }) {
if (!tests.length) {
return;
}
const desc = tests.length + " " + label;
badnews.push(desc);
badnewsDetails.push(desc + ":");
badnewsDetails.push(
...tests.map(
test =>
` ${test.id || test} ${test.expectedError} ${test.actualError}`
)
);
});
console.log(`Testing complete (${summary.count} tests).`);
console.log("Summary:");
console.log(chalk.green(goodnews.join("\n").replace(/^/gm, " ✔ ")));
if (!summary.passed) {
console.log("");
console.log(chalk.red(badnews.join("\n").replace(/^/gm, " ✘ ")));
console.log("");
console.log("Details:");
console.log(badnewsDetails.join("\n").replace(/^/gm, " "));
}
if (this.shouldUpdate) {
await this.updateAllowlist(summary);
console.log("");
console.log("Allowlist file updated.");
} else {
process.exitCode = summary.passed ? 0 : 1;
}
}
}
export default TestRunner;