@zgeoff/rig 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -4
- package/dist/bench/define-bench.d.ts +24 -0
- package/dist/bench/format-benchmark-result.d.ts +3 -0
- package/dist/bench/parse-bench-definition.d.ts +9 -0
- package/dist/bench/pick-median.d.ts +1 -0
- package/dist/bench/run-benchmark.d.ts +7 -0
- package/dist/bench.d.ts +2 -0
- package/dist/browser/collect-descendant-pids.d.ts +2 -0
- package/dist/browser/pick-descendant-stop-step.d.ts +15 -0
- package/dist/browser/pick-listing-read-step.d.ts +10 -0
- package/dist/browser/probe-process.d.ts +1 -1
- package/dist/browser/read-process-table.d.ts +1 -0
- package/dist/browser/stop-descendant-processes.d.ts +10 -0
- package/dist/build-init-scripts.d.ts +1 -1
- package/dist/build-program-environment.d.ts +3 -0
- package/dist/build-program-lane-hooks.d.ts +3 -0
- package/dist/build-test-selection.d.ts +1 -0
- package/dist/collect-bench-files.d.ts +1 -0
- package/dist/collect-eval-files.d.ts +1 -0
- package/dist/collect-unprogrammed-e2e-dirs.d.ts +2 -0
- package/dist/e2e.d.ts +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +83 -3
- package/dist/is-command-name.d.ts +1 -1
- package/dist/pick-e2e-dirs.d.ts +1 -1
- package/dist/probe-workspace-repo.d.ts +1 -0
- package/dist/programs/resolve-program.d.ts +12 -0
- package/dist/programs/resolve-repo-root.d.ts +1 -0
- package/dist/read-cli-args.d.ts +1 -0
- package/dist/run-bench-file.d.ts +1 -0
- package/dist/run-bench.d.ts +10 -0
- package/dist/run-cli.d.ts +3 -1
- package/dist/run-eval.d.ts +8 -0
- package/dist/run-init.d.ts +1 -0
- package/dist/run-lane.d.ts +4 -5
- package/dist/run-program-builds.d.ts +7 -0
- package/dist/run-unit-tests.d.ts +1 -0
- package/dist/test-run-args.d.ts +1 -0
- package/dist/types.d.ts +13 -0
- package/package.json +17 -5
- package/src/bench/define-bench.ts +78 -0
- package/src/bench/format-benchmark-result.ts +11 -0
- package/src/bench/parse-bench-definition.ts +57 -0
- package/src/bench/pick-median.ts +10 -0
- package/src/bench/run-benchmark.ts +38 -0
- package/src/bench.ts +2 -0
- package/src/browser/browser-preload.ts +18 -4
- package/src/browser/collect-descendant-pids.ts +22 -0
- package/src/browser/make-browser-lane-hooks.ts +2 -3
- package/src/browser/pick-descendant-stop-step.ts +40 -0
- package/src/browser/pick-listing-read-step.ts +17 -0
- package/src/browser/probe-host-process.ts +1 -3
- package/src/browser/probe-process.ts +1 -1
- package/src/browser/read-process-table.ts +10 -6
- package/src/browser/stop-descendant-processes.ts +90 -0
- package/src/build-init-scripts.ts +17 -3
- package/src/build-program-environment.ts +20 -0
- package/src/build-program-lane-hooks.ts +24 -0
- package/src/build-test-selection.ts +8 -7
- package/src/build-unit-command.ts +2 -1
- package/src/cli.ts +4 -0
- package/src/collect-bench-files.ts +23 -0
- package/src/collect-eval-files.ts +5 -0
- package/src/collect-unprogrammed-e2e-dirs.ts +21 -0
- package/src/define-rig.ts +65 -2
- package/src/e2e.ts +2 -0
- package/src/index.ts +2 -0
- package/src/is-command-name.ts +1 -1
- package/src/make-repo-lane-hooks.ts +2 -0
- package/src/parse-rig-config.ts +64 -2
- package/src/pick-e2e-dirs.ts +8 -5
- package/src/probe-workspace-repo.ts +15 -0
- package/src/programs/resolve-program.ts +92 -0
- package/src/programs/resolve-repo-root.ts +18 -0
- package/src/read-cli-args.ts +30 -1
- package/src/run-bench-file.ts +25 -0
- package/src/run-bench.ts +132 -0
- package/src/run-cli.ts +16 -1
- package/src/run-eval.ts +74 -0
- package/src/run-init.ts +6 -1
- package/src/run-lane.ts +39 -10
- package/src/run-program-builds.ts +35 -0
- package/src/run-unit-tests.ts +4 -1
- package/src/run-unit.ts +1 -0
- package/src/standard-lanes.ts +1 -1
- package/src/test-run-args.ts +3 -0
- package/src/types.ts +30 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { BenchDefinition } from './define-bench.ts';
|
|
2
|
+
|
|
3
|
+
export type ParsedBenchDefinition =
|
|
4
|
+
| { readonly kind: 'parsed'; readonly definition: BenchDefinition }
|
|
5
|
+
| { readonly kind: 'refused'; readonly reason: string };
|
|
6
|
+
|
|
7
|
+
// A bench file is outside input, so the runner checks that its default export is a `defineBench`
|
|
8
|
+
// result instead of trusting its type.
|
|
9
|
+
export function parseBenchDefinition(value: unknown): ParsedBenchDefinition {
|
|
10
|
+
if (typeof value !== 'object' || value === null) {
|
|
11
|
+
return { kind: 'refused', reason: 'the default export is no defineBench result' };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const name: unknown = Reflect.get(value, 'name');
|
|
15
|
+
const budgetMs: unknown = Reflect.get(value, 'budgetMs');
|
|
16
|
+
const prepare: unknown = Reflect.get(value, 'prepare');
|
|
17
|
+
|
|
18
|
+
if (typeof name !== 'string' || typeof budgetMs !== 'number' || !isFunction(prepare)) {
|
|
19
|
+
return { kind: 'refused', reason: 'the default export is no defineBench result' };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
kind: 'parsed',
|
|
24
|
+
definition: {
|
|
25
|
+
name,
|
|
26
|
+
budgetMs,
|
|
27
|
+
prepare: async () => {
|
|
28
|
+
const prepared: unknown = await prepare();
|
|
29
|
+
|
|
30
|
+
const step: unknown =
|
|
31
|
+
typeof prepared === 'object' && prepared !== null ? Reflect.get(prepared, 'step') : null;
|
|
32
|
+
|
|
33
|
+
const dispose: unknown =
|
|
34
|
+
typeof prepared === 'object' && prepared !== null
|
|
35
|
+
? Reflect.get(prepared, 'dispose')
|
|
36
|
+
: null;
|
|
37
|
+
|
|
38
|
+
if (!isFunction(step) || !isFunction(dispose)) {
|
|
39
|
+
throw new Error(`the benchmark "${name}" prepared no step to measure`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
step: async () => {
|
|
44
|
+
await step();
|
|
45
|
+
},
|
|
46
|
+
dispose: async () => {
|
|
47
|
+
await dispose();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isFunction(value: unknown): value is (...args: unknown[]) => unknown {
|
|
56
|
+
return typeof value === 'function';
|
|
57
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function pickMedian(samples: readonly number[]): number {
|
|
2
|
+
const sorted = samples.toSorted((left, right) => left - right);
|
|
3
|
+
const middle = Math.floor(sorted.length / 2);
|
|
4
|
+
|
|
5
|
+
if (sorted.length % 2 === 1) {
|
|
6
|
+
return sorted[middle] ?? 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return ((sorted[middle - 1] ?? 0) + (sorted[middle] ?? 0)) / 2;
|
|
10
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { BenchDefinition } from './define-bench.ts';
|
|
2
|
+
import { pickMedian } from './pick-median.ts';
|
|
3
|
+
|
|
4
|
+
export const warmUpRuns = 5;
|
|
5
|
+
export const measuredRuns = 20;
|
|
6
|
+
|
|
7
|
+
export interface BenchmarkResult {
|
|
8
|
+
readonly medianMs: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// `prepare` runs once, then `run` warms up before the measured runs, and the median of those is the
|
|
12
|
+
// result. The setup result is disposed of after the measured runs, even when a run throws.
|
|
13
|
+
export async function runBenchmark(
|
|
14
|
+
definition: BenchDefinition,
|
|
15
|
+
now: () => number,
|
|
16
|
+
): Promise<BenchmarkResult> {
|
|
17
|
+
const prepared = await definition.prepare();
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
for (let index = 0; index < warmUpRuns; index += 1) {
|
|
21
|
+
await prepared.step();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const samples: number[] = [];
|
|
25
|
+
|
|
26
|
+
for (let index = 0; index < measuredRuns; index += 1) {
|
|
27
|
+
const start = now();
|
|
28
|
+
|
|
29
|
+
await prepared.step();
|
|
30
|
+
|
|
31
|
+
samples.push(now() - start);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return { medianMs: pickMedian(samples) };
|
|
35
|
+
} finally {
|
|
36
|
+
await prepared.dispose();
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/bench.ts
ADDED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
// A cold Chrome start on a fresh CI runner can run past the test timeout, so the preload starts
|
|
2
|
-
// Chrome before the
|
|
3
|
-
//
|
|
2
|
+
// Chrome before the first test. Bun spawns one Chrome per process, on the options of the first
|
|
3
|
+
// view, and every later view in the process, in every file, reuses it.
|
|
4
4
|
|
|
5
|
+
import { afterAll } from 'bun:test';
|
|
5
6
|
import { hostname } from 'node:os';
|
|
7
|
+
import { systemClock } from '../system-clock.ts';
|
|
6
8
|
import { laneFiles } from './lane-files.ts';
|
|
9
|
+
import { readProcessTable } from './read-process-table.ts';
|
|
7
10
|
import { startBrowser } from './start-browser.ts';
|
|
11
|
+
import { stopDescendantProcesses } from './stop-descendant-processes.ts';
|
|
12
|
+
|
|
13
|
+
// Chrome's helper processes outlive the `bun test` that starts them, and diet then keeps its cgroup
|
|
14
|
+
// for them, so the preload stops them after the last file.
|
|
15
|
+
afterAll(() =>
|
|
16
|
+
stopDescendantProcesses({
|
|
17
|
+
rootPID: process.pid,
|
|
18
|
+
timeoutMs: 5000,
|
|
19
|
+
clock: systemClock,
|
|
20
|
+
readProcessTable,
|
|
21
|
+
signalProcess: process.kill.bind(process),
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
8
24
|
|
|
9
25
|
await startBrowser({
|
|
10
26
|
// The lane starts each test process in the repo's root.
|
|
@@ -12,8 +28,6 @@ await startBrowser({
|
|
|
12
28
|
hostName: hostname(),
|
|
13
29
|
environment: process.env,
|
|
14
30
|
|
|
15
|
-
// `bun test --parallel=1` stops each file's Chrome and loads this module again for the next
|
|
16
|
-
// file. A new Chrome on the last one's profile can start before the last one lets go of it.
|
|
17
31
|
profileName: crypto.randomUUID(),
|
|
18
32
|
readDirNames: laneFiles.readDirNames,
|
|
19
33
|
openView: openChromeView,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ProcessEntry } from './read-process-table.ts';
|
|
2
|
+
|
|
3
|
+
export function collectDescendantPIDs(
|
|
4
|
+
table: readonly ProcessEntry[],
|
|
5
|
+
rootPID: number,
|
|
6
|
+
): readonly number[] {
|
|
7
|
+
const descendants: number[] = [];
|
|
8
|
+
const parents = [rootPID];
|
|
9
|
+
|
|
10
|
+
for (const parentID of parents) {
|
|
11
|
+
const children = table.filter((entry) => entry.parentID === parentID && entry.pid !== rootPID);
|
|
12
|
+
|
|
13
|
+
for (const child of children) {
|
|
14
|
+
if (!descendants.includes(child.pid)) {
|
|
15
|
+
descendants.push(child.pid);
|
|
16
|
+
parents.push(child.pid);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return descendants;
|
|
22
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
2
|
import type { LaneHooks, LaneStartOutcome } from '../lane-hooks.ts';
|
|
3
|
+
import { testRunArgs } from '../test-run-args.ts';
|
|
3
4
|
import type { BrowserRun } from './build-browser-run.ts';
|
|
4
5
|
import type { LaneEnvironments } from './build-lane-environments.ts';
|
|
5
6
|
import { buildLaneEnvironments } from './build-lane-environments.ts';
|
|
@@ -48,8 +49,6 @@ export interface BrowserLaneOptions {
|
|
|
48
49
|
) => Promise<number>;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
// `bun test --parallel=1` isolates each file, so the preload runs again for each one, and the
|
|
52
|
-
// build and the run ID come from here, once per run.
|
|
53
52
|
export function makeBrowserLaneHooks(options: BrowserLaneOptions): LaneHooks {
|
|
54
53
|
const state = { hasRunFolders: false, keepsFolders: false };
|
|
55
54
|
|
|
@@ -190,7 +189,7 @@ async function runBrowserTests(
|
|
|
190
189
|
const testsRecord = await createPendingRecord(options.run.groupsDir, 'tests', options.files);
|
|
191
190
|
|
|
192
191
|
const testsExitCode = await options.runTests(
|
|
193
|
-
['--parallel=1', '--preload', options.preloadPath, ...testArgs],
|
|
192
|
+
['--parallel=1', ...testRunArgs, '--preload', options.preloadPath, ...testArgs],
|
|
194
193
|
environment,
|
|
195
194
|
options.signal,
|
|
196
195
|
testsRecord,
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isZombieState } from './is-zombie-state.ts';
|
|
2
|
+
import type { ProcessEntry } from './read-process-table.ts';
|
|
3
|
+
|
|
4
|
+
export type DescendantStopStep =
|
|
5
|
+
| { readonly kind: 'stopped' }
|
|
6
|
+
| { readonly kind: 'wait' }
|
|
7
|
+
| { readonly kind: 'failed'; readonly running: readonly number[] };
|
|
8
|
+
|
|
9
|
+
export interface DescendantStopState {
|
|
10
|
+
readonly descendants: readonly number[];
|
|
11
|
+
|
|
12
|
+
readonly listing: readonly ProcessEntry[] | null;
|
|
13
|
+
|
|
14
|
+
readonly isPastDeadline: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A killed process stays in its cgroup until its parent or the host's first process reaps it, so
|
|
18
|
+
// the stop waits until each one leaves the listing. A host whose first process never reaps its
|
|
19
|
+
// orphans keeps a zombie forever, so at the deadline only a process that is no zombie fails it.
|
|
20
|
+
export function pickDescendantStopStep(state: DescendantStopState): DescendantStopStep {
|
|
21
|
+
// A failed listing leaves every process's state unknown, so each one counts as running.
|
|
22
|
+
if (state.listing === null) {
|
|
23
|
+
return state.isPastDeadline ? { kind: 'failed', running: state.descendants } : { kind: 'wait' };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const listing = state.listing;
|
|
27
|
+
const listed = state.descendants.flatMap((pid) => listing.filter((entry) => entry.pid === pid));
|
|
28
|
+
|
|
29
|
+
if (listed.length === 0) {
|
|
30
|
+
return { kind: 'stopped' };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!state.isPastDeadline) {
|
|
34
|
+
return { kind: 'wait' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const running = listed.filter((entry) => !isZombieState(entry.state)).map((entry) => entry.pid);
|
|
38
|
+
|
|
39
|
+
return running.length === 0 ? { kind: 'stopped' } : { kind: 'failed', running };
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ProcessEntry } from './read-process-table.ts';
|
|
2
|
+
|
|
3
|
+
export type ListingReadStep =
|
|
4
|
+
| { readonly kind: 'listed'; readonly listing: readonly ProcessEntry[] }
|
|
5
|
+
| { readonly kind: 'retry' }
|
|
6
|
+
| { readonly kind: 'failed' };
|
|
7
|
+
|
|
8
|
+
export function pickListingReadStep(
|
|
9
|
+
listing: readonly ProcessEntry[] | null,
|
|
10
|
+
isPastDeadline: boolean,
|
|
11
|
+
): ListingReadStep {
|
|
12
|
+
if (listing !== null) {
|
|
13
|
+
return { kind: 'listed', listing };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return isPastDeadline ? { kind: 'failed' } : { kind: 'retry' };
|
|
17
|
+
}
|
|
@@ -3,9 +3,7 @@ import { readProcessTable } from './read-process-table.ts';
|
|
|
3
3
|
|
|
4
4
|
export function probeHostProcess(pid: number): boolean {
|
|
5
5
|
return probeProcess(pid, {
|
|
6
|
-
signalProcess: (
|
|
7
|
-
process.kill(target, signal);
|
|
8
|
-
},
|
|
6
|
+
signalProcess: process.kill.bind(process),
|
|
9
7
|
readProcessTable,
|
|
10
8
|
});
|
|
11
9
|
}
|
|
@@ -4,7 +4,7 @@ import type { ProcessEntry } from './read-process-table.ts';
|
|
|
4
4
|
|
|
5
5
|
export interface ProcessProbeDependencies {
|
|
6
6
|
// Throws as `process.kill` does: ESRCH for a missing process, EPERM for another user's.
|
|
7
|
-
readonly signalProcess: (pid: number, signal: number) =>
|
|
7
|
+
readonly signalProcess: (pid: number, signal: number) => boolean;
|
|
8
8
|
|
|
9
9
|
readonly readProcessTable: () => readonly ProcessEntry[] | null;
|
|
10
10
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
export interface ProcessEntry {
|
|
2
2
|
readonly pid: number;
|
|
3
3
|
|
|
4
|
+
readonly parentID: number;
|
|
5
|
+
|
|
4
6
|
readonly groupID: number;
|
|
5
7
|
|
|
6
8
|
readonly state: string;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
// Null when `ps` fails to start or exits nonzero. `ps -A` lists every process, so a nonzero exit
|
|
10
|
-
// is a failed listing, never a sign that a process has exited.
|
|
12
|
+
// is a failed listing, never a sign that a process has exited. It lists itself too, as a child of
|
|
13
|
+
// the reader that has exited by then, so the listing leaves it out.
|
|
11
14
|
export function readProcessTable(): readonly ProcessEntry[] | null {
|
|
12
15
|
const listing = runListing();
|
|
13
16
|
|
|
@@ -19,16 +22,17 @@ export function readProcessTable(): readonly ProcessEntry[] | null {
|
|
|
19
22
|
.toString()
|
|
20
23
|
.split('\n')
|
|
21
24
|
.map((line) => line.trim().split(/\s+/u))
|
|
22
|
-
.flatMap(([pid, groupID, state]) =>
|
|
23
|
-
pid === undefined || groupID === undefined || state === undefined
|
|
25
|
+
.flatMap(([pid, parentID, groupID, state]) =>
|
|
26
|
+
pid === undefined || parentID === undefined || groupID === undefined || state === undefined
|
|
24
27
|
? []
|
|
25
|
-
: [{ pid: Number(pid), groupID: Number(groupID), state }],
|
|
26
|
-
)
|
|
28
|
+
: [{ pid: Number(pid), parentID: Number(parentID), groupID: Number(groupID), state }],
|
|
29
|
+
)
|
|
30
|
+
.filter((entry) => entry.pid !== listing.pid);
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
function runListing() {
|
|
30
34
|
try {
|
|
31
|
-
return Bun.spawnSync(['ps', '-A', '-o', 'pid=,pgid=,stat=']);
|
|
35
|
+
return Bun.spawnSync(['ps', '-A', '-o', 'pid=,ppid=,pgid=,stat=']);
|
|
32
36
|
} catch {
|
|
33
37
|
return null;
|
|
34
38
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
import type { Clock } from '../system-clock.ts';
|
|
3
|
+
import { collectDescendantPIDs } from './collect-descendant-pids.ts';
|
|
4
|
+
import { hasErrorCode } from './has-error-code.ts';
|
|
5
|
+
import { pickDescendantStopStep } from './pick-descendant-stop-step.ts';
|
|
6
|
+
import { pickListingReadStep } from './pick-listing-read-step.ts';
|
|
7
|
+
import type { ProcessEntry } from './read-process-table.ts';
|
|
8
|
+
|
|
9
|
+
export interface DescendantStop {
|
|
10
|
+
readonly rootPID: number;
|
|
11
|
+
|
|
12
|
+
readonly timeoutMs: number;
|
|
13
|
+
|
|
14
|
+
readonly clock: Clock;
|
|
15
|
+
|
|
16
|
+
readonly readProcessTable: () => readonly ProcessEntry[] | null;
|
|
17
|
+
|
|
18
|
+
// Throws as `process.kill` does: ESRCH for a process that has exited.
|
|
19
|
+
readonly signalProcess: (pid: number, signal: NodeJS.Signals) => boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const pollIntervalMs = 50;
|
|
23
|
+
|
|
24
|
+
export async function stopDescendantProcesses(stop: DescendantStop): Promise<void> {
|
|
25
|
+
const deadlineMs = stop.clock.now() + stop.timeoutMs;
|
|
26
|
+
|
|
27
|
+
const table = await readFirstListing(stop, deadlineMs);
|
|
28
|
+
|
|
29
|
+
const descendants = collectDescendantPIDs(table, stop.rootPID);
|
|
30
|
+
|
|
31
|
+
for (const pid of descendants) {
|
|
32
|
+
killProcess(pid, stop);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let isStopped = false;
|
|
36
|
+
|
|
37
|
+
while (!isStopped) {
|
|
38
|
+
const step = pickDescendantStopStep({
|
|
39
|
+
descendants,
|
|
40
|
+
listing: stop.readProcessTable(),
|
|
41
|
+
isPastDeadline: stop.clock.now() >= deadlineMs,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
isStopped = await match(step)
|
|
45
|
+
.with({ kind: 'stopped' }, () => Promise.resolve(true))
|
|
46
|
+
.with({ kind: 'failed' }, (failed) =>
|
|
47
|
+
Promise.reject(
|
|
48
|
+
new Error(
|
|
49
|
+
`the processes ${failed.running.join(', ')} still run ${stop.timeoutMs} ms after SIGKILL`,
|
|
50
|
+
),
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
.with({ kind: 'wait' }, () => stop.clock.sleep(pollIntervalMs).then(() => false))
|
|
54
|
+
.exhaustive();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Only a listing names the processes to stop, so a failed one is read again until the deadline.
|
|
59
|
+
async function readFirstListing(
|
|
60
|
+
stop: DescendantStop,
|
|
61
|
+
deadlineMs: number,
|
|
62
|
+
): Promise<readonly ProcessEntry[]> {
|
|
63
|
+
let listing: readonly ProcessEntry[] | null = null;
|
|
64
|
+
|
|
65
|
+
while (listing === null) {
|
|
66
|
+
const step = pickListingReadStep(stop.readProcessTable(), stop.clock.now() >= deadlineMs);
|
|
67
|
+
|
|
68
|
+
listing = await match(step)
|
|
69
|
+
.with({ kind: 'listed' }, (listed) => Promise.resolve(listed.listing))
|
|
70
|
+
.with({ kind: 'failed' }, () =>
|
|
71
|
+
Promise.reject(
|
|
72
|
+
new Error(`the process listing failed for ${stop.timeoutMs} ms, so no process stopped`),
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
.with({ kind: 'retry' }, () => stop.clock.sleep(pollIntervalMs).then(() => null))
|
|
76
|
+
.exhaustive();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return listing;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function killProcess(pid: number, stop: DescendantStop): void {
|
|
83
|
+
try {
|
|
84
|
+
stop.signalProcess(pid, 'SIGKILL');
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (!hasErrorCode(error, 'ESRCH')) {
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { collectBenchFiles } from './collect-bench-files.ts';
|
|
2
|
+
import { collectEvalFiles } from './collect-eval-files.ts';
|
|
1
3
|
import { pickE2EDirs } from './pick-e2e-dirs.ts';
|
|
2
4
|
import { standardLanes } from './standard-lanes.ts';
|
|
3
5
|
import type { ResolvedLane, RigConfig } from './types.ts';
|
|
@@ -10,26 +12,38 @@ export function buildInitScripts(
|
|
|
10
12
|
oldScripts: PackageScripts,
|
|
11
13
|
config: RigConfig,
|
|
12
14
|
trackedFiles: readonly string[],
|
|
15
|
+
isWorkspaceRepo: boolean,
|
|
13
16
|
): PackageScripts {
|
|
14
17
|
const laneScripts = config.lanes
|
|
15
|
-
.filter((lane) => lane.name !== 'unit' && isLaneWanted(lane, trackedFiles))
|
|
18
|
+
.filter((lane) => lane.name !== 'unit' && isLaneWanted(lane, trackedFiles, isWorkspaceRepo))
|
|
16
19
|
.map((lane): [string, string] => [`test:${lane.name}`, `rig ${lane.name}`]);
|
|
17
20
|
|
|
21
|
+
const benchScripts =
|
|
22
|
+
collectBenchFiles(trackedFiles, isWorkspaceRepo).length > 0 ? { bench: 'rig bench' } : {};
|
|
23
|
+
|
|
24
|
+
const evalScripts = collectEvalFiles(trackedFiles).length > 0 ? { eval: 'rig eval' } : {};
|
|
25
|
+
|
|
18
26
|
return {
|
|
19
27
|
...oldScripts,
|
|
20
28
|
test: 'rig unit',
|
|
21
29
|
'test:related': 'rig related',
|
|
22
30
|
...Object.fromEntries(laneScripts),
|
|
31
|
+
...benchScripts,
|
|
32
|
+
...evalScripts,
|
|
23
33
|
};
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
function isLaneWanted(
|
|
36
|
+
function isLaneWanted(
|
|
37
|
+
lane: ResolvedLane,
|
|
38
|
+
trackedFiles: readonly string[],
|
|
39
|
+
isWorkspaceRepo: boolean,
|
|
40
|
+
): boolean {
|
|
27
41
|
if (!standardLanes.some((standard) => standard.name === lane.name)) {
|
|
28
42
|
return true;
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
if (lane.suffix === null) {
|
|
32
|
-
return pickE2EDirs(trackedFiles).length > 0;
|
|
46
|
+
return pickE2EDirs(trackedFiles, isWorkspaceRepo).length > 0;
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
const suffix = lane.suffix;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import type { ProgramDeclaration } from './types.ts';
|
|
3
|
+
|
|
4
|
+
// The environment variable that holds a program's built artifact, such as RIG_PROGRAM_KETTLE_CLI
|
|
5
|
+
// for the program `kettle-cli`.
|
|
6
|
+
export function buildProgramVariable(name: string): string {
|
|
7
|
+
return `RIG_PROGRAM_${name.toUpperCase().replaceAll('-', '_')}`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function buildProgramEnvironment(
|
|
11
|
+
repoRoot: string,
|
|
12
|
+
programs: Readonly<Record<string, ProgramDeclaration>>,
|
|
13
|
+
): Readonly<Record<string, string>> {
|
|
14
|
+
return Object.fromEntries(
|
|
15
|
+
Object.entries(programs).map(([name, program]) => [
|
|
16
|
+
buildProgramVariable(name),
|
|
17
|
+
join(repoRoot, program.package, program.artifact),
|
|
18
|
+
]),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { buildProgramEnvironment } from './build-program-environment.ts';
|
|
2
|
+
import type { LaneHooks } from './lane-hooks.ts';
|
|
3
|
+
import type { ProgramDeclaration } from './types.ts';
|
|
4
|
+
|
|
5
|
+
// The programs' artifact paths reach the test or bench process beside whatever the lane's own start
|
|
6
|
+
// hook adds. `rig e2e` and `rig bench` both wrap their hooks with this.
|
|
7
|
+
export function buildProgramLaneHooks(
|
|
8
|
+
hooks: LaneHooks,
|
|
9
|
+
repoRoot: string,
|
|
10
|
+
programs: Readonly<Record<string, ProgramDeclaration>>,
|
|
11
|
+
): LaneHooks {
|
|
12
|
+
const programEnvironment = buildProgramEnvironment(repoRoot, programs);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
...hooks,
|
|
16
|
+
start: async () => {
|
|
17
|
+
const started = await hooks.start();
|
|
18
|
+
|
|
19
|
+
return started.kind === 'finish'
|
|
20
|
+
? started
|
|
21
|
+
: { kind: 'proceed', environment: { ...started.environment, ...programEnvironment } };
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -17,6 +17,8 @@ export interface TestSelectionRequest {
|
|
|
17
17
|
|
|
18
18
|
readonly repoRoot: string;
|
|
19
19
|
|
|
20
|
+
readonly isWorkspaceRepo: boolean;
|
|
21
|
+
|
|
20
22
|
readonly trackedFiles: readonly string[];
|
|
21
23
|
|
|
22
24
|
readonly args: TestArgs;
|
|
@@ -26,7 +28,7 @@ export interface TestSelectionRequest {
|
|
|
26
28
|
// narrows the paths to the lane's own files and hands bun test that exact list.
|
|
27
29
|
export function buildTestSelection(request: TestSelectionRequest): TestSelection {
|
|
28
30
|
const split = request.args;
|
|
29
|
-
const laneFiles = collectLaneTestFiles(request
|
|
31
|
+
const laneFiles = collectLaneTestFiles(request);
|
|
30
32
|
|
|
31
33
|
if (split.paths.length === 0) {
|
|
32
34
|
return laneFiles.length === 0 ? { kind: 'empty' } : { kind: 'lane', flags: split.flags };
|
|
@@ -49,12 +51,11 @@ const testFilePattern = /[._](?:test|spec)\.[jt]sx?$/u;
|
|
|
49
51
|
|
|
50
52
|
// The unit lane takes every test file that no other lane claims, the e2e lane the ones under its
|
|
51
53
|
// folders, and any other lane the ones with its suffix.
|
|
52
|
-
function collectLaneTestFiles(
|
|
53
|
-
config
|
|
54
|
-
lane
|
|
55
|
-
trackedFiles
|
|
56
|
-
|
|
57
|
-
const e2eDirs = pickE2EDirs(trackedFiles);
|
|
54
|
+
function collectLaneTestFiles(request: TestSelectionRequest): readonly string[] {
|
|
55
|
+
const config = request.config;
|
|
56
|
+
const lane = request.lane;
|
|
57
|
+
const trackedFiles = request.trackedFiles;
|
|
58
|
+
const e2eDirs = pickE2EDirs(trackedFiles, request.isWorkspaceRepo);
|
|
58
59
|
|
|
59
60
|
const isUnderE2EDir = (file: string) =>
|
|
60
61
|
e2eDirs.some((directory) => file.startsWith(`${directory}/`));
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { testRunArgs } from './test-run-args.ts';
|
|
1
2
|
import type { RigConfig } from './types.ts';
|
|
2
3
|
|
|
3
4
|
// The e2e lane has no suffix, so the unit run leaves out the folders that the e2e lane selects,
|
|
@@ -18,5 +19,5 @@ export function buildUnitCommand(
|
|
|
18
19
|
(pattern) => `--path-ignore-patterns=${pattern}`,
|
|
19
20
|
);
|
|
20
21
|
|
|
21
|
-
return ['bun', 'test', `--parallel=${workerCount}`,
|
|
22
|
+
return ['bun', 'test', `--parallel=${workerCount}`, ...testRunArgs, ...ignoreArgs, ...extraArgs];
|
|
22
23
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { availableParallelism, totalmem } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
4
5
|
import { readOptionalText } from '@zgeoff/std';
|
|
5
6
|
import { wireBrowserLaneHooks } from './browser/wire-browser-lane-hooks.ts';
|
|
6
7
|
import { initFiles } from './init-files.ts';
|
|
8
|
+
import { probeWorkspaceRepo } from './probe-workspace-repo.ts';
|
|
7
9
|
import { readRigConfig } from './read-rig-config.ts';
|
|
8
10
|
import { readTrackedFiles } from './read-tracked-files.ts';
|
|
9
11
|
import { readWorkerCount } from './read-worker-count.ts';
|
|
@@ -46,10 +48,12 @@ try {
|
|
|
46
48
|
},
|
|
47
49
|
{
|
|
48
50
|
dietScript: resolveDietScript(),
|
|
51
|
+
benchScript: join(import.meta.dir, 'run-bench-file.ts'),
|
|
49
52
|
output,
|
|
50
53
|
sources: { cwd: repoRoot, probeFile: existsSync },
|
|
51
54
|
readConfig: (root) =>
|
|
52
55
|
readRigConfig(root, { probeFile: existsSync, importModule: (path) => import(path) }),
|
|
56
|
+
probeWorkspaceRepo,
|
|
53
57
|
readTrackedFiles,
|
|
54
58
|
initFiles,
|
|
55
59
|
readWorkerCount: () =>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
|
|
3
|
+
const benchFilePattern = /^(?:(?<folder>.*)\/)?bench\/[^/]+\.bench\.ts$/u;
|
|
4
|
+
|
|
5
|
+
// A bench file sits in the `bench/` folder of a package, or of the root of a single-package repo.
|
|
6
|
+
export function collectBenchFiles(
|
|
7
|
+
trackedFiles: readonly string[],
|
|
8
|
+
isWorkspaceRepo: boolean,
|
|
9
|
+
): readonly string[] {
|
|
10
|
+
const tracked = new Set(trackedFiles);
|
|
11
|
+
|
|
12
|
+
return trackedFiles.filter((file) => {
|
|
13
|
+
const match = benchFilePattern.exec(file);
|
|
14
|
+
|
|
15
|
+
if (match === null) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const folder = match.groups?.['folder'] ?? '';
|
|
20
|
+
|
|
21
|
+
return folder === '' ? !isWorkspaceRepo : tracked.has(posix.join(folder, 'package.json'));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
import type { ProgramDeclaration } from './types.ts';
|
|
3
|
+
|
|
4
|
+
// An e2e folder runs a program of its package, so a folder in a package that no program names has
|
|
5
|
+
// nothing to run.
|
|
6
|
+
export function collectUnprogrammedE2EDirs(
|
|
7
|
+
e2eDirs: readonly string[],
|
|
8
|
+
programs: Readonly<Record<string, ProgramDeclaration>>,
|
|
9
|
+
): readonly string[] {
|
|
10
|
+
const packages = new Set(
|
|
11
|
+
Object.values(programs).map((program) => toPackageFolder(program.package)),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
return e2eDirs.filter((directory) => !packages.has(toPackageFolder(posix.dirname(directory))));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function toPackageFolder(path: string): string {
|
|
18
|
+
const normalized = posix.normalize(path).replace(/\/$/u, '');
|
|
19
|
+
|
|
20
|
+
return normalized === '.' ? '' : normalized;
|
|
21
|
+
}
|