@smoothbricks/nx-plugin 0.3.11 → 0.3.13
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 +23 -2
- package/dist/bounded-test-policy.d.ts +26 -0
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +194 -0
- package/dist/cargo-workspace.d.ts +67 -0
- package/dist/cargo-workspace.d.ts.map +1 -0
- package/dist/cargo-workspace.js +215 -0
- package/dist/cross-check-policy.d.ts +60 -1
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +65 -1
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +86 -51
- package/dist/executors/bounded-exec/schema.d.ts +13 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +455 -95
- package/dist/package-target-policy.d.ts +2 -1
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +54 -13
- package/dist/workspace-config-policy.js +32 -35
- package/nextest.toml +79 -0
- package/package.json +2 -1
- package/src/bounded-test-policy.test.ts +304 -0
- package/src/bounded-test-policy.ts +218 -0
- package/src/cargo-workspace.ts +243 -0
- package/src/cross-check-policy.test.ts +24 -0
- package/src/cross-check-policy.ts +80 -1
- package/src/executors/bounded-exec/executor.test.ts +147 -3
- package/src/executors/bounded-exec/executor.ts +103 -53
- package/src/executors/bounded-exec/schema.json +7 -2
- package/src/executors/bounded-exec/schema.ts +13 -0
- package/src/index.test.ts +531 -99
- package/src/index.ts +527 -104
- package/src/package-target-policy.test.ts +50 -0
- package/src/package-target-policy.ts +58 -9
- package/src/workspace-config-policy.test.ts +15 -30
- package/src/workspace-config-policy.ts +32 -39
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
boundedTestScriptAlias,
|
|
16
16
|
checkBoundedTestTargetPolicy,
|
|
17
17
|
checkWorkspaceBoundedTestTargetPolicy,
|
|
18
|
+
checkWorkspaceCargoTestReachabilityPolicy,
|
|
18
19
|
ensureBunTestTimeoutFlag,
|
|
19
20
|
resolveTestCommand,
|
|
20
21
|
} from './bounded-test-policy.js';
|
|
@@ -263,6 +264,48 @@ describe('bounded test target policy', () => {
|
|
|
263
264
|
}
|
|
264
265
|
});
|
|
265
266
|
|
|
267
|
+
it('leaves multi-command and non-bun test targets for the check to report', async () => {
|
|
268
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
269
|
+
try {
|
|
270
|
+
await writeJson(join(root, 'package.json'), {
|
|
271
|
+
name: '@scope/root',
|
|
272
|
+
private: true,
|
|
273
|
+
workspaces: ['packages/*'],
|
|
274
|
+
});
|
|
275
|
+
const corpusTarget = {
|
|
276
|
+
executor: 'nx:run-commands',
|
|
277
|
+
options: {
|
|
278
|
+
commands: ['bun test test/', 'cd go && go test ./...'],
|
|
279
|
+
parallel: false,
|
|
280
|
+
cwd: 'packages/schema',
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
const cargoTarget = {
|
|
284
|
+
executor: 'nx:run-commands',
|
|
285
|
+
options: { command: 'cargo test --workspace', cwd: 'packages/native' },
|
|
286
|
+
};
|
|
287
|
+
await writeJson(join(root, 'packages/schema/package.json'), {
|
|
288
|
+
name: '@scope/schema',
|
|
289
|
+
nx: { name: 'schema', targets: { test: corpusTarget } },
|
|
290
|
+
});
|
|
291
|
+
await writeJson(join(root, 'packages/native/package.json'), {
|
|
292
|
+
name: '@scope/native',
|
|
293
|
+
nx: { name: 'native', targets: { test: cargoTarget } },
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
expect(applyWorkspaceBoundedTestTargetPolicy(root)).toBe(false);
|
|
297
|
+
|
|
298
|
+
const schema = JSON.parse(await readFile(join(root, 'packages/schema/package.json'), 'utf8'));
|
|
299
|
+
expect(schema.nx.targets.test).toEqual(corpusTarget);
|
|
300
|
+
const native = JSON.parse(await readFile(join(root, 'packages/native/package.json'), 'utf8'));
|
|
301
|
+
expect(native.nx.targets.test).toEqual(cargoTarget);
|
|
302
|
+
// Still reported: apply refusing to flatten does not silently bless them.
|
|
303
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root).length).toBe(2);
|
|
304
|
+
} finally {
|
|
305
|
+
await rm(root, { recursive: true, force: true });
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
266
309
|
it('preserves a resolved bounded aggregate during workspace updates', async () => {
|
|
267
310
|
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
268
311
|
try {
|
|
@@ -340,6 +383,267 @@ describe('bounded test target policy', () => {
|
|
|
340
383
|
});
|
|
341
384
|
});
|
|
342
385
|
|
|
386
|
+
describe('cargo-test reachability policy', () => {
|
|
387
|
+
it('flags a cargo workspace whose test aggregate never reaches cargo-test', async () => {
|
|
388
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-reach-'));
|
|
389
|
+
try {
|
|
390
|
+
await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
|
|
391
|
+
await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
|
|
392
|
+
|
|
393
|
+
// The shape that shipped: nx.json targetDefaults replaced the inferred
|
|
394
|
+
// dependsOn: [cargo-test] with [^build, build], so `nx test rusty` was a
|
|
395
|
+
// no-op that ran nothing and reported success.
|
|
396
|
+
const clobbered: ResolvedProjectTargets = {
|
|
397
|
+
root: 'packages/rusty',
|
|
398
|
+
targets: new Set(['test', 'cargo-test', 'build']),
|
|
399
|
+
targetDependencies: new Map([['test', ['^build', 'build']]]),
|
|
400
|
+
targetExecutors: new Map([
|
|
401
|
+
['test', 'nx:noop'],
|
|
402
|
+
['cargo-test', 'nx:noop'],
|
|
403
|
+
['build', 'nx:run-commands'],
|
|
404
|
+
]),
|
|
405
|
+
// cargo-test is the runner for a single-crate workspace.
|
|
406
|
+
targetOptions: new Map([['cargo-test', { command: 'cargo --frozen test --workspace' }]]),
|
|
407
|
+
};
|
|
408
|
+
const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
409
|
+
resolvedTargetsByProject: new Map([['rusty', clobbered]]),
|
|
410
|
+
});
|
|
411
|
+
expect(issues).toHaveLength(1);
|
|
412
|
+
expect(issues[0]?.message).toContain('cargo-test');
|
|
413
|
+
|
|
414
|
+
// Transitive reachability counts: cowshed reaches it through napi-test.
|
|
415
|
+
const transitive: ResolvedProjectTargets = {
|
|
416
|
+
...clobbered,
|
|
417
|
+
targets: new Set(['test', 'napi-test', 'cargo-test', 'build']),
|
|
418
|
+
targetDependencies: new Map([
|
|
419
|
+
['test', ['napi-test']],
|
|
420
|
+
['napi-test', ['cargo-test']],
|
|
421
|
+
]),
|
|
422
|
+
};
|
|
423
|
+
expect(
|
|
424
|
+
checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
425
|
+
resolvedTargetsByProject: new Map([['rusty', transitive]]),
|
|
426
|
+
}),
|
|
427
|
+
).toEqual([]);
|
|
428
|
+
} finally {
|
|
429
|
+
await rm(root, { recursive: true, force: true });
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('flags a cargo-test that reaches only compile targets, never a runner', async () => {
|
|
434
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-reach-noop-'));
|
|
435
|
+
try {
|
|
436
|
+
await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
|
|
437
|
+
await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
|
|
438
|
+
|
|
439
|
+
// The shape a `dependsOn` that omits the runner leg produces: `test`
|
|
440
|
+
// still reaches cargo-test, and cargo-test still reaches real work — but
|
|
441
|
+
// the only cargo invocation in the closure is `--no-run`, so no test is
|
|
442
|
+
// ever executed and the target is green. Reachability alone cannot see
|
|
443
|
+
// this. Hoisted so the runner fixture below extends a total map instead
|
|
444
|
+
// of spreading an optional one.
|
|
445
|
+
const compileOnlyOptions = new Map<string, Record<string, unknown>>([
|
|
446
|
+
['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
|
|
447
|
+
['cargo-wasm', { command: 'just wasm' }],
|
|
448
|
+
]);
|
|
449
|
+
const compileOnly: ResolvedProjectTargets = {
|
|
450
|
+
root: 'packages/rusty',
|
|
451
|
+
targets: new Set(['test', 'cargo-test', 'cargo-test-compile', 'cargo-wasm']),
|
|
452
|
+
targetDependencies: new Map([
|
|
453
|
+
['test', ['cargo-test']],
|
|
454
|
+
['cargo-test', ['cargo-test-compile', 'cargo-wasm']],
|
|
455
|
+
]),
|
|
456
|
+
targetExecutors: new Map([
|
|
457
|
+
['test', 'nx:noop'],
|
|
458
|
+
['cargo-test', 'nx:noop'],
|
|
459
|
+
['cargo-test-compile', 'nx:run-commands'],
|
|
460
|
+
['cargo-wasm', 'nx:run-commands'],
|
|
461
|
+
]),
|
|
462
|
+
targetOptions: compileOnlyOptions,
|
|
463
|
+
};
|
|
464
|
+
const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
465
|
+
resolvedTargetsByProject: new Map([['rusty', compileOnly]]),
|
|
466
|
+
});
|
|
467
|
+
expect(issues).toHaveLength(1);
|
|
468
|
+
expect(issues[0]?.message).toContain('RUNS tests');
|
|
469
|
+
|
|
470
|
+
// Restoring a real runner clears it.
|
|
471
|
+
const withRunner: ResolvedProjectTargets = {
|
|
472
|
+
...compileOnly,
|
|
473
|
+
targets: new Set([...compileOnly.targets, 'cargo-test-rusty-core']),
|
|
474
|
+
targetDependencies: new Map([
|
|
475
|
+
['test', ['cargo-test']],
|
|
476
|
+
['cargo-test', ['cargo-test-rusty-core']],
|
|
477
|
+
['cargo-test-rusty-core', ['cargo-test-compile']],
|
|
478
|
+
]),
|
|
479
|
+
targetOptions: new Map([
|
|
480
|
+
...compileOnlyOptions,
|
|
481
|
+
['cargo-test-rusty-core', { command: 'cargo --frozen nextest run --package rusty-core' }],
|
|
482
|
+
]),
|
|
483
|
+
};
|
|
484
|
+
expect(
|
|
485
|
+
checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
486
|
+
resolvedTargetsByProject: new Map([['rusty', withRunner]]),
|
|
487
|
+
}),
|
|
488
|
+
).toEqual([]);
|
|
489
|
+
} finally {
|
|
490
|
+
await rm(root, { recursive: true, force: true });
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('ignores projects with no cargo-test target', async () => {
|
|
495
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-reach-ts-'));
|
|
496
|
+
try {
|
|
497
|
+
await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
|
|
498
|
+
await writeJson(join(root, 'packages/tsonly/package.json'), { name: 'tsonly', nx: { name: 'tsonly' } });
|
|
499
|
+
const tsOnly: ResolvedProjectTargets = {
|
|
500
|
+
root: 'packages/tsonly',
|
|
501
|
+
targets: new Set(['test', 'build']),
|
|
502
|
+
targetDependencies: new Map([['test', ['build']]]),
|
|
503
|
+
targetExecutors: new Map([['test', BOUNDED_TEST_EXECUTOR]]),
|
|
504
|
+
targetOptions: new Map(),
|
|
505
|
+
};
|
|
506
|
+
expect(
|
|
507
|
+
checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
508
|
+
resolvedTargetsByProject: new Map([['tsonly', tsOnly]]),
|
|
509
|
+
}),
|
|
510
|
+
).toEqual([]);
|
|
511
|
+
} finally {
|
|
512
|
+
await rm(root, { recursive: true, force: true });
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it('flags per-crate cargo-test children that do not cover every workspace member', async () => {
|
|
517
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-reach-union-'));
|
|
518
|
+
try {
|
|
519
|
+
await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
|
|
520
|
+
await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
|
|
521
|
+
await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
|
|
522
|
+
await mkdir(join(root, 'packages/rusty/crates/cli'), { recursive: true });
|
|
523
|
+
await writeFile(
|
|
524
|
+
join(root, 'packages/rusty/Cargo.toml'),
|
|
525
|
+
'[workspace]\nmembers = ["crates/core", "crates/cli"]\n',
|
|
526
|
+
);
|
|
527
|
+
await writeFile(join(root, 'packages/rusty/crates/core/Cargo.toml'), '[package]\nname = "rusty-core"\n');
|
|
528
|
+
await writeFile(join(root, 'packages/rusty/crates/cli/Cargo.toml'), '[package]\nname = "rusty-cli"\n');
|
|
529
|
+
|
|
530
|
+
const runnerOptions = new Map<string, Record<string, unknown>>([
|
|
531
|
+
['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
|
|
532
|
+
['cargo-test-rusty-core', { command: 'cargo --frozen nextest run --package rusty-core' }],
|
|
533
|
+
['cargo-test-rusty-cli', { command: 'cargo --frozen nextest run --package rusty-cli' }],
|
|
534
|
+
]);
|
|
535
|
+
const missingChild: ResolvedProjectTargets = {
|
|
536
|
+
root: 'packages/rusty',
|
|
537
|
+
targets: new Set(['test', 'cargo-test', 'cargo-test-compile', 'cargo-test-rusty-core']),
|
|
538
|
+
targetDependencies: new Map([
|
|
539
|
+
['test', ['cargo-test']],
|
|
540
|
+
['cargo-test', ['cargo-test-rusty-core']],
|
|
541
|
+
['cargo-test-rusty-core', ['cargo-test-compile']],
|
|
542
|
+
]),
|
|
543
|
+
targetExecutors: new Map([
|
|
544
|
+
['test', 'nx:noop'],
|
|
545
|
+
['cargo-test', 'nx:noop'],
|
|
546
|
+
['cargo-test-compile', 'nx:run-commands'],
|
|
547
|
+
['cargo-test-rusty-core', BOUNDED_TEST_EXECUTOR],
|
|
548
|
+
]),
|
|
549
|
+
targetOptions: runnerOptions,
|
|
550
|
+
};
|
|
551
|
+
const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
552
|
+
resolvedTargetsByProject: new Map([['rusty', missingChild]]),
|
|
553
|
+
});
|
|
554
|
+
expect(issues).toHaveLength(1);
|
|
555
|
+
expect(issues[0]?.message).toContain('rusty-cli');
|
|
556
|
+
|
|
557
|
+
const covered: ResolvedProjectTargets = {
|
|
558
|
+
...missingChild,
|
|
559
|
+
targets: new Set([...missingChild.targets, 'cargo-test-rusty-cli']),
|
|
560
|
+
targetDependencies: new Map([
|
|
561
|
+
['test', ['cargo-test']],
|
|
562
|
+
['cargo-test', ['cargo-test-rusty-core', 'cargo-test-rusty-cli']],
|
|
563
|
+
['cargo-test-rusty-core', ['cargo-test-compile']],
|
|
564
|
+
['cargo-test-rusty-cli', ['cargo-test-rusty-core']],
|
|
565
|
+
]),
|
|
566
|
+
targetExecutors: new Map([
|
|
567
|
+
...(missingChild.targetExecutors ?? []),
|
|
568
|
+
['cargo-test-rusty-cli', BOUNDED_TEST_EXECUTOR],
|
|
569
|
+
]),
|
|
570
|
+
};
|
|
571
|
+
expect(
|
|
572
|
+
checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
573
|
+
resolvedTargetsByProject: new Map([['rusty', covered]]),
|
|
574
|
+
}),
|
|
575
|
+
).toEqual([]);
|
|
576
|
+
|
|
577
|
+
await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
|
|
578
|
+
const extra = checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
579
|
+
resolvedTargetsByProject: new Map([['rusty', covered]]),
|
|
580
|
+
});
|
|
581
|
+
expect(extra).toHaveLength(1);
|
|
582
|
+
expect(extra[0]?.message).toContain('rusty-cli');
|
|
583
|
+
} finally {
|
|
584
|
+
await rm(root, { recursive: true, force: true });
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it('counts every shard of a sharded crate as covering that crate exactly once', async () => {
|
|
589
|
+
const root = await mkdtemp(join(tmpdir(), 'cargo-reach-shard-'));
|
|
590
|
+
try {
|
|
591
|
+
await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
|
|
592
|
+
await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
|
|
593
|
+
await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
|
|
594
|
+
await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
|
|
595
|
+
await writeFile(
|
|
596
|
+
join(root, 'packages/rusty/crates/core/Cargo.toml'),
|
|
597
|
+
'[package]\nname = "rusty-core"\n\n[package.metadata.smoothbricks.test]\nshards = 2\n',
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
const sharded: ResolvedProjectTargets = {
|
|
601
|
+
root: 'packages/rusty',
|
|
602
|
+
targets: new Set([
|
|
603
|
+
'test',
|
|
604
|
+
'cargo-test',
|
|
605
|
+
'cargo-test-compile',
|
|
606
|
+
'cargo-test-rusty-core-shard1',
|
|
607
|
+
'cargo-test-rusty-core-shard2',
|
|
608
|
+
]),
|
|
609
|
+
targetDependencies: new Map([
|
|
610
|
+
['test', ['cargo-test']],
|
|
611
|
+
['cargo-test', ['cargo-test-rusty-core-shard1', 'cargo-test-rusty-core-shard2']],
|
|
612
|
+
['cargo-test-rusty-core-shard1', ['cargo-test-compile']],
|
|
613
|
+
['cargo-test-rusty-core-shard2', ['cargo-test-rusty-core-shard1']],
|
|
614
|
+
]),
|
|
615
|
+
targetExecutors: new Map([
|
|
616
|
+
['test', 'nx:noop'],
|
|
617
|
+
['cargo-test', 'nx:noop'],
|
|
618
|
+
['cargo-test-compile', 'nx:run-commands'],
|
|
619
|
+
['cargo-test-rusty-core-shard1', BOUNDED_TEST_EXECUTOR],
|
|
620
|
+
['cargo-test-rusty-core-shard2', BOUNDED_TEST_EXECUTOR],
|
|
621
|
+
]),
|
|
622
|
+
targetOptions: new Map<string, Record<string, unknown>>([
|
|
623
|
+
['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
|
|
624
|
+
[
|
|
625
|
+
'cargo-test-rusty-core-shard1',
|
|
626
|
+
{ command: "cargo --frozen nextest run --workspace -E 'package(rusty-core)' --partition hash:1/2" },
|
|
627
|
+
],
|
|
628
|
+
[
|
|
629
|
+
'cargo-test-rusty-core-shard2',
|
|
630
|
+
{ command: "cargo --frozen nextest run --workspace -E 'package(rusty-core)' --partition hash:2/2" },
|
|
631
|
+
],
|
|
632
|
+
]),
|
|
633
|
+
};
|
|
634
|
+
// Shards collapse to their crate: two targets cover one member without
|
|
635
|
+
// reading as an extra member the workspace does not have.
|
|
636
|
+
expect(
|
|
637
|
+
checkWorkspaceCargoTestReachabilityPolicy(root, {
|
|
638
|
+
resolvedTargetsByProject: new Map([['rusty', sharded]]),
|
|
639
|
+
}),
|
|
640
|
+
).toEqual([]);
|
|
641
|
+
} finally {
|
|
642
|
+
await rm(root, { recursive: true, force: true });
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
});
|
|
646
|
+
|
|
343
647
|
function resolvedAggregateProject(): ResolvedProjectTargets {
|
|
344
648
|
return {
|
|
345
649
|
root: 'packages/cowshed',
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
|
+
import { CARGO_TEST_TARGET, listCargoWorkspacePackages, packageNameFromCargoTestTarget } from './cargo-workspace.js';
|
|
3
4
|
import type { PackageTargetPolicyOptions, ResolvedProjectTargets } from './package-target-policy.js';
|
|
4
5
|
|
|
5
6
|
export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
|
|
@@ -83,6 +84,9 @@ export function applyWorkspaceBoundedTestTargetPolicy(root: string, options: Pac
|
|
|
83
84
|
if (checkBoundedTestTargetPolicy(packageJson, { projectName, projectJson, resolvedProject })) {
|
|
84
85
|
continue;
|
|
85
86
|
}
|
|
87
|
+
if (!boundableTestTarget(packageJson, projectJson)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
86
90
|
const beforePackageJson = JSON.stringify(packageJson);
|
|
87
91
|
const beforeProjectJson = JSON.stringify(projectJson);
|
|
88
92
|
applyBoundedTestTargetPolicy(packageJson, { projectName, projectJson });
|
|
@@ -131,6 +135,193 @@ export function checkWorkspaceBoundedTestTargetPolicy(
|
|
|
131
135
|
return issues;
|
|
132
136
|
}
|
|
133
137
|
|
|
138
|
+
/**
|
|
139
|
+
* A cargo workspace's `test` aggregate MUST reach `cargo-test`, and `cargo-test`
|
|
140
|
+
* must actually RUN tests.
|
|
141
|
+
*
|
|
142
|
+
* Inference wires `test` to `cargo-test` (see `index.ts`), but two things unwire
|
|
143
|
+
* it silently and both leave the target green:
|
|
144
|
+
*
|
|
145
|
+
* 1. `targetDefaults.test.dependsOn` in `nx.json` REPLACES an inferred
|
|
146
|
+
* `dependsOn` rather than merging, and a package-local `nx.targets.test`
|
|
147
|
+
* replaces it outright. `nx test <project>` then executes nothing.
|
|
148
|
+
* 2. `dependsOn` REPLACES rather than unions at every merge layer, so a
|
|
149
|
+
* package-local `nx.targets['cargo-test'].dependsOn` that omits the inferred
|
|
150
|
+
* runner leg leaves a target that still "reaches" its dependencies while no
|
|
151
|
+
* test binary is ever run. Reachability alone cannot see this, because
|
|
152
|
+
* `cargo-test-compile` is `--no-run`. (Inference itself no longer skips a
|
|
153
|
+
* declared cargo key, so the executor and options survive the declaration —
|
|
154
|
+
* see `index.ts`.)
|
|
155
|
+
*
|
|
156
|
+
* So this checks three things: `test` reaches `cargo-test`; some target in
|
|
157
|
+
* `cargo-test`'s closure actually executes tests rather than only compiling them;
|
|
158
|
+
* and when the run is split into per-crate `cargo-test-<package>` children, their
|
|
159
|
+
* union equals the workspace member set. A missing child is silent once
|
|
160
|
+
* `--package` actually filters: the remaining targets stay green and the
|
|
161
|
+
* uncovered member never runs.
|
|
162
|
+
*/
|
|
163
|
+
export function checkWorkspaceCargoTestReachabilityPolicy(
|
|
164
|
+
root: string,
|
|
165
|
+
options: PackageTargetPolicyOptions = {},
|
|
166
|
+
): BoundedTestPolicyIssue[] {
|
|
167
|
+
const issues: BoundedTestPolicyIssue[] = [];
|
|
168
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
169
|
+
const packageJson = readPackageJson(packageJsonPath);
|
|
170
|
+
const projectJsonPath = projectJsonPathForPackageJson(packageJsonPath);
|
|
171
|
+
const projectJson = existsSync(projectJsonPath) ? readProjectJson(projectJsonPath) : undefined;
|
|
172
|
+
const projectName = packageProjectName(packageJson, projectJson);
|
|
173
|
+
if (!projectName) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const resolvedProject = resolvedProjectFor(options, projectName);
|
|
177
|
+
// Without a resolved graph there is nothing to judge, and without a
|
|
178
|
+
// cargo-test target this is not a cargo workspace.
|
|
179
|
+
if (!resolvedProject?.targets?.has(CARGO_TEST_TARGET) || !resolvedProject.targets.has('test')) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const scope = projectJson ? 'targets' : 'nx.targets';
|
|
183
|
+
const path = projectJson ? projectJsonPath : packageJsonPath;
|
|
184
|
+
if (!resolvedTargetReaches(resolvedProject, 'test', CARGO_TEST_TARGET)) {
|
|
185
|
+
issues.push({
|
|
186
|
+
path,
|
|
187
|
+
message:
|
|
188
|
+
`${scope}.test must depend on ${CARGO_TEST_TARGET} ` +
|
|
189
|
+
'so the Rust tests run in the test aggregate; nx.json targetDefaults replaces the inferred dependsOn',
|
|
190
|
+
});
|
|
191
|
+
} else if (!resolvedTargetRunsTests(resolvedProject, CARGO_TEST_TARGET)) {
|
|
192
|
+
issues.push({
|
|
193
|
+
path,
|
|
194
|
+
message:
|
|
195
|
+
`${scope}.${CARGO_TEST_TARGET} reaches no target that RUNS tests — only ones that compile them. ` +
|
|
196
|
+
'A declared dependsOn replaces the inferred one; spread it with "..." instead of omitting the runner leg',
|
|
197
|
+
});
|
|
198
|
+
} else {
|
|
199
|
+
const coverage = cargoTestMemberCoverageIssue(root, path, scope, resolvedProject);
|
|
200
|
+
if (coverage !== undefined) {
|
|
201
|
+
issues.push(coverage);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return issues;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* True when `target`, or something in its dependency closure, invokes cargo in a
|
|
210
|
+
* mode that executes tests. `cargo-test-compile` is deliberately excluded: it is
|
|
211
|
+
* `cargo test --no-run`, so it proves the binaries build and nothing about them
|
|
212
|
+
* running.
|
|
213
|
+
*/
|
|
214
|
+
function resolvedTargetRunsTests(project: ResolvedProjectTargets, target: string): boolean {
|
|
215
|
+
const visiting = new Set<string>();
|
|
216
|
+
const visit = (targetName: string): boolean => {
|
|
217
|
+
if (visiting.has(targetName) || !project.targets.has(targetName)) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
visiting.add(targetName);
|
|
221
|
+
const command = commandOf(project, targetName);
|
|
222
|
+
const runs = command !== undefined && /\btest\b|\bnextest\b/.test(command) && !command.includes('--no-run');
|
|
223
|
+
const reached =
|
|
224
|
+
runs ||
|
|
225
|
+
(project.targetDependencies?.get(targetName) ?? []).some((dependency) =>
|
|
226
|
+
matchingResolvedTargets(dependency, project.targets).some(visit),
|
|
227
|
+
);
|
|
228
|
+
visiting.delete(targetName);
|
|
229
|
+
return reached;
|
|
230
|
+
};
|
|
231
|
+
return visit(target);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Per-crate nextest targets that `cargo-test` actually reaches. `cargo-test-compile`
|
|
236
|
+
* is excluded: it is not a runner. Members come from the same Cargo.toml walk
|
|
237
|
+
* inference uses; the child list is the graph, not a hand-written crate list.
|
|
238
|
+
*/
|
|
239
|
+
function cargoTestPackageChildren(project: ResolvedProjectTargets): string[] {
|
|
240
|
+
const packages = new Set<string>();
|
|
241
|
+
const visiting = new Set<string>();
|
|
242
|
+
const visit = (targetName: string): void => {
|
|
243
|
+
if (visiting.has(targetName) || !project.targets.has(targetName)) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
visiting.add(targetName);
|
|
247
|
+
const pkg = packageNameFromCargoTestTarget(targetName);
|
|
248
|
+
if (pkg !== null) {
|
|
249
|
+
packages.add(pkg);
|
|
250
|
+
}
|
|
251
|
+
for (const dependency of project.targetDependencies?.get(targetName) ?? []) {
|
|
252
|
+
for (const matched of matchingResolvedTargets(dependency, project.targets)) {
|
|
253
|
+
visit(matched);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
visit(CARGO_TEST_TARGET);
|
|
258
|
+
return [...packages].sort((left, right) => left.localeCompare(right));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function cargoTestMemberCoverageIssue(
|
|
262
|
+
workspaceRoot: string,
|
|
263
|
+
path: string,
|
|
264
|
+
scope: string,
|
|
265
|
+
project: ResolvedProjectTargets,
|
|
266
|
+
): BoundedTestPolicyIssue | undefined {
|
|
267
|
+
if (typeof project.root !== 'string' || project.root.length === 0) {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
const members = listCargoWorkspacePackages(join(workspaceRoot, project.root)).map((pkg) => pkg.name);
|
|
271
|
+
if (members.length === 0) {
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
const children = cargoTestPackageChildren(project);
|
|
275
|
+
if (children.length === 0) {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
const memberSet = new Set(members);
|
|
279
|
+
const childSet = new Set(children);
|
|
280
|
+
const missing = members.filter((name) => !childSet.has(name));
|
|
281
|
+
const extra = children.filter((name) => !memberSet.has(name));
|
|
282
|
+
if (missing.length === 0 && extra.length === 0) {
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
const details = [
|
|
286
|
+
...(missing.length > 0 ? [`missing ${missing.join(', ')}`] : []),
|
|
287
|
+
...(extra.length > 0 ? [`extra ${extra.join(', ')}`] : []),
|
|
288
|
+
];
|
|
289
|
+
return {
|
|
290
|
+
path,
|
|
291
|
+
message:
|
|
292
|
+
`${scope}.${CARGO_TEST_TARGET} per-crate children must equal workspace members that have tests ` +
|
|
293
|
+
`(${members.join(', ')}); ${details.join('; ')}`,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function commandOf(project: ResolvedProjectTargets, targetName: string): string | undefined {
|
|
298
|
+
const options = project.targetOptions?.get(targetName);
|
|
299
|
+
if (options === undefined || options === null) {
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
const command = (options as Record<string, unknown>).command;
|
|
303
|
+
return typeof command === 'string' ? command : undefined;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function resolvedTargetReaches(project: ResolvedProjectTargets, from: string, goal: string): boolean {
|
|
307
|
+
const visiting = new Set<string>();
|
|
308
|
+
const visit = (targetName: string): boolean => {
|
|
309
|
+
if (targetName === goal) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
if (visiting.has(targetName) || !project.targets.has(targetName)) {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
visiting.add(targetName);
|
|
316
|
+
const reached = (project.targetDependencies?.get(targetName) ?? []).some((dependency) =>
|
|
317
|
+
matchingResolvedTargets(dependency, project.targets).some(visit),
|
|
318
|
+
);
|
|
319
|
+
visiting.delete(targetName);
|
|
320
|
+
return reached;
|
|
321
|
+
};
|
|
322
|
+
return visit(from);
|
|
323
|
+
}
|
|
324
|
+
|
|
134
325
|
export function checkBoundedTestTargetPolicy(
|
|
135
326
|
packageJson: BoundedTestPolicyPackageJson,
|
|
136
327
|
options: ApplyBoundedTestTargetPolicyOptions,
|
|
@@ -157,6 +348,33 @@ export function checkBoundedTestTargetPolicy(
|
|
|
157
348
|
return resolvedAggregateTestIsBounded(options.resolvedProject);
|
|
158
349
|
}
|
|
159
350
|
|
|
351
|
+
/**
|
|
352
|
+
* Apply may only rewrite a test target it can express faithfully as one
|
|
353
|
+
* bounded `bun test` command. A `commands` array (a multi-runner gate) would
|
|
354
|
+
* be flattened to the default command — deleting the very checks the target
|
|
355
|
+
* exists to run — and a non-bun runner (cargo, go) would inherit wall-clock
|
|
356
|
+
* bounds scaled for bun suites. Those targets are left in place for the check
|
|
357
|
+
* to report, so restructuring them stays a human decision.
|
|
358
|
+
*/
|
|
359
|
+
function boundableTestTarget(
|
|
360
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
361
|
+
projectJson: BoundedTestPolicyProjectJson | undefined,
|
|
362
|
+
): boolean {
|
|
363
|
+
const target = projectJson?.targets?.test ?? packageJson.nx?.targets?.test;
|
|
364
|
+
if (!isRecord(target)) {
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
if (isNoopAggregateTarget(target)) {
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
const targetOptions = isRecord(target.options) ? target.options : undefined;
|
|
371
|
+
if (targetOptions && Array.isArray(targetOptions.commands)) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
const command = resolveTargetCommand(target);
|
|
375
|
+
return command === null || BUN_TEST_PREFIX.test(command);
|
|
376
|
+
}
|
|
377
|
+
|
|
160
378
|
function isNoopAggregateTarget(target: Record<string, unknown>): boolean {
|
|
161
379
|
if (target.executor !== undefined && target.executor !== 'nx:noop') {
|
|
162
380
|
return false;
|