|
1 | | -import fs from "node:fs"; |
2 | 1 | import path from "node:path"; |
3 | 2 | import url from "node:url"; |
4 | | -import createEsmUtils from "esm-utils"; |
| 3 | +import { FORMAT_SCRIPT_FILENAME } from "./constants.js"; |
| 4 | +import { getFixtures } from "./get-fixtures.js"; |
5 | 5 | import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js"; |
| 6 | +import { |
| 7 | + isErrorTest as isErrorTestDirectory, |
| 8 | + normalizeDirectory, |
| 9 | +} from "./utilities.js"; |
6 | 10 | import { format } from "./run-prettier.js"; |
| 11 | +import { replacePlaceholders } from "./replace-placeholders.js"; |
7 | 12 | import { runTest } from "./run-test.js"; |
8 | 13 | import { shouldThrowOnFormat } from "./utilities.js"; |
9 | 14 |
|
10 | | -const { __dirname } = createEsmUtils(import.meta); |
11 | | - |
12 | | -const isTestDirectory = (dirname, name) => |
13 | | - (dirname + path.sep).startsWith( |
14 | | - path.join(__dirname, "../format", name) + path.sep, |
15 | | - ); |
16 | | - |
17 | | -function runFormatTest(fixtures, parsers, options) { |
18 | | - let { importMeta, snippets = [] } = fixtures.importMeta |
19 | | - ? fixtures |
20 | | - : { importMeta: fixtures }; |
| 15 | +function runFormatTest(rawFixtures, explicitParsers, rawOptions) { |
| 16 | + const { importMeta, snippets = [] } = rawFixtures.importMeta |
| 17 | + ? rawFixtures |
| 18 | + : { importMeta: rawFixtures }; |
21 | 19 |
|
22 | 20 | const filename = path.basename(new URL(importMeta.url).pathname); |
23 | | - if (filename !== "format.test.js") { |
24 | | - throw new Error(`Format test should run in file named 'format.test.js'.`); |
| 21 | + if (filename !== FORMAT_SCRIPT_FILENAME) { |
| 22 | + throw new Error( |
| 23 | + `Format test should run in file named '${FORMAT_SCRIPT_FILENAME}'.`, |
| 24 | + ); |
25 | 25 | } |
26 | 26 |
|
27 | | - const dirname = path.dirname(url.fileURLToPath(importMeta.url)); |
28 | | - |
29 | | - // `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone` |
30 | | - const IS_PARSER_INFERENCE_TESTS = isTestDirectory( |
31 | | - dirname, |
32 | | - "misc/parser-inference", |
| 27 | + const dirname = normalizeDirectory( |
| 28 | + path.dirname(url.fileURLToPath(importMeta.url)), |
33 | 29 | ); |
34 | 30 |
|
35 | | - // `IS_ERROR_TESTS` mean to watch errors like: |
| 31 | + let options = { ...rawOptions }; |
| 32 | + |
| 33 | + // `IS_ERROR_TEST` mean to watch errors like: |
36 | 34 | // - syntax parser hasn't supported yet |
37 | 35 | // - syntax errors that should throws |
38 | | - const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors"); |
39 | | - if (IS_ERROR_TESTS) { |
40 | | - options = { errors: true, ...options }; |
41 | | - } |
| 36 | + const isErrorTest = isErrorTestDirectory(dirname); |
42 | 37 |
|
43 | | - if (IS_PARSER_INFERENCE_TESTS) { |
44 | | - parsers = [undefined]; |
| 38 | + if (isErrorTest) { |
| 39 | + options = { errors: true, ...options }; |
45 | 40 | } |
46 | 41 |
|
47 | | - snippets = snippets.map((test, index) => { |
48 | | - test = typeof test === "string" ? { code: test } : test; |
49 | | - |
50 | | - if (typeof test.code !== "string") { |
51 | | - throw Object.assign(new Error("Invalid test"), { test }); |
52 | | - } |
53 | | - |
54 | | - return { |
55 | | - ...test, |
56 | | - name: `snippet: ${test.name || `#${index}`}`, |
57 | | - }; |
58 | | - }); |
59 | | - |
60 | | - const files = fs |
61 | | - .readdirSync(dirname, { withFileTypes: true }) |
62 | | - .map((file) => { |
63 | | - const basename = file.name; |
64 | | - const filename = path.join(dirname, basename); |
65 | | - if ( |
66 | | - path.extname(basename) === ".snap" || |
67 | | - !file.isFile() || |
68 | | - basename[0] === "." || |
69 | | - basename === "format.test.js" || |
70 | | - // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191 |
71 | | - basename === "debug.log" |
72 | | - ) { |
73 | | - return; |
74 | | - } |
75 | | - |
76 | | - const text = fs.readFileSync(filename, "utf8"); |
77 | | - |
78 | | - return { |
79 | | - name: basename, |
80 | | - filename, |
81 | | - code: text, |
82 | | - }; |
83 | | - }) |
84 | | - .filter(Boolean); |
85 | | - |
86 | | - const [parser] = parsers; |
87 | | - const allParsers = [...parsers]; |
88 | | - |
89 | | - const stringifiedOptions = stringifyOptionsForTitle(options); |
| 42 | + const context = { |
| 43 | + dirname, |
| 44 | + stringifiedOptions: stringifyOptionsForTitle(rawOptions), |
| 45 | + parsers: [...explicitParsers], |
| 46 | + options, |
| 47 | + explicitParsers, |
| 48 | + rawOptions, |
| 49 | + snippets, |
| 50 | + }; |
| 51 | + |
| 52 | + for (const fixture of getFixtures(context)) { |
| 53 | + const { name, context, filepath } = fixture; |
| 54 | + const { stringifiedOptions, parsers } = context; |
90 | 55 |
|
91 | | - for (const { name, filename, code, output } of [...files, ...snippets]) { |
92 | 56 | const title = `${name}${ |
93 | 57 | stringifiedOptions ? ` - ${stringifiedOptions}` : "" |
94 | 58 | }`; |
95 | 59 |
|
96 | 60 | describe(title, () => { |
97 | | - const formatOptions = { |
98 | | - printWidth: 80, |
99 | | - ...options, |
100 | | - filepath: filename, |
101 | | - parser, |
102 | | - }; |
103 | | - const shouldThrowOnMainParserFormat = shouldThrowOnFormat( |
104 | | - name, |
105 | | - formatOptions, |
106 | | - ); |
107 | | - |
108 | | - let mainParserFormatResult; |
109 | | - if (shouldThrowOnMainParserFormat) { |
110 | | - mainParserFormatResult = { options: formatOptions, error: true }; |
111 | | - } else { |
112 | | - beforeAll(async () => { |
113 | | - mainParserFormatResult = await format(code, formatOptions); |
114 | | - }); |
115 | | - } |
| 61 | + const testCases = parsers.map((parser) => getTestCase(fixture, parser)); |
116 | 62 |
|
117 | | - for (const currentParser of allParsers) { |
| 63 | + for (const testCase of testCases) { |
118 | 64 | const testTitle = |
119 | | - shouldThrowOnMainParserFormat || |
120 | | - formatOptions.parser !== currentParser |
121 | | - ? `[${currentParser}] format` |
| 65 | + testCase.expectFail || |
| 66 | + testCase.formatOptions.parser !== testCase.parser |
| 67 | + ? `[${testCase.parser}] format` |
122 | 68 | : "format"; |
123 | 69 |
|
124 | 70 | test(testTitle, async () => { |
125 | 71 | await runTest({ |
126 | 72 | parsers, |
127 | 73 | name, |
128 | | - filename, |
129 | | - code, |
130 | | - output, |
131 | | - parser: currentParser, |
132 | | - mainParserFormatResult, |
133 | | - mainParserFormatOptions: formatOptions, |
| 74 | + filename: filepath, |
| 75 | + code: testCase.code, |
| 76 | + output: testCase.expectedOutput, |
| 77 | + parser: testCase.parser, |
| 78 | + mainParserFormatResult: await testCase.runFormat(), |
| 79 | + mainParserFormatOptions: testCase.formatOptions, |
134 | 80 | }); |
135 | 81 | }); |
136 | 82 | } |
137 | 83 | }); |
138 | 84 | } |
139 | 85 | } |
140 | 86 |
|
| 87 | +function getTestCase(fixture, parser) { |
| 88 | + const { code: originalText, context, filepath } = fixture; |
| 89 | + |
| 90 | + const { text: code, options: formatOptions } = replacePlaceholders( |
| 91 | + originalText, |
| 92 | + { |
| 93 | + printWidth: 80, |
| 94 | + ...context.options, |
| 95 | + filepath, |
| 96 | + parser, |
| 97 | + }, |
| 98 | + ); |
| 99 | + |
| 100 | + const expectFail = shouldThrowOnFormat(fixture, formatOptions); |
| 101 | + |
| 102 | + let promise; |
| 103 | + |
| 104 | + return { |
| 105 | + context, |
| 106 | + parser, |
| 107 | + filepath, |
| 108 | + originalText, |
| 109 | + code, |
| 110 | + formatOptions, |
| 111 | + expectFail, |
| 112 | + expectedOutput: fixture.output, |
| 113 | + isEmpty: code.trim() === "", |
| 114 | + runFormat: () => |
| 115 | + promise === undefined ? (promise = format(code, formatOptions)) : promise, |
| 116 | + }; |
| 117 | +} |
| 118 | + |
141 | 119 | export default runFormatTest; |
0 commit comments