-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrun-format-test.js
More file actions
141 lines (119 loc) · 3.82 KB
/
run-format-test.js
File metadata and controls
141 lines (119 loc) · 3.82 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import createEsmUtils from "esm-utils";
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
import { format } from "./run-prettier.js";
import { runTest } from "./run-test.js";
import { shouldThrowOnFormat } from "./utilities.js";
const { __dirname } = createEsmUtils(import.meta);
const isTestDirectory = (dirname, name) =>
(dirname + path.sep).startsWith(
path.join(__dirname, "../format", name) + path.sep,
);
function runFormatTest(fixtures, parsers, options) {
let { importMeta, snippets = [] } = fixtures.importMeta
? fixtures
: { importMeta: fixtures };
const filename = path.basename(new URL(importMeta.url).pathname);
if (filename !== "format.test.js") {
throw new Error(`Format test should run in file named 'format.test.js'.`);
}
const dirname = path.dirname(url.fileURLToPath(importMeta.url));
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
dirname,
"misc/parser-inference",
);
// `IS_ERROR_TESTS` mean to watch errors like:
// - syntax parser hasn't supported yet
// - syntax errors that should throws
const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors");
if (IS_ERROR_TESTS) {
options = { errors: true, ...options };
}
if (IS_PARSER_INFERENCE_TESTS) {
parsers = [undefined];
}
snippets = snippets.map((test, index) => {
test = typeof test === "string" ? { code: test } : test;
if (typeof test.code !== "string") {
throw Object.assign(new Error("Invalid test"), { test });
}
return {
...test,
name: `snippet: ${test.name || `#${index}`}`,
};
});
const files = fs
.readdirSync(dirname, { withFileTypes: true })
.map((file) => {
const basename = file.name;
const filename = path.join(dirname, basename);
if (
path.extname(basename) === ".snap" ||
!file.isFile() ||
basename[0] === "." ||
basename === "format.test.js" ||
// VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
basename === "debug.log"
) {
return;
}
const text = fs.readFileSync(filename, "utf8");
return {
name: basename,
filename,
code: text,
};
})
.filter(Boolean);
const [parser] = parsers;
const allParsers = [...parsers];
const stringifiedOptions = stringifyOptionsForTitle(options);
for (const { name, filename, code, output } of [...files, ...snippets]) {
const title = `${name}${
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
}`;
describe(title, () => {
const formatOptions = {
printWidth: 80,
...options,
filepath: filename,
parser,
};
const shouldThrowOnMainParserFormat = shouldThrowOnFormat(
name,
formatOptions,
);
let mainParserFormatResult;
if (shouldThrowOnMainParserFormat) {
mainParserFormatResult = { options: formatOptions, error: true };
} else {
beforeAll(async () => {
mainParserFormatResult = await format(code, formatOptions);
});
}
for (const currentParser of allParsers) {
const testTitle =
shouldThrowOnMainParserFormat ||
formatOptions.parser !== currentParser
? `[${currentParser}] format`
: "format";
test(testTitle, async () => {
await runTest({
parsers,
name,
filename,
code,
output,
parser: currentParser,
mainParserFormatResult,
mainParserFormatOptions: formatOptions,
});
});
}
});
}
}
export default runFormatTest;