|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | 3 | import { FORMAT_SCRIPT_FILENAME } from "./constants.js"; |
| 4 | +import visualizeEndOfLine from "./utils/visualize-end-of-line.js"; |
4 | 5 |
|
5 | | -function getFixtures(context) { |
6 | | - return [...getFiles(context), ...getSnippets(context)]; |
| 6 | +function* getFixtures(context) { |
| 7 | + yield* getFiles(context); |
| 8 | + yield* getSnippets(context); |
7 | 9 | } |
8 | 10 |
|
9 | | -function getFiles({ dirname }) { |
10 | | - return fs |
11 | | - .readdirSync(dirname, { withFileTypes: true }) |
12 | | - .map((file) => { |
13 | | - const basename = file.name; |
14 | | - const filename = path.join(dirname, basename); |
15 | | - if ( |
16 | | - path.extname(basename) === ".snap" || |
17 | | - !file.isFile() || |
18 | | - basename[0] === "." || |
19 | | - basename === FORMAT_SCRIPT_FILENAME || |
20 | | - // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191 |
21 | | - basename === "debug.log" |
22 | | - ) { |
23 | | - return; |
24 | | - } |
25 | | - |
26 | | - const text = fs.readFileSync(filename, "utf8"); |
27 | | - |
28 | | - return { |
29 | | - name: basename, |
30 | | - filename, |
31 | | - code: text, |
32 | | - }; |
33 | | - }) |
34 | | - .filter(Boolean); |
| 11 | +function* getFiles(context) { |
| 12 | + const { dirname } = context; |
| 13 | + for (const file of fs.readdirSync(dirname, { withFileTypes: true })) { |
| 14 | + const filename = file.name; |
| 15 | + const filepath = path.join(dirname, filename); |
| 16 | + if ( |
| 17 | + path.extname(filename) === ".snap" || |
| 18 | + !file.isFile() || |
| 19 | + filename[0] === "." || |
| 20 | + filename === FORMAT_SCRIPT_FILENAME || |
| 21 | + // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191 |
| 22 | + filename === "debug.log" |
| 23 | + ) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + const text = fs.readFileSync(filepath, "utf8"); |
| 28 | + |
| 29 | + yield { |
| 30 | + context, |
| 31 | + name: filename, |
| 32 | + filename, |
| 33 | + filepath, |
| 34 | + code: text, |
| 35 | + }; |
| 36 | + } |
35 | 37 | } |
36 | 38 |
|
37 | | -function getSnippets({ snippets }) { |
38 | | - return snippets.map((test, index) => { |
39 | | - test = typeof test === "string" ? { code: test } : test; |
| 39 | +function* getSnippets(context) { |
| 40 | + for (const [index, snippet] of context.snippets.entries()) { |
| 41 | + const testCase = |
| 42 | + typeof snippet === "string" |
| 43 | + ? { code: snippet, context } |
| 44 | + : { ...snippet, context }; |
40 | 45 |
|
41 | | - if (typeof test.code !== "string") { |
42 | | - throw Object.assign(new Error("Invalid test"), { test }); |
| 46 | + if (typeof testCase.code !== "string") { |
| 47 | + throw Object.assign(new Error("Invalid test"), { snippet }); |
43 | 48 | } |
44 | 49 |
|
45 | | - return { |
46 | | - ...test, |
47 | | - name: `snippet: ${test.name || `#${index}`}`, |
48 | | - }; |
49 | | - }); |
| 50 | + if (typeof testCase.output === "string") { |
| 51 | + testCase.output = visualizeEndOfLine(testCase.output); |
| 52 | + } |
| 53 | + |
| 54 | + testCase.name = `snippet: ${testCase.name || `#${index}`}`; |
| 55 | + |
| 56 | + if (typeof testCase.filename === "string") { |
| 57 | + testCase.filepath = path.join(context.dirname, testCase.filename); |
| 58 | + } |
| 59 | + |
| 60 | + yield testCase; |
| 61 | + } |
50 | 62 | } |
51 | 63 |
|
52 | 64 | export { getFixtures }; |
0 commit comments