-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrun-format-test.js
More file actions
452 lines (394 loc) · 12.5 KB
/
run-format-test.js
File metadata and controls
452 lines (394 loc) · 12.5 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import createEsmUtils from "esm-utils";
import getPrettier from "./get-prettier.js";
import getCreateParser from "./get-create-parser.js";
import getVariantCoverage from "./get-variant-coverage.js";
import getPlugins from "./get-plugins.js";
import compileContract from "./utils/compile-contract.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import createSnapshot from "./utils/create-snapshot.js";
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
import {
isAntlrMismatch,
isAstUnstable,
isUnstable,
} from "./failed-format-tests.js";
const { __dirname } = createEsmUtils(import.meta);
const { FULL_TEST } = process.env;
const BOM = "\uFEFF";
const CURSOR_PLACEHOLDER = "<|>";
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
const testsWithAstChanges = new Map(
[
"Parentheses/AddNoParentheses.sol",
"Parentheses/SubNoParentheses.sol",
"Parentheses/MulNoParentheses.sol",
"Parentheses/DivNoParentheses.sol",
"Parentheses/ModNoParentheses.sol",
"Parentheses/ExpNoParentheses.sol",
"Parentheses/ShiftLNoParentheses.sol",
"Parentheses/ShiftRNoParentheses.sol",
"Parentheses/BitAndNoParentheses.sol",
"Parentheses/BitOrNoParentheses.sol",
"Parentheses/BitXorNoParentheses.sol",
"Parentheses/LogicNoParentheses.sol",
"HexLiteral/HexLiteral.sol",
"ModifierInvocations/ModifierInvocations.sol",
].map((fixture) => {
const [file, compareBytecode = () => true] = Array.isArray(fixture)
? fixture
: [fixture];
return [path.join(__dirname, "../format/", file), compareBytecode];
}),
);
const shouldCompareBytecode = (filename, options) => {
const testFunction = testsWithAstChanges.get(filename);
if (!testFunction) {
return false;
}
return testFunction(options);
};
const shouldThrowOnFormat = (filename, options) => {
const { errors = {} } = options;
if (errors === true) {
return true;
}
const files = errors[options.parser];
if (files === true || (Array.isArray(files) && files.includes(filename))) {
return true;
}
return false;
};
const isTestDirectory = (dirname, name) =>
(dirname + path.sep).startsWith(
path.join(__dirname, "../format", name) + path.sep,
);
function runFormatTest(fixtures, parsers, options) {
let { importMeta, snippets = [] } = fixtures.importMeta
? fixtures
: { importMeta: fixtures };
const filename = path.basename(new URL(importMeta.url).pathname);
if (filename !== "format.test.js") {
throw new Error(`Format test should run in file named 'format.test.js'.`);
}
const dirname = path.dirname(url.fileURLToPath(importMeta.url));
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
dirname,
"misc/parser-inference",
);
// `IS_ERROR_TESTS` mean to watch errors like:
// - syntax parser hasn't supported yet
// - syntax errors that should throws
const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors");
if (IS_ERROR_TESTS) {
options = { errors: true, ...options };
}
if (IS_PARSER_INFERENCE_TESTS) {
parsers = [undefined];
}
snippets = snippets.map((test, index) => {
test = typeof test === "string" ? { code: test } : test;
if (typeof test.code !== "string") {
throw Object.assign(new Error("Invalid test"), { test });
}
return {
...test,
name: `snippet: ${test.name || `#${index}`}`,
};
});
const files = fs
.readdirSync(dirname, { withFileTypes: true })
.map((file) => {
const basename = file.name;
const filename = path.join(dirname, basename);
if (
path.extname(basename) === ".snap" ||
!file.isFile() ||
basename[0] === "." ||
basename === "format.test.js" ||
// VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
basename === "debug.log"
) {
return;
}
const text = fs.readFileSync(filename, "utf8");
return {
name: basename,
filename,
code: text,
};
})
.filter(Boolean);
const [parser] = parsers;
const allParsers = [...parsers];
const stringifiedOptions = stringifyOptionsForTitle(options);
for (const { name, filename, code, output } of [...files, ...snippets]) {
const title = `${name}${
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
}`;
describe(title, () => {
const formatOptions = {
printWidth: 80,
...options,
filepath: filename,
parser,
};
const shouldThrowOnMainParserFormat = shouldThrowOnFormat(
name,
formatOptions,
);
let mainParserFormatResult;
if (shouldThrowOnMainParserFormat) {
mainParserFormatResult = { options: formatOptions, error: true };
} else {
beforeAll(async () => {
mainParserFormatResult = await format(code, formatOptions);
});
}
for (const currentParser of allParsers) {
const testTitle =
shouldThrowOnMainParserFormat ||
formatOptions.parser !== currentParser
? `[${currentParser}] format`
: "format";
test(testTitle, async () => {
await runTest({
parsers,
name,
filename,
code,
output,
parser: currentParser,
mainParserFormatResult,
mainParserFormatOptions: formatOptions,
});
});
}
});
}
}
async function runTest({
parsers,
name,
filename,
code,
output,
parser,
mainParserFormatResult,
mainParserFormatOptions,
}) {
let formatOptions = mainParserFormatOptions;
let formatResult = mainParserFormatResult;
// Verify parsers or error tests
if (
mainParserFormatResult.error ||
mainParserFormatOptions.parser !== parser
) {
formatOptions = { ...mainParserFormatResult.options, parser };
const runFormat = () => format(code, formatOptions);
if (shouldThrowOnFormat(name, formatOptions)) {
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
return;
}
// Verify parsers format result should be the same as main parser
output = mainParserFormatResult.outputWithCursor;
formatResult = await runFormat();
}
// Make sure output has consistent EOL
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)),
);
// The result is assert to equals to `output`
if (typeof output === "string") {
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(output),
);
return;
}
// All parsers have the same result, only snapshot the result from main parser
expect(
createSnapshot(formatResult, {
parsers,
formatOptions,
CURSOR_PLACEHOLDER,
}),
).toMatchSnapshot();
if (!FULL_TEST) {
return;
}
if (formatOptions.parser === "slang") {
const createParser = await getCreateParser();
const variantCoverage = await getVariantCoverage();
const { parser, parseOutput } = createParser(code, formatOptions);
// Check coverage
variantCoverage(parseOutput.tree.asNonterminalNode());
if (!isAntlrMismatch(filename, formatOptions)) {
// Compare with ANTLR's format
const prettier = await getPrettier();
const { formatted: antlrOutput } = await prettier.formatWithCursor(code, {
...formatOptions,
// Since Slang forces us to decide on a compiler version, we need to do the
// same for ANTLR unless it was already given as an option.
compiler: formatOptions.compiler || parser.languageVersion,
parser: "antlr",
plugins: await getPlugins(),
});
expect(antlrOutput).toEqual(formatResult.output);
}
}
const isUnstableTest = isUnstable(filename, formatOptions);
if (
(formatResult.changed || isUnstableTest) &&
// No range and cursor
formatResult.input === code
) {
const { eolVisualizedOutput: firstOutput, output } = formatResult;
const { eolVisualizedOutput: secondOutput } = await format(
output,
formatOptions,
);
if (isUnstableTest) {
// To keep eye on failed tests, this assert never supposed to pass,
// if it fails, just remove the file from `unstableTests`
expect(secondOutput).not.toEqual(firstOutput);
} else {
expect(secondOutput).toEqual(firstOutput);
}
}
const isAstUnstableTest = isAstUnstable(filename, formatOptions);
// Some parsers skip parsing empty files
if (formatResult.changed && code.trim()) {
const { input, output } = formatResult;
const originalAst = await parse(input, formatOptions);
const formattedAst = await parse(output, formatOptions);
if (isAstUnstableTest) {
expect(formattedAst).not.toEqual(originalAst);
} else {
expect(formattedAst).toEqual(originalAst);
}
}
if (!shouldSkipEolTest(code, formatResult.options)) {
for (const eol of ["\r\n", "\r"]) {
const { eolVisualizedOutput: output } = await format(
code.replace(/\n/gu, eol),
formatOptions,
);
// Only if `endOfLine: "auto"` the result will be different
const expected =
formatOptions.endOfLine === "auto"
? visualizeEndOfLine(
// All `code` use `LF`, so the `eol` of result is always `LF`
formatResult.outputWithCursor.replace(/\n/gu, eol),
)
: formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
}
}
if (code.charAt(0) !== BOM) {
const { eolVisualizedOutput: output } = await format(
BOM + code,
formatOptions,
);
const expected = BOM + formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
}
if (shouldCompareBytecode(filename, formatOptions)) {
const output = compileContract(filename, formatResult.output);
const expected = compileContract(filename, formatResult.input);
expect(output).toEqual(expected);
}
}
function shouldSkipEolTest(code, options) {
if (code.includes("\r")) {
return true;
}
const { requirePragma, rangeStart, rangeEnd } = options;
if (requirePragma) {
return true;
}
if (
typeof rangeStart === "number" &&
typeof rangeEnd === "number" &&
rangeStart >= rangeEnd
) {
return true;
}
return false;
}
async function parse(source, options) {
const prettier = await getPrettier();
const { ast } = await prettier.__debug.parse(
source,
{ ...options, plugins: await getPlugins() },
{ massage: true },
);
return ast;
}
const indexProperties = [
{
property: "cursorOffset",
placeholder: CURSOR_PLACEHOLDER,
},
{
property: "rangeStart",
placeholder: RANGE_START_PLACEHOLDER,
},
{
property: "rangeEnd",
placeholder: RANGE_END_PLACEHOLDER,
},
];
function replacePlaceholders(originalText, originalOptions) {
const indexes = indexProperties
.map(({ property, placeholder }) => {
const value = originalText.indexOf(placeholder);
return value === -1 ? undefined : { property, value, placeholder };
})
.filter(Boolean)
.sort((a, b) => a.value - b.value);
const options = { ...originalOptions };
let text = originalText;
let offset = 0;
for (const { property, value, placeholder } of indexes) {
text = text.replace(placeholder, "");
options[property] = value + offset;
offset -= placeholder.length;
}
return { text, options };
}
const insertCursor = (text, cursorOffset) =>
cursorOffset >= 0
? text.slice(0, cursorOffset) +
CURSOR_PLACEHOLDER +
text.slice(cursorOffset)
: text;
async function format(originalText, originalOptions) {
const { text: input, options } = replacePlaceholders(
originalText,
originalOptions,
);
const inputWithCursor = insertCursor(input, options.cursorOffset);
const prettier = await getPrettier();
const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
input,
{ ...options, plugins: await getPlugins() },
);
const outputWithCursor = insertCursor(output, cursorOffset);
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
const changed = outputWithCursor !== inputWithCursor;
return {
changed,
options,
input,
inputWithCursor,
output,
outputWithCursor,
eolVisualizedOutput,
};
}
export default runFormatTest;