-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathget-fixtures.js
More file actions
78 lines (66 loc) · 1.85 KB
/
get-fixtures.js
File metadata and controls
78 lines (66 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import fs from "node:fs";
import path from "node:path";
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
import visualizeEndOfLine from "./visualize-end-of-line.js";
/**
@import {Context} from "./run-format-test.js"
@typedef {Exclude<ReturnType<ReturnType<getFixtures>["next"]>["value"], void>} Fixture
*/
/**
@param {Context} context
*/
function* getFixtures(context) {
yield* getFiles(context);
yield* getSnippets(context);
}
/**
@param {Context} context
*/
function* getFiles(context) {
const { dirname } = context;
for (const file of fs.readdirSync(dirname, { withFileTypes: true })) {
const filename = file.name;
const filepath = path.join(dirname, filename);
if (
!file.isFile() ||
filename[0] === "." ||
filename === FORMAT_SCRIPT_FILENAME ||
// VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
filename === "debug.log" ||
path.extname(filename) === ".snap"
) {
continue;
}
const text = fs.readFileSync(filepath, "utf8");
yield {
context,
name: filename,
filename,
filepath,
code: text,
};
}
}
/**
@param {Context} context
*/
function* getSnippets(context) {
for (const [index, snippet] of context.snippets.entries()) {
const testCase =
typeof snippet === "string"
? { code: snippet, context }
: { ...snippet, context };
if (typeof testCase.code !== "string") {
throw Object.assign(new Error("Invalid test"), { snippet });
}
if (typeof testCase.output === "string") {
testCase.output = visualizeEndOfLine(testCase.output);
}
testCase.name = `snippet: ${testCase.name || `#${index}`}`;
if (typeof testCase.filename === "string") {
testCase.filepath = path.join(context.dirname, testCase.filename);
}
yield testCase;
}
}
export { getFixtures };