Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 3 additions & 71 deletions implementors/node/run-tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { spawn } from "node:child_process";
import { promises as fs } from "node:fs";
import path from "node:path";
import { test, type TestContext } from "node:test";

import { listDirectoryEntries, runFileInSubprocess } from "./tests.ts";

const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");

Expand All @@ -19,79 +19,11 @@ const LOAD_ADDON_MODULE_PATH = path.join(
"load-addon.js"
);

async function listDirectoryEntries(dir: string) {
const entries = await fs.readdir(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 };
}

function runFileInSubprocess(cwd: string, filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[
// Using file scheme prefix when to enable imports on Windows
"--import",
"file://" + ASSERT_MODULE_PATH,
"--import",
"file://" + LOAD_ADDON_MODULE_PATH,
filePath,
],
{ cwd }
);

let stderrOutput = "";
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk) => {
stderrOutput += chunk;
});

child.stdout.pipe(process.stdout);

child.on("error", reject);

child.on("close", (code, signal) => {
if (code === 0) {
resolve();
return;
}

const reason =
code !== null ? `exit code ${code}` : `signal ${signal ?? "unknown"}`;
const trimmedStderr = stderrOutput.trim();
const stderrSuffix = trimmedStderr
? `\n--- stderr ---\n${trimmedStderr}\n--- end stderr ---`
: "";
reject(
new Error(
`Test file ${path.relative(
TESTS_ROOT_PATH,
filePath
)} failed (${reason})${stderrSuffix}`
)
);
});
});
}

async function populateSuite(
testContext: TestContext,
dir: string
): Promise<void> {
const { directories, files } = await listDirectoryEntries(dir);
const { directories, files } = listDirectoryEntries(dir);

for (const file of files) {
await testContext.test(file, () => runFileInSubprocess(dir, file));
Expand Down
95 changes: 95 additions & 0 deletions implementors/node/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import assert from "node:assert";
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

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");
const ASSERT_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"assert.js"
);
const LOAD_ADDON_MODULE_PATH = path.join(
ROOT_PATH,
"implementors",
"node",
"load-addon.js"
);

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
): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[
// Using file scheme prefix when to enable imports on Windows
"--import",
"file://" + ASSERT_MODULE_PATH,
"--import",
"file://" + LOAD_ADDON_MODULE_PATH,
filePath,
],
{ cwd }
);

let stderrOutput = "";
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk) => {
stderrOutput += chunk;
});

child.stdout.pipe(process.stdout);

child.on("error", reject);

child.on("close", (code, signal) => {
if (code === 0) {
resolve();
return;
}

const reason =
code !== null ? `exit code ${code}` : `signal ${signal ?? "unknown"}`;
const trimmedStderr = stderrOutput.trim();
const stderrSuffix = trimmedStderr
? `\n--- stderr ---\n${trimmedStderr}\n--- end stderr ---`
: "";
reject(
new Error(
`Test file ${path.relative(
TESTS_ROOT_PATH,
filePath
)} failed (${reason})${stderrSuffix}`
)
);
});
});
}