@adamhl8/configs 3.2.1 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/dist/configs/gitignore.base +1 -0
- package/dist/test-utils/index.d.ts +2 -1
- package/dist/test-utils/index.js +2 -1
- package/dist/test-utils/subprocess-coverage/child-preload.d.ts +1 -0
- package/dist/test-utils/subprocess-coverage/child-preload.js +60 -0
- package/dist/test-utils/subprocess-coverage/child-preload.js.map +1 -0
- package/dist/test-utils/subprocess-coverage/env.d.ts +7 -0
- package/dist/test-utils/subprocess-coverage/env.d.ts.map +1 -0
- package/dist/test-utils/subprocess-coverage/env.js +19 -0
- package/dist/test-utils/subprocess-coverage/env.js.map +1 -0
- package/dist/test-utils/subprocess-coverage/preload.d.ts +1 -0
- package/dist/test-utils/subprocess-coverage/preload.js +32 -0
- package/dist/test-utils/subprocess-coverage/preload.js.map +1 -0
- package/dist/test-utils/subprocess-coverage/report.js +26 -0
- package/dist/test-utils/subprocess-coverage/report.js.map +1 -0
- package/package.json +25 -1
package/README.md
CHANGED
|
@@ -61,6 +61,7 @@ Some tools are only needed by some projects:
|
|
|
61
61
|
- Published packages: `tsdown publint @arethetypeswrong/core`
|
|
62
62
|
- Releases: `release-it release-it-git-cliff` (see [release-it](#release-it))
|
|
63
63
|
- Astro projects that use Tailwind: `tailwint @tailwindcss/language-server` (see [bask tasks](#bask-tasks))
|
|
64
|
+
- Subprocess test coverage: `@vitest/istanbul-lib-coverage @vitest/istanbul-lib-instrument @vitest/istanbul-lib-report @vitest/istanbul-lib-source-maps` (see [Subprocess coverage](#subprocess-coverage))
|
|
64
65
|
|
|
65
66
|
## Setup
|
|
66
67
|
|
|
@@ -397,6 +398,7 @@ const { exitCode: failedCode } = await run`my-cli --bad-flag`.nothrow()
|
|
|
397
398
|
- `packPackage(root)` runs `bun tsdown` when `root` has a `tsdown.config.ts`, then packs it with `bun pm pack --ignore-scripts` into a temp dir, and returns the tarball path.
|
|
398
399
|
- `minimalEnv(extra)` returns just `PATH` and `HOME`, plus `extra`.
|
|
399
400
|
- `makeTempDir(prefix)` makes a temp dir that's removed after the test run. `copyFixture` and `packPackage` use it.
|
|
401
|
+
- `subprocessCoverageEnv()` returns the variables that turn on [subprocess coverage](#subprocess-coverage) for a shell's `env`, or nothing when it's off.
|
|
400
402
|
|
|
401
403
|
The temp dirs are removed by a preload, which each project adds to its `bunfig.toml`:
|
|
402
404
|
|
|
@@ -413,6 +415,38 @@ import "@adamhl8/configs/test/preload"
|
|
|
413
415
|
|
|
414
416
|
It isn't in the shared bunfig base because `adamhl8-bunfig` merges arrays by index, so a project's own `preload` would replace it.
|
|
415
417
|
|
|
418
|
+
#### Subprocess coverage
|
|
419
|
+
|
|
420
|
+
Bun's coverage only measures the test process, so it misses a package's own code when tests run it from the packed tarball in child processes. A second preload covers that code, imported after the first at the top of the project's own preload:
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
import "@adamhl8/configs/test/preload"
|
|
424
|
+
import "@adamhl8/configs/test/subprocess-coverage"
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
A project without its own preload can list both in `bunfig.toml` instead:
|
|
428
|
+
|
|
429
|
+
```toml
|
|
430
|
+
[test]
|
|
431
|
+
preload = ["@adamhl8/configs/test/preload", "@adamhl8/configs/test/subprocess-coverage"]
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
- It sets `BUN_OPTIONS` and a settings variable on `process.env`. Every Bun process started with them loads a preload that instruments the installed package's `dist/` with Istanbul (the package named in the project's `package.json`).
|
|
435
|
+
- A shell that inherits `process.env` passes the variables on as is. A shell that replaces it, like one built on `minimalEnv()`, adds them with `subprocessCoverageEnv()`:
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
import { copyFixture, minimalEnv, subprocessCoverageEnv } from "@adamhl8/configs/test"
|
|
439
|
+
|
|
440
|
+
const { run } = await copyFixture(FIXTURE, { shell: { env: minimalEnv(subprocessCoverageEnv()) } })
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
- After the run, the coverage from every process is merged, mapped back to `src/` through the package's source maps, printed as a table (statements, branches, functions, lines), and written to `coverage/lcov.info`.
|
|
444
|
+
- Set `coverage = false` under `[test]` in `bunfig.toml`, because Bun's own coverage only sees the test process.
|
|
445
|
+
- The project needs the optional peer dependencies listed under [Installation](#installation) and a build with source maps, which `tsdownConfig` produces. Tests have to install the packed tarball, as with `packPackage`, because only the installed `dist/` is instrumented.
|
|
446
|
+
- Code in the package's own `exit` handlers isn't counted, and a process killed by a signal writes no coverage.
|
|
447
|
+
|
|
448
|
+
Import it only from a preload. Imported from a test file, its `afterAll` would be scoped to that file, so the report would run after that file instead of after the whole run.
|
|
449
|
+
|
|
416
450
|
## GitHub Actions
|
|
417
451
|
|
|
418
452
|
This repo hosts three reusable workflows you can call from other projects instead of copy/pasting them. The calling project must use `bun`, have a `bask.ts` that re-exports the base tasks (see [bask tasks](#bask-tasks)), and have a commitlint config (e.g. via these configs). The workflows set up bun, actionlint, and OpenTofu themselves.
|
|
@@ -574,3 +608,5 @@ The `prepare` task runs `tofu init -upgrade`, and the `tofu-check` task (which `
|
|
|
574
608
|
## Developing this repo
|
|
575
609
|
|
|
576
610
|
bun links a dependency's `package.json` bin executables into the local `node_modules/.bin`, but not the bins of the package being developed itself. This repo depends on itself (`"@adamhl8/configs": "file:."`) so that its own bins can be run directly, e.g. `adamhl8-bunfig` instead of `src/adamhl8-bunfig/index.ts`. That's also why this repo's `bask.ts` runs `tsdown` before the `prepare` task: the bins point at `dist/`, so those builds need to be available.
|
|
611
|
+
|
|
612
|
+
With bun's `isolated` linker, the self-dependency is a copy made at install time. After adding or moving an export, run `bun install --force` to refresh the copy, or type checks of code that imports the new export fail.
|
|
@@ -3,5 +3,6 @@ import { Shell, ShellEnv, ShellOptions, ShellResult, createShell } from "./shell
|
|
|
3
3
|
import { minimalEnv } from "./shell/env.js";
|
|
4
4
|
import { CopyFixtureOptions, copyFixture } from "./fixture.js";
|
|
5
5
|
import { packPackage } from "./pack.js";
|
|
6
|
+
import { subprocessCoverageEnv } from "./subprocess-coverage/env.js";
|
|
6
7
|
import { makeTempDir } from "./temp-dir.js";
|
|
7
|
-
export { type CopyFixtureOptions, type Shell, type ShellCommand, type ShellEnv, type ShellOptions, type ShellResult, copyFixture, createShell, makeTempDir, minimalEnv, packPackage };
|
|
8
|
+
export { type CopyFixtureOptions, type Shell, type ShellCommand, type ShellEnv, type ShellOptions, type ShellResult, copyFixture, createShell, makeTempDir, minimalEnv, packPackage, subprocessCoverageEnv };
|
package/dist/test-utils/index.js
CHANGED
|
@@ -3,4 +3,5 @@ import { createShell } from "./shell/shell.js";
|
|
|
3
3
|
import { makeTempDir } from "./temp-dir.js";
|
|
4
4
|
import { copyFixture } from "./fixture.js";
|
|
5
5
|
import { packPackage } from "./pack.js";
|
|
6
|
-
|
|
6
|
+
import { subprocessCoverageEnv } from "./subprocess-coverage/env.js";
|
|
7
|
+
export { copyFixture, createShell, makeTempDir, minimalEnv, packPackage, subprocessCoverageEnv };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SUBPROCESS_COVERAGE_SETTINGS_VAR } from "./env.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import bun from "bun";
|
|
4
|
+
import crypto from "node:crypto";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import { createInstrumenter } from "@vitest/istanbul-lib-instrument";
|
|
7
|
+
// src/test-utils/subprocess-coverage/child-preload.ts
|
|
8
|
+
const INSTRUMENTER_COVERAGE_VARIABLE = "subprocessCoverage";
|
|
9
|
+
const isCoverageSettings = (value) => typeof value === "object" && value !== null && "rawDir" in value && typeof value.rawDir === "string" && "root" in value && typeof value.root === "string" && "packageName" in value && typeof value.packageName === "string";
|
|
10
|
+
/** Reads the coverage settings from the environment, and throws when they're missing or invalid. */
|
|
11
|
+
const readCoverageSettings = () => {
|
|
12
|
+
const settings = process.env[SUBPROCESS_COVERAGE_SETTINGS_VAR];
|
|
13
|
+
if (settings === void 0) throw new Error(`the subprocess coverage preload loaded without ${SUBPROCESS_COVERAGE_SETTINGS_VAR}`);
|
|
14
|
+
const parsed = JSON.parse(settings);
|
|
15
|
+
if (!isCoverageSettings(parsed)) throw new Error(`${SUBPROCESS_COVERAGE_SETTINGS_VAR} isn't valid: ${settings}`);
|
|
16
|
+
return parsed;
|
|
17
|
+
};
|
|
18
|
+
const { rawDir, root, packageName } = readCoverageSettings();
|
|
19
|
+
const isSourceMap = (value) => typeof value === "object" && value !== null && "sources" in value && Array.isArray(value.sources);
|
|
20
|
+
/** Points each source in the installed package's map at the same file in the project, so reports name `src/`. */
|
|
21
|
+
const readSourceMap = async (file, packageDir, projectDir) => {
|
|
22
|
+
const sourceMap = await bun.file(`${file}.map`).json();
|
|
23
|
+
if (!isSourceMap(sourceMap)) throw new Error(`no source map for ${file}`);
|
|
24
|
+
const sources = sourceMap.sources.map((source) => {
|
|
25
|
+
const installedSource = path.resolve(path.dirname(file), source ?? "");
|
|
26
|
+
return path.join(projectDir, path.relative(packageDir, installedSource));
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
...sourceMap,
|
|
30
|
+
sources
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const instrumenter = createInstrumenter({
|
|
34
|
+
esModules: true,
|
|
35
|
+
coverageVariable: INSTRUMENTER_COVERAGE_VARIABLE
|
|
36
|
+
});
|
|
37
|
+
const installedDist = new RegExp(String.raw`^(.*/node_modules/${RegExp.escape(packageName)})/dist/.*\.js$`, "v");
|
|
38
|
+
bun.plugin({
|
|
39
|
+
name: "subprocess-coverage",
|
|
40
|
+
setup: (build) => {
|
|
41
|
+
build.onLoad({ filter: installedDist }, async ({ path: file }) => {
|
|
42
|
+
const [, packageDir = ""] = installedDist.exec(file) ?? [];
|
|
43
|
+
const code = await bun.file(file).text();
|
|
44
|
+
const sourceMap = await readSourceMap(file, packageDir, root);
|
|
45
|
+
return {
|
|
46
|
+
contents: instrumenter.instrumentSync(code, file, sourceMap),
|
|
47
|
+
loader: "js"
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const hasCoverage = (value) => INSTRUMENTER_COVERAGE_VARIABLE in value;
|
|
53
|
+
process.once("exit", () => {
|
|
54
|
+
const globalScope = globalThis;
|
|
55
|
+
if (!hasCoverage(globalScope)) return;
|
|
56
|
+
fs.writeFileSync(path.join(rawDir, `${crypto.randomUUID()}.json`), JSON.stringify(globalScope[INSTRUMENTER_COVERAGE_VARIABLE]));
|
|
57
|
+
});
|
|
58
|
+
export {};
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=child-preload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"child-preload.js","names":[],"sources":["../../../src/test-utils/subprocess-coverage/child-preload.ts"],"sourcesContent":["// Records coverage of the installed package in every Bun process that a test starts.\n// Tests load this file through `BUN_OPTIONS` once a project imports `subprocess-coverage`.\nimport crypto from \"node:crypto\"\nimport fs from \"node:fs\"\nimport path from \"node:path\"\n\nimport { createInstrumenter } from \"@vitest/istanbul-lib-instrument\"\nimport type { InputSourceMap } from \"@vitest/istanbul-lib-instrument\"\nimport bun from \"bun\"\n\nimport { SUBPROCESS_COVERAGE_SETTINGS_VAR } from \"#test-utils/subprocess-coverage/env.ts\"\nimport type { CoverageSettings } from \"#test-utils/subprocess-coverage/env.ts\"\n\nconst INSTRUMENTER_COVERAGE_VARIABLE = \"subprocessCoverage\"\n\nconst isCoverageSettings = (value: unknown): value is CoverageSettings =>\n typeof value === \"object\"\n && value !== null\n && \"rawDir\" in value\n && typeof value.rawDir === \"string\"\n && \"root\" in value\n && typeof value.root === \"string\"\n && \"packageName\" in value\n && typeof value.packageName === \"string\"\n\n/** Reads the coverage settings from the environment, and throws when they're missing or invalid. */\nconst readCoverageSettings = () => {\n const settings = process.env[SUBPROCESS_COVERAGE_SETTINGS_VAR]\n if (settings === undefined)\n throw new Error(`the subprocess coverage preload loaded without ${SUBPROCESS_COVERAGE_SETTINGS_VAR}`)\n const parsed: unknown = JSON.parse(settings)\n if (!isCoverageSettings(parsed)) throw new Error(`${SUBPROCESS_COVERAGE_SETTINGS_VAR} isn't valid: ${settings}`)\n return parsed\n}\n\nconst { rawDir, root, packageName } = readCoverageSettings()\n\nconst isSourceMap = (value: unknown): value is InputSourceMap =>\n typeof value === \"object\" && value !== null && \"sources\" in value && Array.isArray(value.sources)\n\n/** Points each source in the installed package's map at the same file in the project, so reports name `src/`. */\nconst readSourceMap = async (file: string, packageDir: string, projectDir: string) => {\n const sourceMap: unknown = await bun.file(`${file}.map`).json()\n if (!isSourceMap(sourceMap)) throw new Error(`no source map for ${file}`)\n const sources = sourceMap.sources.map((source) => {\n const installedSource = path.resolve(path.dirname(file), source ?? \"\")\n return path.join(projectDir, path.relative(packageDir, installedSource))\n })\n return { ...sourceMap, sources }\n}\n\nconst instrumenter = createInstrumenter({ esModules: true, coverageVariable: INSTRUMENTER_COVERAGE_VARIABLE })\n// Match the built files of the copy that a test project installs, and capture its package dir.\nconst installedDist = new RegExp(String.raw`^(.*/node_modules/${RegExp.escape(packageName)})/dist/.*\\.js$`, \"v\")\n\nbun.plugin({\n name: \"subprocess-coverage\",\n setup: (build) => {\n build.onLoad({ filter: installedDist }, async ({ path: file }) => {\n const [, packageDir = \"\"] = installedDist.exec(file) ?? []\n const code = await bun.file(file).text()\n const sourceMap = await readSourceMap(file, packageDir, root)\n return { contents: instrumenter.instrumentSync(code, file, sourceMap), loader: \"js\" }\n })\n },\n})\n\nconst hasCoverage = (value: object): value is Record<typeof INSTRUMENTER_COVERAGE_VARIABLE, unknown> =>\n INSTRUMENTER_COVERAGE_VARIABLE in value\n\nprocess.once(\"exit\", () => {\n // Copy `globalThis` to a local because TypeScript doesn't narrow `globalThis` itself.\n const globalScope: object = globalThis\n // Skip a process that never loaded the package, because only instrumented code creates the global.\n if (!hasCoverage(globalScope)) return\n fs.writeFileSync(\n path.join(rawDir, `${crypto.randomUUID()}.json`),\n JSON.stringify(globalScope[INSTRUMENTER_COVERAGE_VARIABLE]),\n )\n})\n"],"mappings":";;;;;;;AAaA,MAAM,iCAAiC;AAEvC,MAAM,sBAAsB,UAC1B,OAAO,UAAU,YACd,UAAU,QACV,YAAY,SACZ,OAAO,MAAM,WAAW,YACxB,UAAU,SACV,OAAO,MAAM,SAAS,YACtB,iBAAiB,SACjB,OAAO,MAAM,gBAAgB;;AAGlC,MAAM,6BAA6B;CACjC,MAAM,WAAW,QAAQ,IAAI;CAC7B,IAAI,aAAa,KAAA,GACf,MAAM,IAAI,MAAM,kDAAkD,kCAAkC;CACtG,MAAM,SAAkB,KAAK,MAAM,QAAQ;CAC3C,IAAI,CAAC,mBAAmB,MAAM,GAAG,MAAM,IAAI,MAAM,GAAG,iCAAiC,gBAAgB,UAAU;CAC/G,OAAO;AACT;AAEA,MAAM,EAAE,QAAQ,MAAM,gBAAgB,qBAAqB;AAE3D,MAAM,eAAe,UACnB,OAAO,UAAU,YAAY,UAAU,QAAQ,aAAa,SAAS,MAAM,QAAQ,MAAM,OAAO;;AAGlG,MAAM,gBAAgB,OAAO,MAAc,YAAoB,eAAuB;CACpF,MAAM,YAAqB,MAAM,IAAI,KAAK,GAAG,KAAK,KAAK,CAAC,CAAC,KAAK;CAC9D,IAAI,CAAC,YAAY,SAAS,GAAG,MAAM,IAAI,MAAM,qBAAqB,MAAM;CACxE,MAAM,UAAU,UAAU,QAAQ,KAAK,WAAW;EAChD,MAAM,kBAAkB,KAAK,QAAQ,KAAK,QAAQ,IAAI,GAAG,UAAU,EAAE;EACrE,OAAO,KAAK,KAAK,YAAY,KAAK,SAAS,YAAY,eAAe,CAAC;CACzE,CAAC;CACD,OAAO;EAAE,GAAG;EAAW;CAAQ;AACjC;AAEA,MAAM,eAAe,mBAAmB;CAAE,WAAW;CAAM,kBAAkB;AAA+B,CAAC;AAE7G,MAAM,gBAAgB,IAAI,OAAO,OAAO,GAAG,qBAAqB,OAAO,OAAO,WAAW,EAAE,iBAAiB,GAAG;AAE/G,IAAI,OAAO;CACT,MAAM;CACN,QAAQ,UAAU;EAChB,MAAM,OAAO,EAAE,QAAQ,cAAc,GAAG,OAAO,EAAE,MAAM,WAAW;GAChE,MAAM,GAAG,aAAa,MAAM,cAAc,KAAK,IAAI,KAAK,CAAC;GACzD,MAAM,OAAO,MAAM,IAAI,KAAK,IAAI,CAAC,CAAC,KAAK;GACvC,MAAM,YAAY,MAAM,cAAc,MAAM,YAAY,IAAI;GAC5D,OAAO;IAAE,UAAU,aAAa,eAAe,MAAM,MAAM,SAAS;IAAG,QAAQ;GAAK;EACtF,CAAC;CACH;AACF,CAAC;AAED,MAAM,eAAe,UACnB,kCAAkC;AAEpC,QAAQ,KAAK,cAAc;CAEzB,MAAM,cAAsB;CAE5B,IAAI,CAAC,YAAY,WAAW,GAAG;CAC/B,GAAG,cACD,KAAK,KAAK,QAAQ,GAAG,OAAO,WAAW,EAAE,MAAM,GAC/C,KAAK,UAAU,YAAY,+BAA+B,CAC5D;AACF,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ShellEnv } from "../shell/shell.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the variables that make child Bun processes record coverage, for a shell that doesn't inherit `process.env`.
|
|
4
|
+
* Returns nothing when the project doesn't import `@adamhl8/configs/test/subprocess-coverage`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const subprocessCoverageEnv: () => ShellEnv;
|
|
7
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","names":[],"sources":["../../../src/test-utils/subprocess-coverage/env.ts"],"mappings":";;;;;qBAiBa,6BAA4B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
// src/test-utils/subprocess-coverage/env.ts
|
|
3
|
+
/** Holds the JSON settings that tell a child's preload where to write coverage and what to instrument. */
|
|
4
|
+
const SUBPROCESS_COVERAGE_SETTINGS_VAR = "ADAMHL8_SUBPROCESS_COVERAGE_SETTINGS";
|
|
5
|
+
/**
|
|
6
|
+
* Returns the variables that make child Bun processes record coverage, for a shell that doesn't inherit `process.env`.
|
|
7
|
+
* Returns nothing when the project doesn't import `@adamhl8/configs/test/subprocess-coverage`.
|
|
8
|
+
*/
|
|
9
|
+
const subprocessCoverageEnv = () => {
|
|
10
|
+
const settings = process.env[SUBPROCESS_COVERAGE_SETTINGS_VAR];
|
|
11
|
+
if (settings === void 0) return {};
|
|
12
|
+
return {
|
|
13
|
+
BUN_OPTIONS: process.env["BUN_OPTIONS"],
|
|
14
|
+
[SUBPROCESS_COVERAGE_SETTINGS_VAR]: settings
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export { SUBPROCESS_COVERAGE_SETTINGS_VAR, subprocessCoverageEnv };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","names":[],"sources":["../../../src/test-utils/subprocess-coverage/env.ts"],"sourcesContent":["import process from \"node:process\"\n\nimport type { ShellEnv } from \"#test-utils/shell/shell.ts\"\n\n/** Holds the JSON settings that tell a child's preload where to write coverage and what to instrument. */\nexport const SUBPROCESS_COVERAGE_SETTINGS_VAR = \"ADAMHL8_SUBPROCESS_COVERAGE_SETTINGS\"\n\nexport interface CoverageSettings {\n rawDir: string\n root: string\n packageName: string\n}\n\n/**\n * Returns the variables that make child Bun processes record coverage, for a shell that doesn't inherit `process.env`.\n * Returns nothing when the project doesn't import `@adamhl8/configs/test/subprocess-coverage`.\n */\nexport const subprocessCoverageEnv = (): ShellEnv => {\n const settings = process.env[SUBPROCESS_COVERAGE_SETTINGS_VAR]\n if (settings === undefined) return {}\n return { BUN_OPTIONS: process.env[\"BUN_OPTIONS\"], [SUBPROCESS_COVERAGE_SETTINGS_VAR]: settings }\n}\n"],"mappings":";;;AAKA,MAAa,mCAAmC;;;;;AAYhD,MAAa,8BAAwC;CACnD,MAAM,WAAW,QAAQ,IAAI;CAC7B,IAAI,aAAa,KAAA,GAAW,OAAO,CAAC;CACpC,OAAO;EAAE,aAAa,QAAQ,IAAI;GAAiB,mCAAmC;CAAS;AACjG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SUBPROCESS_COVERAGE_SETTINGS_VAR } from "./env.js";
|
|
2
|
+
import { reportCoverage } from "./report.js";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import fs from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import bun from "bun";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import { afterAll } from "bun:test";
|
|
9
|
+
// src/test-utils/subprocess-coverage/preload.ts
|
|
10
|
+
const CHILD_PRELOAD = path.join(import.meta.dir, `child-preload${path.extname(import.meta.path)}`);
|
|
11
|
+
const hasName = (value) => typeof value === "object" && value !== null && "name" in value && typeof value.name === "string";
|
|
12
|
+
const root = process.cwd();
|
|
13
|
+
const packageJson = await bun.file(path.join(root, "package.json")).json();
|
|
14
|
+
if (!hasName(packageJson)) throw new Error(`subprocess coverage needs a package name in ${root}/package.json`);
|
|
15
|
+
const rawDir = await fs.mkdtemp(path.join(os.tmpdir(), "subprocess-coverage-"));
|
|
16
|
+
const settings = {
|
|
17
|
+
rawDir,
|
|
18
|
+
root,
|
|
19
|
+
packageName: packageJson.name
|
|
20
|
+
};
|
|
21
|
+
process.env["BUN_OPTIONS"] = `--preload=${CHILD_PRELOAD}`;
|
|
22
|
+
process.env[SUBPROCESS_COVERAGE_SETTINGS_VAR] = JSON.stringify(settings);
|
|
23
|
+
afterAll(async () => {
|
|
24
|
+
await reportCoverage(rawDir, path.join(root, "coverage"));
|
|
25
|
+
await fs.rm(rawDir, {
|
|
26
|
+
recursive: true,
|
|
27
|
+
force: true
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
export {};
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=preload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preload.js","names":[],"sources":["../../../src/test-utils/subprocess-coverage/preload.ts"],"sourcesContent":["// Turns on coverage for the package's own bins and modules that tests run in child processes.\n// Import it once, from the test preload, in the package's repo.\nimport { afterAll } from \"bun:test\"\nimport fs from \"node:fs/promises\"\nimport os from \"node:os\"\nimport path from \"node:path\"\nimport process from \"node:process\"\n\nimport bun from \"bun\"\n\nimport { SUBPROCESS_COVERAGE_SETTINGS_VAR } from \"#test-utils/subprocess-coverage/env.ts\"\nimport type { CoverageSettings } from \"#test-utils/subprocess-coverage/env.ts\"\nimport { reportCoverage } from \"#test-utils/subprocess-coverage/report.ts\"\n\n// Match this file's extension so the preload is the `.ts` source in this repo and the built `.js` in `dist/`.\nconst CHILD_PRELOAD = path.join(import.meta.dir, `child-preload${path.extname(import.meta.path)}`)\n\nconst hasName = (value: unknown): value is { name: string } =>\n typeof value === \"object\" && value !== null && \"name\" in value && typeof value.name === \"string\"\n\nconst root = process.cwd()\nconst packageJson: unknown = await bun.file(path.join(root, \"package.json\")).json()\nif (!hasName(packageJson)) throw new Error(`subprocess coverage needs a package name in ${root}/package.json`)\n\nconst rawDir = await fs.mkdtemp(path.join(os.tmpdir(), \"subprocess-coverage-\"))\nconst settings: CoverageSettings = { rawDir, root, packageName: packageJson.name }\n\n// Set these on `process.env` so `subprocessCoverageEnv` and shells that inherit `process.env` pass them on.\nprocess.env[\"BUN_OPTIONS\"] = `--preload=${CHILD_PRELOAD}`\nprocess.env[SUBPROCESS_COVERAGE_SETTINGS_VAR] = JSON.stringify(settings)\n\nafterAll(async () => {\n await reportCoverage(rawDir, path.join(root, \"coverage\"))\n await fs.rm(rawDir, { recursive: true, force: true })\n})\n"],"mappings":";;;;;;;;;AAeA,MAAM,gBAAgB,KAAK,KAAK,YAAY,KAAK,gBAAgB,KAAK,QAAQ,YAAY,IAAI,GAAG;AAEjG,MAAM,WAAW,UACf,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU,SAAS,OAAO,MAAM,SAAS;AAE1F,MAAM,OAAO,QAAQ,IAAI;AACzB,MAAM,cAAuB,MAAM,IAAI,KAAK,KAAK,KAAK,MAAM,cAAc,CAAC,CAAC,CAAC,KAAK;AAClF,IAAI,CAAC,QAAQ,WAAW,GAAG,MAAM,IAAI,MAAM,+CAA+C,KAAK,cAAc;AAE7G,MAAM,SAAS,MAAM,GAAG,QAAQ,KAAK,KAAK,GAAG,OAAO,GAAG,sBAAsB,CAAC;AAC9E,MAAM,WAA6B;CAAE;CAAQ;CAAM,aAAa,YAAY;AAAK;AAGjF,QAAQ,IAAI,iBAAiB,aAAa;AAC1C,QAAQ,IAAI,oCAAoC,KAAK,UAAU,QAAQ;AAEvE,SAAS,YAAY;CACnB,MAAM,eAAe,QAAQ,KAAK,KAAK,MAAM,UAAU,CAAC;CACxD,MAAM,GAAG,GAAG,QAAQ;EAAE,WAAW;EAAM,OAAO;CAAK,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import bun from "bun";
|
|
4
|
+
import { createCoverageMap } from "@vitest/istanbul-lib-coverage";
|
|
5
|
+
import { create, createContext } from "@vitest/istanbul-lib-report";
|
|
6
|
+
import { createSourceMapStore } from "@vitest/istanbul-lib-source-maps";
|
|
7
|
+
// src/test-utils/subprocess-coverage/report.ts
|
|
8
|
+
const isCoverageMapData = (value) => typeof value === "object" && value !== null;
|
|
9
|
+
/** Merges the coverage from every process in `rawDir`, maps it back to the source, and writes a text and lcov report. */
|
|
10
|
+
const reportCoverage = async (rawDir, reportDir) => {
|
|
11
|
+
const rawFiles = await fs.readdir(rawDir);
|
|
12
|
+
if (rawFiles.length === 0) return;
|
|
13
|
+
const rawCoverages = await Promise.all(rawFiles.map(async (rawFile) => bun.file(path.join(rawDir, rawFile)).json()));
|
|
14
|
+
const coverageMap = createCoverageMap({});
|
|
15
|
+
for (const rawCoverage of rawCoverages) if (isCoverageMapData(rawCoverage)) coverageMap.merge(rawCoverage);
|
|
16
|
+
const sourceCoverageMap = await createSourceMapStore().transformCoverage(coverageMap);
|
|
17
|
+
const context = createContext({
|
|
18
|
+
dir: reportDir,
|
|
19
|
+
coverageMap: sourceCoverageMap
|
|
20
|
+
});
|
|
21
|
+
create("text").execute(context);
|
|
22
|
+
create("lcovonly").execute(context);
|
|
23
|
+
};
|
|
24
|
+
export { reportCoverage };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=report.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report.js","names":[],"sources":["../../../src/test-utils/subprocess-coverage/report.ts"],"sourcesContent":["import fs from \"node:fs/promises\"\nimport path from \"node:path\"\n\nimport { createCoverageMap } from \"@vitest/istanbul-lib-coverage\"\nimport type { CoverageMapData } from \"@vitest/istanbul-lib-coverage\"\nimport { create as createReport, createContext } from \"@vitest/istanbul-lib-report\"\nimport { createSourceMapStore } from \"@vitest/istanbul-lib-source-maps\"\nimport bun from \"bun\"\n\nconst isCoverageMapData = (value: unknown): value is CoverageMapData => typeof value === \"object\" && value !== null\n\n/** Merges the coverage from every process in `rawDir`, maps it back to the source, and writes a text and lcov report. */\nexport const reportCoverage = async (rawDir: string, reportDir: string) => {\n const rawFiles = await fs.readdir(rawDir)\n if (rawFiles.length === 0) return\n\n const rawCoverages = await Promise.all(\n rawFiles.map(async (rawFile): Promise<unknown> => bun.file(path.join(rawDir, rawFile)).json()),\n )\n const coverageMap = createCoverageMap({})\n for (const rawCoverage of rawCoverages) if (isCoverageMapData(rawCoverage)) coverageMap.merge(rawCoverage)\n\n const sourceCoverageMap = await createSourceMapStore().transformCoverage(coverageMap)\n const context = createContext({ dir: reportDir, coverageMap: sourceCoverageMap })\n createReport(\"text\").execute(context)\n createReport(\"lcovonly\").execute(context)\n}\n"],"mappings":";;;;;;;AASA,MAAM,qBAAqB,UAA6C,OAAO,UAAU,YAAY,UAAU;;AAG/G,MAAa,iBAAiB,OAAO,QAAgB,cAAsB;CACzE,MAAM,WAAW,MAAM,GAAG,QAAQ,MAAM;CACxC,IAAI,SAAS,WAAW,GAAG;CAE3B,MAAM,eAAe,MAAM,QAAQ,IACjC,SAAS,IAAI,OAAO,YAA8B,IAAI,KAAK,KAAK,KAAK,QAAQ,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAC/F;CACA,MAAM,cAAc,kBAAkB,CAAC,CAAC;CACxC,KAAK,MAAM,eAAe,cAAc,IAAI,kBAAkB,WAAW,GAAG,YAAY,MAAM,WAAW;CAEzG,MAAM,oBAAoB,MAAM,qBAAqB,CAAC,CAAC,kBAAkB,WAAW;CACpF,MAAM,UAAU,cAAc;EAAE,KAAK;EAAW,aAAa;CAAkB,CAAC;CAChF,OAAa,MAAM,CAAC,CAAC,QAAQ,OAAO;CACpC,OAAa,UAAU,CAAC,CAAC,QAAQ,OAAO;AAC1C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamhl8/configs",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "My shared configs for TS projects",
|
|
5
5
|
"homepage": "https://github.com/adamhl8/configs",
|
|
6
6
|
"bugs": {
|
|
@@ -48,6 +48,10 @@
|
|
|
48
48
|
"./test/preload": {
|
|
49
49
|
"types": "./dist/test-utils/preload.d.ts",
|
|
50
50
|
"import": "./dist/test-utils/preload.js"
|
|
51
|
+
},
|
|
52
|
+
"./test/subprocess-coverage": {
|
|
53
|
+
"types": "./dist/test-utils/subprocess-coverage/preload.d.ts",
|
|
54
|
+
"import": "./dist/test-utils/subprocess-coverage/preload.js"
|
|
51
55
|
}
|
|
52
56
|
},
|
|
53
57
|
"publishConfig": {
|
|
@@ -80,6 +84,10 @@
|
|
|
80
84
|
"@arethetypeswrong/core": "^0.18.5",
|
|
81
85
|
"@commitlint/cli": "^21.2.4",
|
|
82
86
|
"@types/bun": "^1.4.4",
|
|
87
|
+
"@vitest/istanbul-lib-coverage": "^2.0.0",
|
|
88
|
+
"@vitest/istanbul-lib-instrument": "^2.0.0",
|
|
89
|
+
"@vitest/istanbul-lib-report": "^2.0.0",
|
|
90
|
+
"@vitest/istanbul-lib-source-maps": "^2.0.0",
|
|
83
91
|
"knip": "^6.43.0",
|
|
84
92
|
"markdown-toc": "^1.2.0",
|
|
85
93
|
"oxfmt": "^0.74.0",
|
|
@@ -97,6 +105,10 @@
|
|
|
97
105
|
"@commitlint/cli": ">=21.2",
|
|
98
106
|
"@commitlint/config-conventional": ">=21.2",
|
|
99
107
|
"@tailwindcss/language-server": ">=0.16",
|
|
108
|
+
"@vitest/istanbul-lib-coverage": ">=2.0",
|
|
109
|
+
"@vitest/istanbul-lib-instrument": ">=2.0",
|
|
110
|
+
"@vitest/istanbul-lib-report": ">=2.0",
|
|
111
|
+
"@vitest/istanbul-lib-source-maps": ">=2.0",
|
|
100
112
|
"knip": ">=6.43",
|
|
101
113
|
"markdown-toc": ">=1.2",
|
|
102
114
|
"oxfmt": ">=0.74",
|
|
@@ -116,6 +128,18 @@
|
|
|
116
128
|
"@tailwindcss/language-server": {
|
|
117
129
|
"optional": true
|
|
118
130
|
},
|
|
131
|
+
"@vitest/istanbul-lib-coverage": {
|
|
132
|
+
"optional": true
|
|
133
|
+
},
|
|
134
|
+
"@vitest/istanbul-lib-instrument": {
|
|
135
|
+
"optional": true
|
|
136
|
+
},
|
|
137
|
+
"@vitest/istanbul-lib-report": {
|
|
138
|
+
"optional": true
|
|
139
|
+
},
|
|
140
|
+
"@vitest/istanbul-lib-source-maps": {
|
|
141
|
+
"optional": true
|
|
142
|
+
},
|
|
119
143
|
"publint": {
|
|
120
144
|
"optional": true
|
|
121
145
|
},
|