|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { test, type TestContext } from "node:test"; |
| 5 | + |
| 6 | +const ROOT_PATH = path.resolve(import.meta.dirname, "..", ".."); |
| 7 | +const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests"); |
| 8 | +const ASSERT_MODULE_PATH = path.join( |
| 9 | + ROOT_PATH, |
| 10 | + "implementors", |
| 11 | + "node", |
| 12 | + "assert.js" |
| 13 | +); |
| 14 | + |
| 15 | +async function listDirectoryEntries(dir: string) { |
| 16 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 17 | + const directories: string[] = []; |
| 18 | + const files: string[] = []; |
| 19 | + |
| 20 | + for (const entry of entries) { |
| 21 | + if (entry.isDirectory()) { |
| 22 | + directories.push(entry.name); |
| 23 | + } else if (entry.isFile() && entry.name.endsWith(".js")) { |
| 24 | + files.push(entry.name); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + directories.sort(); |
| 29 | + files.sort(); |
| 30 | + |
| 31 | + return { directories, files }; |
| 32 | +} |
| 33 | + |
| 34 | +function runFileInSubprocess(filePath: string): Promise<void> { |
| 35 | + return new Promise((resolve, reject) => { |
| 36 | + const child = spawn(process.execPath, [ |
| 37 | + "--import", |
| 38 | + ASSERT_MODULE_PATH, |
| 39 | + filePath, |
| 40 | + ]); |
| 41 | + |
| 42 | + let stderrOutput = ""; |
| 43 | + child.stderr.setEncoding("utf8"); |
| 44 | + child.stderr.on("data", (chunk) => { |
| 45 | + stderrOutput += chunk; |
| 46 | + }); |
| 47 | + |
| 48 | + child.stdout.pipe(process.stdout); |
| 49 | + |
| 50 | + child.on("error", reject); |
| 51 | + |
| 52 | + child.on("close", (code, signal) => { |
| 53 | + if (code === 0) { |
| 54 | + resolve(); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const reason = |
| 59 | + code !== null ? `exit code ${code}` : `signal ${signal ?? "unknown"}`; |
| 60 | + const trimmedStderr = stderrOutput.trim(); |
| 61 | + const stderrSuffix = trimmedStderr |
| 62 | + ? `\n--- stderr ---\n${trimmedStderr}\n--- end stderr ---` |
| 63 | + : ""; |
| 64 | + reject( |
| 65 | + new Error( |
| 66 | + `Test file ${path.relative( |
| 67 | + TESTS_ROOT_PATH, |
| 68 | + filePath |
| 69 | + )} failed (${reason})${stderrSuffix}` |
| 70 | + ) |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +async function populateSuite( |
| 77 | + testContext: TestContext, |
| 78 | + dir: string |
| 79 | +): Promise<void> { |
| 80 | + const { directories, files } = await listDirectoryEntries(dir); |
| 81 | + |
| 82 | + for (const file of files) { |
| 83 | + const filePath = path.join(dir, file); |
| 84 | + await testContext.test(file, () => runFileInSubprocess(filePath)); |
| 85 | + } |
| 86 | + |
| 87 | + for (const directory of directories) { |
| 88 | + await testContext.test(directory, async (subTest) => { |
| 89 | + await populateSuite(subTest, path.join(dir, directory)); |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +test("harness", async (t) => { |
| 95 | + await populateSuite(t, path.join(TESTS_ROOT_PATH, "harness")); |
| 96 | +}); |
| 97 | + |
| 98 | +test("js-native-api", async (t) => { |
| 99 | + await populateSuite(t, path.join(TESTS_ROOT_PATH, "js-native-api")); |
| 100 | +}); |
| 101 | + |
| 102 | +test("node-api", async (t) => { |
| 103 | + await populateSuite(t, path.join(TESTS_ROOT_PATH, "node-api")); |
| 104 | +}); |
0 commit comments