@zgeoff/rig 0.1.6 → 0.2.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 CHANGED
@@ -64,8 +64,9 @@ no file and fails the run.
64
64
 
65
65
  ## Configuration
66
66
 
67
- `rig.config.ts` at the repo root declares what differs from the built-in lanes, and in `ignore` the
68
- [paths that no lane tests](../../docs/architecture/rig.md#ignored-paths):
67
+ `rig.config.ts` at the repo root declares what differs from the built-in lanes, in `ignore` the
68
+ [paths that no lane tests](../../docs/architecture/rig.md#ignored-paths), and in `bunfigs` the
69
+ [bunfig of a set of unit tests](../../docs/architecture/rig.md#the-bunfig-of-each-test):
69
70
 
70
71
  ```ts
71
72
  import { defineRig } from '@zgeoff/rig';
@@ -2,6 +2,7 @@ import type { TestArgs } from './parse-test-args.ts';
2
2
  import type { ResolvedLane, RigConfig } from './types.ts';
3
3
  export type TestSelection = {
4
4
  readonly kind: 'lane';
5
+ readonly files: readonly string[];
5
6
  readonly flags: readonly string[];
6
7
  } | {
7
8
  readonly kind: 'empty';
@@ -1,2 +1,9 @@
1
+ import type { TestRun } from './collect-test-runs.ts';
1
2
  import type { RigConfig } from './types.ts';
2
- export declare function buildUnitCommand(config: RigConfig, e2eDirs: readonly string[], workerCount: number, extraArgs: readonly string[]): readonly string[];
3
+ export interface UnitCommandRequest {
4
+ readonly e2eDirs: readonly string[];
5
+ readonly workerCount: number;
6
+ readonly testRun: TestRun;
7
+ readonly flags: readonly string[];
8
+ }
9
+ export declare function buildUnitCommand(config: RigConfig, request: UnitCommandRequest): readonly string[];
@@ -0,0 +1,5 @@
1
+ import { z } from 'zod';
2
+ export declare const bunfigDeclarationSchema: z.ZodReadonly<z.ZodObject<{
3
+ path: z.ZodString;
4
+ files: z.ZodArray<z.ZodString>;
5
+ }, z.core.$strip>>;
@@ -0,0 +1,14 @@
1
+ import type { BunfigDeclaration } from './types.ts';
2
+ export interface TestRun {
3
+ readonly folder: string;
4
+ readonly bunfig: string | null;
5
+ readonly files: readonly string[] | null;
6
+ readonly ignore: readonly string[];
7
+ }
8
+ export interface TestRunRequest {
9
+ readonly bunfigs: readonly BunfigDeclaration[];
10
+ readonly trackedFiles: readonly string[];
11
+ readonly tests: readonly string[];
12
+ readonly isDiscovery: boolean;
13
+ }
14
+ export declare function collectTestRuns(request: TestRunRequest): TestRun[];
@@ -0,0 +1,5 @@
1
+ export interface SingleRunFlag {
2
+ readonly flag: string;
3
+ readonly holdsPath: boolean;
4
+ }
5
+ export declare function findSingleRunFlag(flags: readonly string[]): SingleRunFlag | undefined;
package/dist/index.js CHANGED
@@ -1,3 +1,19 @@
1
+ // src/bunfig-declaration-schema.ts
2
+ import { z as z2 } from "zod";
3
+
4
+ // src/repo-glob-schema.ts
5
+ import { z } from "zod";
6
+ var repoGlobSchema = z.string().refine((pattern) => pattern !== "" && !pattern.startsWith("/") && !hasDotSegment(pattern));
7
+ function hasDotSegment(path) {
8
+ return path.split("/").some((segment) => segment === "." || segment === "..");
9
+ }
10
+
11
+ // src/bunfig-declaration-schema.ts
12
+ var bunfigDeclarationSchema = z2.object({
13
+ path: repoGlobSchema.refine((path) => path.endsWith(".toml")),
14
+ files: z2.array(repoGlobSchema).min(1)
15
+ }).readonly();
16
+
1
17
  // src/is-command-name.ts
2
18
  var commandNames = ["bench", "eval", "init", "related"];
3
19
  function isCommandName(name) {
@@ -49,6 +65,7 @@ function defineRig(declaration) {
49
65
  ]));
50
66
  return {
51
67
  ignore: (declaration.ignore ?? []).map((pattern) => readIgnorePattern(pattern)),
68
+ bunfigs: (declaration.bunfigs ?? []).map((bunfig) => readBunfig(bunfig)),
52
69
  lanes,
53
70
  programs,
54
71
  benchLane: readBenchLane(declaration.bench?.lane, lanes)
@@ -173,10 +190,28 @@ function readProgram(name, program) {
173
190
  };
174
191
  }
175
192
  function readIgnorePattern(pattern) {
176
- if (typeof pattern !== "string" || pattern === "" || pattern.startsWith("/") || pattern.split("/").includes("..")) {
177
- throw new Error(`rig.config.ts: the ignore pattern ${JSON.stringify(pattern)} must be a non-empty glob relative to the repo root, with no ".." segment`);
193
+ const parsed = repoGlobSchema.safeParse(pattern);
194
+ if (!parsed.success) {
195
+ throw new Error(`rig.config.ts: the ignore pattern ${JSON.stringify(pattern)} must be a non-empty glob relative to the repo root, with no "." or ".." segment`);
196
+ }
197
+ return parsed.data;
198
+ }
199
+ function readBunfig(bunfig) {
200
+ const parsed = bunfigDeclarationSchema.safeParse(bunfig);
201
+ if (!parsed.success) {
202
+ throw new Error(formatBunfigError(bunfig, parsed.error.issues.at(0)?.path ?? []));
203
+ }
204
+ return parsed.data;
205
+ }
206
+ function formatBunfigError(bunfig, issuePath) {
207
+ const [field, index] = issuePath;
208
+ if (field === "path") {
209
+ return `rig.config.ts: the bunfig path ${JSON.stringify(bunfig.path)} must be a .toml file relative to the repo root, with no "." or ".." segment`;
210
+ }
211
+ if (typeof index === "number") {
212
+ return `rig.config.ts: the bunfig "${bunfig.path}" file glob ${JSON.stringify(bunfig.files[index])} must be a non-empty glob relative to the repo root, with no "." or ".." segment`;
178
213
  }
179
- return pattern;
214
+ return `rig.config.ts: the bunfig "${bunfig.path}" needs files, a list of globs`;
180
215
  }
181
216
  function readBenchLane(lane, lanes) {
182
217
  if (lane === undefined) {
@@ -188,6 +223,7 @@ function readBenchLane(lane, lanes) {
188
223
  return lane;
189
224
  }
190
225
  // src/parse-rig-config.ts
226
+ import { z as z3 } from "zod";
191
227
  function parseRigConfig(value) {
192
228
  if (typeof value !== "object" || value === null || !("lanes" in value)) {
193
229
  return { kind: "refused", reason: "the default export has no lanes" };
@@ -216,9 +252,13 @@ function parseRigConfig(value) {
216
252
  if (ignore === null) {
217
253
  return { kind: "refused", reason: "its ignore is no list of patterns" };
218
254
  }
255
+ const bunfigs = parseBunfigs(Reflect.get(value, "bunfigs"));
256
+ if (bunfigs === null) {
257
+ return { kind: "refused", reason: "its bunfigs are no list of bunfig declarations" };
258
+ }
219
259
  return {
220
260
  kind: "parsed",
221
- config: { ignore, lanes: resolved, programs, benchLane: benchLane ?? null }
261
+ config: { ignore, bunfigs, lanes: resolved, programs, benchLane: benchLane ?? null }
222
262
  };
223
263
  }
224
264
  function parseResolvedLane(value, index) {
@@ -298,20 +338,12 @@ function parseProgram(value) {
298
338
  return { package: packageFolder, source, build, artifact };
299
339
  }
300
340
  function parseIgnore(value) {
301
- if (value === undefined) {
302
- return [];
303
- }
304
- if (!Array.isArray(value)) {
305
- return null;
306
- }
307
- const patterns = [];
308
- for (const pattern of value) {
309
- if (typeof pattern !== "string") {
310
- return null;
311
- }
312
- patterns.push(pattern);
313
- }
314
- return patterns;
341
+ const parsed = z3.array(repoGlobSchema).optional().safeParse(value);
342
+ return parsed.success ? parsed.data ?? [] : null;
343
+ }
344
+ function parseBunfigs(value) {
345
+ const parsed = z3.array(bunfigDeclarationSchema).optional().safeParse(value);
346
+ return parsed.success ? parsed.data ?? [] : null;
315
347
  }
316
348
  // src/browser/run-in-page.ts
317
349
  async function runInPage(view, pageFunction, argument, schema) {
@@ -0,0 +1,15 @@
1
+ import type { TestRun } from './collect-test-runs.ts';
2
+ import type { RigConfig } from './types.ts';
3
+ export interface UnitRunPlanRequest {
4
+ readonly flags: readonly string[];
5
+ readonly tests: readonly string[];
6
+ readonly isDiscovery: boolean;
7
+ }
8
+ export type UnitRunPlan = {
9
+ readonly kind: 'refused';
10
+ readonly message: string;
11
+ } | {
12
+ readonly kind: 'runs';
13
+ readonly testRuns: readonly TestRun[];
14
+ };
15
+ export declare function pickUnitRunPlan(config: RigConfig, request: UnitRunPlanRequest, trackedFiles: readonly string[]): UnitRunPlan;
@@ -0,0 +1,2 @@
1
+ import { z } from 'zod';
2
+ export declare const repoGlobSchema: z.ZodString;
@@ -1,10 +1,9 @@
1
1
  import type { WorkerCount } from './read-worker-count.ts';
2
2
  import type { RelatedTestSources } from './resolve-related-tests.ts';
3
3
  import type { UnitRunDeps } from './run-unit-tests.ts';
4
- import type { RigConfig, RigOutput, RigRun } from './types.ts';
4
+ import type { RigConfig, RigRun } from './types.ts';
5
5
  export interface RelatedTestDeps extends UnitRunDeps {
6
6
  readonly sources: Readonly<RelatedTestSources>;
7
7
  readonly readWorkerCount: () => Promise<WorkerCount>;
8
- readonly output: RigOutput;
9
8
  }
10
9
  export declare function runRelatedTests(args: readonly string[], config: RigConfig, run: RigRun, deps: Readonly<RelatedTestDeps>): Promise<number>;
@@ -1,13 +1,14 @@
1
+ import type { UnitRunPlanRequest } from './pick-unit-run-plan.ts';
1
2
  import type { CommandOptions } from './run-command.ts';
2
- import type { RigConfig, RigRun } from './types.ts';
3
- export interface UnitRequest {
3
+ import type { RigConfig, RigOutput, RigRun } from './types.ts';
4
+ export interface UnitRequest extends UnitRunPlanRequest {
4
5
  readonly workerCount: number;
5
- readonly extraArgs: readonly string[];
6
6
  }
7
7
  export interface UnitRunDeps {
8
8
  readonly dietScript: string;
9
9
  readonly probeWorkspaceRepo: (repoRoot: string) => Promise<boolean>;
10
10
  readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
11
11
  readonly runCommand: (command: readonly string[], options: CommandOptions) => Promise<number>;
12
+ readonly output: RigOutput;
12
13
  }
13
14
  export declare function runUnitTests(config: RigConfig, request: UnitRequest, run: RigRun, deps: UnitRunDeps): Promise<number>;
@@ -1,8 +1,7 @@
1
1
  import type { WorkerCount } from './read-worker-count.ts';
2
2
  import type { UnitRunDeps } from './run-unit-tests.ts';
3
- import type { RigConfig, RigOutput, RigRun } from './types.ts';
3
+ import type { RigConfig, RigRun } from './types.ts';
4
4
  export interface UnitDeps extends UnitRunDeps {
5
5
  readonly readWorkerCount: () => Promise<WorkerCount>;
6
- readonly output: RigOutput;
7
6
  }
8
7
  export declare function runUnit(config: RigConfig, extraArgs: readonly string[], run: RigRun, deps: UnitDeps): Promise<number>;
package/dist/types.d.ts CHANGED
@@ -14,8 +14,13 @@ export interface ProgramDeclaration {
14
14
  export interface BenchDeclaration {
15
15
  readonly lane?: string;
16
16
  }
17
+ export interface BunfigDeclaration {
18
+ readonly path: string;
19
+ readonly files: readonly string[];
20
+ }
17
21
  export interface RigDeclaration {
18
22
  readonly ignore?: readonly string[];
23
+ readonly bunfigs?: readonly BunfigDeclaration[];
19
24
  readonly lanes?: Readonly<Record<string, LaneDeclaration>>;
20
25
  readonly programs?: Readonly<Record<string, ProgramDeclaration>>;
21
26
  readonly bench?: BenchDeclaration;
@@ -32,6 +37,7 @@ export interface ResolvedLane {
32
37
  }
33
38
  export interface RigConfig {
34
39
  readonly ignore: readonly string[];
40
+ readonly bunfigs: readonly BunfigDeclaration[];
35
41
  readonly lanes: readonly ResolvedLane[];
36
42
  readonly programs: Readonly<Record<string, ProgramDeclaration>>;
37
43
  readonly benchLane: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/rig",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "Run every test lane of a repo from one declaration of its limits, with a browser lane built in",
5
5
  "keywords": [
6
6
  "bun",
@@ -8,7 +8,7 @@ import { pickE2EDirs } from './pick-e2e-dirs.ts';
8
8
  import type { ResolvedLane, RigConfig } from './types.ts';
9
9
 
10
10
  export type TestSelection =
11
- | { readonly kind: 'lane'; readonly flags: readonly string[] }
11
+ | { readonly kind: 'lane'; readonly files: readonly string[]; readonly flags: readonly string[] }
12
12
  | { readonly kind: 'empty' }
13
13
  | { readonly kind: 'files'; readonly files: readonly string[]; readonly flags: readonly string[] }
14
14
  | { readonly kind: 'none'; readonly paths: readonly string[] };
@@ -34,7 +34,9 @@ export function buildTestSelection(request: TestSelectionRequest): TestSelection
34
34
  const laneFiles = collectLaneTestFiles(request);
35
35
 
36
36
  if (split.paths.length === 0) {
37
- return laneFiles.length === 0 ? { kind: 'empty' } : { kind: 'lane', flags: split.flags };
37
+ return laneFiles.length === 0
38
+ ? { kind: 'empty' }
39
+ : { kind: 'lane', files: laneFiles, flags: split.flags };
38
40
  }
39
41
 
40
42
  const selectors = split.paths.map((path) =>
@@ -47,7 +49,7 @@ export function buildTestSelection(request: TestSelectionRequest): TestSelection
47
49
 
48
50
  return files.length === 0
49
51
  ? { kind: 'none', paths: split.paths }
50
- : { kind: 'files', files: files.map((file) => `./${file}`), flags: split.flags };
52
+ : { kind: 'files', files, flags: split.flags };
51
53
  }
52
54
 
53
55
  function collectLaneTestFiles(request: TestSelectionRequest): readonly string[] {
@@ -1,24 +1,68 @@
1
+ import { posix } from 'node:path';
2
+ import type { TestRun } from './collect-test-runs.ts';
1
3
  import { testRunArgs } from './test-run-args.ts';
2
4
  import type { RigConfig } from './types.ts';
3
5
 
6
+ export interface UnitCommandRequest {
7
+ readonly e2eDirs: readonly string[];
8
+
9
+ readonly workerCount: number;
10
+
11
+ readonly testRun: TestRun;
12
+
13
+ readonly flags: readonly string[];
14
+ }
15
+
4
16
  // The e2e lane has no suffix, so the unit run leaves out the folders that the e2e lane selects,
5
17
  // from the same list. A --path-ignore-patterns argument replaces bunfig.toml's pathIgnorePatterns,
6
18
  // so the config's ignore list is the only place a repo can leave a path out of the unit run.
7
19
  export function buildUnitCommand(
8
20
  config: RigConfig,
9
- e2eDirs: readonly string[],
10
- workerCount: number,
11
- extraArgs: readonly string[],
21
+ request: UnitCommandRequest,
12
22
  ): readonly string[] {
23
+ const testRun = request.testRun;
24
+
13
25
  const suffixPatterns = config.lanes.flatMap((lane) =>
14
26
  lane.suffix === null ? [] : [`**/*${lane.suffix}.test.ts`, `**/*${lane.suffix}.test.tsx`],
15
27
  );
16
28
 
17
- const e2ePatterns = e2eDirs.map((directory) => `${directory}/**`);
29
+ const e2ePatterns = request.e2eDirs.map((directory) => `${directory}/**`);
18
30
 
19
- const ignoreArgs = [...suffixPatterns, ...e2ePatterns, ...config.ignore].map(
20
- (pattern) => `--path-ignore-patterns=${pattern}`,
31
+ const patterns = collectFolderPatterns(
32
+ [...suffixPatterns, ...e2ePatterns, ...config.ignore, ...testRun.ignore],
33
+ testRun.folder,
21
34
  );
22
35
 
23
- return ['bun', 'test', `--parallel=${workerCount}`, ...testRunArgs, ...ignoreArgs, ...extraArgs];
36
+ const ignoreArgs = patterns.map((pattern) => `--path-ignore-patterns=${pattern}`);
37
+ const configArgs = testRun.bunfig === null ? [] : [`--config=./${testRun.bunfig}`];
38
+
39
+ // bun test reads a bare argument as a name filter; the ./ makes it a path.
40
+ const fileArgs = (testRun.files ?? []).map((file) => `./${posix.relative(testRun.folder, file)}`);
41
+
42
+ return [
43
+ 'bun',
44
+ 'test',
45
+ ...configArgs,
46
+ `--parallel=${request.workerCount}`,
47
+ ...testRunArgs,
48
+ ...ignoreArgs,
49
+ ...request.flags,
50
+ ...fileArgs,
51
+ ];
52
+ }
53
+
54
+ // bun test matches a glob against paths relative to the folder it runs from. A glob that starts
55
+ // with `**/` matches in any folder, and a glob for another folder can match nothing in this one.
56
+ function collectFolderPatterns(patterns: readonly string[], folder: string): readonly string[] {
57
+ if (folder === '') {
58
+ return patterns;
59
+ }
60
+
61
+ return patterns.flatMap((pattern) => {
62
+ if (pattern.startsWith('**/')) {
63
+ return [pattern];
64
+ }
65
+
66
+ return pattern.startsWith(`${folder}/`) ? [pattern.slice(folder.length + 1)] : [];
67
+ });
24
68
  }
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod';
2
+ import { repoGlobSchema } from './repo-glob-schema.ts';
3
+
4
+ // rig runs the bunfig's tests from the repo root, and compares a changed file's bare path with the
5
+ // bunfig's, so the path follows the same rules as a glob.
6
+ export const bunfigDeclarationSchema = z
7
+ .object({
8
+ path: repoGlobSchema.refine((path) => path.endsWith('.toml')),
9
+ files: z.array(repoGlobSchema).min(1),
10
+ })
11
+ .readonly();
@@ -0,0 +1,102 @@
1
+ import { makeIgnoreCheck } from './make-ignore-check.ts';
2
+ import type { BunfigDeclaration } from './types.ts';
3
+
4
+ // One `bun test` process: the folder it runs from, the bunfig it reads, and its tests.
5
+ export interface TestRun {
6
+ // Relative to the repo root, and '' for the root.
7
+ readonly folder: string;
8
+
9
+ // A path relative to the repo root that the run passes as --config, or null for the bunfig.toml
10
+ // of the run's folder.
11
+ readonly bunfig: string | null;
12
+
13
+ // Test files relative to the repo root, or null when bun test finds the tests under the folder.
14
+ readonly files: readonly string[] | null;
15
+
16
+ // Globs relative to the repo root of the tests that another run takes.
17
+ readonly ignore: readonly string[];
18
+ }
19
+
20
+ export interface TestRunRequest {
21
+ readonly bunfigs: readonly BunfigDeclaration[];
22
+
23
+ readonly trackedFiles: readonly string[];
24
+
25
+ // The unit tests to run, relative to the repo root.
26
+ readonly tests: readonly string[];
27
+
28
+ // True when the root run finds its own tests, as a run of the whole unit lane does.
29
+ readonly isDiscovery: boolean;
30
+ }
31
+
32
+ interface TestGroup {
33
+ readonly folder: string;
34
+
35
+ readonly bunfig: string | null;
36
+
37
+ readonly files: string[];
38
+ }
39
+
40
+ // bun test reads only the bunfig.toml of the folder it runs from, and resolves a preload against
41
+ // that folder, even under --config. So a package with a bunfig.toml of its own runs its tests from
42
+ // its folder, and a bunfigs entry runs its tests from the repo root.
43
+ export function collectTestRuns(request: TestRunRequest): TestRun[] {
44
+ const packageFolders = collectPackageBunfigFolders(request.trackedFiles);
45
+
46
+ const bunfigChecks = request.bunfigs.map((bunfig) => ({
47
+ path: bunfig.path,
48
+ isBunfigFile: makeIgnoreCheck(bunfig.files),
49
+ }));
50
+
51
+ const groups = new Map<string, TestGroup>();
52
+
53
+ for (const test of request.tests) {
54
+ const bunfig = bunfigChecks.find((check) => check.isBunfigFile(test))?.path ?? null;
55
+
56
+ const folder =
57
+ bunfig === null ? (packageFolders.find((owner) => test.startsWith(`${owner}/`)) ?? '') : '';
58
+
59
+ const key = JSON.stringify([folder, bunfig]);
60
+ const group = groups.get(key) ?? { folder, bunfig, files: [] };
61
+
62
+ group.files.push(test);
63
+ groups.set(key, group);
64
+ }
65
+
66
+ const otherRunGlobs = [
67
+ ...packageFolders.map((folder) => `${folder}/**`),
68
+ ...request.bunfigs.flatMap((bunfig) => bunfig.files),
69
+ ];
70
+
71
+ return [...groups.values()]
72
+ .toSorted((first, second) => toSortKey(first).localeCompare(toSortKey(second)))
73
+ .map((group) => {
74
+ const isRootDiscovery = request.isDiscovery && group.folder === '' && group.bunfig === null;
75
+
76
+ return {
77
+ folder: group.folder,
78
+ bunfig: group.bunfig,
79
+ files: isRootDiscovery ? null : group.files,
80
+ ignore: isRootDiscovery ? otherRunGlobs : [],
81
+ };
82
+ });
83
+ }
84
+
85
+ // The deepest package folder above a test owns it, so the list runs from the deepest folder up.
86
+ function collectPackageBunfigFolders(trackedFiles: readonly string[]): readonly string[] {
87
+ const tracked = new Set(trackedFiles);
88
+
89
+ const suffix = '/bunfig.toml';
90
+
91
+ return trackedFiles
92
+ .filter((file) => file.endsWith(suffix))
93
+ .map((file) => file.slice(0, -suffix.length))
94
+ .filter((folder) => tracked.has(`${folder}/package.json`))
95
+ .toSorted((first, second) => second.length - first.length);
96
+ }
97
+
98
+ function toSortKey(group: TestGroup): string {
99
+ return group.folder === '' && group.bunfig === null
100
+ ? ''
101
+ : `${group.folder}\0${group.bunfig ?? ''}`;
102
+ }
package/src/define-rig.ts CHANGED
@@ -1,8 +1,11 @@
1
+ import { bunfigDeclarationSchema } from './bunfig-declaration-schema.ts';
1
2
  import { isCommandName } from './is-command-name.ts';
2
3
  import { parseDuration } from './parse-duration.ts';
4
+ import { repoGlobSchema } from './repo-glob-schema.ts';
3
5
  import { standardLanes } from './standard-lanes.ts';
4
6
  import type { StandardLane } from './standard-lanes.ts';
5
7
  import type {
8
+ BunfigDeclaration,
6
9
  LaneDeclaration,
7
10
  ProgramDeclaration,
8
11
  ResolvedLane,
@@ -50,6 +53,7 @@ export function defineRig(declaration: RigDeclaration): RigConfig {
50
53
 
51
54
  return {
52
55
  ignore: (declaration.ignore ?? []).map((pattern) => readIgnorePattern(pattern)),
56
+ bunfigs: (declaration.bunfigs ?? []).map((bunfig) => readBunfig(bunfig)),
53
57
  lanes,
54
58
  programs,
55
59
  benchLane: readBenchLane(declaration.bench?.lane, lanes),
@@ -250,21 +254,40 @@ function readProgram(name: string, program: ProgramDeclaration): ProgramDeclarat
250
254
  };
251
255
  }
252
256
 
253
- // bun test matches an ignore pattern against paths relative to the repo root, so an absolute
254
- // pattern or one that climbs out of the repo would never match.
255
257
  function readIgnorePattern(pattern: unknown): string {
256
- if (
257
- typeof pattern !== 'string' ||
258
- pattern === '' ||
259
- pattern.startsWith('/') ||
260
- pattern.split('/').includes('..')
261
- ) {
258
+ const parsed = repoGlobSchema.safeParse(pattern);
259
+
260
+ if (!parsed.success) {
262
261
  throw new Error(
263
- `rig.config.ts: the ignore pattern ${JSON.stringify(pattern)} must be a non-empty glob relative to the repo root, with no ".." segment`,
262
+ `rig.config.ts: the ignore pattern ${JSON.stringify(pattern)} must be a non-empty glob relative to the repo root, with no "." or ".." segment`,
264
263
  );
265
264
  }
266
265
 
267
- return pattern;
266
+ return parsed.data;
267
+ }
268
+
269
+ function readBunfig(bunfig: BunfigDeclaration): BunfigDeclaration {
270
+ const parsed = bunfigDeclarationSchema.safeParse(bunfig);
271
+
272
+ if (!parsed.success) {
273
+ throw new Error(formatBunfigError(bunfig, parsed.error.issues.at(0)?.path ?? []));
274
+ }
275
+
276
+ return parsed.data;
277
+ }
278
+
279
+ function formatBunfigError(bunfig: BunfigDeclaration, issuePath: readonly PropertyKey[]): string {
280
+ const [field, index] = issuePath;
281
+
282
+ if (field === 'path') {
283
+ return `rig.config.ts: the bunfig path ${JSON.stringify(bunfig.path)} must be a .toml file relative to the repo root, with no "." or ".." segment`;
284
+ }
285
+
286
+ if (typeof index === 'number') {
287
+ return `rig.config.ts: the bunfig "${bunfig.path}" file glob ${JSON.stringify(bunfig.files[index])} must be a non-empty glob relative to the repo root, with no "." or ".." segment`;
288
+ }
289
+
290
+ return `rig.config.ts: the bunfig "${bunfig.path}" needs files, a list of globs`;
268
291
  }
269
292
 
270
293
  function readBenchLane(lane: string | undefined, lanes: readonly ResolvedLane[]): string | null {
@@ -0,0 +1,33 @@
1
+ export interface SingleRunFlag {
2
+ readonly flag: string;
3
+
4
+ // True when the flag writes or reads a path that resolves against the folder the run starts from.
5
+ readonly holdsPath: boolean;
6
+ }
7
+
8
+ // A `--watch` run never exits, so the runs after it never start. Each `--coverage` or
9
+ // `--reporter-outfile` run writes its file relative to its own folder, or over the one before it,
10
+ // and a relative `--preload` resolves against each run's folder. All need one `bun test` run.
11
+ const singleRunFlags = [
12
+ { name: '--watch', holdsPath: false },
13
+ { name: '--coverage', holdsPath: true },
14
+ { name: '--reporter-outfile', holdsPath: true },
15
+ { name: '--preload', holdsPath: true },
16
+ ];
17
+
18
+ export function findSingleRunFlag(flags: readonly string[]): SingleRunFlag | undefined {
19
+ for (const flag of flags) {
20
+ const known = singleRunFlags.find(
21
+ (candidate) =>
22
+ flag === candidate.name ||
23
+ flag.startsWith(`${candidate.name}=`) ||
24
+ flag.startsWith(`${candidate.name}-`),
25
+ );
26
+
27
+ if (known !== undefined) {
28
+ return { flag, holdsPath: known.holdsPath };
29
+ }
30
+ }
31
+
32
+ return undefined;
33
+ }
@@ -1,4 +1,13 @@
1
- import type { LaneParallelism, ProgramDeclaration, ResolvedLane, RigConfig } from './types.ts';
1
+ import { z } from 'zod';
2
+ import { bunfigDeclarationSchema } from './bunfig-declaration-schema.ts';
3
+ import { repoGlobSchema } from './repo-glob-schema.ts';
4
+ import type {
5
+ BunfigDeclaration,
6
+ LaneParallelism,
7
+ ProgramDeclaration,
8
+ ResolvedLane,
9
+ RigConfig,
10
+ } from './types.ts';
2
11
 
3
12
  export type ParsedRigConfig =
4
13
  | { readonly kind: 'parsed'; readonly config: RigConfig }
@@ -47,9 +56,15 @@ export function parseRigConfig(value: unknown): ParsedRigConfig {
47
56
  return { kind: 'refused', reason: 'its ignore is no list of patterns' };
48
57
  }
49
58
 
59
+ const bunfigs = parseBunfigs(Reflect.get(value, 'bunfigs'));
60
+
61
+ if (bunfigs === null) {
62
+ return { kind: 'refused', reason: 'its bunfigs are no list of bunfig declarations' };
63
+ }
64
+
50
65
  return {
51
66
  kind: 'parsed',
52
- config: { ignore, lanes: resolved, programs, benchLane: benchLane ?? null },
67
+ config: { ignore, bunfigs, lanes: resolved, programs, benchLane: benchLane ?? null },
53
68
  };
54
69
  }
55
70
 
@@ -164,23 +179,14 @@ function parseProgram(value: unknown): ProgramDeclaration | null {
164
179
  }
165
180
 
166
181
  function parseIgnore(value: unknown): string[] | null {
167
- if (value === undefined) {
168
- return [];
169
- }
182
+ const parsed = z.array(repoGlobSchema).optional().safeParse(value);
170
183
 
171
- if (!Array.isArray(value)) {
172
- return null;
173
- }
174
-
175
- const patterns: string[] = [];
176
-
177
- for (const pattern of value) {
178
- if (typeof pattern !== 'string') {
179
- return null;
180
- }
184
+ return parsed.success ? (parsed.data ?? []) : null;
185
+ }
181
186
 
182
- patterns.push(pattern);
183
- }
187
+ // A config may leave bunfigs out, and then every unit test runs under the bunfig of its folder.
188
+ function parseBunfigs(value: unknown): readonly BunfigDeclaration[] | null {
189
+ const parsed = z.array(bunfigDeclarationSchema).optional().safeParse(value);
184
190
 
185
- return patterns;
191
+ return parsed.success ? (parsed.data ?? []) : null;
186
192
  }
@@ -0,0 +1,55 @@
1
+ import type { TestRun } from './collect-test-runs.ts';
2
+ import { collectTestRuns } from './collect-test-runs.ts';
3
+ import { findSingleRunFlag } from './find-single-run-flag.ts';
4
+ import type { RigConfig } from './types.ts';
5
+
6
+ export interface UnitRunPlanRequest {
7
+ readonly flags: readonly string[];
8
+
9
+ // The unit tests to run, relative to the repo root.
10
+ readonly tests: readonly string[];
11
+
12
+ // True when the run takes the whole unit lane, so the root run finds its own tests.
13
+ readonly isDiscovery: boolean;
14
+ }
15
+
16
+ export type UnitRunPlan =
17
+ | { readonly kind: 'refused'; readonly message: string }
18
+ | { readonly kind: 'runs'; readonly testRuns: readonly TestRun[] };
19
+
20
+ export function pickUnitRunPlan(
21
+ config: RigConfig,
22
+ request: UnitRunPlanRequest,
23
+ trackedFiles: readonly string[],
24
+ ): UnitRunPlan {
25
+ const testRuns = collectTestRuns({
26
+ bunfigs: config.bunfigs,
27
+ trackedFiles,
28
+ tests: request.tests,
29
+ isDiscovery: request.isDiscovery,
30
+ });
31
+
32
+ const singleRunFlag = findSingleRunFlag(request.flags);
33
+
34
+ if (singleRunFlag === undefined) {
35
+ return { kind: 'runs', testRuns };
36
+ }
37
+
38
+ if (testRuns.length > 1) {
39
+ return {
40
+ kind: 'refused',
41
+ message: `rig: ${singleRunFlag.flag} needs the tests in one bun test run, and these tests need ${testRuns.length}, one for each bunfig; name the tests of one bunfig\n`,
42
+ };
43
+ }
44
+
45
+ const packageFolder = testRuns.find((testRun) => testRun.folder !== '')?.folder;
46
+
47
+ if (singleRunFlag.holdsPath && packageFolder !== undefined) {
48
+ return {
49
+ kind: 'refused',
50
+ message: `rig: ${singleRunFlag.flag} holds a path, and these tests run from ${packageFolder} under its own bunfig.toml, so the path would resolve there; run bun test from that folder\n`,
51
+ };
52
+ }
53
+
54
+ return { kind: 'runs', testRuns };
55
+ }
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod';
2
+
3
+ // rig matches each glob against bare paths relative to the repo root, so an absolute glob, one
4
+ // that climbs out of the repo, or one with a `./` segment would never match.
5
+ export const repoGlobSchema = z
6
+ .string()
7
+ .refine((pattern) => pattern !== '' && !pattern.startsWith('/') && !hasDotSegment(pattern));
8
+
9
+ function hasDotSegment(path: string): boolean {
10
+ return path.split('/').some((segment) => segment === '.' || segment === '..');
11
+ }
package/src/run-lane.ts CHANGED
@@ -130,8 +130,13 @@ function pickLaneTestArgs(
130
130
  ): readonly string[] {
131
131
  const timeoutArg = `--timeout=${lane.limitMs}`;
132
132
 
133
+ // bun test reads a bare argument as a name filter; the ./ makes it a path.
133
134
  return match(selection)
134
- .with({ kind: 'files' }, (files) => [timeoutArg, ...files.files, ...files.flags])
135
+ .with({ kind: 'files' }, (files) => [
136
+ timeoutArg,
137
+ ...files.files.map((file) => `./${file}`),
138
+ ...files.flags,
139
+ ])
135
140
  .with({ kind: 'lane' }, (laneRun) => [
136
141
  timeoutArg,
137
142
  ...pickLaneSelection(lane, e2eDirs),
@@ -5,7 +5,7 @@ import { resolveRelatedTests } from './resolve-related-tests.ts';
5
5
  import type { RelatedTestSources } from './resolve-related-tests.ts';
6
6
  import type { UnitRunDeps } from './run-unit-tests.ts';
7
7
  import { runUnitTests } from './run-unit-tests.ts';
8
- import type { RigConfig, RigOutput, RigRun } from './types.ts';
8
+ import type { RigConfig, RigRun } from './types.ts';
9
9
 
10
10
  // A host with one worker runs each file slower than CI, and a test that passes there must not fail
11
11
  // the push.
@@ -15,8 +15,6 @@ export interface RelatedTestDeps extends UnitRunDeps {
15
15
  readonly sources: Readonly<RelatedTestSources>;
16
16
 
17
17
  readonly readWorkerCount: () => Promise<WorkerCount>;
18
-
19
- readonly output: RigOutput;
20
18
  }
21
19
 
22
20
  // The related run is the unit lane on the tests beside the changed files, in this process.
@@ -36,6 +34,7 @@ export async function runRelatedTests(
36
34
 
37
35
  const plan = buildRelatedTestPlan(
38
36
  options.paths,
37
+ config,
39
38
  resolveRelatedTests(options.paths, config, deps.sources),
40
39
  );
41
40
 
@@ -55,10 +54,12 @@ export async function runRelatedTests(
55
54
 
56
55
  const timeoutArgs = workers.count === 1 ? [`--timeout=${slowHostTestTimeoutMs}`] : [];
57
56
 
58
- // bun test reads a bare argument as a name filter; the ./ makes it a path.
59
- const testArgs = [...timeoutArgs, ...plan.tests.map((path) => `./${path}`)];
60
-
61
- return runUnitTests(config, { workerCount: workers.count, extraArgs: testArgs }, run, deps);
57
+ return runUnitTests(
58
+ config,
59
+ { workerCount: workers.count, flags: timeoutArgs, tests: plan.tests, isDiscovery: false },
60
+ run,
61
+ deps,
62
+ );
62
63
  }
63
64
 
64
65
  type RelatedTestArgs =
@@ -83,8 +84,13 @@ type RelatedTestPlan =
83
84
 
84
85
  const maxTestFiles = 50;
85
86
 
86
- function buildRelatedTestPlan(paths: readonly string[], tests: readonly string[]): RelatedTestPlan {
87
- const shared = paths.find((path) => isSharedTestInput(path));
87
+ function buildRelatedTestPlan(
88
+ paths: readonly string[],
89
+ config: RigConfig,
90
+ tests: readonly string[],
91
+ ): RelatedTestPlan {
92
+ const bunfigPaths = config.bunfigs.map((bunfig) => bunfig.path);
93
+ const shared = paths.find((path) => isSharedTestInput(path, bunfigPaths));
88
94
 
89
95
  if (shared !== undefined) {
90
96
  return {
@@ -107,7 +113,12 @@ function buildRelatedTestPlan(paths: readonly string[], tests: readonly string[]
107
113
  return { kind: 'run', tests };
108
114
  }
109
115
 
110
- // A shared helper or a bunfig.toml reaches tests beyond the siblings of the files it changes.
111
- function isSharedTestInput(path: string): boolean {
112
- return /(?:^|\/)test-utils\//u.test(path) || basename(path) === 'bunfig.toml';
116
+ // A shared helper, a bunfig.toml or a bunfig that the config names reaches tests beyond the
117
+ // siblings of the files it changes.
118
+ function isSharedTestInput(path: string, bunfigPaths: readonly string[]): boolean {
119
+ return (
120
+ /(?:^|\/)test-utils\//u.test(path) ||
121
+ basename(path) === 'bunfig.toml' ||
122
+ bunfigPaths.includes(path)
123
+ );
113
124
  }
@@ -1,13 +1,16 @@
1
+ import { join } from 'node:path';
2
+ import { match } from 'ts-pattern';
1
3
  import { buildDietCommand } from './build-diet-command.ts';
2
4
  import { buildUnitCommand } from './build-unit-command.ts';
5
+ import type { TestRun } from './collect-test-runs.ts';
3
6
  import { pickE2EDirs } from './pick-e2e-dirs.ts';
7
+ import type { UnitRunPlanRequest } from './pick-unit-run-plan.ts';
8
+ import { pickUnitRunPlan } from './pick-unit-run-plan.ts';
4
9
  import type { CommandOptions } from './run-command.ts';
5
- import type { RigConfig, RigRun } from './types.ts';
10
+ import type { RigConfig, RigOutput, RigRun } from './types.ts';
6
11
 
7
- export interface UnitRequest {
12
+ export interface UnitRequest extends UnitRunPlanRequest {
8
13
  readonly workerCount: number;
9
-
10
- readonly extraArgs: readonly string[];
11
14
  }
12
15
 
13
16
  export interface UnitRunDeps {
@@ -18,10 +21,10 @@ export interface UnitRunDeps {
18
21
  readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
19
22
 
20
23
  readonly runCommand: (command: readonly string[], options: CommandOptions) => Promise<number>;
24
+
25
+ readonly output: RigOutput;
21
26
  }
22
27
 
23
- // The unit lane's command is built from the lane table, so a lane that the table lists never
24
- // reaches the unit run.
25
28
  export async function runUnitTests(
26
29
  config: RigConfig,
27
30
  request: UnitRequest,
@@ -31,15 +34,68 @@ export async function runUnitTests(
31
34
  const trackedFiles = await deps.readTrackedFiles(run.repoRoot);
32
35
  const isWorkspaceRepo = await deps.probeWorkspaceRepo(run.repoRoot);
33
36
 
34
- const e2eDirs = pickE2EDirs(trackedFiles, isWorkspaceRepo);
35
- const memory = config.lanes.find((lane) => lane.name === 'unit')?.memory ?? null;
36
-
37
- return deps.runCommand(
38
- buildDietCommand(
39
- deps.dietScript,
40
- memory,
41
- buildUnitCommand(config, e2eDirs, request.workerCount, request.extraArgs),
42
- ),
43
- { cwd: run.repoRoot, environment: run.environment, signal: run.signal },
44
- );
37
+ return match(pickUnitRunPlan(config, request, trackedFiles))
38
+ .with({ kind: 'refused' }, (refused) => {
39
+ deps.output.err(refused.message);
40
+
41
+ return 2;
42
+ })
43
+ .with({ kind: 'runs' }, (plan) =>
44
+ runTestRuns(plan.testRuns, pickE2EDirs(trackedFiles, isWorkspaceRepo), {
45
+ config,
46
+ request,
47
+ run,
48
+ deps,
49
+ }),
50
+ )
51
+ .exhaustive();
52
+ }
53
+
54
+ interface TestRunsContext {
55
+ readonly config: RigConfig;
56
+
57
+ readonly request: UnitRequest;
58
+
59
+ readonly run: RigRun;
60
+
61
+ readonly deps: UnitRunDeps;
62
+ }
63
+
64
+ // The unit lane's command is built from the lane table, so a lane that the table lists never
65
+ // reaches the unit run. Every run goes ahead after one fails, so the output shows every failure.
66
+ async function runTestRuns(
67
+ testRuns: readonly TestRun[],
68
+ e2eDirs: readonly string[],
69
+ ctx: TestRunsContext,
70
+ ): Promise<number> {
71
+ const memory = ctx.config.lanes.find((lane) => lane.name === 'unit')?.memory ?? null;
72
+ let exitCode = 0;
73
+
74
+ for (const testRun of testRuns) {
75
+ const runExitCode = await ctx.deps.runCommand(
76
+ buildDietCommand(
77
+ ctx.deps.dietScript,
78
+ memory,
79
+ buildUnitCommand(ctx.config, {
80
+ e2eDirs,
81
+ workerCount: ctx.request.workerCount,
82
+ testRun,
83
+ flags: ctx.request.flags,
84
+ }),
85
+ ),
86
+ {
87
+ cwd: join(ctx.run.repoRoot, testRun.folder),
88
+ environment: ctx.run.environment,
89
+ signal: ctx.run.signal,
90
+ },
91
+ );
92
+
93
+ exitCode = exitCode === 0 ? runExitCode : exitCode;
94
+
95
+ if (ctx.run.signal.aborted) {
96
+ break;
97
+ }
98
+ }
99
+
100
+ return exitCode;
45
101
  }
package/src/run-unit.ts CHANGED
@@ -6,12 +6,10 @@ import { parseTestArgs } from './parse-test-args.ts';
6
6
  import type { WorkerCount } from './read-worker-count.ts';
7
7
  import type { UnitRunDeps } from './run-unit-tests.ts';
8
8
  import { runUnitTests } from './run-unit-tests.ts';
9
- import type { RigConfig, RigOutput, RigRun } from './types.ts';
9
+ import type { RigConfig, RigRun } from './types.ts';
10
10
 
11
11
  export interface UnitDeps extends UnitRunDeps {
12
12
  readonly readWorkerCount: () => Promise<WorkerCount>;
13
-
14
- readonly output: RigOutput;
15
13
  }
16
14
 
17
15
  export async function runUnit(
@@ -57,13 +55,23 @@ export async function runUnit(
57
55
  .with({ kind: 'files' }, (files) =>
58
56
  runUnitTests(
59
57
  config,
60
- { workerCount: workers.count, extraArgs: [...files.files, ...files.flags] },
58
+ { workerCount: workers.count, flags: files.flags, tests: files.files, isDiscovery: false },
61
59
  run,
62
60
  deps,
63
61
  ),
64
62
  )
65
63
  .with({ kind: 'lane' }, (laneRun) =>
66
- runUnitTests(config, { workerCount: workers.count, extraArgs: laneRun.flags }, run, deps),
64
+ runUnitTests(
65
+ config,
66
+ {
67
+ workerCount: workers.count,
68
+ flags: laneRun.flags,
69
+ tests: laneRun.files,
70
+ isDiscovery: true,
71
+ },
72
+ run,
73
+ deps,
74
+ ),
67
75
  )
68
76
  .exhaustive();
69
77
  }
package/src/types.ts CHANGED
@@ -35,9 +35,20 @@ export interface BenchDeclaration {
35
35
  readonly lane?: string;
36
36
  }
37
37
 
38
+ // A bunfig that the unit tests matching its globs run under, such as one with a DOM preload.
39
+ export interface BunfigDeclaration {
40
+ // A path relative to the repo root.
41
+ readonly path: string;
42
+
43
+ // Globs relative to the repo root.
44
+ readonly files: readonly string[];
45
+ }
46
+
38
47
  export interface RigDeclaration {
39
48
  readonly ignore?: readonly string[];
40
49
 
50
+ readonly bunfigs?: readonly BunfigDeclaration[];
51
+
41
52
  readonly lanes?: Readonly<Record<string, LaneDeclaration>>;
42
53
 
43
54
  readonly programs?: Readonly<Record<string, ProgramDeclaration>>;
@@ -68,6 +79,8 @@ export interface ResolvedLane {
68
79
  export interface RigConfig {
69
80
  readonly ignore: readonly string[];
70
81
 
82
+ readonly bunfigs: readonly BunfigDeclaration[];
83
+
71
84
  readonly lanes: readonly ResolvedLane[];
72
85
 
73
86
  readonly programs: Readonly<Record<string, ProgramDeclaration>>;