-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtests.ts
More file actions
56 lines (45 loc) · 1.46 KB
/
tests.ts
File metadata and controls
56 lines (45 loc) · 1.46 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
import assert from "node:assert";
import fs from "node:fs";
import path from "node:path";
import { spawnTest } from "./child_process.js";
assert(
typeof import.meta.dirname === "string",
"Expecting a recent Node.js runtime API version"
);
const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");
export function listDirectoryEntries(dir: string) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const directories: string[] = [];
const files: string[] = [];
for (const entry of entries) {
if (entry.isDirectory()) {
directories.push(entry.name);
} else if (entry.isFile() && entry.name.endsWith(".js")) {
files.push(entry.name);
}
}
directories.sort();
files.sort();
return { directories, files };
}
export function runFileInSubprocess(cwd: string, filePath: string): void {
const { status, signal, stdout, stderr } = spawnTest(filePath, {
cwd,
nodeFlags: ["--expose-gc"],
});
if (stdout) process.stdout.write(stdout);
if (status === 0) return;
const reason =
status !== null ? `exit code ${status}` : `signal ${signal ?? "unknown"}`;
const trimmedStderr = stderr.trim();
const stderrSuffix = trimmedStderr
? `\n--- stderr ---\n${trimmedStderr}\n--- end stderr ---`
: "";
throw new Error(
`Test file ${path.relative(
TESTS_ROOT_PATH,
path.join(cwd, filePath)
)} failed (${reason})${stderrSuffix}`
);
}