@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.
Files changed (343) hide show
  1. package/README.md +994 -117
  2. package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
  3. package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-bound-failures.js +31 -0
  5. package/dist/bin/smoo-nx-cargo-hash.js +28 -3
  6. package/dist/bin/smoo-nx-exec.js +15 -73
  7. package/dist/bin/smoo-nx-failures.d.ts +3 -0
  8. package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
  9. package/dist/bin/smoo-nx-failures.js +3 -0
  10. package/dist/bin/smoo-nx-nextest-extract.js +3 -2
  11. package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
  12. package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
  13. package/dist/bin/smoo-nx-test-walks.js +34 -0
  14. package/dist/bounded-test-policy.d.ts +7 -1
  15. package/dist/bounded-test-policy.d.ts.map +1 -1
  16. package/dist/bounded-test-policy.js +101 -40
  17. package/dist/build-state-output-policy.d.ts +25 -0
  18. package/dist/build-state-output-policy.d.ts.map +1 -0
  19. package/dist/build-state-output-policy.js +64 -0
  20. package/dist/cargo-closure-input.d.ts +3 -2
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +16 -15
  23. package/dist/cargo-dev-profile.d.ts +37 -0
  24. package/dist/cargo-dev-profile.d.ts.map +1 -0
  25. package/dist/cargo-dev-profile.js +108 -0
  26. package/dist/cargo-source-hash.d.ts +43 -6
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +309 -73
  29. package/dist/cargo-toolchain-policy.d.ts +4 -0
  30. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  31. package/dist/cargo-toolchain-policy.js +5 -0
  32. package/dist/cargo-workspace.d.ts +101 -5
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +291 -10
  35. package/dist/cross-check-policy.d.ts +21 -3
  36. package/dist/cross-check-policy.d.ts.map +1 -1
  37. package/dist/cross-check-policy.js +24 -6
  38. package/dist/disk-command-policy.d.ts +7 -0
  39. package/dist/disk-command-policy.d.ts.map +1 -0
  40. package/dist/disk-command-policy.js +98 -0
  41. package/dist/ensure-built-receipt.d.ts +25 -0
  42. package/dist/ensure-built-receipt.d.ts.map +1 -0
  43. package/dist/ensure-built-receipt.js +261 -0
  44. package/dist/ensure-built-witness.d.ts +57 -0
  45. package/dist/ensure-built-witness.d.ts.map +1 -0
  46. package/dist/ensure-built-witness.js +412 -0
  47. package/dist/ensure-built.d.ts +207 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +576 -202
  50. package/dist/eslint-file-set.d.ts +7 -0
  51. package/dist/eslint-file-set.d.ts.map +1 -0
  52. package/dist/eslint-file-set.js +12 -0
  53. package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
  54. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
  55. package/dist/executors/bounded-exec/cpu-pool.js +379 -0
  56. package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
  57. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
  58. package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
  59. package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
  60. package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/disk-lease.js +91 -0
  62. package/dist/executors/bounded-exec/executor.d.ts +45 -2
  63. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  64. package/dist/executors/bounded-exec/executor.js +447 -52
  65. package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
  66. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
  67. package/dist/executors/bounded-exec/gateway-lease.js +143 -0
  68. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
  69. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
  70. package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
  71. package/dist/executors/bounded-exec/progress.d.ts +35 -0
  72. package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
  73. package/dist/executors/bounded-exec/progress.js +79 -0
  74. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
  75. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
  76. package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
  77. package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
  78. package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
  79. package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
  80. package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
  81. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
  82. package/dist/executors/bounded-exec/ram-temp.js +1528 -0
  83. package/dist/executors/bounded-exec/schema.d.ts +29 -11
  84. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  85. package/dist/executors/bounded-exec/verdict.d.ts +162 -0
  86. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/verdict.js +318 -0
  88. package/dist/executors/shard-timings/executor.d.ts +14 -0
  89. package/dist/executors/shard-timings/executor.d.ts.map +1 -0
  90. package/dist/executors/shard-timings/executor.js +25 -0
  91. package/dist/executors/shard-timings/schema.d.ts +4 -0
  92. package/dist/executors/shard-timings/schema.d.ts.map +1 -0
  93. package/dist/executors/shard-timings/schema.js +1 -0
  94. package/dist/failure-summary.d.ts +5 -0
  95. package/dist/failure-summary.d.ts.map +1 -0
  96. package/dist/failure-summary.js +118 -0
  97. package/dist/generators/create-package/generator.d.ts.map +1 -1
  98. package/dist/generators/create-package/generator.js +6 -2
  99. package/dist/index.d.ts +14 -2
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +370 -138
  102. package/dist/managed-files/ci-workflow.d.ts +13 -0
  103. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.js +44 -13
  105. package/dist/managed-files/files.d.ts +7 -1
  106. package/dist/managed-files/files.d.ts.map +1 -1
  107. package/dist/managed-files/files.js +62 -8
  108. package/dist/managed-files/json-edit.d.ts +21 -0
  109. package/dist/managed-files/json-edit.d.ts.map +1 -0
  110. package/dist/managed-files/json-edit.js +295 -0
  111. package/dist/managed-files/managed-format.d.ts +33 -1
  112. package/dist/managed-files/managed-format.d.ts.map +1 -1
  113. package/dist/managed-files/managed-format.js +82 -2
  114. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  115. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  116. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  117. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/publish-workflow.js +231 -48
  119. package/dist/managed-files/tree.d.ts +5 -0
  120. package/dist/managed-files/tree.d.ts.map +1 -1
  121. package/dist/managed-files/tree.js +5 -3
  122. package/dist/manifest-hash.d.ts.map +1 -1
  123. package/dist/manifest-hash.js +2 -5
  124. package/dist/nextest-code-bound.d.ts +62 -0
  125. package/dist/nextest-code-bound.d.ts.map +1 -0
  126. package/dist/nextest-code-bound.js +154 -0
  127. package/dist/nextest-extraction.d.ts +24 -8
  128. package/dist/nextest-extraction.d.ts.map +1 -1
  129. package/dist/nextest-extraction.js +42 -7
  130. package/dist/package-target-policy.d.ts.map +1 -1
  131. package/dist/package-target-policy.js +3 -5
  132. package/dist/release-config-policy.d.ts.map +1 -1
  133. package/dist/release-config-policy.js +3 -5
  134. package/dist/runtime-input.d.ts +14 -0
  135. package/dist/runtime-input.d.ts.map +1 -0
  136. package/dist/runtime-input.js +15 -0
  137. package/dist/rust-tokens.d.ts +11 -0
  138. package/dist/rust-tokens.d.ts.map +1 -0
  139. package/dist/rust-tokens.js +81 -0
  140. package/dist/task-results.d.ts +6 -0
  141. package/dist/task-results.d.ts.map +1 -0
  142. package/dist/task-results.js +61 -0
  143. package/dist/test-program.d.ts +54 -0
  144. package/dist/test-program.d.ts.map +1 -0
  145. package/dist/test-program.js +292 -0
  146. package/dist/test-timings.d.ts +71 -0
  147. package/dist/test-timings.d.ts.map +1 -0
  148. package/dist/test-timings.js +393 -0
  149. package/dist/test-walk-policy.d.ts +103 -0
  150. package/dist/test-walk-policy.d.ts.map +1 -0
  151. package/dist/test-walk-policy.js +657 -0
  152. package/dist/testing.d.ts +177 -0
  153. package/dist/testing.d.ts.map +1 -0
  154. package/dist/testing.js +824 -0
  155. package/dist/type-guards.d.ts +3 -0
  156. package/dist/type-guards.d.ts.map +1 -0
  157. package/dist/type-guards.js +4 -0
  158. package/dist/typecheck-test-policy.d.ts +8 -5
  159. package/dist/typecheck-test-policy.d.ts.map +1 -1
  160. package/dist/typecheck-test-policy.js +113 -100
  161. package/dist/workspace-config-policy.d.ts +8 -0
  162. package/dist/workspace-config-policy.d.ts.map +1 -1
  163. package/dist/workspace-config-policy.js +175 -63
  164. package/dist/workspace-index.d.ts +12 -0
  165. package/dist/workspace-index.d.ts.map +1 -0
  166. package/dist/workspace-index.js +17 -0
  167. package/dist/workspace-manifest.d.ts +37 -0
  168. package/dist/workspace-manifest.d.ts.map +1 -1
  169. package/executors.json +5 -0
  170. package/loader.js +13 -11
  171. package/managed/raw/envrc +33 -67
  172. package/managed/raw/git-format-staged.yml +30 -35
  173. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  174. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  175. package/managed/raw/tooling/direnv/developer-links.ts +569 -11
  176. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
  177. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  178. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  179. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  180. package/managed/raw/tooling/direnv/repo-path +14 -2
  181. package/managed/raw/tooling/direnv/setup-environment.ts +1146 -159
  182. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  183. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  184. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  185. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  186. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  187. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  188. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  189. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  190. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  191. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  192. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  194. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  196. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  197. package/managed/templates/github/workflows/managed-files.yml +1 -2
  198. package/nextest.toml +8 -41
  199. package/package.json +74 -12
  200. package/runtime-input.sh +81 -0
  201. package/src/__tests__/counted-cargo.ts +33 -14
  202. package/src/__tests__/fixture-nx-env.ts +111 -46
  203. package/src/__tests__/progress-clock.ts +43 -0
  204. package/src/__tests__/runtime-input.ts +23 -0
  205. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  206. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  207. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  208. package/src/bin/smoo-nx-exec.ts +16 -74
  209. package/src/bin/smoo-nx-failures.ts +4 -0
  210. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  211. package/src/bin/smoo-nx-test-walks.ts +42 -0
  212. package/src/bounded-test-policy.test.ts +268 -21
  213. package/src/bounded-test-policy.ts +114 -42
  214. package/src/build-state-output-policy.test.ts +115 -0
  215. package/src/build-state-output-policy.ts +90 -0
  216. package/src/bun-bootstrap.test.ts +57 -3
  217. package/src/bun-file-packages.test.ts +47 -0
  218. package/src/cargo-closure-input.test.ts +138 -66
  219. package/src/cargo-closure-input.ts +17 -15
  220. package/src/cargo-dev-profile.ts +137 -0
  221. package/src/cargo-resolve-cache.test.ts +70 -1
  222. package/src/cargo-source-hash.test.ts +193 -97
  223. package/src/cargo-source-hash.ts +345 -68
  224. package/src/cargo-toolchain-policy.ts +6 -0
  225. package/src/cargo-workspace.test.ts +320 -0
  226. package/src/cargo-workspace.ts +336 -10
  227. package/src/cross-check-policy.test.ts +59 -1
  228. package/src/cross-check-policy.ts +25 -6
  229. package/src/disk-command-policy.test.ts +61 -0
  230. package/src/disk-command-policy.ts +107 -0
  231. package/src/ensure-built-receipt.test.ts +184 -0
  232. package/src/ensure-built-receipt.ts +263 -0
  233. package/src/ensure-built-witness.test.ts +344 -0
  234. package/src/ensure-built-witness.ts +252 -0
  235. package/src/ensure-built.test.ts +1036 -578
  236. package/src/ensure-built.ts +820 -224
  237. package/src/eslint-file-set.ts +9 -0
  238. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  239. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  240. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  241. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  242. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  243. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  244. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  245. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  246. package/src/executors/bounded-exec/executor.ts +565 -67
  247. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  248. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  249. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  250. package/src/executors/bounded-exec/progress.test.ts +79 -0
  251. package/src/executors/bounded-exec/progress.ts +118 -0
  252. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  253. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  254. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  255. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  256. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  257. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  258. package/src/executors/bounded-exec/schema.json +11 -5
  259. package/src/executors/bounded-exec/schema.ts +27 -11
  260. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  261. package/src/executors/bounded-exec/verdict.ts +433 -0
  262. package/src/executors/shard-timings/executor.ts +36 -0
  263. package/src/executors/shard-timings/schema.json +21 -0
  264. package/src/executors/shard-timings/schema.ts +4 -0
  265. package/src/failure-summary.ts +119 -0
  266. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  267. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  268. package/src/generators/create-package/generator.test.ts +6 -0
  269. package/src/generators/create-package/generator.ts +6 -2
  270. package/src/host-controller-wiring.test.ts +31 -0
  271. package/src/index.test.ts +909 -295
  272. package/src/index.ts +441 -143
  273. package/src/linked-snapshots.nx.test.ts +172 -0
  274. package/src/managed-files/ci-workflow.ts +54 -13
  275. package/src/managed-files/files.ts +65 -9
  276. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  277. package/src/managed-files/generator.test.ts +60 -1
  278. package/src/managed-files/json-edit.test.ts +216 -0
  279. package/src/managed-files/json-edit.ts +290 -0
  280. package/src/managed-files/managed-format-json.test.ts +308 -0
  281. package/src/managed-files/managed-format.ts +90 -2
  282. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  283. package/src/managed-files/publish-workflow.ts +285 -67
  284. package/src/managed-files/repo-path.test.ts +129 -0
  285. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  286. package/src/managed-files/tree.ts +11 -3
  287. package/src/manifest-hash.ts +2 -6
  288. package/src/nextest-code-bound.test.ts +155 -0
  289. package/src/nextest-code-bound.ts +195 -0
  290. package/src/nextest-extraction.test.ts +37 -5
  291. package/src/nextest-extraction.ts +51 -9
  292. package/src/nextest-partitions.test.ts +427 -82
  293. package/src/nx-cache-bound.test.ts +5 -10
  294. package/src/nx-cache-restore-mtime.test.ts +170 -0
  295. package/src/nx-daemon-claim.test.ts +277 -0
  296. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  297. package/src/nx-daemon-idle-policy.test.ts +51 -0
  298. package/src/nx-daemon-ignore-files.test.ts +210 -0
  299. package/src/nx-daemon-root-normalization.test.ts +105 -0
  300. package/src/nx-daemon-validity.test.ts +925 -0
  301. package/src/nx-dot-input-policy.test.ts +353 -0
  302. package/src/nx-failure-life-cycles.test.ts +207 -0
  303. package/src/nx-files-archive-racy.test.ts +97 -0
  304. package/src/nx-foreign-workspace-env.test.ts +329 -0
  305. package/src/nx-incomplete-run-summary.test.ts +70 -0
  306. package/src/nx-plugin-load-errors.test.ts +120 -0
  307. package/src/nx-project-resolution-cache.test.ts +176 -0
  308. package/src/nx-raw-inputs.test.ts +166 -0
  309. package/src/nx-resident-graph-workers.test.ts +7 -16
  310. package/src/nx-run-information.test.ts +118 -0
  311. package/src/nx-skipped-task-exit.test.ts +180 -0
  312. package/src/nx-state-location.test.ts +63 -0
  313. package/src/nx-store-resolution.test.ts +26 -25
  314. package/src/nx-task-graph-cycles.test.ts +148 -0
  315. package/src/nx-task-output-lease.test.ts +414 -0
  316. package/src/nx-use-local.test.ts +103 -0
  317. package/src/package-target-policy.ts +3 -6
  318. package/src/release-config-policy.ts +3 -6
  319. package/src/runtime-input.test.ts +215 -0
  320. package/src/runtime-input.ts +17 -0
  321. package/src/rust-tokens.ts +78 -0
  322. package/src/serial-group-wiring.test.ts +326 -0
  323. package/src/task-history-namespace.test.ts +87 -89
  324. package/src/task-results.test.ts +226 -0
  325. package/src/task-results.ts +75 -0
  326. package/src/test-program.test.ts +303 -0
  327. package/src/test-program.ts +341 -0
  328. package/src/test-timings.test.ts +240 -0
  329. package/src/test-timings.ts +311 -0
  330. package/src/test-walk-policy.test.ts +585 -0
  331. package/src/test-walk-policy.ts +731 -0
  332. package/src/testing.test.ts +509 -0
  333. package/src/testing.ts +922 -0
  334. package/src/type-guards.ts +4 -0
  335. package/src/typecheck-test-policy.test.ts +496 -11
  336. package/src/typecheck-test-policy.ts +137 -103
  337. package/src/workspace-config-policy.test.ts +212 -13
  338. package/src/workspace-config-policy.ts +101 -73
  339. package/src/workspace-index.ts +18 -0
  340. package/src/workspace-manifest.ts +41 -0
  341. package/test-timings.json +85 -30
  342. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
  343. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
@@ -0,0 +1,303 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import {
3
+ includeGlobsFor,
4
+ listTypeScriptSources,
5
+ type ProjectTree,
6
+ type TestProgram,
7
+ testProgramFault,
8
+ } from './test-program.js';
9
+
10
+ type Configs = Record<string, Record<string, unknown>>;
11
+
12
+ function program(files: string[], configs: Configs = {}): TestProgram {
13
+ return { projectRoot: 'packages/app', files, readConfig: (path) => configs[path] ?? null };
14
+ }
15
+
16
+ /** The fault of a project whose tsconfig.test.json is `config`, with `configs` readable beside it. */
17
+ function faultOf(files: string[], config: Record<string, unknown>, configs: Configs = {}) {
18
+ return testProgramFault(program(files, configs), config);
19
+ }
20
+
21
+ const SRC_TESTS = ['src/a.test.ts'];
22
+
23
+ describe('which files a test program selects', () => {
24
+ it('selects the tests its include names', () => {
25
+ expect(faultOf(SRC_TESTS, { include: ['src/**/*.test.ts'] })).toBeNull();
26
+ });
27
+
28
+ it('names the tests an include leaves out', () => {
29
+ expect(faultOf(SRC_TESTS, { include: ['lib/**/*.ts'] })).toEqual({ kind: 'tests-unselected', tests: SRC_TESTS });
30
+ });
31
+
32
+ it('reports a project with no tests when no file is selected, and none when some is', () => {
33
+ expect(faultOf(['src/index.ts'], { include: ['src/**/*.test.ts'] })).toEqual({ kind: 'no-tests' });
34
+ expect(faultOf(['src/index.ts'], { include: ['src/**/*.ts'] })).toBeNull();
35
+ });
36
+
37
+ it('reads a last segment with no wildcard and no extension as a directory', () => {
38
+ expect(faultOf(SRC_TESTS, { include: ['src'] })).toBeNull();
39
+ expect(faultOf(['lib/a.test.ts'], { include: ['src'] })).toEqual({
40
+ kind: 'tests-unselected',
41
+ tests: ['lib/a.test.ts'],
42
+ });
43
+ });
44
+
45
+ it('reads `?` as one character and `*` as one segment', () => {
46
+ expect(faultOf(['src/a.test.ts'], { include: ['src/?.test.ts'] })).toBeNull();
47
+ expect(faultOf(['src/deep/a.test.ts'], { include: ['src/*.test.ts'] })).not.toBeNull();
48
+ });
49
+
50
+ it('applies the default include, every file, when neither include nor files is declared', () => {
51
+ expect(faultOf(SRC_TESTS, {})).toBeNull();
52
+ });
53
+
54
+ it('selects only the named files when `files` is declared without an include', () => {
55
+ expect(faultOf(['src/a.test.ts', 'src/index.ts'], { files: ['src/a.test.ts'] })).toBeNull();
56
+ expect(faultOf(['src/a.test.ts', 'src/index.ts'], { files: ['src/index.ts'] })).toEqual({
57
+ kind: 'tests-unselected',
58
+ tests: ['src/a.test.ts'],
59
+ });
60
+ });
61
+
62
+ it('removes what an exclude names, and everything beneath it', () => {
63
+ expect(faultOf(SRC_TESTS, { include: ['src/**/*.ts'], exclude: ['src'] })).not.toBeNull();
64
+ expect(faultOf(SRC_TESTS, { include: ['src/**/*.ts'], exclude: ['src/**'] })).not.toBeNull();
65
+ expect(faultOf(SRC_TESTS, { include: ['src/**/*.ts'], exclude: ['src/**/__tests__/**'] })).toBeNull();
66
+ });
67
+
68
+ it('ends an exclude at a segment boundary, so a prefix pattern drops the directory it matches', () => {
69
+ // TypeScript terminates an exclude at `$` or `/`: `src/foo*` removes src/foobar/ whole.
70
+ expect(faultOf(['src/foobar/a.test.ts'], { include: ['src/**/*.ts'], exclude: ['src/foo*'] })).not.toBeNull();
71
+ expect(faultOf(['src/bar/a.test.ts'], { include: ['src/**/*.ts'], exclude: ['src/foo*'] })).toBeNull();
72
+ });
73
+
74
+ it('keeps node_modules out when no exclude is declared', () => {
75
+ expect(faultOf(['node_modules/x/a.test.ts'], { include: ['**/*.ts'] })).toEqual({
76
+ kind: 'tests-unselected',
77
+ tests: ['node_modules/x/a.test.ts'],
78
+ });
79
+ });
80
+ });
81
+
82
+ describe('where a test program inherits its selection from', () => {
83
+ const base = (include: string[]): Record<string, unknown> => ({ include });
84
+
85
+ it('takes an include from the base it extends, and exclude from the same chain', () => {
86
+ const configs: Configs = {
87
+ 'packages/app/tsconfig.json': { include: ['src/**/*.ts'], exclude: ['src/**/*.test.ts'] },
88
+ };
89
+ expect(faultOf(SRC_TESTS, { extends: './tsconfig.json' }, configs)).toEqual({
90
+ kind: 'tests-unselected',
91
+ tests: SRC_TESTS,
92
+ });
93
+ });
94
+
95
+ it('lets a later base win over an earlier one', () => {
96
+ const configs: Configs = { 'packages/app/a.json': base(['lib/**/*']), 'packages/app/b.json': base(['src/**/*']) };
97
+
98
+ expect(faultOf(SRC_TESTS, { extends: ['./a.json', './b.json'] }, configs)).toBeNull();
99
+ expect(faultOf(SRC_TESTS, { extends: ['./b.json', './a.json'] }, configs)).not.toBeNull();
100
+ });
101
+
102
+ it('lets the file win over every base', () => {
103
+ const configs: Configs = { 'packages/app/a.json': base(['lib/**/*']) };
104
+
105
+ expect(faultOf(SRC_TESTS, { extends: './a.json', include: ['src/**/*'] }, configs)).toBeNull();
106
+ });
107
+
108
+ it('writes an inherited pattern against the config that declared it', () => {
109
+ const configs: Configs = { 'packages/app/base/tsconfig.json': base(['../src/**/*.ts']) };
110
+
111
+ expect(faultOf(SRC_TESTS, { extends: './base/tsconfig.json' }, configs)).toBeNull();
112
+ });
113
+
114
+ it('selects nothing for an include ending in a bare **, which tsc itself rejects (TS5010)', () => {
115
+ expect(faultOf(SRC_TESTS, { include: ['src/**'] })).toEqual({ kind: 'tests-unselected', tests: SRC_TESTS });
116
+ });
117
+
118
+ it('never selects a hidden file with a wildcard, as TypeScript does not', () => {
119
+ expect(faultOf(['src/.a.test.ts'], { include: ['src/**/*.test.ts'] })).toEqual({
120
+ kind: 'tests-unselected',
121
+ tests: ['src/.a.test.ts'],
122
+ });
123
+ });
124
+
125
+ it('lets an exclude remove hidden paths, though an include never selects them with a wildcard', () => {
126
+ const hidden = ['src/.hid/a.test.ts'];
127
+
128
+ expect(faultOf(hidden, { include: ['src/.hid/*.test.ts'] })).toBeNull();
129
+ expect(faultOf(hidden, { include: ['src/.hid/*.test.ts'], exclude: ['src/*'] })).toEqual({
130
+ kind: 'tests-unselected',
131
+ tests: hidden,
132
+ });
133
+ });
134
+
135
+ it('counts only what the tool treats as a test: a fixture is not one', () => {
136
+ expect(faultOf(['fixtures/a.test.ts', 'src/index.ts'], { include: ['src/**/*.test.ts'] })).toEqual({
137
+ kind: 'no-tests',
138
+ });
139
+ });
140
+
141
+ it('writes the configDir template against the config being resolved, wherever it was declared', () => {
142
+ const configs: Configs = { 'packages/app/base/tsconfig.json': base(['${configDir}/src']) };
143
+
144
+ expect(faultOf(SRC_TESTS, { extends: './base/tsconfig.json' }, configs)).toBeNull();
145
+ });
146
+
147
+ it('cannot say when a base is a package, or missing', () => {
148
+ expect(faultOf([], { extends: '@acme/tsconfig/bun.json' })).toBeNull();
149
+ expect(faultOf([], { extends: './missing.json' })).toBeNull();
150
+ });
151
+
152
+ it('cannot say for a pattern outside the project, an absolute one, or one for files it does not list', () => {
153
+ expect(faultOf([], { include: ['../other/**/*.ts'] })).toBeNull();
154
+ expect(faultOf([], { include: ['/abs/src/**/*.ts'] })).toBeNull();
155
+ expect(faultOf(SRC_TESTS, { include: ['src/**/*.test.js'] })).toBeNull();
156
+ });
157
+ });
158
+
159
+ describe('where a test program looks for tests', () => {
160
+ const canonical = (files: string[], isNew = true) => includeGlobsFor(program(files), {}, isNew);
161
+
162
+ it('is src, with the tracer, for a project whose tests live there', () => {
163
+ expect(canonical(SRC_TESTS)).toEqual([
164
+ 'src/**/*.test.ts',
165
+ 'src/**/*.spec.ts',
166
+ 'src/**/__tests__/**/*.ts',
167
+ 'src/**/__tests__/**/*.tsx',
168
+ 'src/test-suite-tracer.ts',
169
+ ]);
170
+ });
171
+
172
+ it('is src for a project with no tests yet', () => {
173
+ expect(canonical(['src/index.ts'])).toContain('src/**/*.test.ts');
174
+ });
175
+
176
+ it('adds the .tsx globs only where .tsx tests exist, so a project without them is unchanged', () => {
177
+ expect(canonical(['src/App.test.tsx'])).toEqual([
178
+ 'src/**/*.test.ts',
179
+ 'src/**/*.spec.ts',
180
+ 'src/**/*.test.tsx',
181
+ 'src/**/*.spec.tsx',
182
+ 'src/**/__tests__/**/*.ts',
183
+ 'src/**/__tests__/**/*.tsx',
184
+ 'src/test-suite-tracer.ts',
185
+ ]);
186
+ const selected = program(['src/App.test.tsx']);
187
+ expect(testProgramFault(selected, { include: canonical(['src/App.test.tsx']) })).toBeNull();
188
+ });
189
+
190
+ it('is the directory itself for a top-level __tests__, not a second __tests__ beneath it', () => {
191
+ const files = ['__tests__/a.ts'];
192
+
193
+ expect(canonical(files)).toEqual(['__tests__/**/*.ts', '__tests__/**/*.tsx']);
194
+ expect(testProgramFault(program(files), { include: canonical(files) })).toBeNull();
195
+ });
196
+
197
+ it('is the project root for a test beside the manifest', () => {
198
+ expect(canonical(['a.test.ts'])).toEqual(['*.test.ts', '*.spec.ts']);
199
+ });
200
+
201
+ it('is every directory a src-less project keeps tests in, and never its fixtures', () => {
202
+ expect(canonical(['direnv/a.test.ts', 'scripts/b.spec.ts', 'fixtures/c.test.ts'])).toEqual([
203
+ 'direnv/**/*.test.ts',
204
+ 'direnv/**/*.spec.ts',
205
+ 'direnv/**/__tests__/**/*.ts',
206
+ 'direnv/**/__tests__/**/*.tsx',
207
+ 'scripts/**/*.test.ts',
208
+ 'scripts/**/*.spec.ts',
209
+ 'scripts/**/__tests__/**/*.ts',
210
+ 'scripts/**/__tests__/**/*.tsx',
211
+ ]);
212
+ });
213
+
214
+ it('stays on src when tests also live elsewhere: those are strays, not a reason to widen', () => {
215
+ expect(canonical(['src/a.test.ts', 'scripts/b.test.ts'])).not.toContain('scripts/**/*.test.ts');
216
+ });
217
+
218
+ it('leaves a declared include alone for tests it already selects', () => {
219
+ const files = ['direnv/a.test.ts'];
220
+
221
+ expect(includeGlobsFor(program(files), { include: ['direnv/**/*'] }, false)).toEqual([]);
222
+ expect(includeGlobsFor(program(files), { include: ['lib/**/*'] }, false)).toContain('direnv/**/*.test.ts');
223
+ });
224
+ });
225
+
226
+ /** An in-memory project listing: what `listTypeScriptSources` walks, built from the file paths it should contain. */
227
+ function listing(files: string[]): ProjectTree {
228
+ const paths = new Set(files);
229
+ const children = new Map<string, Set<string>>();
230
+ for (const file of files) {
231
+ const parts = file.split('/');
232
+ for (let depth = 1; depth <= parts.length; depth++) {
233
+ const parent = parts.slice(0, depth - 1).join('/');
234
+ children.set(parent, (children.get(parent) ?? new Set()).add(parts[depth - 1] ?? ''));
235
+ }
236
+ }
237
+ return { children: (directory) => [...(children.get(directory) ?? [])], isFile: (path) => paths.has(path) };
238
+ }
239
+
240
+ describe('which sources belong to a project', () => {
241
+ const sources = (files: string[]) => listTypeScriptSources(listing(files), 'packages/app');
242
+
243
+ it('lists the TypeScript the project holds, project-relative and sorted', () => {
244
+ expect(
245
+ sources([
246
+ 'packages/app/src/b.test.ts',
247
+ 'packages/app/src/a.ts',
248
+ 'packages/app/README.md',
249
+ 'packages/app/src/view.tsx',
250
+ ]),
251
+ ).toEqual(['src/a.ts', 'src/b.test.ts', 'src/view.tsx']);
252
+ });
253
+
254
+ it('leaves out a nested project: its files are its own, and its own policy pass covers them', () => {
255
+ expect(
256
+ sources([
257
+ 'packages/app/src/a.test.ts',
258
+ 'packages/app/oracle-gates/project.json',
259
+ 'packages/app/oracle-gates/src/gate.test.ts',
260
+ 'packages/app/vendored/package.json',
261
+ 'packages/app/vendored/x.test.ts',
262
+ ]),
263
+ ).toEqual(['src/a.test.ts']);
264
+ });
265
+
266
+ it('keeps a directory whose only config is a tsconfig: that is a routing file, not a program anyone runs', () => {
267
+ // In a consumer monorepo, a package's tests/, fleet-it/ and nx-it/ each carry a tsconfig.json that extends ../tsconfig.test.json
268
+ // so ttsc routes their files; nothing typechecks them but the parent's tsconfig.test.json.
269
+ expect(
270
+ sources([
271
+ 'packages/app/src/tsconfig.lib.json',
272
+ 'packages/app/src/a.test.ts',
273
+ 'packages/app/fleet-it/tsconfig.json',
274
+ 'packages/app/fleet-it/plan.test.ts',
275
+ 'packages/app/tests/tsconfig.test.json',
276
+ 'packages/app/tests/b.test.ts',
277
+ 'packages/app/deep/inner/tsconfig.lib.json',
278
+ 'packages/app/deep/inner/x.test.ts',
279
+ ]),
280
+ ).toEqual(['deep/inner/x.test.ts', 'fleet-it/plan.test.ts', 'src/a.test.ts', 'tests/b.test.ts']);
281
+ });
282
+
283
+ it('counts the tests under a routing tsconfig, so a program that selects none of them is a fault', () => {
284
+ const files = sources(['packages/app/tests/tsconfig.json', 'packages/app/tests/a.test.ts']);
285
+ const app = { projectRoot: 'packages/app', files, readConfig: () => null };
286
+
287
+ expect(testProgramFault(app, { include: ['tests/**/*.ts'] })).toBeNull();
288
+ expect(testProgramFault(app, { include: ['src/**/*.ts'] })).toEqual({
289
+ kind: 'tests-unselected',
290
+ tests: ['tests/a.test.ts'],
291
+ });
292
+ });
293
+
294
+ it('keeps a nested directory of src that only the tests share', () => {
295
+ expect(sources(['packages/app/src/lib/b.test.ts'])).toEqual(['src/lib/b.test.ts']);
296
+ });
297
+
298
+ it('still reads the project root itself, though it has the manifest and the configs', () => {
299
+ expect(
300
+ sources(['packages/app/package.json', 'packages/app/tsconfig.json', 'packages/app/scripts/a.test.ts']),
301
+ ).toEqual(['scripts/a.test.ts']);
302
+ });
303
+ });
@@ -0,0 +1,341 @@
1
+ import { posix } from 'node:path';
2
+ import type { Tree } from 'nx/src/devkit-exports.js';
3
+ import { isNonSourceDirectory } from './source-directories.js';
4
+
5
+ /**
6
+ * Everything the test typecheck needs to know about one project's files.
7
+ *
8
+ * `tsconfig.test.json` is a program, and a program that selects none of the
9
+ * project's tests passes `tsc --noEmit` by compiling nothing: the gate is green
10
+ * while no test has ever been typechecked. So its `include` has to come from
11
+ * where the tests are, and one that selects none of them has to be reported
12
+ * rather than trusted.
13
+ */
14
+ export interface TestProgram {
15
+ /** Workspace-relative root of the project: `tooling`, `packages/app`. */
16
+ readonly projectRoot: string;
17
+ /** Project-relative TypeScript sources under the project, outside any build-output tree. */
18
+ readonly files: readonly string[];
19
+ /** A tsconfig by workspace-relative path; null when the file is absent. */
20
+ readonly readConfig: (path: string) => Record<string, unknown> | null;
21
+ }
22
+
23
+ /** A directory listing the walker can use: an Nx `Tree`, or the disk behind a small adapter. */
24
+ export type ProjectTree = Pick<Tree, 'children' | 'isFile'>;
25
+
26
+ /** Why a test program typechecks none of what it exists to typecheck. */
27
+ export type TestProgramFault =
28
+ | { readonly kind: 'no-tests' }
29
+ | { readonly kind: 'tests-unselected'; readonly tests: readonly string[] };
30
+
31
+ const TYPESCRIPT_SOURCE = /\.(?:[cm]?ts|tsx)$/;
32
+ const TEST_FILE = /\.(?:test|spec)\.tsx?$/;
33
+ const TSX_TEST_FILE = /\.(?:test|spec)\.tsx$/;
34
+ const IN_TESTS_DIRECTORY = /(?:^|\/)__tests__\/.*\.tsx?$/;
35
+ /** An include that names an extension the TypeScript-source listing does not carry cannot be answered from it. */
36
+ const NON_TYPESCRIPT_PATTERN = /\.(?:[cm]?jsx?|json)$/;
37
+ /** TypeScript 5.5's template for the directory of the config being resolved, usable in an inherited pattern. */
38
+ const CONFIG_DIR = /^\$\{configDir\}/;
39
+ /** Matches no path: what an include ending in a bare `**` selects. */
40
+ const NEVER = /(?!)/;
41
+ /** What makes a directory a project of its own: a manifest or an Nx project file. */
42
+ const PROJECT_FILE = /^(?:package|project)\.json$/;
43
+ /**
44
+ * Top-level directories whose `*.test.ts` files are data a harness compiles,
45
+ * not suites. The stray-test policy skips the same one.
46
+ */
47
+ const FIXTURE_DIRECTORY = 'fixtures';
48
+ const PACKAGE_CONVENTION_ROOT = 'src';
49
+ const TESTS_DIRECTORY = '__tests__';
50
+
51
+ /**
52
+ * The TypeScript sources of a project, project-relative and sorted.
53
+ *
54
+ * A directory below the project that is a project of its own (its own manifest or Nx project file) is not part of
55
+ * this one: its files are its own tests, found by its own policy pass, and folding them in would count them twice.
56
+ * A directory that merely carries a tsconfig is not that: in a monorepo of Bun programs such a file routes the
57
+ * directory's files to the parent's program (a `tests/tsconfig.json` extending `../tsconfig.test.json`), and
58
+ * nothing typechecks the directory but that program.
59
+ */
60
+ export function listTypeScriptSources(tree: ProjectTree, projectRoot: string): string[] {
61
+ const files: string[] = [];
62
+ const walk = (directory: string): void => {
63
+ for (const name of childrenOf(tree, directory)) {
64
+ const path = posix.join(directory, name);
65
+ if (tree.isFile(path)) {
66
+ if (TYPESCRIPT_SOURCE.test(name)) files.push(posix.relative(projectRoot, path));
67
+ } else if (!isNonSourceDirectory(name) && !isNestedProject(tree, path)) {
68
+ walk(path);
69
+ }
70
+ }
71
+ };
72
+ walk(projectRoot);
73
+ return files.sort();
74
+ }
75
+
76
+ /** A directory with its own manifest or Nx project file is a project, and its files are that project's. */
77
+ function isNestedProject(tree: ProjectTree, directory: string): boolean {
78
+ return childrenOf(tree, directory).some(
79
+ (name) => PROJECT_FILE.test(name) && tree.isFile(posix.join(directory, name)),
80
+ );
81
+ }
82
+
83
+ /** A dangling link (a collected build out-link) is neither a file nor a directory; anything else is a real fault. */
84
+ function childrenOf(tree: ProjectTree, directory: string): string[] {
85
+ try {
86
+ return tree.children(directory);
87
+ } catch (error) {
88
+ // ELOOP is a link cycle: there is nothing beneath it to list.
89
+ if (
90
+ error instanceof Error &&
91
+ 'code' in error &&
92
+ (error.code === 'ENOENT' || error.code === 'ENOTDIR' || error.code === 'ELOOP')
93
+ ) {
94
+ return [];
95
+ }
96
+ throw error;
97
+ }
98
+ }
99
+
100
+ function isTestSource(file: string): boolean {
101
+ return TEST_FILE.test(file) || IN_TESTS_DIRECTORY.test(file);
102
+ }
103
+
104
+ /**
105
+ * The top-level directory a test file belongs to (`.` for a file beside the
106
+ * manifest), or null for a file that is not a test.
107
+ */
108
+ function testRootOf(file: string): string | null {
109
+ if (!isTestSource(file)) return null;
110
+ const [first, second] = file.split('/');
111
+ if (first === FIXTURE_DIRECTORY && second !== undefined) return null;
112
+ return second === undefined ? '.' : (first ?? '.');
113
+ }
114
+
115
+ /**
116
+ * The directories the test program is about. A package whose tests live in
117
+ * `src/` is on the package convention, and stays on it: a test anywhere else is
118
+ * a stray the stray-test policy reports, and widening the program here would
119
+ * silently bless it. A project with no `src/` tests (the repository's `tooling`
120
+ * keeps them beside its shell scripts) is on no convention, so the directories
121
+ * its tests are in are the answer. A project with no tests yet gets the
122
+ * convention, whose program then selects nothing and is reported.
123
+ */
124
+ function programRoots(files: readonly string[]): string[] {
125
+ const roots = [...new Set(files.map(testRootOf).filter((root): root is string => root !== null))].sort();
126
+ if (roots.includes(PACKAGE_CONVENTION_ROOT)) return [PACKAGE_CONVENTION_ROOT];
127
+ return roots.length > 0 ? roots : [PACKAGE_CONVENTION_ROOT];
128
+ }
129
+
130
+ /** The canonical globs for a test directory, plus the `.tsx` ones only where `.tsx` tests exist. */
131
+ function includeGlobs(roots: readonly string[], files: readonly string[]): string[] {
132
+ return roots.flatMap((root) => {
133
+ if (root === TESTS_DIRECTORY) return ['__tests__/**/*.ts', '__tests__/**/*.tsx'];
134
+ const prefix = root === '.' ? '' : `${root}/**/`;
135
+ const hasTsxTests = files.some((file) => testRootOf(file) === root && TSX_TEST_FILE.test(file));
136
+ return [
137
+ `${prefix}*.test.ts`,
138
+ `${prefix}*.spec.ts`,
139
+ ...(hasTsxTests ? [`${prefix}*.test.tsx`, `${prefix}*.spec.tsx`] : []),
140
+ ...(root === '.' ? [] : [`${root}/**/__tests__/**/*.ts`, `${root}/**/__tests__/**/*.tsx`]),
141
+ ...(root === PACKAGE_CONVENTION_ROOT ? ['src/test-suite-tracer.ts'] : []),
142
+ ];
143
+ });
144
+ }
145
+
146
+ /**
147
+ * The globs a test program's `include` must carry, given the `include` it has.
148
+ *
149
+ * A new file, or a package on the `src/` convention, gets the canonical globs
150
+ * (merging them is a no-op when they are there). A project off the convention
151
+ * that wrote an `include` of its own gets a directory's globs only for the
152
+ * directories whose tests that `include` does not already select, so a
153
+ * hand-written include that already names `direnv`'s tests is never followed by four redundant entries.
154
+ */
155
+ export function includeGlobsFor(program: TestProgram, config: Record<string, unknown>, isNew: boolean): string[] {
156
+ const roots = programRoots(program.files);
157
+ if (isNew || roots.includes(PACKAGE_CONVENTION_ROOT)) return includeGlobs(roots, program.files);
158
+ const selects = fileSelector(program, config);
159
+ if (selects === null) return includeGlobs(roots, program.files);
160
+ const uncovered = roots.filter((root) =>
161
+ program.files.some((file) => testRootOf(file) === root && !selects(posix.join(program.projectRoot, file))),
162
+ );
163
+ return includeGlobs(uncovered, program.files);
164
+ }
165
+
166
+ /**
167
+ * How, if at all, this program typechecks none of what it exists to typecheck:
168
+ * none of the project's tests, or, for a project with no tests at all, no file.
169
+ * Unknowable is not a fault.
170
+ */
171
+ export function testProgramFault(program: TestProgram, config: Record<string, unknown>): TestProgramFault | null {
172
+ const selects = fileSelector(program, config);
173
+ if (selects === null) return null;
174
+ const selected = (file: string): boolean => selects(posix.join(program.projectRoot, file));
175
+ const tests = program.files.filter((file) => testRootOf(file) !== null);
176
+ if (tests.length === 0) return program.files.some(selected) ? null : { kind: 'no-tests' };
177
+ return tests.some(selected) ? null : { kind: 'tests-unselected', tests };
178
+ }
179
+
180
+ interface Inherited {
181
+ readonly values: readonly string[];
182
+ /** The directory the values' relative patterns are written against: the config that declared them. */
183
+ readonly directory: string;
184
+ }
185
+
186
+ /**
187
+ * A tsconfig list option as TypeScript resolves it: the file's own, else the
188
+ * last base in its `extends` that declares one. 'none' when no config in the
189
+ * chain does; 'unknown' when the chain leaves what the plugin can read (a
190
+ * package base such as `@tsconfig/node`).
191
+ */
192
+ function inheritedList(
193
+ program: TestProgram,
194
+ configPath: string,
195
+ config: Record<string, unknown>,
196
+ field: 'files' | 'include' | 'exclude',
197
+ ): Inherited | 'none' | 'unknown' {
198
+ const own = config[field];
199
+ if (Array.isArray(own)) {
200
+ return {
201
+ values: own.filter((entry): entry is string => typeof entry === 'string'),
202
+ directory: posix.dirname(configPath),
203
+ };
204
+ }
205
+ const extended = config.extends;
206
+ const bases = typeof extended === 'string' ? [extended] : Array.isArray(extended) ? extended : [];
207
+ for (let index = bases.length - 1; index >= 0; index--) {
208
+ const base: unknown = bases[index];
209
+ if (typeof base !== 'string' || !base.startsWith('.')) return 'unknown';
210
+ const basePath = posix.join(posix.dirname(configPath), base.endsWith('.json') ? base : `${base}.json`);
211
+ const baseConfig = program.readConfig(basePath);
212
+ if (baseConfig === null) return 'unknown';
213
+ const inherited = inheritedList(program, basePath, baseConfig, field);
214
+ if (inherited !== 'none') return inherited;
215
+ }
216
+ return 'none';
217
+ }
218
+
219
+ /**
220
+ * A pattern as a workspace-relative path: against the config that declared it,
221
+ * or, for `${configDir}`, against the config being resolved. Null for one the
222
+ * workspace-relative form cannot express (an absolute path).
223
+ */
224
+ function resolvePattern(value: string, declaredIn: string, leafDirectory: string): string | null {
225
+ if (value.startsWith('/')) return null;
226
+ if (CONFIG_DIR.test(value)) return posix.join(leafDirectory, value.replace(CONFIG_DIR, ''));
227
+ return posix.join(declaredIn, value);
228
+ }
229
+
230
+ /**
231
+ * Whether TypeScript would put a workspace-relative file in this program, or
232
+ * null when the answer depends on configuration outside the tree, a pattern
233
+ * outside the project, or files the TypeScript listing does not carry. Follows
234
+ * `files`, `include` and `exclude` through `extends`, each pattern relative to
235
+ * the config that wrote it, and TypeScript's two defaults: no `include` and no
236
+ * `files` means every file, and no `exclude` means `node_modules` and its kin.
237
+ */
238
+ function fileSelector(program: TestProgram, config: Record<string, unknown>): ((path: string) => boolean) | null {
239
+ const configPath = posix.join(program.projectRoot, 'tsconfig.test.json');
240
+ const leaf = posix.dirname(configPath);
241
+ const files = inheritedList(program, configPath, config, 'files');
242
+ const include = inheritedList(program, configPath, config, 'include');
243
+ const exclude = inheritedList(program, configPath, config, 'exclude');
244
+ if (files === 'unknown' || include === 'unknown' || exclude === 'unknown') return null;
245
+
246
+ const resolved = (list: Inherited): (string | null)[] =>
247
+ list.values.map((value) => resolvePattern(value, list.directory, leaf));
248
+ const fileList = files === 'none' ? [] : resolved(files);
249
+ const includeList = include !== 'none' ? resolved(include) : files === 'none' ? [posix.join(leaf, '**/*')] : [];
250
+ const excludeList =
251
+ exclude === 'none'
252
+ ? ['node_modules', 'bower_components', 'jspm_packages'].map((name) => posix.join(leaf, '**', name))
253
+ : resolved(exclude);
254
+ if ([...fileList, ...includeList, ...excludeList].some((pattern) => pattern === null)) return null;
255
+ const known = (list: (string | null)[]): string[] => list.filter((pattern): pattern is string => pattern !== null);
256
+ const includePatterns = known(includeList);
257
+ if (
258
+ includePatterns.some(
259
+ (pattern) => !reachesInto(pattern, program.projectRoot) || NON_TYPESCRIPT_PATTERN.test(pattern),
260
+ )
261
+ ) {
262
+ return null;
263
+ }
264
+
265
+ const included = includePatterns.map(includeRegExp);
266
+ const excluded = known(excludeList).map(excludeRegExp);
267
+ const named = known(fileList);
268
+ return (path) =>
269
+ named.includes(path) || (included.some((it) => it.test(path)) && !excluded.some((it) => it.test(path)));
270
+ }
271
+
272
+ /** Whether a pattern's fixed leading directories are inside, or contain, the project: else the project's files cannot answer for it. */
273
+ function reachesInto(pattern: string, projectRoot: string): boolean {
274
+ const fixed: string[] = [];
275
+ for (const segment of pattern.split('/')) {
276
+ if (/[*?]/.test(segment)) break;
277
+ fixed.push(segment);
278
+ }
279
+ const prefix = fixed.join('/');
280
+ return (
281
+ prefix === '' ||
282
+ prefix === projectRoot ||
283
+ prefix.startsWith(`${projectRoot}/`) ||
284
+ projectRoot.startsWith(`${prefix}/`)
285
+ );
286
+ }
287
+
288
+ /**
289
+ * The regular-expression source for a run of pattern segments: `*` within a
290
+ * segment, `?` one character, `**` any number of directories. In an `include`,
291
+ * as in TypeScript, no wildcard matches a leading `.`, so hidden files and
292
+ * directories are never selected by one; an `exclude` has no such rule and
293
+ * removes them like anything else.
294
+ */
295
+ function segmentsSource(segments: readonly string[], skipsHidden: boolean): string {
296
+ const hidden = skipsHidden ? '(?!\\.)' : '';
297
+ let source = '';
298
+ segments.forEach((segment, index) => {
299
+ if (segment === '**') {
300
+ source += `(?:${hidden}[^/]+/)*`;
301
+ return;
302
+ }
303
+ if (segment.startsWith('*') || segment.startsWith('?')) source += hidden;
304
+ source += segment
305
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&')
306
+ .replaceAll('*', '[^/]*')
307
+ .replaceAll('?', '[^/]');
308
+ if (index < segments.length - 1) source += '/';
309
+ });
310
+ return source;
311
+ }
312
+
313
+ function patternSegments(pattern: string): string[] {
314
+ return pattern.split('/').filter((segment) => segment !== '' && segment !== '.');
315
+ }
316
+
317
+ /**
318
+ * An `include` pattern as a regular expression over workspace-relative paths. A
319
+ * last segment with no wildcard and no extension names a directory. A trailing
320
+ * `**` is an error to TypeScript (TS5010) that selects nothing, so it selects
321
+ * nothing here: reading it as "everything beneath" would bless a config that
322
+ * `tsc` itself rejects.
323
+ */
324
+ function includeRegExp(pattern: string): RegExp {
325
+ const segments = patternSegments(pattern);
326
+ const last = segments.at(-1);
327
+ if (last === '**') return NEVER;
328
+ if (last !== undefined && !/[*?.]/.test(last)) segments.push('**', '*');
329
+ return new RegExp(`^${segmentsSource(segments, true)}$`);
330
+ }
331
+
332
+ /**
333
+ * An `exclude` pattern: it removes what it matches and everything beneath a
334
+ * match, so `src/foo*` drops `src/foobar/a.test.ts` (TypeScript ends the
335
+ * pattern at `$` or `/`, not at the end of the path).
336
+ */
337
+ function excludeRegExp(pattern: string): RegExp {
338
+ const segments = patternSegments(pattern);
339
+ if (segments.at(-1) === '**') segments.pop();
340
+ return new RegExp(`^${segmentsSource(segments, false)}(?:$|/)`);
341
+ }