supercov 4.29.14 → 4.29.16

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.
@@ -2214,10 +2214,20 @@ function installNodeRequestPropagation() {
2214
2214
  }
2215
2215
  installNodeRequestPropagation();
2216
2216
 
2217
+ // The context of a request this process is serving, made as it came in
2218
+ // (`owningRequest`): it carries the request's lifetime.
2219
+ function servedRequestContext(context) {
2220
+ return context !== void 0 && context[REQUEST_LIFETIME] !== void 0 ? context : void 0;
2221
+ }
2217
2222
  function withRequestPhase(handler, event) {
2218
2223
  if (!serverPhaseStorage)
2219
2224
  return handler;
2220
- const registeredContext = currentRequestContext();
2225
+ // A handler wrapped while a request was being served -- a route module a
2226
+ // framework loads on its first request, as Next.js does -- belongs to no
2227
+ // test: that request was one attempt's, and every later request naming no
2228
+ // test (a webhook from a mock) ran as that attempt's, in a strong pass with
2229
+ // its mutant rather than the one of the test that set the request off.
2230
+ const registeredContext = servedRequestContext(serverPhaseStorage.getStore()) ? {} : currentRequestContext();
2221
2231
  return function coverageRequestPhase(...args) {
2222
2232
  var _a8, _b, _c, _d;
2223
2233
  const requestContext = args.map((argument) => requestCoverageContext(argument)).find((context2) => context2 !== void 0);
@@ -2227,7 +2237,14 @@ function withRequestPhase(handler, event) {
2227
2237
  const context = __spreadValues(__spreadValues({}, ((_a8 = requestContext == null ? void 0 : requestContext.scope) != null ? _a8 : inheritedContext.scope) ? { scope: (_b = requestContext == null ? void 0 : requestContext.scope) != null ? _b : inheritedContext.scope } : {}), ((_c = requestContext == null ? void 0 : requestContext.phaseId) != null ? _c : inheritedContext.phaseId) ? { phaseId: (_d = requestContext == null ? void 0 : requestContext.phaseId) != null ? _d : inheritedContext.phaseId } : {});
2228
2238
  if (requestContext !== void 0 && !context.scope)
2229
2239
  context.request = nextUnscopedRequest();
2230
- const lifetime = requestContext !== void 0 ? args.map((argument) => argument && typeof argument === "object" ? argument[REQUEST_LIFETIME] : void 0).find((found) => found !== void 0) : void 0;
2240
+ let lifetime = requestContext !== void 0 ? args.map((argument) => argument && typeof argument === "object" ? argument[REQUEST_LIFETIME] : void 0).find((found) => found !== void 0) : void 0;
2241
+ // A framework's own request object -- the web `Request` a Next.js route
2242
+ // handler is given -- carries no lifetime, and work the handler left
2243
+ // running after the answer (`after()`) stayed the request's. The request
2244
+ // being served, whose context the handler is called in, has it.
2245
+ const served = servedRequestContext(serverPhaseStorage.getStore());
2246
+ if (lifetime === void 0 && requestContext !== void 0 && served !== void 0 && (context.scope ? served.scope !== void 0 && served.scope.attemptId === context.scope.attemptId : served.scope === void 0))
2247
+ lifetime = served[REQUEST_LIFETIME];
2231
2248
  if (lifetime !== void 0)
2232
2249
  context[REQUEST_LIFETIME] = lifetime;
2233
2250
  const invoke = () => {
@@ -3098,6 +3115,8 @@ function strongHit() {
3098
3115
  const key = `${attemptId}\0${mutant.key}`;
3099
3116
  const hits = (strong.requestHits.get(key) || 0) + 1;
3100
3117
  strong.requestHits.set(key, hits);
3118
+ if (hits === 1)
3119
+ noteRequestHit(mutant.key, attemptId);
3101
3120
  if (hits > strong.limit) {
3102
3121
  noteMutantTimeout(mutant.file, mutant.index, attemptId);
3103
3122
  throw new MutantTimeoutError(mutant.file, mutant.index, hits);
@@ -3112,6 +3131,22 @@ function strongHit() {
3112
3131
  if (strong.hits === 1)
3113
3132
  noteEverywhereHit(strong);
3114
3133
  }
3134
+ // An attempt's own mutant ran here, in work one of its requests set off: the
3135
+ // pass trusts a survivor of a shared server only where an attempt of it ran
3136
+ // it (playwrightStrongReporter.mjs). A mutant its candidates reached when
3137
+ // measured, which none of its attempts ran, ran where the request's switch
3138
+ // never reached; it runs again alone, with the server started with it.
3139
+ function noteRequestHit(mutant, attemptId) {
3140
+ try {
3141
+ if (isBrowser || typeof process === "undefined" || !process.env)
3142
+ return;
3143
+ const results = process.env["SUPERCOV_STRONG_RESULTS"];
3144
+ const fs = results ? getFs() : void 0;
3145
+ if (fs)
3146
+ fs.appendFileSync(`${results}.reached.jsonl`, JSON.stringify({ attemptId, mutant }) + "\n");
3147
+ } catch (e) {
3148
+ }
3149
+ }
3115
3150
  // A process started with a mutant switched on everywhere, in a round where
3116
3151
  // each Playwright invocation runs one mutant of its own (its runner set
3117
3152
  // SUPERCOV_ACTIVE_MUTANT for the processes it starts, the app's server among
@@ -20,7 +20,11 @@
20
20
  // whose parent was not chosen) runs whole from then on. A kill by a job that
21
21
  // ran other tests than its baseline did is confirmed by running the job's
22
22
  // tests again without the mutant. A job past 3x its baseline and 10 s is
23
- // stopped: a timeout, unless a test had already failed.
23
+ // stopped: a timeout, unless a test had already failed. A baseline ran with
24
+ // the machine to itself, and a job that ran beside others may have spent its
25
+ // time waiting on them (the CPU; Cargo's lock on a build directory, where
26
+ // each job builds Rust compiled to wasm): such a job runs again alone, and
27
+ // only one past its limit there is a timeout.
24
28
  import { fork, spawn } from "node:child_process";
25
29
  import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
26
30
  import { availableParallelism, constants, cpus } from "node:os";
@@ -96,6 +100,49 @@ export function slots(size) {
96
100
  };
97
101
  }
98
102
 
103
+ // Turns at the machine: `shared` work runs beside other shared work, and
104
+ // `alone` work waits until none runs, while no shared work starts. A shared
105
+ // turn learns whether other work ran beside it (`crowded`).
106
+ export function turns() {
107
+ const running = new Set();
108
+ const idle = [];
109
+ let alone;
110
+ return {
111
+ async shared(work) {
112
+ while (alone)
113
+ await alone;
114
+ const turn = { crowded: running.size > 0 };
115
+ for (const other of running)
116
+ other.crowded = true;
117
+ running.add(turn);
118
+ try {
119
+ return await work(turn);
120
+ }
121
+ finally {
122
+ running.delete(turn);
123
+ if (running.size === 0)
124
+ for (const wake of idle.splice(0))
125
+ wake();
126
+ }
127
+ },
128
+ async alone(work) {
129
+ while (alone)
130
+ await alone;
131
+ let end;
132
+ alone = new Promise((done) => { end = done; });
133
+ try {
134
+ if (running.size > 0)
135
+ await new Promise((wake) => idle.push(wake));
136
+ return await work({ crowded: false });
137
+ }
138
+ finally {
139
+ alone = undefined;
140
+ end();
141
+ }
142
+ },
143
+ };
144
+ }
145
+
99
146
  // Every file under `directory`, recursively.
100
147
  function filesUnder(directory) {
101
148
  let entries;
@@ -431,16 +478,19 @@ export async function drive({ command, plan, resultsPath, root = process.cwd(),
431
478
  });
432
479
  return handed ? parallel(work, priority) : result;
433
480
  };
434
- const profile = { jobs: 0, baselines: 0, confirmations: 0, overturned: 0, stopped: 0, jobMs: 0 };
481
+ const profile = { jobs: 0, baselines: 0, confirmations: 0, overturned: 0, stopped: 0, alone: 0, jobMs: 0 };
482
+ const machine = turns();
435
483
  let jobNumber = 0;
436
484
 
437
485
  // One start of the command: `tests` of `file` (null: all of them), with
438
486
  // `mutant` on (null: none), stopped at `limitMs`. A job of a mutant's
439
487
  // later batch (`priority` 1, 2, ...) waits behind every first file's,
440
488
  // so that the kills most mutants get at once come first, and a
441
- // survivor's many files fill what is left.
442
- function job({ mutant, file, tests: chosen, bail, limitMs, priority = 0 }) {
443
- return run(() => new Promise((resolveJob) => {
489
+ // survivor's many files fill what is left. One run `alone` waits for no
490
+ // slot: it has the machine to itself.
491
+ function job({ mutant, file, tests: chosen, bail, limitMs, priority = 0, alone = false }) {
492
+ const start = (work) => (alone ? machine.alone(work) : run(() => machine.shared(work), priority));
493
+ return start((turn) => new Promise((resolveJob) => {
444
494
  const number = ++jobNumber;
445
495
  profile.jobs += 1;
446
496
  const directory = join(work, String(number));
@@ -458,7 +508,7 @@ export async function drive({ command, plan, resultsPath, root = process.cwd(),
458
508
  const statuses = outcomes(evidence);
459
509
  if (environment.SUPERCOV_STRONG_KEEP !== "1")
460
510
  rmSync(directory, { recursive: true, force: true });
461
- return { ...outcome, statuses };
511
+ return { ...outcome, statuses, crowded: turn.crowded };
462
512
  };
463
513
  // A runner's own command, started for this job alone.
464
514
  const spawnJob = () => {
@@ -561,7 +611,7 @@ export async function drive({ command, plan, resultsPath, root = process.cwd(),
561
611
  durationMs: Date.now() - begun,
562
612
  }));
563
613
  });
564
- }), priority);
614
+ }));
565
615
  }
566
616
 
567
617
  // What a file does without a mutant: once with every test a mutant will
@@ -633,9 +683,16 @@ export async function drive({ command, plan, resultsPath, root = process.cwd(),
633
683
  const limitMs = 3 * baseline.durationMs + graceMs;
634
684
  // A test that fails without the mutant must not stop the job.
635
685
  const bail = !whole || [...baseline.statuses.values()].every((status) => status === "passed");
636
- const result = await job({ mutant, file: group.file, tests: chosen, bail, limitMs, priority });
686
+ let result = await job({ mutant, file: group.file, tests: chosen, bail, limitMs, priority });
687
+ const killerIn = (outcome) => [...outcome.statuses].find(([id, status]) => status === "failed" && (chosen === null || chosen.has(id)) && !excluded(id))?.[0];
688
+ // Past its limit beside other jobs, with nothing failed: it may
689
+ // only have waited on them, so it runs again alone.
690
+ if (result.timedOut && result.crowded && !result.mutantTimedOut && killerIn(result) === undefined) {
691
+ profile.alone += 1;
692
+ result = await job({ mutant, file: group.file, tests: chosen, bail, limitMs, alone: true });
693
+ }
637
694
  const ran = [...result.statuses].filter(([id, status]) => status !== "skipped" && (chosen === null || chosen.has(id)));
638
- const killer = ran.find(([id, status]) => status === "failed" && !excluded(id))?.[0];
695
+ const killer = killerIn(result);
639
696
  if (result.mutantTimedOut)
640
697
  return { status: "timeout", reason: "the mutant's evaluations passed its limit", tests: ran.length };
641
698
  if (killer === undefined) {