Skip to content

Commit 0b469b1

Browse files
committed
updating the fixture logic. returning the new more complete fixture object and using a generator pattern to avoid calling filter(Boolean) over the array.
1 parent 5a1aa15 commit 0b469b1

2 files changed

Lines changed: 51 additions & 39 deletions

File tree

tests/config/get-fixtures.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
4+
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
45

5-
function getFixtures(context) {
6-
return [...getFiles(context), ...getSnippets(context)];
6+
function* getFixtures(context) {
7+
yield* getFiles(context);
8+
yield* getSnippets(context);
79
}
810

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+
}
3537
}
3638

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 };
4045

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 });
4348
}
4449

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+
}
5062
}
5163

5264
export { getFixtures };

tests/config/run-format-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { runTest } from "./run-test.js";
1212
import { shouldThrowOnFormat } from "./utilities.js";
1313

1414
function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
15-
let { importMeta, snippets = [] } = rawFixtures.importMeta
15+
const { importMeta, snippets = [] } = rawFixtures.importMeta
1616
? rawFixtures
1717
: { importMeta: rawFixtures };
1818

0 commit comments

Comments
 (0)