@hughescr/stryker-bun-runner 1.5.2 → 1.6.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hughescr/stryker-bun-runner",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "Stryker mutation testing test runner plugin for Bun (bun test), with per-test coverage",
5
5
  "keywords": [
6
6
  "stryker",
@@ -35,16 +35,19 @@
35
35
  "dist"
36
36
  ],
37
37
  "scripts": {
38
- "build": "bun build src/index.ts --outdir dist --target node --format esm --packages external && bun build src/coverage/preload-logic.ts --outdir dist/coverage --target bun --format esm --packages external && dts-bundle-generator --config dts-bundle-generator.config.json && cp -r src/templates dist/",
38
+ "build": "rm -rf dist && bun build src/index.ts --outdir dist --target node --format esm --packages external && bun scripts/build-preload.ts dist/templates && dts-bundle-generator --config dts-bundle-generator.config.json",
39
39
  "lint": "eslint --cache --format unix .",
40
40
  "mutate": "stryker run",
41
41
  "prepublishOnly": "bun run build",
42
42
  "test": "bun test",
43
43
  "typecheck": "node node_modules/typescript/bin/tsc --noEmit && node node_modules/typescript/bin/tsc --noEmit -p scripts",
44
+ "bench": "sh bench/lock.sh --exclusive -- bun bench/run.ts",
45
+ "checks": "sh bench/lock.sh --shared -- sh -c 'bun test && bun run lint && bun run typecheck'",
44
46
  "postversion": "git commit -m \"Bump package version to $npm_package_version\" package.json; git flow release start $npm_package_version; git flow release finish -m $npm_package_version $npm_package_version; git checkout develop; git merge main"
45
47
  },
46
48
  "dependencies": {
47
49
  "@stryker-mutator/api": "^9.0.0 || ^10.0.0",
50
+ "picomatch": "^4.0.4",
48
51
  "smol-toml": "^1.7.0",
49
52
  "tinyglobby": "^0.2.17",
50
53
  "ws": "^8.21.0"
@@ -1,255 +0,0 @@
1
- // @bun
2
- // src/coverage/preload-logic.ts
3
- import { appendFileSync, closeSync, constants, createReadStream, openSync, writeSync } from "fs";
4
- var preloadFs = { appendFileSync, closeSync, openSync, writeSync };
5
- var HIT_LIMIT_TOKEN_ENV = "__STRYKER_HIT_LIMIT_TOKEN__";
6
- var SYNC_TOKEN_ENV = "__STRYKER_SYNC_TOKEN__";
7
- function getPreloadConfig() {
8
- return {
9
- syncPort: process.env.__STRYKER_SYNC_PORT__,
10
- syncToken: process.env[SYNC_TOKEN_ENV],
11
- coverageFile: process.env.__STRYKER_COVERAGE_FILE__,
12
- activeMutant: process.env.__STRYKER_ACTIVE_MUTANT__,
13
- hitLimit: parseHitLimit(process.env.__STRYKER_HIT_LIMIT__),
14
- hitLimitToken: process.env[HIT_LIMIT_TOKEN_ENV]
15
- };
16
- }
17
- function parseHitLimit(raw) {
18
- const limit = Number(raw);
19
- return Number.isInteger(limit) && limit > 0 ? limit : undefined;
20
- }
21
- function shouldCollectCoverage(config) {
22
- return !config.activeMutant && !!config.coverageFile;
23
- }
24
- function waitForSyncReady(socket) {
25
- return new Promise((resolve) => {
26
- socket.addEventListener("message", (event) => {
27
- if (event.data === "ready") {
28
- resolve({ ready: true });
29
- }
30
- });
31
- socket.addEventListener("error", () => {
32
- resolve({ ready: false, problem: "failed with a connection error" });
33
- });
34
- socket.addEventListener("close", (event) => {
35
- const reason = event.reason ? `, reason "${event.reason}"` : "";
36
- resolve({ ready: false, problem: `closed (code ${event.code ?? "unknown"}${reason})` });
37
- });
38
- });
39
- }
40
- function syncAddress(syncPort) {
41
- return `ws://127.0.0.1:${syncPort}/sync`;
42
- }
43
- async function awaitRunnerReady(deps) {
44
- const openSocket = deps.openSocket ?? ((url) => new WebSocket(url));
45
- const fail = deps.fail ?? ((message) => {
46
- console.error(message);
47
- process.exit(1);
48
- });
49
- let socket;
50
- let outcome;
51
- try {
52
- socket = openSocket(`${syncAddress(deps.syncPort)}/${deps.syncToken}`);
53
- outcome = await waitForSyncReady(socket);
54
- } catch (error) {
55
- const reason = redactToken(error instanceof Error ? error.message : String(error), deps.syncToken);
56
- return fail(formatSyncNotReadyMessage(deps.syncPort, `could not be opened (${reason})`));
57
- }
58
- if (!outcome.ready) {
59
- return fail(formatSyncNotReadyMessage(deps.syncPort, outcome.problem));
60
- }
61
- if (deps.keepOpen) {
62
- return socket;
63
- }
64
- socket.close();
65
- return null;
66
- }
67
- function redactToken(text, token) {
68
- return token === "" ? text : text.replaceAll(token, "[redacted]");
69
- }
70
- function formatSyncNotReadyMessage(syncPort, problem) {
71
- return [
72
- `[Stryker] stryker-bun-runner: the dry run's sync connection to the runner (${syncAddress(syncPort)}) ${problem}`,
73
- "before the runner signalled that it is ready to observe the tests. Exiting without running any test: tests run now",
74
- "would not be seen by the runner's inspector, so their results and per-test coverage would be lost."
75
- ].join(" ");
76
- }
77
- function initializeStrykerNamespace(globalObj) {
78
- const g = globalObj;
79
- g.__stryker__ ??= { mutantCoverage: { static: {}, perTest: {} } };
80
- const strykerGlobal = g.__stryker__;
81
- strykerGlobal.mutantCoverage ??= { static: {}, perTest: {} };
82
- g.__mutantCoverage__ = strykerGlobal.mutantCoverage;
83
- return strykerGlobal;
84
- }
85
- function setActiveMutant(strykerNamespace, activeMutant) {
86
- strykerNamespace.activeMutant = activeMutant;
87
- }
88
- var HIT_LIMIT_SENTINEL = "__STRYKER_HIT_LIMIT_REACHED__";
89
- function formatHitLimitSentinel(token, hitCount) {
90
- return `
91
- ${HIT_LIMIT_SENTINEL} ${token} ${hitCount}
92
- `;
93
- }
94
- function installHitLimit(strykerNamespace, hitLimit, token, report = (line) => {
95
- preloadFs.writeSync(2, line);
96
- }) {
97
- let hitCount = 0;
98
- let reported = false;
99
- strykerNamespace.hitLimit = hitLimit;
100
- Object.defineProperty(strykerNamespace, "hitCount", {
101
- configurable: true,
102
- enumerable: true,
103
- get: () => hitCount,
104
- set: (value) => {
105
- hitCount = value;
106
- if (!reported && hitCount > hitLimit) {
107
- reported = true;
108
- report(formatHitLimitSentinel(token, hitCount));
109
- }
110
- }
111
- });
112
- }
113
- function detectGapWindowBleed(staticCountsAtLastBoundary, staticCoverageNow) {
114
- if (!staticCoverageNow) {
115
- return [];
116
- }
117
- const bledIds = [];
118
- for (const [id, countNow] of Object.entries(staticCoverageNow)) {
119
- const countAtBoundary = staticCountsAtLastBoundary.get(id) ?? 0;
120
- if (countNow > countAtBoundary) {
121
- bledIds.push(id);
122
- }
123
- }
124
- return bledIds;
125
- }
126
- function formatCoverageData(mutantCoverage, counterToName, lateHits = []) {
127
- if (!mutantCoverage) {
128
- return { perTest: {}, static: {} };
129
- }
130
- const perTest = {};
131
- for (const [testId, coverage] of Object.entries(mutantCoverage.perTest ?? {})) {
132
- const actualName = counterToName.get(testId) ?? testId;
133
- perTest[actualName] = { ...coverage };
134
- }
135
- const staticCoverage = { ...mutantCoverage.static };
136
- return {
137
- perTest,
138
- static: staticCoverage,
139
- ...lateHits.length > 0 ? { lateHits: [...lateHits] } : {}
140
- };
141
- }
142
- var COVERAGE_APPEND_FLAGS = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW;
143
- function writeCoverageToFile(coverageFile, data) {
144
- const fd = preloadFs.openSync(coverageFile, COVERAGE_APPEND_FLAGS, 384);
145
- try {
146
- preloadFs.appendFileSync(fd, `${JSON.stringify(data)}
147
- `, "utf8");
148
- } finally {
149
- preloadFs.closeSync(fd);
150
- }
151
- }
152
- var LIFELINE_FD_ENV = "__STRYKER_LIFELINE_FD__";
153
- function describeError(error) {
154
- const { code, message } = error;
155
- return String(code ?? message);
156
- }
157
- function parseLifelineFd(value) {
158
- if (value === undefined || !/^\d+$/.test(value)) {
159
- return;
160
- }
161
- const fd = Number(value);
162
- return Number.isSafeInteger(fd) && fd > 2 ? fd : undefined;
163
- }
164
- function watchParentLifeline(deps) {
165
- let settled = false;
166
- const unavailable = (reason) => {
167
- if (!settled) {
168
- settled = true;
169
- deps.onUnavailable(reason);
170
- }
171
- };
172
- const fd = parseLifelineFd(deps.fdValue);
173
- if (fd === undefined) {
174
- unavailable(`${LIFELINE_FD_ENV}=${deps.fdValue ?? "(unset)"} names no lifeline descriptor`);
175
- return;
176
- }
177
- const open = deps.open ?? ((lifelineFd) => createReadStream("", { fd: lifelineFd }));
178
- let lifeline;
179
- try {
180
- lifeline = open(fd);
181
- } catch (error) {
182
- unavailable(`opening fd ${fd} failed: ${describeError(error)}`);
183
- return;
184
- }
185
- lifeline.once("end", () => {
186
- settled = true;
187
- deps.onOrphaned();
188
- });
189
- lifeline.on("error", (error) => {
190
- unavailable(`reading fd ${fd} failed: ${describeError(error)}`);
191
- });
192
- lifeline.resume();
193
- }
194
- function startOrphanWatchdog(deps) {
195
- const originalPpid = deps.originalPpid ?? deps.getPpid();
196
- let fired = false;
197
- const intervalId = setInterval(() => {
198
- if (fired || deps.getPpid() === originalPpid) {
199
- return;
200
- }
201
- fired = true;
202
- deps.onOrphaned();
203
- }, deps.intervalMs ?? 1000);
204
- intervalId.unref();
205
- return () => {
206
- clearInterval(intervalId);
207
- };
208
- }
209
- function guardAgainstOrphaning(deps) {
210
- const originalPpid = deps.getPpid();
211
- watchParentLifeline({
212
- fdValue: deps.fdValue,
213
- onOrphaned: deps.onOrphaned,
214
- open: deps.open,
215
- onUnavailable: (reason) => {
216
- deps.log?.(`[Stryker] parent lifeline unavailable (${reason}); polling the parent PID on a timer instead`);
217
- startOrphanWatchdog({ getPpid: deps.getPpid, onOrphaned: deps.onOrphaned, intervalMs: deps.intervalMs, originalPpid });
218
- }
219
- });
220
- }
221
- var ORPHAN_DEBUG_ENV = "STRYKER_BUN_RUNNER_DEBUG";
222
- function orphanDebugLog(value, write) {
223
- if (value === undefined || value === "" || value === "0" || value === "false") {
224
- return;
225
- }
226
- return (message) => {
227
- write(`${message}
228
- `);
229
- };
230
- }
231
- export {
232
- HIT_LIMIT_SENTINEL,
233
- HIT_LIMIT_TOKEN_ENV,
234
- LIFELINE_FD_ENV,
235
- ORPHAN_DEBUG_ENV,
236
- SYNC_TOKEN_ENV,
237
- awaitRunnerReady,
238
- detectGapWindowBleed,
239
- formatCoverageData,
240
- formatHitLimitSentinel,
241
- formatSyncNotReadyMessage,
242
- getPreloadConfig,
243
- guardAgainstOrphaning,
244
- initializeStrykerNamespace,
245
- installHitLimit,
246
- orphanDebugLog,
247
- parseHitLimit,
248
- preloadFs,
249
- setActiveMutant,
250
- shouldCollectCoverage,
251
- startOrphanWatchdog,
252
- waitForSyncReady,
253
- watchParentLifeline,
254
- writeCoverageToFile
255
- };