Skip to content

Commit 5a1aa15

Browse files
committed
moving get fixtures logic into its own file
1 parent ebb63ab commit 5a1aa15

2 files changed

Lines changed: 65 additions & 44 deletions

File tree

tests/config/get-fixtures.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
4+
5+
function getFixtures(context) {
6+
return [...getFiles(context), ...getSnippets(context)];
7+
}
8+
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);
35+
}
36+
37+
function getSnippets({ snippets }) {
38+
return snippets.map((test, index) => {
39+
test = typeof test === "string" ? { code: test } : test;
40+
41+
if (typeof test.code !== "string") {
42+
throw Object.assign(new Error("Invalid test"), { test });
43+
}
44+
45+
return {
46+
...test,
47+
name: `snippet: ${test.name || `#${index}`}`,
48+
};
49+
});
50+
}
51+
52+
export { getFixtures };

tests/config/run-format-test.js

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import fs from "node:fs";
21
import path from "node:path";
32
import url from "node:url";
43
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
4+
import { getFixtures } from "./get-fixtures.js";
55
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
66
import {
77
isErrorTest as isErrorTestDirectory,
@@ -38,53 +38,22 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
3838
options = { errors: true, ...options };
3939
}
4040

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-
8041
const [parser] = explicitParsers;
8142
const allParsers = [...explicitParsers];
8243

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)) {
8655
const title = `${name}${
87-
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
56+
context.stringifiedOptions ? ` - ${context.stringifiedOptions}` : ""
8857
}`;
8958

9059
describe(title, () => {

0 commit comments

Comments
 (0)