Use Node.js APIs for test runner

This commit is contained in:
Ashcon Partovi
2023-04-28 09:07:48 -07:00
parent 6cf4cabab1
commit 36df170348
3 changed files with 28 additions and 17 deletions

View File

@@ -37,4 +37,4 @@ jobs:
bun install
bun install --cwd test/bun.js
bun install --cwd test/bun.js/third-party/body-parser-test
bun run scripts/bun-test.ts test/ --isolated
bunx ts-node --esm scripts/bun-test.ts --isolated test/

View File

@@ -1,5 +1,6 @@
import { readdirSync, writeSync, fsyncSync, appendFileSync } from "node:fs";
import { join, basename } from "node:path";
import { readdirSync, writeSync, fsyncSync, appendFileSync } from "node:fs";
import { spawn } from "node:child_process";
export { parseTest, runTest, formatTest };
@@ -206,7 +207,7 @@ function parseInfo(line: string): TestInfo | undefined {
return {
name,
version,
revision: Bun.revision.startsWith(revision) ? Bun.revision : revision,
revision: "Bun" in globalThis && Bun.revision.startsWith(revision) ? Bun.revision : revision,
};
}
@@ -393,24 +394,31 @@ async function* runTest(options: RunTestOptions): AsyncGenerator<RunTestResult,
}
}
const runSingleTest = async (args: string[]) => {
const runner = Bun.spawn({
const runner = spawn("bun", ["test", ...args], {
cwd,
cmd: ["bun", "test", ...args],
env: {
...process.env,
"FORCE_COLOR": "1",
},
stdout: "pipe",
stderr: "pipe",
stdio: "pipe",
});
let stderr = "";
let stdout = "";
const exitCode = await new Promise<number | null>(resolve => {
runner.stdout.on("data", (data: Buffer) => {
stdout += data.toString("utf-8");
});
runner.stderr.on("data", (data: Buffer) => {
stderr += data.toString("utf-8");
});
runner.on("error", ({ name, message }) => {
stderr += `${name}: ${message}`;
resolve(null);
});
runner.on("exit", exitCode => {
resolve(exitCode);
});
});
const exitCode = await Promise.race([
runner.exited,
Bun.sleep(timeout).then(() => {
runner.kill();
return null;
}),
]);
const [stdout, stderr] = await Promise.all([readStream(runner.stdout), readStream(runner.stderr)]);
const lines = stderr.split("\n").map(stripAnsi);
const result = parseTest(lines, { cwd, paths });
return {
@@ -539,7 +547,7 @@ ${tests}`;
}
function printTest(result: RunTestResult): void {
const isAction = !!process.env["GITHUB_ACTIONS"] || true;
const isAction = !!process.env["GITHUB_ACTIONS"];
const isGroup = result.files.length === 1;
if (isGroup) {
const { file, status } = result.files[0];
@@ -628,6 +636,6 @@ async function main() {
process.exit(0);
}
if (import.meta.main) {
if (import.meta.main || import.meta.url === `file://${process.argv[1]}`) {
await main();
}

3
scripts/package.json Normal file
View File

@@ -0,0 +1,3 @@
{
"type": "module"
}