@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
package/src/define-rig.ts
CHANGED
|
@@ -2,7 +2,13 @@ import { isCommandName } from './is-command-name.ts';
|
|
|
2
2
|
import { parseDuration } from './parse-duration.ts';
|
|
3
3
|
import { standardLanes } from './standard-lanes.ts';
|
|
4
4
|
import type { StandardLane } from './standard-lanes.ts';
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
LaneDeclaration,
|
|
7
|
+
ProgramDeclaration,
|
|
8
|
+
ResolvedLane,
|
|
9
|
+
RigConfig,
|
|
10
|
+
RigDeclaration,
|
|
11
|
+
} from './types.ts';
|
|
6
12
|
|
|
7
13
|
const laneNamePattern = /^[a-z][a-z0-9-]*$/u;
|
|
8
14
|
|
|
@@ -35,7 +41,14 @@ export function defineRig(declaration: RigDeclaration): RigConfig {
|
|
|
35
41
|
|
|
36
42
|
assertDistinctSuffixes(lanes);
|
|
37
43
|
|
|
38
|
-
|
|
44
|
+
const programs = Object.fromEntries(
|
|
45
|
+
Object.entries(declaration.programs ?? {}).map(([name, program]) => [
|
|
46
|
+
name,
|
|
47
|
+
readProgram(name, program),
|
|
48
|
+
]),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return { lanes, programs, benchLane: readBenchLane(declaration.bench?.lane, lanes) };
|
|
39
52
|
}
|
|
40
53
|
|
|
41
54
|
function resolveStandardLane(
|
|
@@ -193,3 +206,53 @@ function assertDistinctSuffixes(lanes: readonly ResolvedLane[]): void {
|
|
|
193
206
|
owners.set(lane.suffix, lane.name);
|
|
194
207
|
}
|
|
195
208
|
}
|
|
209
|
+
|
|
210
|
+
const programNamePattern = /^[a-z][a-z0-9-]*$/u;
|
|
211
|
+
const programPathFields = ['package', 'source', 'artifact'] as const;
|
|
212
|
+
|
|
213
|
+
function readProgram(name: string, program: ProgramDeclaration): ProgramDeclaration {
|
|
214
|
+
if (!programNamePattern.test(name)) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`rig.config.ts: the program name "${name}" must be lowercase letters, digits and dashes, starting with a letter`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
for (const field of programPathFields) {
|
|
221
|
+
const path: unknown = program[field];
|
|
222
|
+
|
|
223
|
+
if (typeof path !== 'string' || path === '') {
|
|
224
|
+
throw new Error(`rig.config.ts: program "${name}" needs a ${field}, a non-empty path`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (path.startsWith('/') || path.split('/').includes('..')) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
`rig.config.ts: program "${name}" has the ${field} "${path}", which must be a relative path with no ".." segment`,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const build: unknown = program.build;
|
|
235
|
+
|
|
236
|
+
if (typeof build !== 'string' || build.trim() === '') {
|
|
237
|
+
throw new Error(`rig.config.ts: program "${name}" needs a build, a shell command`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
package: program.package,
|
|
242
|
+
source: program.source,
|
|
243
|
+
build: program.build,
|
|
244
|
+
artifact: program.artifact,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function readBenchLane(lane: string | undefined, lanes: readonly ResolvedLane[]): string | null {
|
|
249
|
+
if (lane === undefined) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (!lanes.some((candidate) => candidate.name === lane)) {
|
|
254
|
+
throw new Error(`rig.config.ts: bench.lane "${lane}" names no lane`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return lane;
|
|
258
|
+
}
|
package/src/e2e.ts
ADDED
package/src/index.ts
CHANGED
package/src/is-command-name.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The built-in commands that `rig` dispatches before it looks for a lane, so no lane may take one
|
|
2
2
|
// of these names.
|
|
3
|
-
export const commandNames = ['init', 'related'] as const;
|
|
3
|
+
export const commandNames = ['bench', 'eval', 'init', 'related'] as const;
|
|
4
4
|
|
|
5
5
|
export type CommandName = (typeof commandNames)[number];
|
|
6
6
|
|
|
@@ -4,6 +4,7 @@ import type { LaneHooks } from './lane-hooks.ts';
|
|
|
4
4
|
import { parseLaneHook } from './parse-lane-hook.ts';
|
|
5
5
|
import { parseLaneStart } from './parse-lane-start.ts';
|
|
6
6
|
import type { CommandOptions } from './run-command.ts';
|
|
7
|
+
import { testRunArgs } from './test-run-args.ts';
|
|
7
8
|
import type { LaneContext } from './types.ts';
|
|
8
9
|
|
|
9
10
|
export interface RepoLaneOptions {
|
|
@@ -47,6 +48,7 @@ export function makeRepoLaneHooks(options: RepoLaneOptions): LaneHooks {
|
|
|
47
48
|
'bun',
|
|
48
49
|
'test',
|
|
49
50
|
`--parallel=${options.parallel}`,
|
|
51
|
+
...testRunArgs,
|
|
50
52
|
...testArgs,
|
|
51
53
|
]),
|
|
52
54
|
{ cwd: options.context.repoRoot, environment, signal: options.signal },
|
package/src/parse-rig-config.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LaneParallelism, ResolvedLane, RigConfig } from './types.ts';
|
|
1
|
+
import type { LaneParallelism, ProgramDeclaration, ResolvedLane, RigConfig } from './types.ts';
|
|
2
2
|
|
|
3
3
|
export type ParsedRigConfig =
|
|
4
4
|
| { readonly kind: 'parsed'; readonly config: RigConfig }
|
|
@@ -29,7 +29,22 @@ export function parseRigConfig(value: unknown): ParsedRigConfig {
|
|
|
29
29
|
resolved.push(parsed.lane);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
const programs = parsePrograms(Reflect.get(value, 'programs'));
|
|
33
|
+
|
|
34
|
+
if (programs === null) {
|
|
35
|
+
return { kind: 'refused', reason: 'its programs are no record of program declarations' };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const benchLane: unknown = Reflect.get(value, 'benchLane');
|
|
39
|
+
|
|
40
|
+
if (benchLane !== undefined && !isNullableString(benchLane)) {
|
|
41
|
+
return { kind: 'refused', reason: `its benchLane is ${JSON.stringify(benchLane)}` };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
kind: 'parsed',
|
|
46
|
+
config: { lanes: resolved, programs, benchLane: benchLane ?? null },
|
|
47
|
+
};
|
|
33
48
|
}
|
|
34
49
|
|
|
35
50
|
type ParsedLane =
|
|
@@ -94,3 +109,50 @@ function refuseField(lane: string, field: keyof ResolvedLane, value: unknown): P
|
|
|
94
109
|
function isParallelism(value: unknown): value is LaneParallelism {
|
|
95
110
|
return value === 'workers' || (typeof value === 'number' && Number.isInteger(value) && value > 0);
|
|
96
111
|
}
|
|
112
|
+
|
|
113
|
+
// A config from before programs existed has none, and means no programs.
|
|
114
|
+
function parsePrograms(value: unknown): Record<string, ProgramDeclaration> | null {
|
|
115
|
+
if (value === undefined) {
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof value !== 'object' || value === null) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const programs: Record<string, ProgramDeclaration> = {};
|
|
124
|
+
|
|
125
|
+
for (const [name, program] of Object.entries(value)) {
|
|
126
|
+
const parsed = parseProgram(program);
|
|
127
|
+
|
|
128
|
+
if (parsed === null) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
programs[name] = parsed;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return programs;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function parseProgram(value: unknown): ProgramDeclaration | null {
|
|
139
|
+
if (typeof value !== 'object' || value === null) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const packageFolder: unknown = Reflect.get(value, 'package');
|
|
144
|
+
const source: unknown = Reflect.get(value, 'source');
|
|
145
|
+
const build: unknown = Reflect.get(value, 'build');
|
|
146
|
+
const artifact: unknown = Reflect.get(value, 'artifact');
|
|
147
|
+
|
|
148
|
+
if (
|
|
149
|
+
typeof packageFolder !== 'string' ||
|
|
150
|
+
typeof source !== 'string' ||
|
|
151
|
+
typeof build !== 'string' ||
|
|
152
|
+
typeof artifact !== 'string'
|
|
153
|
+
) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return { package: packageFolder, source, build, artifact };
|
|
158
|
+
}
|
package/src/pick-e2e-dirs.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
// An `e2e` folder counts
|
|
2
|
-
//
|
|
3
|
-
export function pickE2EDirs(
|
|
1
|
+
// An `e2e` folder counts in a folder that holds a package.json, and at the repo root only in a
|
|
2
|
+
// single-package repo: a workspace repo keeps each e2e folder in the package it tests.
|
|
3
|
+
export function pickE2EDirs(
|
|
4
|
+
trackedFiles: readonly string[],
|
|
5
|
+
isWorkspaceRepo: boolean,
|
|
6
|
+
): readonly string[] {
|
|
4
7
|
const tracked = new Set(trackedFiles);
|
|
5
8
|
const dirs = new Set<string>();
|
|
6
9
|
|
|
@@ -10,10 +13,10 @@ export function pickE2EDirs(trackedFiles: readonly string[]): readonly string[]
|
|
|
10
13
|
for (const [index, name] of segments.entries()) {
|
|
11
14
|
const parent = segments.slice(0, index).join('/');
|
|
12
15
|
const prefix = parent === '' ? '' : `${parent}/`;
|
|
16
|
+
const isRoot = parent === '';
|
|
13
17
|
|
|
14
18
|
const isE2EDir =
|
|
15
|
-
|
|
16
|
-
(name.endsWith('-e2e') && tracked.has(`${prefix}${name}/package.json`));
|
|
19
|
+
name === 'e2e' && (isRoot ? !isWorkspaceRepo : tracked.has(`${prefix}package.json`));
|
|
17
20
|
|
|
18
21
|
if (isE2EDir) {
|
|
19
22
|
dirs.add(`${prefix}${name}`);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { isRecord } from '@zgeoff/std';
|
|
3
|
+
|
|
4
|
+
// A repo whose root package.json declares `workspaces` keeps its tests in its packages.
|
|
5
|
+
export async function probeWorkspaceRepo(repoRoot: string): Promise<boolean> {
|
|
6
|
+
const file = Bun.file(join(repoRoot, 'package.json'));
|
|
7
|
+
|
|
8
|
+
if (!(await file.exists())) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const manifest: unknown = await file.json();
|
|
13
|
+
|
|
14
|
+
return isRecord(manifest) && manifest['workspaces'] !== undefined;
|
|
15
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { isRecord } from '@zgeoff/std';
|
|
4
|
+
import { buildProgramVariable } from '../build-program-environment.ts';
|
|
5
|
+
import { parseRigConfig } from '../parse-rig-config.ts';
|
|
6
|
+
import type { ProgramDeclaration } from '../types.ts';
|
|
7
|
+
import { resolveRepoRoot } from './resolve-repo-root.ts';
|
|
8
|
+
|
|
9
|
+
export interface ProgramLaunch {
|
|
10
|
+
readonly command: readonly string[];
|
|
11
|
+
|
|
12
|
+
// The repo root, which the command runs from unless the caller picks another folder.
|
|
13
|
+
readonly cwd: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ProgramLaunchSources {
|
|
17
|
+
readonly environment: Readonly<Record<string, string | undefined>>;
|
|
18
|
+
|
|
19
|
+
readonly cwd: string;
|
|
20
|
+
|
|
21
|
+
readonly importModule: (path: string) => Promise<unknown>;
|
|
22
|
+
|
|
23
|
+
readonly probeFile: (path: string) => boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function readLaunchSources(): ProgramLaunchSources {
|
|
27
|
+
return {
|
|
28
|
+
environment: process.env,
|
|
29
|
+
cwd: process.cwd(),
|
|
30
|
+
importModule: (path) => import(path),
|
|
31
|
+
probeFile: existsSync,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The command that runs a program rig.config.ts declares, and the repo root; the test spawns it.
|
|
36
|
+
// `rig e2e` names the built artifact in the environment. Outside CI, without that variable, the
|
|
37
|
+
// command runs the source with bun, so the test runs under `bun test` with no build.
|
|
38
|
+
export async function resolveProgram(
|
|
39
|
+
name: string,
|
|
40
|
+
sources: ProgramLaunchSources = readLaunchSources(),
|
|
41
|
+
): Promise<ProgramLaunch> {
|
|
42
|
+
const artifact = sources.environment[buildProgramVariable(name)];
|
|
43
|
+
|
|
44
|
+
if (artifact !== undefined && artifact !== '') {
|
|
45
|
+
return {
|
|
46
|
+
command: /\.(?:js|ts)$/u.test(artifact) ? ['bun', artifact] : [artifact],
|
|
47
|
+
cwd: resolveRepoRoot(sources.cwd, sources.probeFile),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const ci = sources.environment['CI'];
|
|
52
|
+
|
|
53
|
+
if (ci !== undefined && ci !== '') {
|
|
54
|
+
throw new Error(`program ${name} isn't built: run the e2e lane with rig e2e`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const repoRoot = resolveRepoRoot(sources.cwd, sources.probeFile);
|
|
58
|
+
|
|
59
|
+
const program = await readProgram(name, repoRoot, sources);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
command: ['bun', join(repoRoot, program.package, program.source)],
|
|
63
|
+
cwd: repoRoot,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function readProgram(
|
|
68
|
+
name: string,
|
|
69
|
+
repoRoot: string,
|
|
70
|
+
sources: ProgramLaunchSources,
|
|
71
|
+
): Promise<ProgramDeclaration> {
|
|
72
|
+
const path = join(repoRoot, 'rig.config.ts');
|
|
73
|
+
|
|
74
|
+
const loaded = await sources.importModule(path);
|
|
75
|
+
|
|
76
|
+
const defaultExport = isRecord(loaded) ? loaded['default'] : null;
|
|
77
|
+
const parsed = parseRigConfig(defaultExport);
|
|
78
|
+
|
|
79
|
+
if (parsed.kind === 'refused') {
|
|
80
|
+
throw new Error(`${path} must default-export the result of defineRig: ${parsed.reason}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const program = Object.hasOwn(parsed.config.programs, name)
|
|
84
|
+
? parsed.config.programs[name]
|
|
85
|
+
: undefined;
|
|
86
|
+
|
|
87
|
+
if (program === undefined) {
|
|
88
|
+
throw new Error(`program ${name} isn't declared: add it to rig.config.ts programs`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return program;
|
|
92
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
|
|
3
|
+
// The repo root is the nearest folder above `cwd`, or `cwd` itself, that holds rig.config.ts.
|
|
4
|
+
export function resolveRepoRoot(cwd: string, probeFile: (path: string) => boolean): string {
|
|
5
|
+
let directory = cwd;
|
|
6
|
+
|
|
7
|
+
while (!probeFile(join(directory, 'rig.config.ts'))) {
|
|
8
|
+
const parent = dirname(directory);
|
|
9
|
+
|
|
10
|
+
if (parent === directory) {
|
|
11
|
+
throw new Error(`no rig.config.ts sits in ${cwd} or any folder above it`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
directory = parent;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return directory;
|
|
18
|
+
}
|
package/src/read-cli-args.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface CLIArgs {
|
|
|
7
7
|
// Every argument after the command, flags and paths alike, which the command passes on to
|
|
8
8
|
// `bun test`.
|
|
9
9
|
readonly extraArgs: readonly string[];
|
|
10
|
+
|
|
11
|
+
readonly hasIsolateOption: boolean;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
const argSchema = { command: { type: 'positional' } } as const;
|
|
@@ -27,6 +29,7 @@ export async function readCLIArgs(argv: readonly string[]): Promise<CLIArgs | st
|
|
|
27
29
|
parsed = {
|
|
28
30
|
command: ctx.values.command,
|
|
29
31
|
extraArgs: dropFirstSeparator(argv.slice(1)),
|
|
32
|
+
hasIsolateOption: false,
|
|
30
33
|
};
|
|
31
34
|
},
|
|
32
35
|
});
|
|
@@ -37,7 +40,13 @@ export async function readCLIArgs(argv: readonly string[]): Promise<CLIArgs | st
|
|
|
37
40
|
return formatParseError(error);
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
if (parsed === null) {
|
|
44
|
+
return 'the command line could not be parsed';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const commandArgs: CLIArgs = parsed;
|
|
48
|
+
|
|
49
|
+
return { ...commandArgs, hasIsolateOption: await probeIsolateOption(commandArgs.extraArgs) };
|
|
41
50
|
}
|
|
42
51
|
|
|
43
52
|
function dropFirstSeparator(args: readonly string[]): readonly string[] {
|
|
@@ -55,3 +64,23 @@ function formatParseError(error: unknown): string {
|
|
|
55
64
|
|
|
56
65
|
return error instanceof Error ? error.message : String(error);
|
|
57
66
|
}
|
|
67
|
+
|
|
68
|
+
const forwardedArgSchema = { isolate: { type: 'boolean' } } as const;
|
|
69
|
+
|
|
70
|
+
// The forwarded arguments hold `bun test` options that rig never declares, so this parse is not
|
|
71
|
+
// strict, and it reads only whether an isolate option, in any of its forms, is among them.
|
|
72
|
+
async function probeIsolateOption(args: readonly string[]): Promise<boolean> {
|
|
73
|
+
let isExplicit = false;
|
|
74
|
+
|
|
75
|
+
const forwarded = define({
|
|
76
|
+
name: 'rig',
|
|
77
|
+
args: forwardedArgSchema,
|
|
78
|
+
run: (ctx) => {
|
|
79
|
+
isExplicit = ctx.explicit.isolate;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await cli([...args], forwarded, { strict: false });
|
|
84
|
+
|
|
85
|
+
return isExplicit;
|
|
86
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { formatBenchmarkResult } from './bench/format-benchmark-result.ts';
|
|
2
|
+
import { parseBenchDefinition } from './bench/parse-bench-definition.ts';
|
|
3
|
+
import { runBenchmark } from './bench/run-benchmark.ts';
|
|
4
|
+
|
|
5
|
+
// `rig bench` runs this once per bench file, in the environment the benchmarks get.
|
|
6
|
+
const file = process.argv[2] ?? '';
|
|
7
|
+
|
|
8
|
+
const loaded: unknown = await import(file);
|
|
9
|
+
|
|
10
|
+
const defaultExport: unknown =
|
|
11
|
+
typeof loaded === 'object' && loaded !== null ? Reflect.get(loaded, 'default') : null;
|
|
12
|
+
|
|
13
|
+
const parsed = parseBenchDefinition(defaultExport);
|
|
14
|
+
|
|
15
|
+
if (parsed.kind === 'refused') {
|
|
16
|
+
process.stderr.write(`rig bench: ${file}: ${parsed.reason}\n`);
|
|
17
|
+
|
|
18
|
+
process.exitCode = 2;
|
|
19
|
+
} else {
|
|
20
|
+
const result = await runBenchmark(parsed.definition, () => performance.now());
|
|
21
|
+
|
|
22
|
+
process.stdout.write(formatBenchmarkResult(parsed.definition, result));
|
|
23
|
+
|
|
24
|
+
process.exitCode = result.medianMs > parsed.definition.budgetMs ? 1 : 0;
|
|
25
|
+
}
|
package/src/run-bench.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { basename, join } from 'node:path';
|
|
2
|
+
import { pickExitCodeAfterTests } from './browser/pick-exit-code-after-tests.ts';
|
|
3
|
+
import { buildDietCommand } from './build-diet-command.ts';
|
|
4
|
+
import { buildProgramLaneHooks } from './build-program-lane-hooks.ts';
|
|
5
|
+
import { collectBenchFiles } from './collect-bench-files.ts';
|
|
6
|
+
import type { LaneHooks } from './lane-hooks.ts';
|
|
7
|
+
import { makeRepoLaneHooks } from './make-repo-lane-hooks.ts';
|
|
8
|
+
import { runLaneHooks } from './run-lane-hooks.ts';
|
|
9
|
+
import type { ProgramBuildDeps } from './run-program-builds.ts';
|
|
10
|
+
import { runProgramBuilds } from './run-program-builds.ts';
|
|
11
|
+
import type { RigConfig, RigRun } from './types.ts';
|
|
12
|
+
|
|
13
|
+
export interface BenchDeps extends ProgramBuildDeps {
|
|
14
|
+
readonly dietScript: string;
|
|
15
|
+
|
|
16
|
+
// The script that runs one bench file.
|
|
17
|
+
readonly benchScript: string;
|
|
18
|
+
|
|
19
|
+
readonly probeWorkspaceRepo: (repoRoot: string) => Promise<boolean>;
|
|
20
|
+
|
|
21
|
+
readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
|
|
22
|
+
|
|
23
|
+
readonly importModule: (path: string) => Promise<unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Builds each program once, as `rig e2e` does, then runs each bench file in a process of its own,
|
|
27
|
+
// between the start and stop hooks of the bench lane when the config names one. The bench process
|
|
28
|
+
// gets RIG_PROGRAM_<NAME> for each program, so resolveProgram gives the built artifact.
|
|
29
|
+
export async function runBench(
|
|
30
|
+
args: readonly string[],
|
|
31
|
+
config: RigConfig,
|
|
32
|
+
run: RigRun,
|
|
33
|
+
deps: BenchDeps,
|
|
34
|
+
): Promise<number> {
|
|
35
|
+
if (args.length > 1) {
|
|
36
|
+
deps.output.err('rig bench: takes at most one benchmark name\n');
|
|
37
|
+
|
|
38
|
+
return 2;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isWorkspaceRepo = await deps.probeWorkspaceRepo(run.repoRoot);
|
|
42
|
+
const trackedFiles = await deps.readTrackedFiles(run.repoRoot);
|
|
43
|
+
|
|
44
|
+
const benchFiles = collectBenchFiles(trackedFiles, isWorkspaceRepo);
|
|
45
|
+
const [name] = args;
|
|
46
|
+
|
|
47
|
+
const files =
|
|
48
|
+
name === undefined ? benchFiles : benchFiles.filter((file) => toBenchName(file) === name);
|
|
49
|
+
|
|
50
|
+
if (files.length === 0) {
|
|
51
|
+
return reportNoBenchmark(name, benchFiles, deps);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const buildExitCode = await runProgramBuilds(config.programs, run, deps);
|
|
55
|
+
|
|
56
|
+
if (buildExitCode !== 0) {
|
|
57
|
+
return buildExitCode;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const hooks = buildProgramLaneHooks(
|
|
61
|
+
makeBenchHooks(config, run, deps),
|
|
62
|
+
run.repoRoot,
|
|
63
|
+
config.programs,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const exitCode = await runLaneHooks(files, hooks);
|
|
67
|
+
|
|
68
|
+
return pickExitCodeAfterTests(exitCode, run.signal.aborted);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function toBenchName(file: string): string {
|
|
72
|
+
return basename(file, '.bench.ts');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function reportNoBenchmark(
|
|
76
|
+
name: string | undefined,
|
|
77
|
+
benchFiles: readonly string[],
|
|
78
|
+
deps: BenchDeps,
|
|
79
|
+
): number {
|
|
80
|
+
if (name === undefined) {
|
|
81
|
+
deps.output.out('rig: the repo has no benchmarks\n');
|
|
82
|
+
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const names = benchFiles.map((file) => toBenchName(file)).join(', ');
|
|
87
|
+
|
|
88
|
+
deps.output.err(`rig bench: no benchmark named "${name}"; the benchmarks are ${names}\n`);
|
|
89
|
+
|
|
90
|
+
return 1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function makeBenchHooks(config: RigConfig, run: RigRun, deps: BenchDeps): LaneHooks {
|
|
94
|
+
const lane = config.lanes.find((candidate) => candidate.name === config.benchLane);
|
|
95
|
+
|
|
96
|
+
const laneHooks: Pick<LaneHooks, 'start' | 'stop'> =
|
|
97
|
+
lane === undefined
|
|
98
|
+
? {
|
|
99
|
+
start: () => Promise.resolve({ kind: 'proceed', environment: run.environment }),
|
|
100
|
+
stop: () => Promise.resolve(),
|
|
101
|
+
}
|
|
102
|
+
: makeRepoLaneHooks({
|
|
103
|
+
context: { repoRoot: run.repoRoot, lane, runID: run.runID, environment: run.environment },
|
|
104
|
+
dietScript: deps.dietScript,
|
|
105
|
+
parallel: 1,
|
|
106
|
+
signal: run.signal,
|
|
107
|
+
importModule: deps.importModule,
|
|
108
|
+
runCommand: deps.runCommand,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
...laneHooks,
|
|
113
|
+
run: async (files, environment) => {
|
|
114
|
+
let exitCode = 0;
|
|
115
|
+
|
|
116
|
+
for (const file of files) {
|
|
117
|
+
const fileExitCode = await deps.runCommand(
|
|
118
|
+
buildDietCommand(deps.dietScript, lane?.memory ?? null, [
|
|
119
|
+
'bun',
|
|
120
|
+
deps.benchScript,
|
|
121
|
+
join(run.repoRoot, file),
|
|
122
|
+
]),
|
|
123
|
+
{ cwd: run.repoRoot, environment, signal: run.signal },
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
exitCode = fileExitCode === 0 ? exitCode : 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return exitCode;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
package/src/run-cli.ts
CHANGED
|
@@ -2,6 +2,10 @@ import { match } from 'ts-pattern';
|
|
|
2
2
|
import { findTimeoutArg } from './find-timeout-arg.ts';
|
|
3
3
|
import { commandNames, isCommandName } from './is-command-name.ts';
|
|
4
4
|
import { readCLIArgs } from './read-cli-args.ts';
|
|
5
|
+
import { runBench } from './run-bench.ts';
|
|
6
|
+
import type { BenchDeps } from './run-bench.ts';
|
|
7
|
+
import { runEval } from './run-eval.ts';
|
|
8
|
+
import type { EvalDeps } from './run-eval.ts';
|
|
5
9
|
import { runInit } from './run-init.ts';
|
|
6
10
|
import type { InitDeps } from './run-init.ts';
|
|
7
11
|
import type { LaneDeps } from './run-lane.ts';
|
|
@@ -12,7 +16,8 @@ import { runUnit } from './run-unit.ts';
|
|
|
12
16
|
import type { UnitDeps } from './run-unit.ts';
|
|
13
17
|
import type { RigConfig, RigRun } from './types.ts';
|
|
14
18
|
|
|
15
|
-
export interface CLIDeps
|
|
19
|
+
export interface CLIDeps
|
|
20
|
+
extends LaneDeps, UnitDeps, RelatedTestDeps, InitDeps, BenchDeps, EvalDeps {
|
|
16
21
|
readonly readConfig: (repoRoot: string) => Promise<RigConfig>;
|
|
17
22
|
}
|
|
18
23
|
|
|
@@ -31,6 +36,8 @@ export async function runCLI(argv: readonly string[], run: RigRun, deps: CLIDeps
|
|
|
31
36
|
|
|
32
37
|
if (isCommandName(command)) {
|
|
33
38
|
return match(command)
|
|
39
|
+
.with('bench', () => runBench(args.extraArgs, config, run, deps))
|
|
40
|
+
.with('eval', () => runEval(args.extraArgs, run, deps))
|
|
34
41
|
.with('init', () => runInit(run.repoRoot, config, deps))
|
|
35
42
|
.with('related', () => runRelatedTests(args.extraArgs, config, run, deps))
|
|
36
43
|
.exhaustive();
|
|
@@ -54,6 +61,14 @@ export async function runCLI(argv: readonly string[], run: RigRun, deps: CLIDeps
|
|
|
54
61
|
return 2;
|
|
55
62
|
}
|
|
56
63
|
|
|
64
|
+
if (args.hasIsolateOption) {
|
|
65
|
+
deps.output.err(
|
|
66
|
+
`rig: rig ${command} refuses --isolate, because every lane runs its files in one global per process\n`,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return 2;
|
|
70
|
+
}
|
|
71
|
+
|
|
57
72
|
return lane.name === 'unit'
|
|
58
73
|
? runUnit(config, args.extraArgs, run, deps)
|
|
59
74
|
: runLane({ config, lane, extraArgs: args.extraArgs }, run, deps);
|