@smoothbricks/nx-plugin 0.4.26 → 0.4.28
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 +994 -117
- package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-bound-failures.js +31 -0
- package/dist/bin/smoo-nx-cargo-hash.js +28 -3
- package/dist/bin/smoo-nx-exec.js +15 -73
- package/dist/bin/smoo-nx-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-failures.js +3 -0
- package/dist/bin/smoo-nx-nextest-extract.js +3 -2
- package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
- package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
- package/dist/bin/smoo-nx-test-walks.js +34 -0
- package/dist/bounded-test-policy.d.ts +7 -1
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +101 -40
- package/dist/build-state-output-policy.d.ts +25 -0
- package/dist/build-state-output-policy.d.ts.map +1 -0
- package/dist/build-state-output-policy.js +64 -0
- package/dist/cargo-closure-input.d.ts +3 -2
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +16 -15
- package/dist/cargo-dev-profile.d.ts +37 -0
- package/dist/cargo-dev-profile.d.ts.map +1 -0
- package/dist/cargo-dev-profile.js +108 -0
- package/dist/cargo-source-hash.d.ts +43 -6
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +309 -73
- package/dist/cargo-toolchain-policy.d.ts +4 -0
- package/dist/cargo-toolchain-policy.d.ts.map +1 -1
- package/dist/cargo-toolchain-policy.js +5 -0
- package/dist/cargo-workspace.d.ts +101 -5
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +291 -10
- package/dist/cross-check-policy.d.ts +21 -3
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +24 -6
- package/dist/disk-command-policy.d.ts +7 -0
- package/dist/disk-command-policy.d.ts.map +1 -0
- package/dist/disk-command-policy.js +98 -0
- package/dist/ensure-built-receipt.d.ts +25 -0
- package/dist/ensure-built-receipt.d.ts.map +1 -0
- package/dist/ensure-built-receipt.js +261 -0
- package/dist/ensure-built-witness.d.ts +57 -0
- package/dist/ensure-built-witness.d.ts.map +1 -0
- package/dist/ensure-built-witness.js +412 -0
- package/dist/ensure-built.d.ts +207 -12
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +576 -202
- package/dist/eslint-file-set.d.ts +7 -0
- package/dist/eslint-file-set.d.ts.map +1 -0
- package/dist/eslint-file-set.js +12 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-pool.js +379 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/disk-lease.js +91 -0
- package/dist/executors/bounded-exec/executor.d.ts +45 -2
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +447 -52
- package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
- package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/gateway-lease.js +143 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
- package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
- package/dist/executors/bounded-exec/progress.d.ts +35 -0
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
- package/dist/executors/bounded-exec/progress.js +79 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp.js +1528 -0
- package/dist/executors/bounded-exec/schema.d.ts +29 -11
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.d.ts +162 -0
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
- package/dist/executors/bounded-exec/verdict.js +318 -0
- package/dist/executors/shard-timings/executor.d.ts +14 -0
- package/dist/executors/shard-timings/executor.d.ts.map +1 -0
- package/dist/executors/shard-timings/executor.js +25 -0
- package/dist/executors/shard-timings/schema.d.ts +4 -0
- package/dist/executors/shard-timings/schema.d.ts.map +1 -0
- package/dist/executors/shard-timings/schema.js +1 -0
- package/dist/failure-summary.d.ts +5 -0
- package/dist/failure-summary.d.ts.map +1 -0
- package/dist/failure-summary.js +118 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -1
- package/dist/generators/create-package/generator.js +6 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +370 -138
- package/dist/managed-files/ci-workflow.d.ts +13 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +44 -13
- package/dist/managed-files/files.d.ts +7 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +62 -8
- package/dist/managed-files/json-edit.d.ts +21 -0
- package/dist/managed-files/json-edit.d.ts.map +1 -0
- package/dist/managed-files/json-edit.js +295 -0
- package/dist/managed-files/managed-format.d.ts +33 -1
- package/dist/managed-files/managed-format.d.ts.map +1 -1
- package/dist/managed-files/managed-format.js +82 -2
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +231 -48
- package/dist/managed-files/tree.d.ts +5 -0
- package/dist/managed-files/tree.d.ts.map +1 -1
- package/dist/managed-files/tree.js +5 -3
- package/dist/manifest-hash.d.ts.map +1 -1
- package/dist/manifest-hash.js +2 -5
- package/dist/nextest-code-bound.d.ts +62 -0
- package/dist/nextest-code-bound.d.ts.map +1 -0
- package/dist/nextest-code-bound.js +154 -0
- package/dist/nextest-extraction.d.ts +24 -8
- package/dist/nextest-extraction.d.ts.map +1 -1
- package/dist/nextest-extraction.js +42 -7
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +3 -5
- package/dist/release-config-policy.d.ts.map +1 -1
- package/dist/release-config-policy.js +3 -5
- package/dist/runtime-input.d.ts +14 -0
- package/dist/runtime-input.d.ts.map +1 -0
- package/dist/runtime-input.js +15 -0
- package/dist/rust-tokens.d.ts +11 -0
- package/dist/rust-tokens.d.ts.map +1 -0
- package/dist/rust-tokens.js +81 -0
- package/dist/task-results.d.ts +6 -0
- package/dist/task-results.d.ts.map +1 -0
- package/dist/task-results.js +61 -0
- package/dist/test-program.d.ts +54 -0
- package/dist/test-program.d.ts.map +1 -0
- package/dist/test-program.js +292 -0
- package/dist/test-timings.d.ts +71 -0
- package/dist/test-timings.d.ts.map +1 -0
- package/dist/test-timings.js +393 -0
- package/dist/test-walk-policy.d.ts +103 -0
- package/dist/test-walk-policy.d.ts.map +1 -0
- package/dist/test-walk-policy.js +657 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +824 -0
- package/dist/type-guards.d.ts +3 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +4 -0
- package/dist/typecheck-test-policy.d.ts +8 -5
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +113 -100
- package/dist/workspace-config-policy.d.ts +8 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +175 -63
- package/dist/workspace-index.d.ts +12 -0
- package/dist/workspace-index.d.ts.map +1 -0
- package/dist/workspace-index.js +17 -0
- package/dist/workspace-manifest.d.ts +37 -0
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/executors.json +5 -0
- package/loader.js +13 -11
- package/managed/raw/envrc +33 -67
- package/managed/raw/git-format-staged.yml +30 -35
- package/managed/raw/patches/nx@23.2.1.patch +3484 -0
- package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
- package/managed/raw/tooling/direnv/developer-links.ts +569 -11
- package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
- package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
- package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
- package/managed/raw/tooling/direnv/repo-path +14 -2
- package/managed/raw/tooling/direnv/setup-environment.ts +1146 -159
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
- package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
- package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
- package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
- package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
- package/managed/templates/github/actions/cache-nx/action.yml +1 -1
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
- package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
- package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
- package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
- package/managed/templates/github/workflows/managed-files.yml +1 -2
- package/nextest.toml +8 -41
- package/package.json +74 -12
- package/runtime-input.sh +81 -0
- package/src/__tests__/counted-cargo.ts +33 -14
- package/src/__tests__/fixture-nx-env.ts +111 -46
- package/src/__tests__/progress-clock.ts +43 -0
- package/src/__tests__/runtime-input.ts +23 -0
- package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
- package/src/bin/smoo-nx-bound-failures.ts +33 -0
- package/src/bin/smoo-nx-cargo-hash.ts +28 -3
- package/src/bin/smoo-nx-exec.ts +16 -74
- package/src/bin/smoo-nx-failures.ts +4 -0
- package/src/bin/smoo-nx-nextest-extract.ts +3 -2
- package/src/bin/smoo-nx-test-walks.ts +42 -0
- package/src/bounded-test-policy.test.ts +268 -21
- package/src/bounded-test-policy.ts +114 -42
- package/src/build-state-output-policy.test.ts +115 -0
- package/src/build-state-output-policy.ts +90 -0
- package/src/bun-bootstrap.test.ts +57 -3
- package/src/bun-file-packages.test.ts +47 -0
- package/src/cargo-closure-input.test.ts +138 -66
- package/src/cargo-closure-input.ts +17 -15
- package/src/cargo-dev-profile.ts +137 -0
- package/src/cargo-resolve-cache.test.ts +70 -1
- package/src/cargo-source-hash.test.ts +193 -97
- package/src/cargo-source-hash.ts +345 -68
- package/src/cargo-toolchain-policy.ts +6 -0
- package/src/cargo-workspace.test.ts +320 -0
- package/src/cargo-workspace.ts +336 -10
- package/src/cross-check-policy.test.ts +59 -1
- package/src/cross-check-policy.ts +25 -6
- package/src/disk-command-policy.test.ts +61 -0
- package/src/disk-command-policy.ts +107 -0
- package/src/ensure-built-receipt.test.ts +184 -0
- package/src/ensure-built-receipt.ts +263 -0
- package/src/ensure-built-witness.test.ts +344 -0
- package/src/ensure-built-witness.ts +252 -0
- package/src/ensure-built.test.ts +1036 -578
- package/src/ensure-built.ts +820 -224
- package/src/eslint-file-set.ts +9 -0
- package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
- package/src/executors/bounded-exec/cpu-pool.ts +417 -0
- package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
- package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
- package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
- package/src/executors/bounded-exec/disk-lease.ts +139 -0
- package/src/executors/bounded-exec/executor-host.test.ts +778 -0
- package/src/executors/bounded-exec/executor.test.ts +1116 -63
- package/src/executors/bounded-exec/executor.ts +565 -67
- package/src/executors/bounded-exec/gateway-lease.ts +176 -0
- package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
- package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
- package/src/executors/bounded-exec/progress.test.ts +79 -0
- package/src/executors/bounded-exec/progress.ts +118 -0
- package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
- package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
- package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
- package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
- package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
- package/src/executors/bounded-exec/ram-temp.ts +1918 -0
- package/src/executors/bounded-exec/schema.json +11 -5
- package/src/executors/bounded-exec/schema.ts +27 -11
- package/src/executors/bounded-exec/verdict.test.ts +440 -0
- package/src/executors/bounded-exec/verdict.ts +433 -0
- package/src/executors/shard-timings/executor.ts +36 -0
- package/src/executors/shard-timings/schema.json +21 -0
- package/src/executors/shard-timings/schema.ts +4 -0
- package/src/failure-summary.ts +119 -0
- package/src/generators/bounded-test-targets/generator.test.ts +4 -4
- package/src/generators/bun-test-tracing/generator.test.ts +2 -2
- package/src/generators/create-package/generator.test.ts +6 -0
- package/src/generators/create-package/generator.ts +6 -2
- package/src/host-controller-wiring.test.ts +31 -0
- package/src/index.test.ts +909 -295
- package/src/index.ts +441 -143
- package/src/linked-snapshots.nx.test.ts +172 -0
- package/src/managed-files/ci-workflow.ts +54 -13
- package/src/managed-files/files.ts +65 -9
- package/src/managed-files/forgejo-artifact.test.ts +395 -0
- package/src/managed-files/generator.test.ts +60 -1
- package/src/managed-files/json-edit.test.ts +216 -0
- package/src/managed-files/json-edit.ts +290 -0
- package/src/managed-files/managed-format-json.test.ts +308 -0
- package/src/managed-files/managed-format.ts +90 -2
- package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
- package/src/managed-files/publish-workflow.ts +285 -67
- package/src/managed-files/repo-path.test.ts +129 -0
- package/src/managed-files/trace-db-artifact.test.ts +7 -0
- package/src/managed-files/tree.ts +11 -3
- package/src/manifest-hash.ts +2 -6
- package/src/nextest-code-bound.test.ts +155 -0
- package/src/nextest-code-bound.ts +195 -0
- package/src/nextest-extraction.test.ts +37 -5
- package/src/nextest-extraction.ts +51 -9
- package/src/nextest-partitions.test.ts +427 -82
- package/src/nx-cache-bound.test.ts +5 -10
- package/src/nx-cache-restore-mtime.test.ts +170 -0
- package/src/nx-daemon-claim.test.ts +277 -0
- package/src/nx-daemon-idle-cpu.test.ts +81 -0
- package/src/nx-daemon-idle-policy.test.ts +51 -0
- package/src/nx-daemon-ignore-files.test.ts +210 -0
- package/src/nx-daemon-root-normalization.test.ts +105 -0
- package/src/nx-daemon-validity.test.ts +925 -0
- package/src/nx-dot-input-policy.test.ts +353 -0
- package/src/nx-failure-life-cycles.test.ts +207 -0
- package/src/nx-files-archive-racy.test.ts +97 -0
- package/src/nx-foreign-workspace-env.test.ts +329 -0
- package/src/nx-incomplete-run-summary.test.ts +70 -0
- package/src/nx-plugin-load-errors.test.ts +120 -0
- package/src/nx-project-resolution-cache.test.ts +176 -0
- package/src/nx-raw-inputs.test.ts +166 -0
- package/src/nx-resident-graph-workers.test.ts +7 -16
- package/src/nx-run-information.test.ts +118 -0
- package/src/nx-skipped-task-exit.test.ts +180 -0
- package/src/nx-state-location.test.ts +63 -0
- package/src/nx-store-resolution.test.ts +26 -25
- package/src/nx-task-graph-cycles.test.ts +148 -0
- package/src/nx-task-output-lease.test.ts +414 -0
- package/src/nx-use-local.test.ts +103 -0
- package/src/package-target-policy.ts +3 -6
- package/src/release-config-policy.ts +3 -6
- package/src/runtime-input.test.ts +215 -0
- package/src/runtime-input.ts +17 -0
- package/src/rust-tokens.ts +78 -0
- package/src/serial-group-wiring.test.ts +326 -0
- package/src/task-history-namespace.test.ts +87 -89
- package/src/task-results.test.ts +226 -0
- package/src/task-results.ts +75 -0
- package/src/test-program.test.ts +303 -0
- package/src/test-program.ts +341 -0
- package/src/test-timings.test.ts +240 -0
- package/src/test-timings.ts +311 -0
- package/src/test-walk-policy.test.ts +585 -0
- package/src/test-walk-policy.ts +731 -0
- package/src/testing.test.ts +509 -0
- package/src/testing.ts +922 -0
- package/src/type-guards.ts +4 -0
- package/src/typecheck-test-policy.test.ts +496 -11
- package/src/typecheck-test-policy.ts +137 -103
- package/src/workspace-config-policy.test.ts +212 -13
- package/src/workspace-config-policy.ts +101 -73
- package/src/workspace-index.ts +18 -0
- package/src/workspace-manifest.ts +41 -0
- package/test-timings.json +85 -30
- package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
- package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
// One walk per test path. Within one test file, a case that walks another case's path again, differs from it only in
|
|
2
|
+
// literals, or replays its scenario with one injected step changed, tests one path twice: the cases belong in one walk
|
|
3
|
+
// that pays the setup once and asserts after each step, or in one table or property over what differs.
|
|
4
|
+
//
|
|
5
|
+
// Each top-level statement of a case body is normalized three ways: `exact` renames the names the case declares to
|
|
6
|
+
// their declaration index and keeps literals, so equal statements do the same thing; `shape` also abstracts every
|
|
7
|
+
// literal, so equal shapes differ only in values; `skeleton` also collapses every declared name, so a statement
|
|
8
|
+
// matches across cases that declare a different number of names before it. Field, method, key and path names are
|
|
9
|
+
// never renamed. Callers choose the files and, for a diff-scoped check, the changed line ranges: a pair is judged when
|
|
10
|
+
// either of its cases overlaps a change.
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
13
|
+
import { extname, join } from 'node:path';
|
|
14
|
+
import ts from 'typescript';
|
|
15
|
+
import { rustTokens } from './rust-tokens.js';
|
|
16
|
+
export const TEST_ONE_WALK = {
|
|
17
|
+
id: 'TEST-ONE-WALK',
|
|
18
|
+
help: 'Fold the cases into one walk that pays the setup once and asserts after each step, or into one ' +
|
|
19
|
+
'table/property over the values or injected step that differ. Keep a separate case only for a distinct ' +
|
|
20
|
+
'boundary or edge.',
|
|
21
|
+
/** Leading statements two cases must share exactly before one that only asserts beyond them repeats the other. */
|
|
22
|
+
samePath: 2,
|
|
23
|
+
/** Statements a case needs before a literal-only twin counts. */
|
|
24
|
+
literalTwin: 2,
|
|
25
|
+
/** A replayed scenario shares at least this many leading statements in skeleton (the setup)… */
|
|
26
|
+
scenarioHead: 1,
|
|
27
|
+
/** …and this many trailing ones (the action and its outcome), after the longer case's extra trailing assertions… */
|
|
28
|
+
scenarioTail: 3,
|
|
29
|
+
};
|
|
30
|
+
/** Whether a kind refuses (a lint error) or only advises (a warning for the writer and reviewer). */
|
|
31
|
+
export const REFUSES = {
|
|
32
|
+
'same-path': true,
|
|
33
|
+
'literal-twin': true,
|
|
34
|
+
'same-scenario': false,
|
|
35
|
+
};
|
|
36
|
+
function step(tokens, assertion) {
|
|
37
|
+
return {
|
|
38
|
+
exact: tokens.map((token) => token.text).join(' '),
|
|
39
|
+
shape: tokens.map((token) => (token.literal ? '$L' : token.text)).join(' '),
|
|
40
|
+
skeleton: tokens.map((token) => (token.literal ? '$L' : token.local ? '$' : token.text)).join(' '),
|
|
41
|
+
flow: tokens.map((token) => (token.local ? '$' : token.text)).join(' '),
|
|
42
|
+
assertion,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const JS_EXTENSION = /^\.(?:[cm]?[jt]sx?)$/u;
|
|
46
|
+
/** A file under a test directory, or named as a test. Rust test fns are read from any `.rs` file. */
|
|
47
|
+
export const TEST_PATH = /(?:^|\/)(?:__tests__|tests?|nx-it)(?:\/|$)|(?:^|\/)(?:test[-_]|[^/]+\.(?:test|spec)\.)|[-_]test\.(?:sh|py|[cm]?tsx?)$/u;
|
|
48
|
+
// --- TypeScript -------------------------------------------------------------------------------------------------
|
|
49
|
+
const CASE_CALLEE = /^(?:test|it)(?:\.(?:only|concurrent|serial|failing))?$/u;
|
|
50
|
+
const ASSERT_CALLEE = /^(?:expect|expectTypeOf|assert\w*)$/u;
|
|
51
|
+
/** Matchers that run the subject: a throw, a rejection or a resolution is a path of its own. */
|
|
52
|
+
const ACTING_MATCHER = /^(?:rejects|resolves|toThrow\w*)$/u;
|
|
53
|
+
function declaredNames(body) {
|
|
54
|
+
const names = new Map();
|
|
55
|
+
const bind = (name) => {
|
|
56
|
+
if (ts.isIdentifier(name)) {
|
|
57
|
+
if (!names.has(name.text))
|
|
58
|
+
names.set(name.text, names.size);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// A shorthand `{ name }` binds the property it reads: renaming it would equate `{ trace }` with `{ flush }`.
|
|
62
|
+
for (const element of name.elements)
|
|
63
|
+
if (!ts.isOmittedExpression(element) && (ts.isArrayBindingPattern(name) || element.propertyName !== undefined))
|
|
64
|
+
bind(element.name);
|
|
65
|
+
};
|
|
66
|
+
const visit = (node) => {
|
|
67
|
+
if (ts.isVariableDeclaration(node) || ts.isParameter(node))
|
|
68
|
+
bind(node.name);
|
|
69
|
+
if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name !== undefined)
|
|
70
|
+
bind(node.name);
|
|
71
|
+
ts.forEachChild(node, visit);
|
|
72
|
+
};
|
|
73
|
+
visit(body);
|
|
74
|
+
return names;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `expect(…)…` or `assert…(…)` that calls only methods (no free function, local closure or constructor) and runs no
|
|
78
|
+
* function, rejection or throw; or `if (…) throw …` with no else.
|
|
79
|
+
*/
|
|
80
|
+
function tsAssertion(statement) {
|
|
81
|
+
if (ts.isIfStatement(statement)) {
|
|
82
|
+
const then = statement.thenStatement;
|
|
83
|
+
const thrown = ts.isThrowStatement(then) || (ts.isBlock(then) && then.statements.every(ts.isThrowStatement));
|
|
84
|
+
return statement.elseStatement === undefined && thrown;
|
|
85
|
+
}
|
|
86
|
+
if (!ts.isExpressionStatement(statement))
|
|
87
|
+
return false;
|
|
88
|
+
let asserts = false;
|
|
89
|
+
let acts = false;
|
|
90
|
+
const visit = (node) => {
|
|
91
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isNewExpression(node))
|
|
92
|
+
acts = true;
|
|
93
|
+
else if (ts.isPropertyAccessExpression(node) && ACTING_MATCHER.test(node.name.text))
|
|
94
|
+
acts = true;
|
|
95
|
+
else if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
96
|
+
if (ASSERT_CALLEE.test(node.expression.text))
|
|
97
|
+
asserts = true;
|
|
98
|
+
else
|
|
99
|
+
acts = true;
|
|
100
|
+
}
|
|
101
|
+
ts.forEachChild(node, visit);
|
|
102
|
+
};
|
|
103
|
+
visit(statement.expression);
|
|
104
|
+
return asserts && !acts;
|
|
105
|
+
}
|
|
106
|
+
function tsTokens(node, source, locals) {
|
|
107
|
+
const tokens = [];
|
|
108
|
+
const visit = (child) => {
|
|
109
|
+
if (ts.isIdentifier(child)) {
|
|
110
|
+
// A property name (`.name`, `name:`, `{ name }`) is a key, never the local it may share a spelling with.
|
|
111
|
+
const parent = child.parent;
|
|
112
|
+
const key = ((ts.isPropertyAccessExpression(parent) || ts.isPropertyAssignment(parent)) && parent.name === child) ||
|
|
113
|
+
ts.isShorthandPropertyAssignment(parent);
|
|
114
|
+
const local = key ? undefined : locals.get(child.text);
|
|
115
|
+
tokens.push(local === undefined
|
|
116
|
+
? { text: child.text, literal: false, local: false }
|
|
117
|
+
: { text: `$${local}`, literal: false, local: true });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
switch (child.kind) {
|
|
121
|
+
case ts.SyntaxKind.StringLiteral:
|
|
122
|
+
case ts.SyntaxKind.NumericLiteral:
|
|
123
|
+
case ts.SyntaxKind.BigIntLiteral:
|
|
124
|
+
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
|
|
125
|
+
case ts.SyntaxKind.RegularExpressionLiteral:
|
|
126
|
+
case ts.SyntaxKind.TemplateHead:
|
|
127
|
+
case ts.SyntaxKind.TemplateMiddle:
|
|
128
|
+
case ts.SyntaxKind.TemplateTail:
|
|
129
|
+
case ts.SyntaxKind.TrueKeyword:
|
|
130
|
+
case ts.SyntaxKind.FalseKeyword:
|
|
131
|
+
tokens.push({ text: child.getText(source), literal: true, local: false });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const children = child.getChildren(source);
|
|
135
|
+
if (children.length === 0)
|
|
136
|
+
tokens.push({ text: child.getText(source), literal: false, local: false });
|
|
137
|
+
else
|
|
138
|
+
for (const grandchild of children)
|
|
139
|
+
visit(grandchild);
|
|
140
|
+
};
|
|
141
|
+
visit(node);
|
|
142
|
+
return tokens;
|
|
143
|
+
}
|
|
144
|
+
const HOOK_CALLEE = /^(?:before|after)(?:Each|All)$/u;
|
|
145
|
+
/**
|
|
146
|
+
* What the enclosing `describe` chain fixes for a case, outermost first, as source text: its `before*`/`after*` hook
|
|
147
|
+
* calls and its own declarations (`const make = () => new MemoryStore()` makes a different subject than a DiskStore).
|
|
148
|
+
*/
|
|
149
|
+
function describeChain(node, source) {
|
|
150
|
+
const context = [];
|
|
151
|
+
for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
|
|
152
|
+
if (!(ts.isArrowFunction(scope) || ts.isFunctionExpression(scope)) || !ts.isBlock(scope.body))
|
|
153
|
+
continue;
|
|
154
|
+
const call = scope.parent;
|
|
155
|
+
if (!ts.isCallExpression(call) || !/^describe(?:[.(]|$)/u.test(call.expression.getText(source)))
|
|
156
|
+
continue;
|
|
157
|
+
const own = scope.body.statements
|
|
158
|
+
.filter((statement) => ts.isVariableStatement(statement) ||
|
|
159
|
+
ts.isFunctionDeclaration(statement) ||
|
|
160
|
+
(ts.isExpressionStatement(statement) &&
|
|
161
|
+
ts.isCallExpression(statement.expression) &&
|
|
162
|
+
ts.isIdentifier(statement.expression.expression) &&
|
|
163
|
+
HOOK_CALLEE.test(statement.expression.expression.text)))
|
|
164
|
+
.map((statement) => statement.getText(source).replace(/\s+/gu, ' '));
|
|
165
|
+
context.unshift(...own);
|
|
166
|
+
}
|
|
167
|
+
return context;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The steps a case walks. A body that is one call whose last argument is a function, such as
|
|
171
|
+
* `await withHarness(memoryBackend, async (h) => { … })`, walks the call itself as its first step, with the function
|
|
172
|
+
* replaced by `$body` so the wrapper's other arguments still tell cases apart, and then that function's statements.
|
|
173
|
+
*/
|
|
174
|
+
function caseSteps(body, source, locals) {
|
|
175
|
+
const steps = [];
|
|
176
|
+
let statements = ts.isBlock(body) ? body.statements : [body];
|
|
177
|
+
for (;;) {
|
|
178
|
+
const only = statements.length === 1 ? statements[0] : undefined;
|
|
179
|
+
let expression = only !== undefined && ts.isExpressionStatement(only) ? only.expression : only;
|
|
180
|
+
while (expression !== undefined && ts.isAwaitExpression(expression))
|
|
181
|
+
expression = expression.expression;
|
|
182
|
+
const call = expression !== undefined && ts.isCallExpression(expression) ? expression : undefined;
|
|
183
|
+
const inner = call?.arguments.at(-1);
|
|
184
|
+
if (call === undefined ||
|
|
185
|
+
inner === undefined ||
|
|
186
|
+
!(ts.isArrowFunction(inner) || ts.isFunctionExpression(inner)) ||
|
|
187
|
+
!ts.isBlock(inner.body))
|
|
188
|
+
break;
|
|
189
|
+
const punct = (text) => ({ text, literal: false, local: false });
|
|
190
|
+
const wrapper = [
|
|
191
|
+
...tsTokens(call.expression, source, locals),
|
|
192
|
+
punct('('),
|
|
193
|
+
...call.arguments.slice(0, -1).flatMap((argument) => [...tsTokens(argument, source, locals), punct(',')]),
|
|
194
|
+
punct('$body'),
|
|
195
|
+
punct(')'),
|
|
196
|
+
];
|
|
197
|
+
steps.push(step(wrapper, false));
|
|
198
|
+
statements = inner.body.statements;
|
|
199
|
+
}
|
|
200
|
+
for (const statement of statements)
|
|
201
|
+
steps.push(step(tsTokens(statement, source, locals), tsAssertion(statement)));
|
|
202
|
+
return steps;
|
|
203
|
+
}
|
|
204
|
+
export function typeScriptCases(file, text) {
|
|
205
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true);
|
|
206
|
+
const cases = [];
|
|
207
|
+
const line = (offset) => source.getLineAndCharacterOfPosition(offset).line + 1;
|
|
208
|
+
const visit = (node) => {
|
|
209
|
+
const callee = ts.isCallExpression(node) ? node.expression.getText(source) : '';
|
|
210
|
+
if (ts.isCallExpression(node) && CASE_CALLEE.test(callee)) {
|
|
211
|
+
const body = node.arguments.find((argument) => ts.isArrowFunction(argument) || ts.isFunctionExpression(argument));
|
|
212
|
+
const title = node.arguments[0];
|
|
213
|
+
if (body !== undefined && title !== undefined) {
|
|
214
|
+
cases.push({
|
|
215
|
+
name: ts.isStringLiteralLike(title) ? title.text : title.getText(source),
|
|
216
|
+
start: line(node.getStart(source)),
|
|
217
|
+
column: source.getLineAndCharacterOfPosition(node.getStart(source)).character + 1,
|
|
218
|
+
end: line(node.end),
|
|
219
|
+
identity: [callee.endsWith('.failing') ? 'failing' : '', ...describeChain(node, source)].join('\n'),
|
|
220
|
+
steps: caseSteps(body.body, source, declaredNames(body)),
|
|
221
|
+
});
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
ts.forEachChild(node, visit);
|
|
226
|
+
};
|
|
227
|
+
visit(source);
|
|
228
|
+
return cases;
|
|
229
|
+
}
|
|
230
|
+
// --- Rust -------------------------------------------------------------------------------------------------------
|
|
231
|
+
const RUST_ASSERT = /^(?:debug_)?(?:prop_)?assert(?:_eq|_ne|_matches)?$/u;
|
|
232
|
+
/**
|
|
233
|
+
* `assert*!(…)` that borrows nothing mutably, calls no method on a `let mut` local (`mutable`, by renamed name), and
|
|
234
|
+
* otherwise calls only methods, macros and `Type::` items.
|
|
235
|
+
*/
|
|
236
|
+
function rustAssertion(tokens, mutable) {
|
|
237
|
+
if (!RUST_ASSERT.test(tokens[0]?.text ?? '') || tokens[1]?.text !== '!')
|
|
238
|
+
return false;
|
|
239
|
+
return tokens.every((token, index) => {
|
|
240
|
+
const before = tokens[index - 1]?.text;
|
|
241
|
+
if (token.text === 'mut')
|
|
242
|
+
return before !== '&';
|
|
243
|
+
if (mutable.has(token.text) && tokens[index + 1]?.text === '.' && tokens[index + 3]?.text === '(')
|
|
244
|
+
return false;
|
|
245
|
+
if (tokens[index + 1]?.text !== '(' || !/^[a-z_$]/u.test(token.text) || before === '.')
|
|
246
|
+
return true;
|
|
247
|
+
return before === '::' && /^[A-Z]/u.test(tokens[index - 2]?.text ?? '');
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const OPEN = { '(': true, '[': true, '{': true };
|
|
251
|
+
const CLOSE = { ')': true, ']': true, '}': true };
|
|
252
|
+
/** Statements that end at their closing brace with no `;`. */
|
|
253
|
+
const BLOCK_STATEMENT = {
|
|
254
|
+
if: true,
|
|
255
|
+
for: true,
|
|
256
|
+
while: true,
|
|
257
|
+
loop: true,
|
|
258
|
+
match: true,
|
|
259
|
+
unsafe: true,
|
|
260
|
+
'{': true,
|
|
261
|
+
};
|
|
262
|
+
/** Pattern keywords that bind nothing. */
|
|
263
|
+
const PATTERN_WORDS = { mut: true, ref: true, _: true };
|
|
264
|
+
/** The index of the token closing the group opened at `open`. */
|
|
265
|
+
function closing(tokens, open) {
|
|
266
|
+
let depth = 0;
|
|
267
|
+
for (let index = open; index < tokens.length; index += 1) {
|
|
268
|
+
const token = tokens[index];
|
|
269
|
+
if (token?.kind !== 'punct')
|
|
270
|
+
continue;
|
|
271
|
+
if (OPEN[token.text])
|
|
272
|
+
depth += 1;
|
|
273
|
+
else if (CLOSE[token.text]) {
|
|
274
|
+
depth -= 1;
|
|
275
|
+
if (depth === 0)
|
|
276
|
+
return index;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return tokens.length - 1;
|
|
280
|
+
}
|
|
281
|
+
function rustSteps(body) {
|
|
282
|
+
const locals = new Map();
|
|
283
|
+
const mutable = new Set();
|
|
284
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
285
|
+
if (body[index]?.text !== 'let')
|
|
286
|
+
continue;
|
|
287
|
+
for (let cursor = index + 1; cursor < body.length; cursor += 1) {
|
|
288
|
+
const token = body[cursor];
|
|
289
|
+
if (token === undefined || token.text === '=' || token.text === ':' || token.text === ';')
|
|
290
|
+
break;
|
|
291
|
+
if (token.kind !== 'ident' || !/^[a-z_]/u.test(token.text) || PATTERN_WORDS[token.text])
|
|
292
|
+
continue;
|
|
293
|
+
if (!locals.has(token.text))
|
|
294
|
+
locals.set(token.text, locals.size);
|
|
295
|
+
if (body[cursor - 1]?.text === 'mut')
|
|
296
|
+
mutable.add(`$${locals.get(token.text)}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const steps = [];
|
|
300
|
+
let current = [];
|
|
301
|
+
let depth = 0;
|
|
302
|
+
const flush = () => {
|
|
303
|
+
if (current.length === 0)
|
|
304
|
+
return;
|
|
305
|
+
steps.push(step(current, rustAssertion(current, mutable)));
|
|
306
|
+
current = [];
|
|
307
|
+
};
|
|
308
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
309
|
+
const token = body[index];
|
|
310
|
+
if (token === undefined)
|
|
311
|
+
break;
|
|
312
|
+
// A name after `.` or `::` is a field, method or path item, and a name before a lone `:` that opens or follows a
|
|
313
|
+
// `,` in a group is a struct-literal key: neither is the local it may share a spelling with.
|
|
314
|
+
const before = body[index - 1]?.text;
|
|
315
|
+
const member = before === '.' || before === '::';
|
|
316
|
+
const key = body[index + 1]?.text === ':' && (before === '{' || before === ',');
|
|
317
|
+
const local = token.kind === 'ident' && !member && !key ? locals.get(token.text) : undefined;
|
|
318
|
+
current.push(local === undefined ? token : { text: `$${local}`, literal: false, local: true });
|
|
319
|
+
if (token.kind !== 'punct')
|
|
320
|
+
continue;
|
|
321
|
+
if (OPEN[token.text])
|
|
322
|
+
depth += 1;
|
|
323
|
+
else if (CLOSE[token.text])
|
|
324
|
+
depth -= 1;
|
|
325
|
+
if (depth !== 0)
|
|
326
|
+
continue;
|
|
327
|
+
if (token.text === ';')
|
|
328
|
+
flush();
|
|
329
|
+
else if (token.text === '}' && BLOCK_STATEMENT[current[0]?.text ?? '']) {
|
|
330
|
+
const next = body[index + 1]?.text;
|
|
331
|
+
if (next !== 'else' && next !== '.' && next !== '?')
|
|
332
|
+
flush();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
flush();
|
|
336
|
+
return steps;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* An `async { … }` block that is the whole of a one-statement body, as in `runtime().block_on(async { … })`: the tokens
|
|
340
|
+
* before it (the wrapper, kept as the case's first step so a different runtime still tells cases apart) and the
|
|
341
|
+
* block's statements. Undefined for any other body.
|
|
342
|
+
*/
|
|
343
|
+
function asyncBody(body) {
|
|
344
|
+
const at = body.findIndex((token) => token.text === 'async');
|
|
345
|
+
if (at === -1)
|
|
346
|
+
return undefined;
|
|
347
|
+
const open = body[at + 1]?.text === 'move' ? at + 2 : at + 1;
|
|
348
|
+
if (body[open]?.text !== '{')
|
|
349
|
+
return undefined;
|
|
350
|
+
const close = closing(body, open);
|
|
351
|
+
// Only the call's closing parentheses, a `;` and an unwrap or `?` may follow the block.
|
|
352
|
+
return body.length - close <= 8 ? { wrapper: body.slice(0, at), inner: body.slice(open + 1, close) } : undefined;
|
|
353
|
+
}
|
|
354
|
+
/** Attributes that change what a test's outcome means or where it runs: an expected panic, not running it, a cfg gate. */
|
|
355
|
+
const OUTCOME_ATTRIBUTE = { should_panic: true, ignore: true, cfg: true, cfg_attr: true };
|
|
356
|
+
export function rustCases(text) {
|
|
357
|
+
const tokens = rustTokens(text);
|
|
358
|
+
const newlines = [];
|
|
359
|
+
for (let offset = text.indexOf('\n'); offset !== -1; offset = text.indexOf('\n', offset + 1))
|
|
360
|
+
newlines.push(offset);
|
|
361
|
+
const line = (offset) => {
|
|
362
|
+
let [low, high] = [0, newlines.length];
|
|
363
|
+
while (low < high) {
|
|
364
|
+
const middle = (low + high) >> 1;
|
|
365
|
+
if ((newlines[middle] ?? 0) < offset)
|
|
366
|
+
low = middle + 1;
|
|
367
|
+
else
|
|
368
|
+
high = middle;
|
|
369
|
+
}
|
|
370
|
+
return low + 1;
|
|
371
|
+
};
|
|
372
|
+
const cases = [];
|
|
373
|
+
// Outcome attributes may sit before or after `#[test]`; those before it are held until a non-attribute token.
|
|
374
|
+
let held = [];
|
|
375
|
+
let heldUntil = -1;
|
|
376
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
377
|
+
if (tokens[index]?.text !== '#' || tokens[index + 1]?.text !== '[') {
|
|
378
|
+
if (index !== heldUntil)
|
|
379
|
+
held = [];
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
const close = closing(tokens, index + 1);
|
|
383
|
+
const attribute = tokens.slice(index + 2, close).map((token) => token.text);
|
|
384
|
+
const pathEnd = attribute.findIndex((text) => text === '(' || text === '=');
|
|
385
|
+
const path = (pathEnd === -1 ? attribute : attribute.slice(0, pathEnd)).at(-1) ?? '';
|
|
386
|
+
if (path !== 'test') {
|
|
387
|
+
if (index !== heldUntil)
|
|
388
|
+
held = [];
|
|
389
|
+
if (OUTCOME_ATTRIBUTE[path])
|
|
390
|
+
held.push(attribute.join(' '));
|
|
391
|
+
heldUntil = close + 1;
|
|
392
|
+
index = close;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const modifiers = [...held];
|
|
396
|
+
held = [];
|
|
397
|
+
let cursor = close + 1;
|
|
398
|
+
while (cursor < tokens.length && tokens[cursor]?.text !== 'fn') {
|
|
399
|
+
if (tokens[cursor]?.text === '#' && tokens[cursor + 1]?.text === '[') {
|
|
400
|
+
const end = closing(tokens, cursor + 1);
|
|
401
|
+
const attribute = tokens.slice(cursor + 2, end).map((token) => token.text);
|
|
402
|
+
if (OUTCOME_ATTRIBUTE[attribute[0] ?? ''])
|
|
403
|
+
modifiers.push(attribute.join(' '));
|
|
404
|
+
cursor = end;
|
|
405
|
+
}
|
|
406
|
+
cursor += 1;
|
|
407
|
+
}
|
|
408
|
+
const name = tokens[cursor + 1]?.text;
|
|
409
|
+
while (cursor < tokens.length && tokens[cursor]?.text !== '{') {
|
|
410
|
+
if (tokens[cursor]?.text === '(')
|
|
411
|
+
cursor = closing(tokens, cursor);
|
|
412
|
+
cursor += 1;
|
|
413
|
+
}
|
|
414
|
+
if (name === undefined || cursor >= tokens.length)
|
|
415
|
+
break;
|
|
416
|
+
const end = closing(tokens, cursor);
|
|
417
|
+
const start = tokens[index]?.offset ?? 0;
|
|
418
|
+
const body = tokens.slice(cursor + 1, end);
|
|
419
|
+
const steps = rustSteps(body);
|
|
420
|
+
const wrapped = steps.length === 1 ? asyncBody(body) : undefined;
|
|
421
|
+
cases.push({
|
|
422
|
+
name,
|
|
423
|
+
start: line(start),
|
|
424
|
+
column: start - (newlines[line(start) - 2] ?? -1),
|
|
425
|
+
end: line(tokens[end]?.offset ?? start),
|
|
426
|
+
identity: modifiers.sort().join('\n'),
|
|
427
|
+
steps: wrapped === undefined ? steps : [step(wrapped.wrapper, false), ...rustSteps(wrapped.inner)],
|
|
428
|
+
});
|
|
429
|
+
index = end;
|
|
430
|
+
}
|
|
431
|
+
return cases;
|
|
432
|
+
}
|
|
433
|
+
// --- Comparison -------------------------------------------------------------------------------------------------
|
|
434
|
+
/**
|
|
435
|
+
* The duplication between two cases of one identity, if either walks the other's path again, differs from it only in
|
|
436
|
+
* literals, or replays its scenario with one injected step changed.
|
|
437
|
+
*/
|
|
438
|
+
export function duplication(first, second) {
|
|
439
|
+
if (first.identity !== second.identity)
|
|
440
|
+
return undefined;
|
|
441
|
+
const [a, b] = [first.steps, second.steps];
|
|
442
|
+
if (a.length === b.length && a.length >= TEST_ONE_WALK.literalTwin && a.every((s, i) => s.shape === b[i]?.shape))
|
|
443
|
+
return { kind: 'literal-twin', shared: a.length };
|
|
444
|
+
const shared = exactPrefix(a, b);
|
|
445
|
+
if (shared >= TEST_ONE_WALK.samePath && (onlyAssertsAfter(a, shared) || onlyAssertsAfter(b, shared)))
|
|
446
|
+
return { kind: 'same-path', shared };
|
|
447
|
+
return sameScenario(a, b);
|
|
448
|
+
}
|
|
449
|
+
function exactPrefix(a, b) {
|
|
450
|
+
let shared = 0;
|
|
451
|
+
while (shared < a.length && shared < b.length && a[shared]?.exact === b[shared]?.exact)
|
|
452
|
+
shared += 1;
|
|
453
|
+
return shared;
|
|
454
|
+
}
|
|
455
|
+
function onlyAssertsAfter(steps, shared) {
|
|
456
|
+
return steps.slice(shared).every((s) => s.assertion);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* One case is the other's setup (equal in skeleton) and action and outcome (equal in flow: the same calls with the
|
|
460
|
+
* same values, up to declared names) around exactly one statement of its own: the same scenario with a different
|
|
461
|
+
* injected step. The other case may inject more steps, and either case may end with extra pure assertions; they are
|
|
462
|
+
* more checks on the same outcome. The shared tail must hold an action, so two cases that only assert alike after
|
|
463
|
+
* different actions are not one scenario.
|
|
464
|
+
*/
|
|
465
|
+
function sameScenario(a, b) {
|
|
466
|
+
const shared = Math.max(framed(a, b), framed(b, a));
|
|
467
|
+
return shared > 0 ? { kind: 'same-scenario', shared } : undefined;
|
|
468
|
+
}
|
|
469
|
+
/** How many trailing statements of `steps` are pure assertions. */
|
|
470
|
+
function trailingAssertions(steps) {
|
|
471
|
+
let count = 0;
|
|
472
|
+
while (count < steps.length && steps[steps.length - 1 - count]?.assertion === true)
|
|
473
|
+
count += 1;
|
|
474
|
+
return count;
|
|
475
|
+
}
|
|
476
|
+
/** The head and tail `own` shares with `other` when exactly one statement of `own` lies between them, else 0. */
|
|
477
|
+
function framed(own, other) {
|
|
478
|
+
let head = 0;
|
|
479
|
+
while (head < own.length && own[head]?.skeleton === other[head]?.skeleton)
|
|
480
|
+
head += 1;
|
|
481
|
+
if (head < TEST_ONE_WALK.scenarioHead)
|
|
482
|
+
return 0;
|
|
483
|
+
let best = 0;
|
|
484
|
+
for (let ownExtra = 0; ownExtra <= trailingAssertions(own); ownExtra += 1) {
|
|
485
|
+
const ownEnd = own.length - ownExtra;
|
|
486
|
+
for (let otherExtra = 0; otherExtra <= trailingAssertions(other); otherExtra += 1) {
|
|
487
|
+
const otherEnd = other.length - otherExtra;
|
|
488
|
+
let tail = 0;
|
|
489
|
+
while (tail < ownEnd - head &&
|
|
490
|
+
tail < otherEnd - head &&
|
|
491
|
+
own[ownEnd - 1 - tail]?.flow === other[otherEnd - 1 - tail]?.flow)
|
|
492
|
+
tail += 1;
|
|
493
|
+
const acts = own.slice(ownEnd - tail, ownEnd).some((s) => !s.assertion);
|
|
494
|
+
if (acts && tail >= TEST_ONE_WALK.scenarioTail && ownEnd - head - tail === 1)
|
|
495
|
+
best = Math.max(best, head + tail);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return best;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* For each case, the earlier case it most nearly repeats among the pairs `judged` admits (a diff-scoped caller admits
|
|
502
|
+
* a pair when either case changed; partners are chosen only among those). The nearest refusing partner and the nearest
|
|
503
|
+
* advisory partner are reported separately, so an advisory pair can never hide a refusing one.
|
|
504
|
+
*/
|
|
505
|
+
export function duplicateScenarios(file, cases, judged = () => true) {
|
|
506
|
+
const found = [];
|
|
507
|
+
cases.forEach((second, later) => {
|
|
508
|
+
const best = {};
|
|
509
|
+
for (const first of cases.slice(0, later)) {
|
|
510
|
+
if (!judged(first, second))
|
|
511
|
+
continue;
|
|
512
|
+
const match = duplication(first, second);
|
|
513
|
+
if (match === undefined)
|
|
514
|
+
continue;
|
|
515
|
+
const slot = REFUSES[match.kind] ? 'refusing' : 'advisory';
|
|
516
|
+
const held = best[slot];
|
|
517
|
+
if (held === undefined || match.shared > held.shared)
|
|
518
|
+
best[slot] = { file, first, second, ...match };
|
|
519
|
+
}
|
|
520
|
+
if (best.refusing !== undefined)
|
|
521
|
+
found.push(best.refusing);
|
|
522
|
+
if (best.advisory !== undefined)
|
|
523
|
+
found.push(best.advisory);
|
|
524
|
+
});
|
|
525
|
+
return found;
|
|
526
|
+
}
|
|
527
|
+
export function testCases(file, text) {
|
|
528
|
+
if (JS_EXTENSION.test(extname(file)))
|
|
529
|
+
return TEST_PATH.test(file) ? typeScriptCases(file, text) : undefined;
|
|
530
|
+
return extname(file) === '.rs' ? rustCases(text) : undefined;
|
|
531
|
+
}
|
|
532
|
+
export function findDuplicateScenarios(root, files, changes) {
|
|
533
|
+
const found = [];
|
|
534
|
+
for (const file of files) {
|
|
535
|
+
const path = join(root, file);
|
|
536
|
+
if (!existsSync(path))
|
|
537
|
+
continue;
|
|
538
|
+
const cases = testCases(file, readFileSync(path, 'utf8'));
|
|
539
|
+
if (cases === undefined)
|
|
540
|
+
continue;
|
|
541
|
+
const ranges = changes?.get(file);
|
|
542
|
+
const touched = (test) => ranges === undefined || ranges.some((range) => range.start <= test.end && range.end >= test.start);
|
|
543
|
+
found.push(...duplicateScenarios(file, cases, (first, second) => touched(first) || touched(second)));
|
|
544
|
+
}
|
|
545
|
+
return found;
|
|
546
|
+
}
|
|
547
|
+
export function formatDuplicateScenario(duplicate) {
|
|
548
|
+
const { first, second, shared } = duplicate;
|
|
549
|
+
const [a, b] = [`\`${first.name}\``, `\`${second.name}\``];
|
|
550
|
+
const verbatim = first.steps.length === second.steps.length && first.steps.every((s, i) => s.exact === second.steps[i]?.exact);
|
|
551
|
+
const asserter = onlyAssertsAfter(first.steps, shared) ? [a, b] : [b, a];
|
|
552
|
+
const what = duplicate.kind === 'literal-twin'
|
|
553
|
+
? verbatim
|
|
554
|
+
? `${b} repeats ${a} verbatim (${shared} statements)`
|
|
555
|
+
: `${b} differs from ${a} only in literals (${shared} statements of one shape)`
|
|
556
|
+
: duplicate.kind === 'same-path'
|
|
557
|
+
? `${asserter[0]} only asserts after the ${shared} statements it shares with ${asserter[1]}: its assertions ` +
|
|
558
|
+
`belong in ${asserter[1]} after statement ${shared}`
|
|
559
|
+
: `${b} replays the scenario of ${a} with one step changed (${shared} shared statements of setup, action ` +
|
|
560
|
+
'and outcome)';
|
|
561
|
+
return (`${REFUSES[duplicate.kind] ? 'error' : 'warning'}[${TEST_ONE_WALK.id}]: ${what}\n` +
|
|
562
|
+
` --> ${duplicate.file}:${second.start}:${second.column}\n` +
|
|
563
|
+
` --> ${duplicate.file}:${first.start}:${first.column}\n` +
|
|
564
|
+
` = help: ${TEST_ONE_WALK.help}`);
|
|
565
|
+
}
|
|
566
|
+
/** Runs git without global or system configuration, so a sandboxed caller gets the same answer as its host. */
|
|
567
|
+
function git(root, args) {
|
|
568
|
+
const result = spawnSync('git', args, {
|
|
569
|
+
cwd: root,
|
|
570
|
+
encoding: 'utf8',
|
|
571
|
+
env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' },
|
|
572
|
+
maxBuffer: 1 << 28,
|
|
573
|
+
});
|
|
574
|
+
return { status: result.status ?? -1, stdout: result.stdout, stderr: result.stderr.trim() };
|
|
575
|
+
}
|
|
576
|
+
function failed(args, result) {
|
|
577
|
+
return `git ${args.join(' ')}: ${result.stderr || `exit ${result.status}`}`;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* The merge base of HEAD and the upstream main. A set `NX_BASE` (exported by nx-set-shas in CI) is the upstream and
|
|
581
|
+
* must name a commit. Otherwise the upstream is the first that exists of `refs/remotes/main/main`, the local main's
|
|
582
|
+
* upstream branch and `refs/remotes/origin/main`, and local `main` when none does.
|
|
583
|
+
*/
|
|
584
|
+
export function upstreamBase(root) {
|
|
585
|
+
const nxBase = process.env.NX_BASE;
|
|
586
|
+
const exists = (ref) => {
|
|
587
|
+
const args = ['rev-parse', '--verify', '--quiet', ref];
|
|
588
|
+
const result = git(root, args);
|
|
589
|
+
// `--verify --quiet` answers an absent ref with status 1; any other failure is git failing, not an answer.
|
|
590
|
+
if (result.status === 0 || result.status === 1)
|
|
591
|
+
return { ok: true, value: result.status === 0 };
|
|
592
|
+
return { ok: false, reason: failed(args, result) };
|
|
593
|
+
};
|
|
594
|
+
let upstream = 'main';
|
|
595
|
+
if (nxBase !== undefined && nxBase !== '') {
|
|
596
|
+
const named = exists(`${nxBase}^{commit}`);
|
|
597
|
+
if (!named.ok)
|
|
598
|
+
return named;
|
|
599
|
+
if (!named.value)
|
|
600
|
+
return { ok: false, reason: `NX_BASE=${nxBase} names no commit` };
|
|
601
|
+
upstream = nxBase;
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
// The local main's upstream, when it has one: `for-each-ref` answers empty, never fails, when either is missing.
|
|
605
|
+
const tracked = git(root, ['for-each-ref', '--format=%(upstream)', 'refs/heads/main']).stdout.trim();
|
|
606
|
+
for (const ref of ['refs/remotes/main/main', tracked, 'refs/remotes/origin/main'].filter(Boolean)) {
|
|
607
|
+
const found = exists(ref);
|
|
608
|
+
if (!found.ok)
|
|
609
|
+
return found;
|
|
610
|
+
if (found.value) {
|
|
611
|
+
upstream = ref;
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
const args = ['merge-base', 'HEAD', upstream];
|
|
617
|
+
const result = git(root, args);
|
|
618
|
+
if (result.status !== 0)
|
|
619
|
+
return {
|
|
620
|
+
ok: false,
|
|
621
|
+
reason: `${failed(args, result)} (fetch main, set refs/remotes/main/main, or export NX_BASE)`,
|
|
622
|
+
};
|
|
623
|
+
return { ok: true, value: result.stdout.trim() };
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* The files changed since `base`, tracked or untracked, and the new-side line ranges of each change; an untracked
|
|
627
|
+
* file is changed throughout.
|
|
628
|
+
*/
|
|
629
|
+
export function diffScope(root, base) {
|
|
630
|
+
const read = (args) => {
|
|
631
|
+
const result = git(root, args);
|
|
632
|
+
return result.status === 0 ? { ok: true, value: result.stdout } : { ok: false, reason: failed(args, result) };
|
|
633
|
+
};
|
|
634
|
+
const changed = read(['diff', '--name-only', '-z', base, '--']);
|
|
635
|
+
if (!changed.ok)
|
|
636
|
+
return changed;
|
|
637
|
+
const listed = read(['ls-files', '--others', '--exclude-standard', '-z']);
|
|
638
|
+
if (!listed.ok)
|
|
639
|
+
return listed;
|
|
640
|
+
const untracked = new Set(listed.value.split('\0'));
|
|
641
|
+
const files = [...new Set([...changed.value.split('\0'), ...untracked].filter(Boolean))].sort();
|
|
642
|
+
const changes = new Map();
|
|
643
|
+
for (const file of files) {
|
|
644
|
+
if (untracked.has(file)) {
|
|
645
|
+
changes.set(file, [{ start: 1, end: Number.POSITIVE_INFINITY }]);
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const diff = read(['diff', '--unified=0', '--no-ext-diff', '--no-color', base, '--', file]);
|
|
649
|
+
if (!diff.ok)
|
|
650
|
+
return diff;
|
|
651
|
+
changes.set(file, Array.from(diff.value.matchAll(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gmu), (match) => {
|
|
652
|
+
const start = Number(match[1]);
|
|
653
|
+
return { start, end: start + Math.max(Number(match[2] ?? 1), 1) - 1 };
|
|
654
|
+
}));
|
|
655
|
+
}
|
|
656
|
+
return { ok: true, value: { files, changes } };
|
|
657
|
+
}
|