Skip to content

Commit 566435d

Browse files
committed
Refactor node implementor for reuse
1 parent 25a8cae commit 566435d

2 files changed

Lines changed: 98 additions & 63 deletions

File tree

implementors/node/run-tests.ts

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { spawn } from "node:child_process";
2-
import { promises as fs } from "node:fs";
32
import path from "node:path";
43
import { test, type TestContext } from "node:test";
54

5+
import { listDirectoryEntries, runFileInSubprocess } from "./tests";
6+
67
const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
78
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");
89
const ASSERT_MODULE_PATH = path.join(
@@ -12,72 +13,11 @@ const ASSERT_MODULE_PATH = path.join(
1213
"assert.js"
1314
);
1415

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-
7616
async function populateSuite(
7717
testContext: TestContext,
7818
dir: string
7919
): Promise<void> {
80-
const { directories, files } = await listDirectoryEntries(dir);
20+
const { directories, files } = listDirectoryEntries(dir);
8121

8222
for (const file of files) {
8323
const filePath = path.join(dir, file);

implementors/node/tests.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import assert from "node:assert";
2+
import { spawn } from "node:child_process";
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
6+
assert(
7+
typeof import.meta.dirname === "string",
8+
"Expecting a recent Node.js runtime API version"
9+
);
10+
11+
const ROOT_PATH = path.resolve(import.meta.dirname, "..", "..");
12+
const TESTS_ROOT_PATH = path.join(ROOT_PATH, "tests");
13+
const ASSERT_MODULE_PATH = path.join(
14+
ROOT_PATH,
15+
"implementors",
16+
"node",
17+
"assert.js"
18+
);
19+
const LOAD_ADDON_MODULE_PATH = path.join(
20+
ROOT_PATH,
21+
"implementors",
22+
"node",
23+
"load-addon.js"
24+
);
25+
26+
export function listDirectoryEntries(dir: string) {
27+
const entries = fs.readdirSync(dir, { withFileTypes: true });
28+
const directories: string[] = [];
29+
const files: string[] = [];
30+
31+
for (const entry of entries) {
32+
if (entry.isDirectory()) {
33+
directories.push(entry.name);
34+
} else if (entry.isFile() && entry.name.endsWith(".js")) {
35+
files.push(entry.name);
36+
}
37+
}
38+
39+
directories.sort();
40+
files.sort();
41+
42+
return { directories, files };
43+
}
44+
45+
export function runFileInSubprocess(
46+
cwd: string,
47+
filePath: string
48+
): Promise<void> {
49+
return new Promise((resolve, reject) => {
50+
const child = spawn(
51+
process.execPath,
52+
[
53+
// Using file scheme prefix when to enable imports on Windows
54+
"--import",
55+
"file://" + ASSERT_MODULE_PATH,
56+
"--import",
57+
"file://" + LOAD_ADDON_MODULE_PATH,
58+
filePath,
59+
],
60+
{ cwd }
61+
);
62+
63+
let stderrOutput = "";
64+
child.stderr.setEncoding("utf8");
65+
child.stderr.on("data", (chunk) => {
66+
stderrOutput += chunk;
67+
});
68+
69+
child.stdout.pipe(process.stdout);
70+
71+
child.on("error", reject);
72+
73+
child.on("close", (code, signal) => {
74+
if (code === 0) {
75+
resolve();
76+
return;
77+
}
78+
79+
const reason =
80+
code !== null ? `exit code ${code}` : `signal ${signal ?? "unknown"}`;
81+
const trimmedStderr = stderrOutput.trim();
82+
const stderrSuffix = trimmedStderr
83+
? `\n--- stderr ---\n${trimmedStderr}\n--- end stderr ---`
84+
: "";
85+
reject(
86+
new Error(
87+
`Test file ${path.relative(
88+
TESTS_ROOT_PATH,
89+
filePath
90+
)} failed (${reason})${stderrSuffix}`
91+
)
92+
);
93+
});
94+
});
95+
}

0 commit comments

Comments
 (0)