Answer the review

This commit is contained in:
Chris Tate
2026-11-12 00:53:26 -06:00
parent e65644cdac
commit c4e63e280b
9 changed files with 119 additions and 32 deletions
+2 -2
View File
@@ -34,7 +34,7 @@ $ node --test --test-reporter=@scriptc/experimental/dual-run/reporter --test-rep
- A `✔` line means every test in the file behaves the same in both runs.
- A `✖` line that names a test and shows a diff means the compiled binary's output differs from Node.js, and the diff starts at the line where it does.
- A `✖` line with test counts means Node.js reported a failed test in the file. The failing tests and their messages follow, so the report explains the exit code on its own.
- A `✖` line with test counts means Node.js reported a failed test in the file. The failing tests and their messages follow, so the report explains the exit code on its own. A test declared in a module the file imports counts toward the file Node.js ran, so its failure appears on that file's line and the imported module is not compared as a file of its own.
- A `✖` line without counts is a file Node.js itself could not run, for example because its TypeScript loader rejects the file's syntax; the failure Node.js reported follows, and the file is still compared.
- An `ℹ` line marks a file whose output differed between two Node.js runs. The reporter does not blame the binary for that, and the comparison does not fail the run.
@@ -69,7 +69,7 @@ Both runs render a Node.js spec report, and the two reports are compared as text
Everything else must match: test names, pass, fail, skip and todo results, assertion messages, the failing-tests section, and the program's own stdout and stderr.
Both lanes run with `TZ=UTC` and `LANG=C`, so dates and collation do not depend on the machine. The Node.js lane also runs with `--experimental-transform-types`, which a compiled program does not need, and with `--disallow-code-generation-from-strings`, the same refusal compiled programs apply to `eval` and `Function`. Node's warnings about the transform flag and about a `.ts` file in a package that does not set `"type": "module"` are turned off, because they carry content the binary never prints, the process id included.
Both lanes run with `TZ=UTC` and `LANG=C`, so dates and collation do not depend on the machine. They also run without `NODE_TEST_CONTEXT`, which `node --test` sets for each test file it starts and which would make the Node.js lane answer with binary test events instead of the spec report. The Node.js lane also runs with `--experimental-transform-types`, which a compiled program does not need, and with `--disallow-code-generation-from-strings`, the same refusal compiled programs apply to `eval` and `Function`. Node's warnings about the transform flag and about a `.ts` file in a package that does not set `"type": "module"` are turned off, because they carry content the binary never prints, the process id included.
## API
+10
View File
@@ -108,11 +108,21 @@ function resolveScriptcBin({ cwd = process.cwd(), env = process.env } = {}) {
);
}
/** Variables that would make a lane behave as a node:test child rather than
* as a plain `node <file>` run. Node's test runner sets NODE_TEST_CONTEXT for
* every test file it starts, and node:test then writes binary test events
* instead of the spec report; a compiled program never does that, so a lane
* that inherited the variable would differ for reasons the program does not
* control. `dualRun` is often called from inside a test, so both lanes drop
* it even when the caller names it. */
const LANE_ENV_DROPPED = ["NODE_TEST_CONTEXT"];
/** The merged environment both lanes run with. The fixed variables win over
* the caller's, since a diff that depends on the machine's locale or time
* zone would not be about the program. */
function laneEnv(env) {
const merged = { ...process.env, ...env, ...BASE_ENV };
for (const key of LANE_ENV_DROPPED) delete merged[key];
for (const key of Object.keys(merged)) if (merged[key] === undefined) delete merged[key];
return merged;
}
+3 -1
View File
@@ -12,7 +12,9 @@ export interface DualRunOptions {
/** Written to both lanes' standard input, which is closed afterwards. */
stdin?: string | Uint8Array;
/** Added to both lanes' environment. The environment always sets TZ=UTC
* and LANG=C. An entry left undefined removes the variable. */
* and LANG=C, and never carries NODE_TEST_CONTEXT, which would make a
* node:test program write binary test events instead of its report. An
* entry left undefined removes the variable. */
env?: Record<string, string | undefined>;
/** Working directory of both lanes and of the build. Defaults to
* process.cwd(). */
+12 -3
View File
@@ -31,6 +31,15 @@ import {
* and each comparison is one cached build plus two short runs. */
const CONCURRENCY = 4;
/** The file Node ran that a result belongs to. A test declared in a module
* the entry imports reports that module as `file`, with `entryFile` naming
* the file Node started; keying by the entry keeps one line per file Node
* ran and keeps the failure with the file that ran it. A file Node could not
* load reports no `entryFile`, and its own path is the entry. */
function entryFileOf(data) {
return typeof data.entryFile === "string" ? data.entryFile : data.file;
}
/** The report stream is whatever Node was told to use; the reporter only
* yields text. */
export default async function* reporter(source) {
@@ -62,13 +71,13 @@ export default async function* reporter(source) {
// A suite reports its children's failure again as its own; the leaf
// test is the one with the message.
if (typeof data?.file !== "string" || data.details?.type === "suite") continue;
stateFor(data.file).failures.push(data);
stateFor(entryFileOf(data)).failures.push(data);
continue;
}
if (event.type !== "test:summary") continue;
const file = data?.file;
// The run's own summary has no file; every file's has one.
if (typeof file !== "string") continue;
if (typeof data?.file !== "string") continue;
const file = entryFileOf(data);
stateFor(file).summary = data;
queue(file);
// Yield results that are already in, so a long run reports as it goes.
+2 -2
View File
@@ -651,7 +651,7 @@
"tests/harness/decl-boundary.test.ts": 128069,
"tests/harness/dgram.test.ts": 5714,
"tests/harness/diagnostics.test.ts": 97554,
"tests/harness/dual-run.test.ts": 9000,
"tests/harness/dual-run.test.ts": 13400,
"tests/harness/dyncheck.test.ts": 64478,
"tests/harness/error-accessor-reflection.test.ts": 2615,
"tests/harness/error-cache-sweep.test.ts": 2947,
@@ -1447,7 +1447,7 @@
"tests/harness/decl-boundary.test.ts": 582885,
"tests/harness/dgram.test.ts": 17971,
"tests/harness/diagnostics.test.ts": 82530,
"tests/harness/dual-run.test.ts": 13900,
"tests/harness/dual-run.test.ts": 18400,
"tests/harness/dyncheck.test.ts": 142435,
"tests/harness/error-accessor-reflection.test.ts": 3504,
"tests/harness/errors.test.ts": 261554,
+14
View File
@@ -0,0 +1,14 @@
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { dualRun } from "@scriptc/experimental/dual-run";
// A node:test file that compares another file from inside the runner. The
// process the runner starts carries NODE_TEST_CONTEXT, which must not reach
// either lane: node:test would answer with binary test events instead of the
// spec report, and a compiled program never does that.
test("dualRun inside the runner compares the file", async () => {
const result = await dualRun(fileURLToPath(new URL("./passing.test.mts", import.meta.url)), {
cwd: process.cwd(),
});
if (!result.equal) throw new Error(result.diff);
});
+11
View File
@@ -0,0 +1,11 @@
import { test } from "node:test";
// Declared in a module the entry imports. Node reports the failure with the
// module as `file` and the file it ran as `entryFile`, so the reporter must
// keep the failure on the entry's line instead of comparing this module as
// a file of its own.
test("helper fails", () => {
throw new Error("from the helper");
});
test("helper passes", () => {});
+4
View File
@@ -0,0 +1,4 @@
import { test } from "node:test";
import "./helper-failure.mjs";
test("entry passes", () => {});
+61 -24
View File
@@ -27,6 +27,39 @@ function cliEnv(env: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv {
return { ...process.env, SCRIPTC_BIN: cli, ...env };
}
/** Runs files through `node --test`, optionally with the dual-run reporter,
* the way a CI job would, and returns what it printed and its exit code. */
function runNodeTest(
files: string[],
{ withReporter }: { withReporter: boolean },
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
const args = withReporter
? ["--test", `--test-reporter=${reporter}`, "--test-reporter-destination=stdout", ...files]
: ["--test", ...files];
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, args, {
cwd: repoRoot,
env: cliEnv(),
stdio: ["ignore", "pipe", "pipe"],
});
const out: Buffer[] = [];
const err: Buffer[] = [];
child.stdout.on("data", (c: Buffer) => out.push(c));
child.stderr.on("data", (c: Buffer) => err.push(c));
child.on("close", (code, signal) => {
if (signal) {
reject(new Error(`node --test died to ${signal}\n${Buffer.concat(err)}`));
return;
}
resolve({
stdout: Buffer.concat(out).toString("utf8"),
stderr: Buffer.concat(err).toString("utf8"),
exitCode: code ?? 0,
});
});
});
}
describe("dualRun", () => {
test("a passing program matches its binary", async () => {
const result = await dualRun(join(fixturesDir, "passing.test.mts"), {
@@ -83,6 +116,19 @@ describe("dualRun", () => {
expect(error!.entry).toBe(entry);
expect(error!.stderr).toContain("SC2020");
}, 180_000);
test("a call from inside a node:test run compares the file", async () => {
// The runner starts its test files with NODE_TEST_CONTEXT set, where
// node:test answers with binary test events instead of the spec report;
// the lanes must not inherit it, or every call made from a test would
// report a divergence that is not the program's.
const run = await runNodeTest([join(fixturesDir, "dual-run-in-test.test.mts")], {
withReporter: false,
});
expect(run.stderr).toBe("");
expect(run.stdout).toContain("✔ dualRun inside the runner compares the file");
expect(run.exitCode).toBe(0);
}, 180_000);
});
describe("dual-run reporter", () => {
@@ -93,30 +139,7 @@ describe("dual-run reporter", () => {
"diverges.test.mts",
"load-failure.mts",
].map((name) => join(fixturesDir, name));
const run = await new Promise<{ stdout: string; stderr: string; exitCode: number }>(
(resolve, reject) => {
const child = spawn(
process.execPath,
["--test", `--test-reporter=${reporter}`, "--test-reporter-destination=stdout", ...files],
{ cwd: repoRoot, env: cliEnv(), stdio: ["ignore", "pipe", "pipe"] },
);
const out: Buffer[] = [];
const err: Buffer[] = [];
child.stdout.on("data", (c: Buffer) => out.push(c));
child.stderr.on("data", (c: Buffer) => err.push(c));
child.on("close", (code, signal) => {
if (signal) {
reject(new Error(`reporter run died to ${signal}\n${Buffer.concat(err)}`));
return;
}
resolve({
stdout: Buffer.concat(out).toString("utf8"),
stderr: Buffer.concat(err).toString("utf8"),
exitCode: code ?? 0,
});
});
},
);
const run = await runNodeTest(files, { withReporter: true });
const lines = run.stdout.split("\n");
const lineFor = (name: string) =>
lines.find((line) => line.includes(`tests/fixtures/dual-run/${name}`));
@@ -133,4 +156,18 @@ describe("dual-run reporter", () => {
expect(run.stderr).toBe("");
expect(run.exitCode).toBe(1);
}, 180_000);
test("a failure declared in an imported module stays on the entry's line", async () => {
const entry = join(fixturesDir, "imports-helper.test.mts");
const run = await runNodeTest([entry], { withReporter: true });
// Node counts the imported module's tests in the entry it ran, and its
// failure names the module as the declaring file; the report keeps one
// line for the entry and does not compare the module as a file of its
// own.
expect(run.stdout).toMatch(/^✖ .*imports-helper\.test\.mts \(2 passed, 1 failed\)$/m);
expect(run.stdout).toContain("✖ helper fails: from the helper");
expect(run.stdout).not.toContain("helper-failure.mjs");
expect(run.stderr).toBe("");
expect(run.exitCode).toBe(1);
}, 180_000);
});