@zgeoff/rig 0.0.4 → 0.0.6
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 +14 -2
- 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/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/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/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/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 +15 -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/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/run-bench-file.ts +25 -0
- package/src/run-bench.ts +132 -0
- package/src/run-cli.ts +8 -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/types.ts +30 -0
|
@@ -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,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,5 +1,7 @@
|
|
|
1
1
|
import type { RigConfig } from './types.ts';
|
|
2
2
|
|
|
3
|
+
const isolationFlags: ReadonlySet<string> = new Set(['--isolate', '--no-isolate']);
|
|
4
|
+
|
|
3
5
|
// The e2e lane has no suffix, so the unit run leaves out the folders that the e2e lane selects,
|
|
4
6
|
// from the same list, and nothing else.
|
|
5
7
|
export function buildUnitCommand(
|
|
@@ -18,5 +20,17 @@ export function buildUnitCommand(
|
|
|
18
20
|
(pattern) => `--path-ignore-patterns=${pattern}`,
|
|
19
21
|
);
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
const isolationArgs = extraArgs.some((argument) => isolationFlags.has(argument))
|
|
24
|
+
? []
|
|
25
|
+
: ['--no-isolate'];
|
|
26
|
+
|
|
27
|
+
return [
|
|
28
|
+
'bun',
|
|
29
|
+
'test',
|
|
30
|
+
`--parallel=${workerCount}`,
|
|
31
|
+
'--randomize',
|
|
32
|
+
...isolationArgs,
|
|
33
|
+
...ignoreArgs,
|
|
34
|
+
...extraArgs,
|
|
35
|
+
];
|
|
22
36
|
}
|
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
|
+
}
|
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
|
|
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
|
+
}
|
|
@@ -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
|
+
}
|