This repository was archived by the owner on Sep 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
executable file
·136 lines (131 loc) · 3.74 KB
/
test.mjs
File metadata and controls
executable file
·136 lines (131 loc) · 3.74 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
#!/usr/bin/env node
//@ts-check
/**
* https://nodejs.org/api/test.html#test-runner-execution-model
*/
import { execSync } from "node:child_process";
import { readFile, readdir, stat } from "node:fs/promises";
import { fileURLToPath, pathToFileURL } from "node:url";
import { program } from "commander";
import mm from "micromatch";
/** @type {{name: string, version: string}} */
const { name, version } = JSON.parse(
await readFile(new URL("./package.json", import.meta.url), "utf-8"),
);
program
.name(name)
.version(version)
.usage("[options] -- [directoriesOrPatterns...]")
.argument(
"[directoriesOrPatterns...]",
"Directories or micromatch patterns to match test files",
)
.option("-e, --exclude <patterns...>", "Patterns to exclude files")
.option(
"-c, --coverage",
[
"Enables --experimental-test-coverage. This is for human readability.",
"https://nodejs.org/api/cli.html#--experimental-test-coverage",
"Use NODE_V8_COVERAGE if you only need coverage data.",
"https://nodejs.org/api/cli.html#node_v8_coveragedir",
].join("\n"),
)
.option(
"--nocoverage",
'Overwrites NODE_V8_COVERAGE with "" (for internal testing)',
)
.parse();
const rootDir = pathToFileURL(process.cwd().replace(/\/*$/, "/"));
/** @param {URL} file */
const relativePath = (file) => file.pathname.slice(rootDir.pathname.length);
/** @type {Array<string>} */
const patterns = [];
{
const patternsOrDirectories = program.args;
const basePatterns = [
"**/*.test.*(m)+(j|t)s*(x)",
"**/test/**/*.*(m)+(j|t)s*(x)",
];
if (patternsOrDirectories.length === 0) {
patterns.push(...basePatterns);
} else {
/** @param {URL} file */
const statOrNull = async (file) => await stat(file).catch(() => null);
for (let patternOrDirectory of patternsOrDirectories) {
patternOrDirectory = patternOrDirectory.replace(/\/*$/, "");
const stats = await statOrNull(new URL(patternOrDirectory, rootDir));
if (stats?.isDirectory()) {
patterns.push(...basePatterns.map((p) => `${patternOrDirectory}/${p}`));
} else {
patterns.push(patternOrDirectory);
}
}
}
}
const {
/** @type {Array<string>} */
exclude: excludePatterns = [],
/** @type {boolean} */
coverage = false,
/** @type {boolean} */
nocoverage = false,
} = program.opts();
if (nocoverage) {
process.env.NODE_V8_COVERAGE = "";
}
const files = [];
{
excludePatterns.push("**/node_modules", "**/.*");
/** @param {URL} file */
const isExternalFile = (file) => !file.pathname.startsWith(rootDir.pathname);
/** @param {URL} file */
const isExcludedFile = (file) => {
if (isExternalFile(file)) {
return true;
}
return mm.isMatch(relativePath(file), excludePatterns, { dot: true });
};
/** @param {URL} file */
const isTestFile = (file) => {
if (isExternalFile(file)) {
return false;
}
return mm.isMatch(relativePath(file), patterns, { dot: false });
};
/** @param {URL} file */
const listFiles = async function* (file) {
if (isExcludedFile(file)) {
return;
}
const stats = await stat(file);
if (stats.isDirectory()) {
const dirUrl = new URL(file);
if (!dirUrl.pathname.endsWith("/")) {
dirUrl.pathname += "/";
}
for (const fileName of await readdir(dirUrl)) {
yield* listFiles(new URL(fileName, dirUrl));
}
} else if (stats.isFile() && isTestFile(file)) {
yield file;
}
};
for await (const file of listFiles(rootDir)) {
files.push(file);
}
}
{
// --import pattern
const registerFile = new URL("./register.mjs", import.meta.url);
let command = "node";
command += ` --import=${registerFile}`;
if (coverage) {
command += " --experimental-test-coverage";
}
command += " --enable-source-maps";
command += " --test";
for (const file of files) {
command += ` ${fileURLToPath(file)}`;
}
execSync(command, { cwd: rootDir, stdio: "inherit" });
}