|
1 | | -import fs from "node:fs"; |
2 | 1 | import path from "node:path"; |
3 | 2 | import url from "node:url"; |
4 | 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 | 6 | import { |
7 | 7 | isErrorTest as isErrorTestDirectory, |
@@ -38,53 +38,22 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) { |
38 | 38 | options = { errors: true, ...options }; |
39 | 39 | } |
40 | 40 |
|
41 | | - snippets = snippets.map((test, index) => { |
42 | | - test = typeof test === "string" ? { code: test } : test; |
43 | | - |
44 | | - if (typeof test.code !== "string") { |
45 | | - throw Object.assign(new Error("Invalid test"), { test }); |
46 | | - } |
47 | | - |
48 | | - return { |
49 | | - ...test, |
50 | | - name: `snippet: ${test.name || `#${index}`}`, |
51 | | - }; |
52 | | - }); |
53 | | - |
54 | | - const files = fs |
55 | | - .readdirSync(dirname, { withFileTypes: true }) |
56 | | - .map((file) => { |
57 | | - const basename = file.name; |
58 | | - const filename = path.join(dirname, basename); |
59 | | - if ( |
60 | | - path.extname(basename) === ".snap" || |
61 | | - !file.isFile() || |
62 | | - basename[0] === "." || |
63 | | - basename === "format.test.js" || |
64 | | - // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191 |
65 | | - basename === "debug.log" |
66 | | - ) { |
67 | | - return; |
68 | | - } |
69 | | - |
70 | | - const text = fs.readFileSync(filename, "utf8"); |
71 | | - |
72 | | - return { |
73 | | - name: basename, |
74 | | - filename, |
75 | | - code: text, |
76 | | - }; |
77 | | - }) |
78 | | - .filter(Boolean); |
79 | | - |
80 | 41 | const [parser] = explicitParsers; |
81 | 42 | const allParsers = [...explicitParsers]; |
82 | 43 |
|
83 | | - const stringifiedOptions = stringifyOptionsForTitle(options); |
84 | | - |
85 | | - for (const { name, filename, code, output } of [...files, ...snippets]) { |
| 44 | + const context = { |
| 45 | + dirname, |
| 46 | + stringifiedOptions: stringifyOptionsForTitle(rawOptions), |
| 47 | + parsers: allParsers, |
| 48 | + options, |
| 49 | + explicitParsers, |
| 50 | + rawOptions, |
| 51 | + snippets, |
| 52 | + }; |
| 53 | + |
| 54 | + for (const { name, filename, code, output } of getFixtures(context)) { |
86 | 55 | const title = `${name}${ |
87 | | - stringifiedOptions ? ` - ${stringifiedOptions}` : "" |
| 56 | + context.stringifiedOptions ? ` - ${context.stringifiedOptions}` : "" |
88 | 57 | }`; |
89 | 58 |
|
90 | 59 | describe(title, () => { |
|
0 commit comments