Files
reverse-engineering-anything/scripts/ci/test-group.mjs
T
morluto 062b3835f9 ci: trace script modules and built-server consumers in PR test selection, and balance selected shards (#11516)
* ci: trace shared script modules to the tests that import them

A pull request that changed any scripts/lib module ran the complete
suite. Shared script modules now pass a change through to the tests,
verifiers and other shared modules that import them, like test helpers.
Another consumer (a build or test-runner script, root configuration,
package metadata, a hook or a workflow) that imports the module or names
it by file name still selects the complete suite.

The tracing core is exported as traceAffectedTests so a bounded local
gate can reuse it.

Fixes #11381

* ci: follow verifier source loads and named verifiers when tracing script modules

- Module URLs (new URL("./x.mts", import.meta.url), as loaded by
  importVerifierSource) are import edges.
- A test that names a verifier or shared module the change reaches is
  selected, not only one that names a changed path.
- A changed shared module that no test reaches runs the complete suite
  even when other changes select tests.

* ci: select tests that start the built server beside a changed presentation owner (#11519)

* ci: select tests that start the built server beside a changed presentation owner

A test that starts the built CLI or MCP server reaches server, contract,
entrypoint and CLI modules through the build, not an import, so a change
there selected none of them (#11146 failed on main this way). Such a
test is now selected when it, or a helper it imports, starts
scripts/rea.mjs or dist/, and it imports a module from the changed
owner's directory.

Fixes #11366

* ci: recognise only the repository's built entry points as runtime launchers

A package subpath such as ajv/dist/2020.js matched the dist/ pattern, so
in-process tests importing Ajv counted as built-runtime launchers.

* ci: balance selected test shards by recorded duration (#11522)

A selected PR run split its files across groups round-robin, so a few
slow files could land together. The plan now records the selected files
and a shard count (version 3), and each job runs one --shard of the
selection through the exact-test runner with the timing history main
already publishes, so the CI sequencer balances shards by per-file
duration (the median for files without a timing). Without history,
Vitest's own deterministic sharding applies.

Fixes #11382
2026-11-16 06:14:14 +08:00

84 lines
2.6 KiB
JavaScript

import { spawn } from "node:child_process";
import { resolve } from "node:path";
import {
BUILT_TEST_PROJECTS,
SOURCE_TEST_PROJECTS,
} from "../lib/test-projects.mjs";
import { parseTestSelection, testShards } from "./test-selection.mjs";
const [group, ...arguments_] = process.argv.slice(2);
// The group owns project and file selection; callers can request reporters
// or (for project groups) shards.
if (
arguments_.some((argument) => /^--(?:project|exclude)(?:=|$)/u.test(argument))
)
throw new Error("Test groups cannot override their project inventory");
let runner;
let environment = process.env;
if (group === "selected") {
// `selected <k/n>` runs shard k of the plan's selected test files, by exact
// identity, in whichever projects own them. With REA_CI_TEST_HISTORY the CI
// sequencer balances the shards by main's recorded per-file durations.
const [shard, ...rest] = arguments_;
const plan = parseTestSelection(process.env.CI_TEST_SELECTION ?? "");
if (plan.mode !== "selected" || !testShards("fast", plan).includes(shard))
throw new Error(`No selected test group ${shard ?? ""} in this plan`);
if (rest.some((argument) => /^--shard(?:=|$)/u.test(argument)))
throw new Error("Selected test groups are already sharded");
runner = [
resolve("scripts/run-exact-tests.mjs"),
"run",
`--shard=${shard}`,
...rest,
...plan.files,
];
// Each shard writes its own blob report for the merge.
environment = {
...process.env,
VITEST_BLOB_LABEL: `selected-${shard.split("/")[0]}`,
};
} else {
const projects = {
source: SOURCE_TEST_PROJECTS,
built: BUILT_TEST_PROJECTS,
}[group];
if (!projects) throw new Error("Expected source, built or selected tests");
runner = [
resolve("node_modules/vitest/vitest.mjs"),
"run",
...projects.flatMap(({ name }) => ["--project", name]),
...arguments_,
];
}
const child = spawn(
process.execPath,
[
// Reuse the development runner's POSIX process-group owner. Windows
// source tests must remain usable without the built native Job Object.
...(process.platform === "win32"
? []
: [resolve("scripts/run-exclusive.mjs"), "--unlocked", process.execPath]),
...runner,
],
{ stdio: "inherit", env: environment },
);
let shutdownSignal;
for (const signal of ["SIGINT", "SIGTERM"])
process.on(signal, () => {
shutdownSignal ??= signal;
child.kill(signal);
});
child.once("error", (error) => {
console.error(error.message);
process.exitCode = 1;
});
child.once("close", (code) => {
process.exitCode =
shutdownSignal === "SIGINT"
? 130
: shutdownSignal === "SIGTERM"
? 143
: (code ?? 1);
});