@zgeoff/rig 0.0.1 → 0.0.3
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 +87 -3
- package/dist/browser/browser-preload.d.ts +1 -0
- package/dist/browser/build-browser-run.d.ts +9 -0
- package/dist/browser/build-lane-environments.d.ts +8 -0
- package/dist/browser/build-recorded-command.d.ts +1 -0
- package/dist/browser/find-browser-run.d.ts +4 -0
- package/dist/browser/find-private-dir-problem.d.ts +2 -0
- package/dist/browser/has-browser-build-script.d.ts +1 -0
- package/dist/browser/has-error-code.d.ts +1 -0
- package/dist/browser/is-zombie-state.d.ts +1 -0
- package/dist/browser/lane-files.d.ts +13 -0
- package/dist/browser/make-browser-lane-hooks.d.ts +22 -0
- package/dist/browser/pick-chrome-dirs.d.ts +7 -0
- package/dist/browser/pick-exit-code-after-tests.d.ts +1 -0
- package/dist/browser/pick-step-after-build.d.ts +7 -0
- package/dist/browser/pick-stop-step.d.ts +17 -0
- package/dist/browser/probe-host-process.d.ts +1 -0
- package/dist/browser/probe-process.d.ts +6 -0
- package/dist/browser/probe-run.d.ts +9 -0
- package/dist/browser/process-group-still-running-error.d.ts +4 -0
- package/dist/browser/process-group-stop-error.d.ts +4 -0
- package/dist/browser/process-groups.d.ts +5 -0
- package/dist/browser/process-runtime.d.ts +15 -0
- package/dist/browser/read-dir-names.d.ts +1 -0
- package/dist/browser/read-process-table.d.ts +6 -0
- package/dist/browser/remove-exited-run-dirs.d.ts +2 -0
- package/dist/browser/remove-path.d.ts +1 -0
- package/dist/browser/run-in-page.d.ts +23 -0
- package/dist/browser/should-keep-run-folders.d.ts +1 -0
- package/dist/browser/start-browser.d.ts +9 -0
- package/dist/browser/start-process-group.d.ts +17 -0
- package/dist/browser/wire-browser-lane-hooks.d.ts +12 -0
- package/dist/build-diet-command.d.ts +1 -0
- package/dist/build-init-scripts.d.ts +3 -0
- package/dist/build-test-selection.d.ts +23 -0
- package/dist/build-unit-command.d.ts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/define-rig.d.ts +2 -0
- package/dist/find-timeout-arg.d.ts +1 -0
- package/dist/format-no-match-message.d.ts +1 -0
- package/dist/format-no-tests-message.d.ts +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +255 -0
- package/dist/init-files.d.ts +6 -0
- package/dist/is-command-name.d.ts +3 -0
- package/dist/lane-hooks.d.ts +13 -0
- package/dist/make-repo-lane-hooks.d.ts +12 -0
- package/dist/parse-duration.d.ts +1 -0
- package/dist/parse-lane-hook.d.ts +3 -0
- package/dist/parse-lane-start.d.ts +2 -0
- package/dist/parse-rig-config.d.ts +9 -0
- package/dist/parse-test-args.d.ts +5 -0
- package/dist/pick-e2e-dirs.d.ts +1 -0
- package/dist/pick-lane-selection.d.ts +2 -0
- package/dist/read-cli-args.d.ts +5 -0
- package/dist/read-memory-budget.d.ts +8 -0
- package/dist/read-rig-config.d.ts +6 -0
- package/dist/read-tracked-files.d.ts +1 -0
- package/dist/read-worker-count.d.ts +13 -0
- package/dist/resolve-diet-script.d.ts +1 -0
- package/dist/resolve-related-tests.d.ts +6 -0
- package/dist/run-cli.d.ts +9 -0
- package/dist/run-command.d.ts +6 -0
- package/dist/run-init.d.ts +8 -0
- package/dist/run-lane-hooks.d.ts +2 -0
- package/dist/run-lane.d.ts +18 -0
- package/dist/run-related-tests.d.ts +10 -0
- package/dist/run-unit-tests.d.ts +12 -0
- package/dist/run-unit.d.ts +8 -0
- package/dist/standard-lanes.d.ts +8 -0
- package/dist/system-clock.d.ts +5 -0
- package/dist/types.d.ts +42 -0
- package/package.json +41 -3
- package/src/browser/browser-preload.ts +36 -0
- package/src/browser/build-browser-run.ts +32 -0
- package/src/browser/build-lane-environments.ts +33 -0
- package/src/browser/build-recorded-command.ts +12 -0
- package/src/browser/find-browser-run.ts +15 -0
- package/src/browser/find-private-dir-problem.ts +21 -0
- package/src/browser/has-browser-build-script.ts +7 -0
- package/src/browser/has-error-code.ts +3 -0
- package/src/browser/is-zombie-state.ts +4 -0
- package/src/browser/lane-files.ts +56 -0
- package/src/browser/make-browser-lane-hooks.ts +228 -0
- package/src/browser/pick-chrome-dirs.ts +34 -0
- package/src/browser/pick-exit-code-after-tests.ts +5 -0
- package/src/browser/pick-step-after-build.ts +20 -0
- package/src/browser/pick-stop-step.ts +28 -0
- package/src/browser/probe-host-process.ts +11 -0
- package/src/browser/probe-process.ts +35 -0
- package/src/browser/probe-run.ts +38 -0
- package/src/browser/process-group-still-running-error.ts +9 -0
- package/src/browser/process-group-stop-error.ts +10 -0
- package/src/browser/process-groups.ts +45 -0
- package/src/browser/process-runtime.ts +40 -0
- package/src/browser/read-dir-names.ts +15 -0
- package/src/browser/read-process-table.ts +35 -0
- package/src/browser/remove-exited-run-dirs.ts +21 -0
- package/src/browser/remove-path.ts +5 -0
- package/src/browser/run-in-page.ts +103 -0
- package/src/browser/should-keep-run-folders.ts +7 -0
- package/src/browser/start-browser.ts +40 -0
- package/src/browser/start-process-group.ts +168 -0
- package/src/browser/wire-browser-lane-hooks.ts +88 -0
- package/src/build-diet-command.ts +11 -0
- package/src/build-init-scripts.ts +40 -0
- package/src/build-test-selection.ts +124 -0
- package/src/build-unit-command.ts +22 -0
- package/src/cli.ts +73 -0
- package/src/define-rig.ts +195 -0
- package/src/find-timeout-arg.ts +3 -0
- package/src/format-no-match-message.ts +3 -0
- package/src/format-no-tests-message.ts +3 -0
- package/src/index.ts +14 -0
- package/src/init-files.ts +15 -0
- package/src/is-command-name.ts +9 -0
- package/src/lane-hooks.ts +15 -0
- package/src/make-repo-lane-hooks.ts +70 -0
- package/src/parse-duration.ts +14 -0
- package/src/parse-lane-hook.ts +20 -0
- package/src/parse-lane-start.ts +30 -0
- package/src/parse-rig-config.ts +96 -0
- package/src/parse-test-args.ts +14 -0
- package/src/pick-e2e-dirs.ts +25 -0
- package/src/pick-lane-selection.ts +14 -0
- package/src/read-cli-args.ts +57 -0
- package/src/read-memory-budget.ts +65 -0
- package/src/read-rig-config.ts +32 -0
- package/src/read-tracked-files.ts +20 -0
- package/src/read-worker-count.ts +40 -0
- package/src/resolve-diet-script.ts +7 -0
- package/src/resolve-related-tests.ts +52 -0
- package/src/run-cli.ts +68 -0
- package/src/run-command.ts +36 -0
- package/src/run-init.ts +71 -0
- package/src/run-lane-hooks.ts +49 -0
- package/src/run-lane.ts +138 -0
- package/src/run-related-tests.ts +113 -0
- package/src/run-unit-tests.ts +42 -0
- package/src/run-unit.ts +68 -0
- package/src/standard-lanes.ts +23 -0
- package/src/system-clock.ts +10 -0
- package/src/types.ts +75 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { LaneParallelism, ResolvedLane, RigConfig } from './types.ts';
|
|
2
|
+
|
|
3
|
+
export type ParsedRigConfig =
|
|
4
|
+
| { readonly kind: 'parsed'; readonly config: RigConfig }
|
|
5
|
+
| { readonly kind: 'refused'; readonly reason: string };
|
|
6
|
+
|
|
7
|
+
// The module that a config file default-exports comes from outside the program, so rig's loader
|
|
8
|
+
// and the lint plugin's both parse it field by field instead of trusting its type.
|
|
9
|
+
export function parseRigConfig(value: unknown): ParsedRigConfig {
|
|
10
|
+
if (typeof value !== 'object' || value === null || !('lanes' in value)) {
|
|
11
|
+
return { kind: 'refused', reason: 'the default export has no lanes' };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const lanes: unknown = value.lanes;
|
|
15
|
+
|
|
16
|
+
if (!Array.isArray(lanes)) {
|
|
17
|
+
return { kind: 'refused', reason: 'its lanes are no list' };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const resolved: ResolvedLane[] = [];
|
|
21
|
+
|
|
22
|
+
for (const [index, lane] of lanes.entries()) {
|
|
23
|
+
const parsed = parseResolvedLane(lane, index);
|
|
24
|
+
|
|
25
|
+
if (parsed.kind === 'refused') {
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
resolved.push(parsed.lane);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return { kind: 'parsed', config: { lanes: resolved } };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type ParsedLane =
|
|
36
|
+
| { readonly kind: 'parsed'; readonly lane: ResolvedLane }
|
|
37
|
+
| { readonly kind: 'refused'; readonly reason: string };
|
|
38
|
+
|
|
39
|
+
function parseResolvedLane(value: unknown, index: number): ParsedLane {
|
|
40
|
+
if (typeof value !== 'object' || value === null) {
|
|
41
|
+
return { kind: 'refused', reason: `lane ${index} is no object` };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const name: unknown = Reflect.get(value, 'name');
|
|
45
|
+
const suffix: unknown = Reflect.get(value, 'suffix');
|
|
46
|
+
const limitMs: unknown = Reflect.get(value, 'limitMs');
|
|
47
|
+
const parallel: unknown = Reflect.get(value, 'parallel');
|
|
48
|
+
const memory: unknown = Reflect.get(value, 'memory');
|
|
49
|
+
const start: unknown = Reflect.get(value, 'start');
|
|
50
|
+
const stop: unknown = Reflect.get(value, 'stop');
|
|
51
|
+
|
|
52
|
+
if (typeof name !== 'string') {
|
|
53
|
+
return { kind: 'refused', reason: `lane ${index} has no name` };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!isNullableString(suffix)) {
|
|
57
|
+
return refuseField(name, 'suffix', suffix);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof limitMs !== 'number') {
|
|
61
|
+
return refuseField(name, 'limitMs', limitMs);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!isParallelism(parallel)) {
|
|
65
|
+
return refuseField(name, 'parallel', parallel);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!isNullableString(memory)) {
|
|
69
|
+
return refuseField(name, 'memory', memory);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!isNullableString(start)) {
|
|
73
|
+
return refuseField(name, 'start', start);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!isNullableString(stop)) {
|
|
77
|
+
return refuseField(name, 'stop', stop);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return { kind: 'parsed', lane: { name, suffix, limitMs, parallel, memory, start, stop } };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isNullableString(value: unknown): value is string | null {
|
|
84
|
+
return value === null || typeof value === 'string';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function refuseField(lane: string, field: keyof ResolvedLane, value: unknown): ParsedLane {
|
|
88
|
+
return {
|
|
89
|
+
kind: 'refused',
|
|
90
|
+
reason: `lane "${lane}" has the ${field} ${JSON.stringify(value)}, which defineRig never resolves`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isParallelism(value: unknown): value is LaneParallelism {
|
|
95
|
+
return value === 'workers' || (typeof value === 'number' && Number.isInteger(value) && value > 0);
|
|
96
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface TestArgs {
|
|
2
|
+
readonly flags: readonly string[];
|
|
3
|
+
|
|
4
|
+
readonly paths: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// An option of bun test takes its value after `=`, so every argument that is no option is a path,
|
|
8
|
+
// and rig needs no copy of the options that bun test reads a value for.
|
|
9
|
+
export function parseTestArgs(args: readonly string[]): TestArgs {
|
|
10
|
+
return {
|
|
11
|
+
flags: args.filter((arg) => arg.startsWith('-')),
|
|
12
|
+
paths: args.filter((arg) => !arg.startsWith('-')),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// An `e2e` folder counts at the repo root or in an app root, which holds a package.json, and a
|
|
2
|
+
// folder whose name ends in `-e2e` counts as a package of its own.
|
|
3
|
+
export function pickE2EDirs(trackedFiles: readonly string[]): readonly string[] {
|
|
4
|
+
const tracked = new Set(trackedFiles);
|
|
5
|
+
const dirs = new Set<string>();
|
|
6
|
+
|
|
7
|
+
for (const file of trackedFiles) {
|
|
8
|
+
const segments = file.split('/').slice(0, -1);
|
|
9
|
+
|
|
10
|
+
for (const [index, name] of segments.entries()) {
|
|
11
|
+
const parent = segments.slice(0, index).join('/');
|
|
12
|
+
const prefix = parent === '' ? '' : `${parent}/`;
|
|
13
|
+
|
|
14
|
+
const isE2EDir =
|
|
15
|
+
(name === 'e2e' && (parent === '' || tracked.has(`${prefix}package.json`))) ||
|
|
16
|
+
(name.endsWith('-e2e') && tracked.has(`${prefix}${name}/package.json`));
|
|
17
|
+
|
|
18
|
+
if (isE2EDir) {
|
|
19
|
+
dirs.add(`${prefix}${name}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return [...dirs].toSorted();
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ResolvedLane } from './types.ts';
|
|
2
|
+
|
|
3
|
+
// bun test reads a bare argument as a filter on the path, so a suffix filter picks the lane's files
|
|
4
|
+
// and a ./ path picks a folder.
|
|
5
|
+
export function pickLaneSelection(
|
|
6
|
+
lane: ResolvedLane,
|
|
7
|
+
e2eDirs: readonly string[],
|
|
8
|
+
): readonly string[] {
|
|
9
|
+
if (lane.suffix === null) {
|
|
10
|
+
return e2eDirs.map((directory) => `./${directory}/`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return [`${lane.suffix}.test.ts`, `${lane.suffix}.test.tsx`];
|
|
14
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { cli } from 'gunshi/bone';
|
|
2
|
+
import { define } from 'gunshi/definition';
|
|
3
|
+
|
|
4
|
+
export interface CLIArgs {
|
|
5
|
+
readonly command: string;
|
|
6
|
+
|
|
7
|
+
// Every argument after the command, flags and paths alike, which the command passes on to
|
|
8
|
+
// `bun test`.
|
|
9
|
+
readonly extraArgs: readonly string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const argSchema = { command: { type: 'positional' } } as const;
|
|
13
|
+
|
|
14
|
+
// The parser reads only the command, because `bun run <script> -- --seed=1` drops the `--` before
|
|
15
|
+
// rig starts, so a flag meant for `bun test` arrives with nothing to mark it.
|
|
16
|
+
export async function readCLIArgs(argv: readonly string[]): Promise<CLIArgs | string> {
|
|
17
|
+
let parsed: CLIArgs | null = null;
|
|
18
|
+
|
|
19
|
+
const command = define({
|
|
20
|
+
name: 'rig',
|
|
21
|
+
args: argSchema,
|
|
22
|
+
run: (ctx) => {
|
|
23
|
+
if (ctx.validationError !== undefined) {
|
|
24
|
+
throw ctx.validationError;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
parsed = {
|
|
28
|
+
command: ctx.values.command,
|
|
29
|
+
extraArgs: dropFirstSeparator(argv.slice(1)),
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
await cli(argv.slice(0, 1), command, { strict: true });
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return formatParseError(error);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return parsed ?? 'the command line could not be parsed';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function dropFirstSeparator(args: readonly string[]): readonly string[] {
|
|
44
|
+
const separator = args.indexOf('--');
|
|
45
|
+
|
|
46
|
+
return separator === -1 ? args : args.toSpliced(separator, 1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The parser throws one error that holds every reason the command line is refused, so each reason
|
|
50
|
+
// prints on a line of its own.
|
|
51
|
+
function formatParseError(error: unknown): string {
|
|
52
|
+
if (error instanceof AggregateError) {
|
|
53
|
+
return error.errors.map((reason) => formatParseError(reason)).join('\n');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return error instanceof Error ? error.message : String(error);
|
|
57
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export interface MemoryBudgetSources {
|
|
5
|
+
readonly totalMemory: number;
|
|
6
|
+
readonly procRoot: string;
|
|
7
|
+
readonly cgroupRoot: string;
|
|
8
|
+
readonly readText: (path: string) => Promise<string | undefined>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function readMemoryBudget(sources: Readonly<MemoryBudgetSources>): Promise<number> {
|
|
12
|
+
const caps = await readCgroupCaps(sources);
|
|
13
|
+
|
|
14
|
+
return Math.min(sources.totalMemory, ...caps);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function readCgroupCaps(sources: Readonly<MemoryBudgetSources>): Promise<number[]> {
|
|
18
|
+
const membership = await sources.readText(join(sources.procRoot, 'self', 'cgroup'));
|
|
19
|
+
|
|
20
|
+
// macOS has no procfs.
|
|
21
|
+
if (membership === undefined) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const own = /^0::(?<path>\/.*)$/mu.exec(membership)?.groups?.['path'];
|
|
26
|
+
|
|
27
|
+
// A host on cgroup v1 alone has no 0:: line.
|
|
28
|
+
if (own === undefined) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const caps = await Promise.all(
|
|
33
|
+
collectAncestorPaths(own).map((path) =>
|
|
34
|
+
readCgroupCap(sources, join(sources.cgroupRoot, path, 'memory.max')),
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return caps.filter((cap) => cap !== undefined);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function collectAncestorPaths(path: string): string[] {
|
|
42
|
+
const parts = path.split('/').filter((part) => part !== '');
|
|
43
|
+
|
|
44
|
+
return ['/', ...parts.map((_, index) => `/${parts.slice(0, index + 1).join('/')}`)];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The root cgroup has no memory.max, and a cgroup with no cap holds max.
|
|
48
|
+
async function readCgroupCap(
|
|
49
|
+
sources: Readonly<MemoryBudgetSources>,
|
|
50
|
+
path: string,
|
|
51
|
+
): Promise<number | undefined> {
|
|
52
|
+
const text = await sources.readText(path);
|
|
53
|
+
|
|
54
|
+
const value = text?.trim();
|
|
55
|
+
|
|
56
|
+
return value === undefined || value === 'max' ? undefined : Number(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Bun.file's text reads as many bytes as stat reports, and procfs and cgroupfs report 0 for every
|
|
60
|
+
// file; readFile reads to the end.
|
|
61
|
+
export async function readOptionalText(path: string): Promise<string | undefined> {
|
|
62
|
+
const exists = await Bun.file(path).exists();
|
|
63
|
+
|
|
64
|
+
return exists ? readFile(path, 'utf8') : undefined;
|
|
65
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { isRecord } from '@zgeoff/std';
|
|
3
|
+
import { defineRig } from './define-rig.ts';
|
|
4
|
+
import { parseRigConfig } from './parse-rig-config.ts';
|
|
5
|
+
import type { RigConfig } from './types.ts';
|
|
6
|
+
|
|
7
|
+
export interface RigConfigSources {
|
|
8
|
+
readonly probeFile: (path: string) => boolean;
|
|
9
|
+
readonly importModule: (path: string) => Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function readRigConfig(
|
|
13
|
+
repoRoot: string,
|
|
14
|
+
dependencies: RigConfigSources,
|
|
15
|
+
): Promise<RigConfig> {
|
|
16
|
+
const path = join(repoRoot, 'rig.config.ts');
|
|
17
|
+
|
|
18
|
+
if (!dependencies.probeFile(path)) {
|
|
19
|
+
return defineRig({});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const loaded = await dependencies.importModule(path);
|
|
23
|
+
|
|
24
|
+
const defaultExport = isRecord(loaded) ? loaded['default'] : null;
|
|
25
|
+
const parsed = parseRigConfig(defaultExport);
|
|
26
|
+
|
|
27
|
+
if (parsed.kind === 'refused') {
|
|
28
|
+
throw new Error(`${path} must default-export the result of defineRig: ${parsed.reason}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return parsed.config;
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Lists the files git tracks in the repo, so a lane and `rig init` look at what a push would
|
|
2
|
+
// carry, never at build output or an untracked scratch folder.
|
|
3
|
+
export async function readTrackedFiles(repoRoot: string): Promise<readonly string[]> {
|
|
4
|
+
const listing = Bun.spawn(['git', 'ls-files', '-z'], {
|
|
5
|
+
cwd: repoRoot,
|
|
6
|
+
stdout: 'pipe',
|
|
7
|
+
stderr: 'inherit',
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const [exitCode, output] = await Promise.all([
|
|
11
|
+
listing.exited,
|
|
12
|
+
new Response(listing.stdout).text(),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
if (exitCode !== 0) {
|
|
16
|
+
throw new Error(`git ls-files failed in ${repoRoot} with exit code ${exitCode}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return output.split('\0').filter((path) => path !== '');
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { readMemoryBudget } from './read-memory-budget.ts';
|
|
2
|
+
import type { MemoryBudgetSources } from './read-memory-budget.ts';
|
|
3
|
+
|
|
4
|
+
export interface WorkerCountSources extends MemoryBudgetSources {
|
|
5
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
6
|
+
readonly cores: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type WorkerCount =
|
|
10
|
+
| { readonly kind: 'count'; readonly count: number }
|
|
11
|
+
| { readonly kind: 'invalid'; readonly message: string };
|
|
12
|
+
|
|
13
|
+
const gibibyte = 1024 ** 3;
|
|
14
|
+
|
|
15
|
+
// Test memory grows in a straight line with the workers, and a host under this budget runs out with
|
|
16
|
+
// a second one.
|
|
17
|
+
const smallHostBudget = 4 * gibibyte;
|
|
18
|
+
const maxWorkers = 4;
|
|
19
|
+
|
|
20
|
+
export async function readWorkerCount(sources: Readonly<WorkerCountSources>): Promise<WorkerCount> {
|
|
21
|
+
const override = sources.env['TEST_WORKERS'];
|
|
22
|
+
|
|
23
|
+
if (override !== undefined) {
|
|
24
|
+
const count = Number(override);
|
|
25
|
+
|
|
26
|
+
return /^[1-9]\d*$/u.test(override) && Number.isSafeInteger(count)
|
|
27
|
+
? { kind: 'count', count }
|
|
28
|
+
: {
|
|
29
|
+
kind: 'invalid',
|
|
30
|
+
message: `TEST_WORKERS is ${override}; set it to a whole number above 0`,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const budget = await readMemoryBudget(sources);
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
kind: 'count',
|
|
38
|
+
count: budget < smallHostBudget ? 1 : Math.min(sources.cores, maxWorkers),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
|
|
3
|
+
// diet is a dependency of this package, so its script resolves from here, whatever the repo's PATH
|
|
4
|
+
// holds.
|
|
5
|
+
export function resolveDietScript(): string {
|
|
6
|
+
return join(dirname(Bun.resolveSync('@zgeoff/diet/package.json', import.meta.dir)), 'diet.sh');
|
|
7
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { basename, join } from 'node:path';
|
|
2
|
+
import type { RigConfig } from './types.ts';
|
|
3
|
+
|
|
4
|
+
export interface RelatedTestSources {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
|
|
7
|
+
readonly probeFile: (path: string) => boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// The unit run covers a changed file through the test beside it, so each changed path maps to its
|
|
11
|
+
// sibling test, and the files of a lane drop out.
|
|
12
|
+
export function resolveRelatedTests(
|
|
13
|
+
paths: readonly string[],
|
|
14
|
+
config: RigConfig,
|
|
15
|
+
sources: Readonly<RelatedTestSources>,
|
|
16
|
+
): string[] {
|
|
17
|
+
const suffixes = config.lanes.flatMap((lane) => (lane.suffix === null ? [] : [lane.suffix]));
|
|
18
|
+
|
|
19
|
+
const selected = new Set<string>();
|
|
20
|
+
|
|
21
|
+
for (const path of paths) {
|
|
22
|
+
const test = findRelatedTest(path, suffixes);
|
|
23
|
+
|
|
24
|
+
if (test !== undefined && sources.probeFile(join(sources.cwd, test))) {
|
|
25
|
+
selected.add(test);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [...selected];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const sourcePattern = /^(?<stem>.+?)(?:\.test)?(?<ext>\.tsx?)$/u;
|
|
33
|
+
|
|
34
|
+
function findRelatedTest(path: string, suffixes: readonly string[]): string | undefined {
|
|
35
|
+
const groups = sourcePattern.exec(path)?.groups;
|
|
36
|
+
|
|
37
|
+
if (groups === undefined || path.endsWith('.d.ts') || isLaneFile(path, suffixes)) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return `${groups['stem']}.test${groups['ext']}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// A lane's tests need a real system, such as a browser, a container or a live model API, and run
|
|
45
|
+
// only in CI.
|
|
46
|
+
function isLaneFile(path: string, suffixes: readonly string[]): boolean {
|
|
47
|
+
const stem = basename(path).replace(/(?:\.test)?\.tsx?$/u, '');
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
/(?:^|\/)(?:e2e|[^/]+-e2e)\//u.test(path) || suffixes.some((suffix) => stem.endsWith(suffix))
|
|
51
|
+
);
|
|
52
|
+
}
|
package/src/run-cli.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
import { findTimeoutArg } from './find-timeout-arg.ts';
|
|
3
|
+
import { commandNames, isCommandName } from './is-command-name.ts';
|
|
4
|
+
import { readCLIArgs } from './read-cli-args.ts';
|
|
5
|
+
import { runInit } from './run-init.ts';
|
|
6
|
+
import type { InitDeps } from './run-init.ts';
|
|
7
|
+
import type { LaneDeps } from './run-lane.ts';
|
|
8
|
+
import { runLane } from './run-lane.ts';
|
|
9
|
+
import type { RelatedTestDeps } from './run-related-tests.ts';
|
|
10
|
+
import { runRelatedTests } from './run-related-tests.ts';
|
|
11
|
+
import { runUnit } from './run-unit.ts';
|
|
12
|
+
import type { UnitDeps } from './run-unit.ts';
|
|
13
|
+
import type { RigConfig, RigRun } from './types.ts';
|
|
14
|
+
|
|
15
|
+
export interface CLIDeps extends LaneDeps, UnitDeps, RelatedTestDeps, InitDeps {
|
|
16
|
+
readonly readConfig: (repoRoot: string) => Promise<RigConfig>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function runCLI(argv: readonly string[], run: RigRun, deps: CLIDeps): Promise<number> {
|
|
20
|
+
const args = await readCLIArgs(argv);
|
|
21
|
+
|
|
22
|
+
if (typeof args === 'string') {
|
|
23
|
+
deps.output.err(`rig: ${args}\n`);
|
|
24
|
+
|
|
25
|
+
return 2;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const config = await deps.readConfig(run.repoRoot);
|
|
29
|
+
|
|
30
|
+
const command = args.command;
|
|
31
|
+
|
|
32
|
+
if (isCommandName(command)) {
|
|
33
|
+
return match(command)
|
|
34
|
+
.with('init', () => runInit(run.repoRoot, config, deps))
|
|
35
|
+
.with('related', () => runRelatedTests(args.extraArgs, config, run, deps))
|
|
36
|
+
.exhaustive();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const lane = config.lanes.find((candidate) => candidate.name === command);
|
|
40
|
+
|
|
41
|
+
if (lane === undefined) {
|
|
42
|
+
return reportUsage(config, deps, command);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A forwarded timeout would override the lane's limit, and the pre-push timeout check reads only
|
|
46
|
+
// `bun test` scripts, so a `rig <lane> --timeout=<n>` script would pass it unseen.
|
|
47
|
+
const timeoutArg = findTimeoutArg(args.extraArgs);
|
|
48
|
+
|
|
49
|
+
if (timeoutArg !== undefined) {
|
|
50
|
+
deps.output.err(
|
|
51
|
+
`rig: rig ${command} refuses ${timeoutArg}, because the lane limit lives in rig.config.ts\n`,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return 2;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return lane.name === 'unit'
|
|
58
|
+
? runUnit(config, args.extraArgs, run, deps)
|
|
59
|
+
: runLane({ config, lane, extraArgs: args.extraArgs }, run, deps);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function reportUsage(config: RigConfig, deps: CLIDeps, command: string): number {
|
|
63
|
+
const names = [...commandNames, ...config.lanes.map((lane) => lane.name)].join(', ');
|
|
64
|
+
|
|
65
|
+
deps.output.err(`rig: unknown command "${command}"; use one of ${names}\n`);
|
|
66
|
+
|
|
67
|
+
return 2;
|
|
68
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { processGroups } from './browser/process-groups.ts';
|
|
2
|
+
import { processRuntime } from './browser/process-runtime.ts';
|
|
3
|
+
import { startProcessGroup } from './browser/start-process-group.ts';
|
|
4
|
+
|
|
5
|
+
export interface CommandOptions {
|
|
6
|
+
readonly cwd: string;
|
|
7
|
+
|
|
8
|
+
readonly environment: Readonly<Record<string, string | undefined>>;
|
|
9
|
+
|
|
10
|
+
readonly signal: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A command gets this long to exit after SIGTERM before its group gets SIGKILL.
|
|
14
|
+
const stopDeadlineMs = 10_000;
|
|
15
|
+
|
|
16
|
+
// A test can start a process of its own, such as a `sleep` or a server, that a signal to the
|
|
17
|
+
// command alone leaves running, so the command runs in a process group that an abort stops whole.
|
|
18
|
+
// An aborted run fails, whatever the command's exit code.
|
|
19
|
+
export async function runCommand(
|
|
20
|
+
command: readonly string[],
|
|
21
|
+
options: CommandOptions,
|
|
22
|
+
): Promise<number> {
|
|
23
|
+
const group = startProcessGroup(command, {
|
|
24
|
+
cwd: options.cwd,
|
|
25
|
+
environment: options.environment,
|
|
26
|
+
signal: options.signal,
|
|
27
|
+
stdout: 'inherit',
|
|
28
|
+
stopDeadlineMs,
|
|
29
|
+
groups: processGroups,
|
|
30
|
+
runtime: processRuntime,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const exitCode = await group.exited;
|
|
34
|
+
|
|
35
|
+
return options.signal.aborted ? 1 : exitCode;
|
|
36
|
+
}
|
package/src/run-init.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { isRecord } from '@zgeoff/std';
|
|
3
|
+
import { buildInitScripts } from './build-init-scripts.ts';
|
|
4
|
+
import type { InitFiles } from './init-files.ts';
|
|
5
|
+
import type { RigConfig, RigOutput } from './types.ts';
|
|
6
|
+
|
|
7
|
+
export interface InitDeps {
|
|
8
|
+
readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
|
|
9
|
+
|
|
10
|
+
readonly initFiles: InitFiles;
|
|
11
|
+
|
|
12
|
+
readonly output: RigOutput;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const starterConfig = `import { defineRig } from '@zgeoff/rig';
|
|
16
|
+
|
|
17
|
+
export default defineRig({ lanes: {} });
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
// A second run finds the scripts and the config in place and writes nothing.
|
|
21
|
+
export async function runInit(
|
|
22
|
+
repoRoot: string,
|
|
23
|
+
config: RigConfig,
|
|
24
|
+
deps: InitDeps,
|
|
25
|
+
): Promise<number> {
|
|
26
|
+
const manifestPath = join(repoRoot, 'package.json');
|
|
27
|
+
|
|
28
|
+
const manifest = await deps.initFiles.readJSON(manifestPath);
|
|
29
|
+
|
|
30
|
+
if (!isRecord(manifest)) {
|
|
31
|
+
deps.output.err(`rig init: ${manifestPath} must hold a JSON object\n`);
|
|
32
|
+
|
|
33
|
+
return 2;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const trackedFiles = await deps.readTrackedFiles(repoRoot);
|
|
37
|
+
|
|
38
|
+
const oldScripts = readScripts(manifest['scripts']);
|
|
39
|
+
const newScripts = buildInitScripts(oldScripts, config, trackedFiles);
|
|
40
|
+
|
|
41
|
+
if (JSON.stringify(oldScripts) !== JSON.stringify(newScripts)) {
|
|
42
|
+
await deps.initFiles.writeText(
|
|
43
|
+
manifestPath,
|
|
44
|
+
`${JSON.stringify({ ...manifest, scripts: newScripts }, null, 2)}\n`,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
deps.output.out('rig init: wrote the test scripts to package.json\n');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const configPath = join(repoRoot, 'rig.config.ts');
|
|
51
|
+
|
|
52
|
+
if (!(await deps.initFiles.probeFile(configPath))) {
|
|
53
|
+
await deps.initFiles.writeText(configPath, starterConfig);
|
|
54
|
+
|
|
55
|
+
deps.output.out('rig init: wrote rig.config.ts\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readScripts(scripts: unknown): Record<string, string> {
|
|
62
|
+
if (!isRecord(scripts)) {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return Object.fromEntries(
|
|
67
|
+
Object.entries(scripts).filter(
|
|
68
|
+
(entry): entry is [string, string] => typeof entry[1] === 'string',
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
import type { LaneHooks } from './lane-hooks.ts';
|
|
3
|
+
|
|
4
|
+
type LaneOutcome =
|
|
5
|
+
| { readonly kind: 'exited'; readonly exitCode: number }
|
|
6
|
+
| { readonly kind: 'failed'; readonly error: unknown };
|
|
7
|
+
|
|
8
|
+
export async function runLaneHooks(testArgs: readonly string[], hooks: LaneHooks): Promise<number> {
|
|
9
|
+
const outcome = await runStartedLane(testArgs, hooks).then(
|
|
10
|
+
(exitCode): LaneOutcome => ({ kind: 'exited', exitCode }),
|
|
11
|
+
(error: unknown): LaneOutcome => ({ kind: 'failed', error }),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await hooks.stop();
|
|
16
|
+
} catch (stopError) {
|
|
17
|
+
throw buildStopFailure(outcome, stopError);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (outcome.kind === 'failed') {
|
|
21
|
+
throw outcome.error;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return outcome.exitCode;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function runStartedLane(testArgs: readonly string[], hooks: LaneHooks): Promise<number> {
|
|
28
|
+
const started = await hooks.start();
|
|
29
|
+
|
|
30
|
+
if (started.kind === 'finish') {
|
|
31
|
+
return started.exitCode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return hooks.run(testArgs, started.environment);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function buildStopFailure(outcome: LaneOutcome, stopError: unknown): unknown {
|
|
38
|
+
return match(outcome)
|
|
39
|
+
.with({ kind: 'failed' }, (failed) => buildLaneAndStopFailure(failed.error, stopError))
|
|
40
|
+
.with({ kind: 'exited', exitCode: 0 }, () => stopError)
|
|
41
|
+
.with({ kind: 'exited' }, (exited) =>
|
|
42
|
+
buildLaneAndStopFailure(new Error(`the lane exited with code ${exited.exitCode}`), stopError),
|
|
43
|
+
)
|
|
44
|
+
.exhaustive();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function buildLaneAndStopFailure(laneError: unknown, stopError: unknown): AggregateError {
|
|
48
|
+
return new AggregateError([laneError, stopError], 'the lane failed, and so did its stop hook');
|
|
49
|
+
}
|