@cosyte/vitest-config 0.1.3 → 0.1.6
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/CHANGELOG.md +51 -0
- package/README.md +222 -8
- package/bin/cosyte-mutate.js +42 -0
- package/index.d.ts +5 -0
- package/index.js +14 -6
- package/mutate.d.ts +118 -0
- package/mutate.js +444 -0
- package/package.json +52 -4
- package/snippets.d.ts +123 -1
- package/snippets.js +375 -43
- package/stryker.d.ts +76 -0
- package/stryker.js +199 -0
package/mutate.js
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// @cosyte/vitest-config/mutate
|
|
2
|
+
//
|
|
3
|
+
// THE TWO RUNS: `full` mutates everything the config names and writes the report the floor file's
|
|
4
|
+
// mutation score is read from; `changed` mutates only the source files a change touched against a
|
|
5
|
+
// base ref, and writes its report to a separate file, `changed.json` beside the full one, so a run
|
|
6
|
+
// over a few files can never be read as the package's score.
|
|
7
|
+
//
|
|
8
|
+
// What a change touched is read from git by @cosyte/script-utils/changes, the same reading the
|
|
9
|
+
// ratchet judges changed lines with, so the two cannot disagree about which files a pull request
|
|
10
|
+
// changed. It is an optional peer dependency, imported only when a changed run starts, so a package
|
|
11
|
+
// that uses only the Vitest config installs nothing for it. A changed run with no changed source file starts no mutation run: it writes an empty
|
|
12
|
+
// report, so the ratchet can tell "ran, nothing to mutate" from "never ran", and exits 0.
|
|
13
|
+
//
|
|
14
|
+
// A SHARDED FULL RUN. A full run of a large library takes hours on one machine, so a CI workflow can
|
|
15
|
+
// split it across parallel jobs. Each job sets COSYTE_MUTATE_SHARD=<k>/<n>: the run lists the files
|
|
16
|
+
// the config names, keeps those whose FNV-1a hash of the path mod n is k - 1, mutates just those,
|
|
17
|
+
// and writes shard-<k>-of-<n>.json beside the full report in place of it, for the workflow to merge.
|
|
18
|
+
// The hash, not a position in a sorted list, decides a file's shard, so a file added elsewhere does
|
|
19
|
+
// not move every other file to another shard. COSYTE_MUTATE_INCREMENTAL_FILE names the file where
|
|
20
|
+
// Stryker keeps its incremental results, so a later run retests only what changed. A changed run
|
|
21
|
+
// reads neither variable, and a full run with neither set runs exactly as it always has.
|
|
22
|
+
//
|
|
23
|
+
// Exit codes: 0 the run finished (the score is judged by cosyte-ratchet, not here); 2 it could not
|
|
24
|
+
// run (bad arguments, no config, git could not answer, Stryker failed).
|
|
25
|
+
|
|
26
|
+
import { mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
27
|
+
import { dirname, join, matchesGlob, relative, resolve, sep } from "node:path";
|
|
28
|
+
import { pathToFileURL } from "node:url";
|
|
29
|
+
|
|
30
|
+
import { FULL_REPORT, htmlReportFor } from "./stryker.js";
|
|
31
|
+
|
|
32
|
+
/** The config file a run reads when `--config` names none, relative to the working directory. */
|
|
33
|
+
export const DEFAULT_CONFIG = "stryker.config.mjs";
|
|
34
|
+
|
|
35
|
+
/** The variable a sharded full run reads its share from: `<k>/<n>`. */
|
|
36
|
+
export const SHARD_VARIABLE = "COSYTE_MUTATE_SHARD";
|
|
37
|
+
|
|
38
|
+
/** The variable naming the file Stryker reads and writes its incremental results in. */
|
|
39
|
+
export const INCREMENTAL_VARIABLE = "COSYTE_MUTATE_INCREMENTAL_FILE";
|
|
40
|
+
|
|
41
|
+
/** Thrown for anything that stops a run before Stryker starts. Exit 2. */
|
|
42
|
+
export class MutateUnreadable extends Error {
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} message What stopped the run.
|
|
45
|
+
*/
|
|
46
|
+
constructor(message) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.name = "MutateUnreadable";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const HELP = `cosyte-mutate: run StrykerJS with the shared config, over everything or over a change.
|
|
53
|
+
|
|
54
|
+
cosyte-mutate full [--config <file>]
|
|
55
|
+
cosyte-mutate changed --base <ref> [--config <file>]
|
|
56
|
+
|
|
57
|
+
full mutates every file the config names and writes its JSON report where the config says
|
|
58
|
+
(reports/mutation/mutation.json with @cosyte/vitest-config/stryker).
|
|
59
|
+
changed mutates only the source files changed against the merge base of <ref> and HEAD (with
|
|
60
|
+
edits not yet committed and untracked files), and writes changed.json beside the full
|
|
61
|
+
report. With no changed source file it writes an empty report and exits 0.
|
|
62
|
+
--config the Stryker config file, default ${DEFAULT_CONFIG}, relative to the working directory.
|
|
63
|
+
|
|
64
|
+
A full run reads two variables; with neither set it runs as above:
|
|
65
|
+
${SHARD_VARIABLE}=<k>/<n>
|
|
66
|
+
mutate only shard k of n of the files the config names, and write
|
|
67
|
+
shard-<k>-of-<n>.json beside the full report in place of it (an empty report when the
|
|
68
|
+
shard holds no file). A value that is not <k>/<n> with 1 <= k <= n exits 2.
|
|
69
|
+
${INCREMENTAL_VARIABLE}=<file>
|
|
70
|
+
where Stryker reads and writes its incremental results, relative to the working
|
|
71
|
+
directory, so a later run retests only what changed.
|
|
72
|
+
|
|
73
|
+
Exit 0 when the run finished (cosyte-ratchet judges the score), 2 when it could not run.
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Parse the command line.
|
|
78
|
+
*
|
|
79
|
+
* @param {string[]} argv The arguments after the bin's name.
|
|
80
|
+
* @returns {{ mode: "full" | "changed", base: string | null, config: string }} The options.
|
|
81
|
+
*/
|
|
82
|
+
export function parseMutateArgs(argv) {
|
|
83
|
+
const [mode, ...rest] = argv;
|
|
84
|
+
if (mode !== "full" && mode !== "changed") {
|
|
85
|
+
throw new MutateUnreadable(`the mode must be full or changed, not ${JSON.stringify(mode)}`);
|
|
86
|
+
}
|
|
87
|
+
const options = { mode, base: null, config: DEFAULT_CONFIG };
|
|
88
|
+
for (let i = 0; i < rest.length; i += 2) {
|
|
89
|
+
const flag = rest[i];
|
|
90
|
+
const value = rest[i + 1];
|
|
91
|
+
if (flag !== "--base" && flag !== "--config") {
|
|
92
|
+
throw new MutateUnreadable(`unknown argument ${JSON.stringify(flag)}`);
|
|
93
|
+
}
|
|
94
|
+
if (value === undefined || value.startsWith("--")) {
|
|
95
|
+
throw new MutateUnreadable(`${flag} needs a value`);
|
|
96
|
+
}
|
|
97
|
+
if (flag === "--base") options.base = value;
|
|
98
|
+
else options.config = value;
|
|
99
|
+
}
|
|
100
|
+
if (mode === "changed" && options.base === null) {
|
|
101
|
+
throw new MutateUnreadable(
|
|
102
|
+
"changed needs --base <ref>, the ref the change is measured against",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (mode === "full" && options.base !== null) {
|
|
106
|
+
throw new MutateUnreadable("full takes no --base: it mutates everything the config names");
|
|
107
|
+
}
|
|
108
|
+
return options;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Pick the changed files a config mutates: each must match one of its patterns and none of its
|
|
113
|
+
* `!` patterns. Patterns and the result are relative to the working directory, as Stryker reads
|
|
114
|
+
* them; a changed file outside it is never picked.
|
|
115
|
+
*
|
|
116
|
+
* @param {object} input The inputs.
|
|
117
|
+
* @param {Iterable<string>} input.changed Changed files, relative to the repository root.
|
|
118
|
+
* @param {string} input.root The repository root.
|
|
119
|
+
* @param {string} input.cwd The directory Stryker runs in.
|
|
120
|
+
* @param {readonly string[]} input.patterns The config's `mutate` patterns.
|
|
121
|
+
* @returns {string[]} The files to mutate, sorted.
|
|
122
|
+
*/
|
|
123
|
+
export function selectChangedFiles({ changed, root, cwd, patterns }) {
|
|
124
|
+
const include = patterns.filter((p) => !p.startsWith("!"));
|
|
125
|
+
const exclude = patterns.filter((p) => p.startsWith("!")).map((p) => p.slice(1));
|
|
126
|
+
const picked = [];
|
|
127
|
+
for (const file of changed) {
|
|
128
|
+
const path = relative(cwd, resolve(root, file)).split(sep).join("/");
|
|
129
|
+
if (path.startsWith("../")) continue;
|
|
130
|
+
if (!include.some((p) => matchesGlob(path, p))) continue;
|
|
131
|
+
if (exclude.some((p) => matchesGlob(path, p))) continue;
|
|
132
|
+
picked.push(path);
|
|
133
|
+
}
|
|
134
|
+
return picked.sort();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Read the shard a full run mutates from the value of {@link SHARD_VARIABLE}.
|
|
139
|
+
*
|
|
140
|
+
* @param {string | undefined} value `<k>/<n>`, two whole numbers with 1 <= k <= n.
|
|
141
|
+
* @returns {{ k: number, n: number } | null} The shard, or null when the value is unset or empty.
|
|
142
|
+
*/
|
|
143
|
+
export function parseShard(value) {
|
|
144
|
+
if (value === undefined || value === "") return null;
|
|
145
|
+
const match = /^(\d+)\/(\d+)$/.exec(value);
|
|
146
|
+
const k = match === null ? 0 : Number(match[1]);
|
|
147
|
+
const n = match === null ? 0 : Number(match[2]);
|
|
148
|
+
if (k < 1 || k > n) {
|
|
149
|
+
throw new MutateUnreadable(
|
|
150
|
+
`${SHARD_VARIABLE} must be <k>/<n>, two whole numbers with 1 <= k <= n, not ${JSON.stringify(value)}`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return { k, n };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 32-bit FNV-1a over the UTF-8 bytes of a text: the hash a file's shard is read from.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} text The text, here a path relative to the working directory with forward slashes.
|
|
160
|
+
* @returns {number} The hash, an unsigned 32-bit integer.
|
|
161
|
+
*/
|
|
162
|
+
export function fnv1a32(text) {
|
|
163
|
+
let hash = 0x811c9dc5;
|
|
164
|
+
// Buffer.from encodes a string as UTF-8 when it is given no encoding.
|
|
165
|
+
for (const byte of Buffer.from(text)) {
|
|
166
|
+
hash = Math.imul(hash ^ byte, 0x01000193) >>> 0;
|
|
167
|
+
}
|
|
168
|
+
return hash;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The files of one shard: those whose {@link fnv1a32} hash mod `n` is `k - 1`. Over the shards
|
|
173
|
+
* 1 to `n`, every file is in exactly one.
|
|
174
|
+
*
|
|
175
|
+
* @param {readonly string[]} files Paths relative to the working directory, with forward slashes.
|
|
176
|
+
* @param {{ k: number, n: number }} shard The shard.
|
|
177
|
+
* @returns {string[]} The shard's files, in their given order.
|
|
178
|
+
*/
|
|
179
|
+
export function filesInShard(files, { k, n }) {
|
|
180
|
+
return files.filter((file) => fnv1a32(file) % n === k - 1);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* List the files under the working directory that a config's `mutate` patterns name, picked by the
|
|
185
|
+
* same rule as {@link selectChangedFiles}. It never enters a `node_modules` or `.git` directory, or
|
|
186
|
+
* Stryker's temp directory.
|
|
187
|
+
*
|
|
188
|
+
* @param {object} input The inputs.
|
|
189
|
+
* @param {string} input.cwd The directory Stryker runs in.
|
|
190
|
+
* @param {readonly string[]} input.patterns The config's `mutate` patterns.
|
|
191
|
+
* @param {string} [input.tempDirName] The config's `tempDirName`, relative to `cwd`.
|
|
192
|
+
* @returns {string[]} The files, relative to `cwd` with forward slashes, sorted.
|
|
193
|
+
*/
|
|
194
|
+
export function expandMutate({ cwd, patterns, tempDirName = ".stryker-tmp" }) {
|
|
195
|
+
const root = resolve(cwd);
|
|
196
|
+
const temp = resolve(root, tempDirName);
|
|
197
|
+
const found = [];
|
|
198
|
+
const walk = (dir) => {
|
|
199
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
200
|
+
const path = join(dir, entry.name);
|
|
201
|
+
if (entry.isFile()) found.push(relative(root, path));
|
|
202
|
+
else if (
|
|
203
|
+
entry.isDirectory() &&
|
|
204
|
+
entry.name !== "node_modules" &&
|
|
205
|
+
entry.name !== ".git" &&
|
|
206
|
+
path !== temp
|
|
207
|
+
) {
|
|
208
|
+
walk(path);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
walk(root);
|
|
213
|
+
return selectChangedFiles({ changed: found, root, cwd: root, patterns });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Where a full run writes its JSON report: the config's `jsonReporter.fileName`, or Stryker's own
|
|
218
|
+
* default, which is also this package's.
|
|
219
|
+
*
|
|
220
|
+
* @param {Record<string, any>} config The loaded Stryker config.
|
|
221
|
+
* @returns {string} The path, relative to the working directory.
|
|
222
|
+
*/
|
|
223
|
+
export function fullReportPath(config) {
|
|
224
|
+
const full = config.jsonReporter?.fileName;
|
|
225
|
+
return typeof full === "string" ? full : FULL_REPORT;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Where a changed run writes its report: `changed.json` beside the full report.
|
|
230
|
+
*
|
|
231
|
+
* @param {Record<string, any>} config The loaded Stryker config.
|
|
232
|
+
* @returns {string} The path, relative to the working directory.
|
|
233
|
+
*/
|
|
234
|
+
export function changedReportPath(config) {
|
|
235
|
+
return join(dirname(fullReportPath(config)), "changed.json");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Where a sharded full run writes its report: `shard-<k>-of-<n>.json` beside the full report.
|
|
240
|
+
*
|
|
241
|
+
* @param {Record<string, any>} config The loaded Stryker config.
|
|
242
|
+
* @param {{ k: number, n: number }} shard The shard.
|
|
243
|
+
* @returns {string} The path, relative to the working directory.
|
|
244
|
+
*/
|
|
245
|
+
export function shardReportPath(config, { k, n }) {
|
|
246
|
+
return join(dirname(fullReportPath(config)), `shard-${k}-of-${n}.json`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* An empty report in the mutation-testing-report-schema shape: no file, no mutant.
|
|
251
|
+
*
|
|
252
|
+
* @param {string} projectRoot The directory the run would have run in.
|
|
253
|
+
* @returns {string} The JSON text.
|
|
254
|
+
*/
|
|
255
|
+
export function emptyReport(projectRoot) {
|
|
256
|
+
return `${JSON.stringify({
|
|
257
|
+
schemaVersion: "2",
|
|
258
|
+
thresholds: { high: 100, low: 90 },
|
|
259
|
+
projectRoot,
|
|
260
|
+
files: {},
|
|
261
|
+
})}\n`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Load a Stryker config module and return its default export.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} path The config file, absolute.
|
|
268
|
+
* @returns {Promise<Record<string, any>>} The config.
|
|
269
|
+
*/
|
|
270
|
+
export async function loadConfig(path) {
|
|
271
|
+
let mod;
|
|
272
|
+
try {
|
|
273
|
+
mod = await import(pathToFileURL(path).href);
|
|
274
|
+
} catch (cause) {
|
|
275
|
+
throw new MutateUnreadable(`the Stryker config ${path} could not be loaded: ${cause.message}`);
|
|
276
|
+
}
|
|
277
|
+
const config = mod.default;
|
|
278
|
+
if (typeof config !== "object" || config === null || !Array.isArray(config.mutate)) {
|
|
279
|
+
throw new MutateUnreadable(`the Stryker config ${path} exports no config with a mutate list`);
|
|
280
|
+
}
|
|
281
|
+
return config;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Load the change reader from @cosyte/script-utils, the optional peer dependency a changed run needs.
|
|
286
|
+
*
|
|
287
|
+
* @param {(specifier: string) => Promise<any>} [load] Imports a module; replaced in tests.
|
|
288
|
+
* @returns {Promise<(opts: { base: string, cwd?: string }) => { root: string, files: Map<string, Set<number>> }>}
|
|
289
|
+
* Its `readChanges`.
|
|
290
|
+
*/
|
|
291
|
+
export async function loadChangeReader(load = (specifier) => import(specifier)) {
|
|
292
|
+
try {
|
|
293
|
+
const { readChanges } = await load("@cosyte/script-utils/changes");
|
|
294
|
+
return readChanges;
|
|
295
|
+
} catch (cause) {
|
|
296
|
+
throw new MutateUnreadable(
|
|
297
|
+
"a changed run reads what a change touched with @cosyte/script-utils, an optional peer " +
|
|
298
|
+
`dependency of @cosyte/vitest-config; install it beside this package (${cause.message})`,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Start StrykerJS with these options and wait for it to finish.
|
|
305
|
+
*
|
|
306
|
+
* @param {Record<string, unknown>} options Stryker options; `configFile` names the config, and
|
|
307
|
+
* the rest override it.
|
|
308
|
+
* @returns {Promise<void>} Resolves when the run finishes.
|
|
309
|
+
*/
|
|
310
|
+
export async function runStryker(options) {
|
|
311
|
+
const { Stryker } = await import("@stryker-mutator/core");
|
|
312
|
+
await new Stryker(options).runMutationTest();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The full run, whole or one shard of it, with Stryker's incremental results or without.
|
|
317
|
+
*
|
|
318
|
+
* @param {object} opts Options.
|
|
319
|
+
* @param {string} opts.cwd The directory Stryker runs in.
|
|
320
|
+
* @param {Record<string, string | undefined>} opts.env Where the two variables are read.
|
|
321
|
+
* @param {(text: string) => void} opts.out Writes to stdout.
|
|
322
|
+
* @param {typeof runStryker} opts.stryker Starts Stryker.
|
|
323
|
+
* @param {string} opts.configFile The config file, absolute.
|
|
324
|
+
* @param {Record<string, any>} opts.config The loaded config.
|
|
325
|
+
* @param {string} opts.name The config file as the command line named it.
|
|
326
|
+
* @returns {Promise<number>} The exit code.
|
|
327
|
+
*/
|
|
328
|
+
async function runFull({ cwd, env, out, stryker, configFile, config, name }) {
|
|
329
|
+
const shard = parseShard(env[SHARD_VARIABLE]);
|
|
330
|
+
const incrementalFile = env[INCREMENTAL_VARIABLE];
|
|
331
|
+
const incremental =
|
|
332
|
+
incrementalFile === undefined || incrementalFile === ""
|
|
333
|
+
? {}
|
|
334
|
+
: { incremental: true, incrementalFile: resolve(cwd, incrementalFile) };
|
|
335
|
+
const sayIncremental = () => {
|
|
336
|
+
if (incrementalFile)
|
|
337
|
+
out(`cosyte-mutate: incremental results read from and written to ${incrementalFile}\n`);
|
|
338
|
+
};
|
|
339
|
+
if (shard === null) {
|
|
340
|
+
// A report left from an earlier run must not outlive a run that failed.
|
|
341
|
+
rmSync(resolve(cwd, fullReportPath(config)), { force: true });
|
|
342
|
+
out(`cosyte-mutate: mutating everything ${name} names\n`);
|
|
343
|
+
sayIncremental();
|
|
344
|
+
await stryker({ configFile, ...incremental });
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
|
347
|
+
const report = shardReportPath(config, shard);
|
|
348
|
+
rmSync(resolve(cwd, report), { force: true });
|
|
349
|
+
const all = expandMutate({ cwd, patterns: config.mutate, tempDirName: config.tempDirName });
|
|
350
|
+
const files = filesInShard(all, shard);
|
|
351
|
+
const which = `shard ${shard.k} of ${shard.n}`;
|
|
352
|
+
if (files.length === 0) {
|
|
353
|
+
mkdirSync(dirname(resolve(cwd, report)), { recursive: true });
|
|
354
|
+
writeFileSync(resolve(cwd, report), emptyReport(cwd));
|
|
355
|
+
out(
|
|
356
|
+
`cosyte-mutate: ${which} holds none of the ${all.length} file(s) ${name} names; nothing to mutate. Wrote an empty ${report}.\n`,
|
|
357
|
+
);
|
|
358
|
+
return 0;
|
|
359
|
+
}
|
|
360
|
+
out(
|
|
361
|
+
`cosyte-mutate: ${which}: mutating ${files.length} of the ${all.length} file(s) ${name} names:\n`,
|
|
362
|
+
);
|
|
363
|
+
for (const file of files) out(` ${file}\n`);
|
|
364
|
+
sayIncremental();
|
|
365
|
+
await stryker({
|
|
366
|
+
configFile,
|
|
367
|
+
mutate: files,
|
|
368
|
+
jsonReporter: { fileName: report },
|
|
369
|
+
htmlReporter: { fileName: htmlReportFor(report) },
|
|
370
|
+
...incremental,
|
|
371
|
+
});
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Run `cosyte-mutate`.
|
|
377
|
+
*
|
|
378
|
+
* @param {object} opts Options.
|
|
379
|
+
* @param {string[]} opts.argv The arguments after the bin's name.
|
|
380
|
+
* @param {string} opts.cwd The directory Stryker runs in.
|
|
381
|
+
* @param {Record<string, string | undefined>} [opts.env] Where a full run reads
|
|
382
|
+
* {@link SHARD_VARIABLE} and {@link INCREMENTAL_VARIABLE}; the process's environment when left out.
|
|
383
|
+
* @param {(text: string) => void} opts.out Writes to stdout.
|
|
384
|
+
* @param {(text: string) => void} opts.err Writes to stderr.
|
|
385
|
+
* @param {(opts: { base: string, cwd: string }) => { root: string, files: Map<string, Set<number>> }} [opts.changes]
|
|
386
|
+
* Reads what a change touched; replaced in tests. Left out, a changed run loads it from
|
|
387
|
+
* @cosyte/script-utils.
|
|
388
|
+
* @param {typeof runStryker} [opts.stryker] Starts Stryker; replaced in tests.
|
|
389
|
+
* @returns {Promise<number>} The exit code.
|
|
390
|
+
*/
|
|
391
|
+
export async function runMutate({
|
|
392
|
+
argv,
|
|
393
|
+
cwd,
|
|
394
|
+
env = process.env,
|
|
395
|
+
out,
|
|
396
|
+
err,
|
|
397
|
+
changes,
|
|
398
|
+
stryker = runStryker,
|
|
399
|
+
}) {
|
|
400
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
401
|
+
out(HELP);
|
|
402
|
+
return 0;
|
|
403
|
+
}
|
|
404
|
+
try {
|
|
405
|
+
const options = parseMutateArgs(argv);
|
|
406
|
+
const configFile = resolve(cwd, options.config);
|
|
407
|
+
const config = await loadConfig(configFile);
|
|
408
|
+
if (options.mode === "full") {
|
|
409
|
+
return await runFull({ cwd, env, out, stryker, configFile, config, name: options.config });
|
|
410
|
+
}
|
|
411
|
+
const readChanges = changes ?? (await loadChangeReader());
|
|
412
|
+
const change = readChanges({ base: options.base, cwd });
|
|
413
|
+
const files = selectChangedFiles({
|
|
414
|
+
changed: change.files.keys(),
|
|
415
|
+
root: change.root,
|
|
416
|
+
cwd,
|
|
417
|
+
patterns: config.mutate,
|
|
418
|
+
});
|
|
419
|
+
const report = changedReportPath(config);
|
|
420
|
+
rmSync(resolve(cwd, report), { force: true });
|
|
421
|
+
if (files.length === 0) {
|
|
422
|
+
mkdirSync(dirname(resolve(cwd, report)), { recursive: true });
|
|
423
|
+
writeFileSync(resolve(cwd, report), emptyReport(cwd));
|
|
424
|
+
out(
|
|
425
|
+
`cosyte-mutate: no changed source file against ${options.base}; nothing to mutate. Wrote an empty ${report}.\n`,
|
|
426
|
+
);
|
|
427
|
+
return 0;
|
|
428
|
+
}
|
|
429
|
+
out(`cosyte-mutate: mutating ${files.length} changed file(s) against ${options.base}:\n`);
|
|
430
|
+
for (const file of files) out(` ${file}\n`);
|
|
431
|
+
await stryker({
|
|
432
|
+
configFile,
|
|
433
|
+
mutate: files,
|
|
434
|
+
jsonReporter: { fileName: report },
|
|
435
|
+
htmlReporter: { fileName: htmlReportFor(report) },
|
|
436
|
+
});
|
|
437
|
+
return 0;
|
|
438
|
+
} catch (error) {
|
|
439
|
+
// The change reader's own refusal is known by name: its class lives in the optional peer.
|
|
440
|
+
const known = error instanceof MutateUnreadable || error?.name === "ChangesUnreadable";
|
|
441
|
+
err(`cosyte-mutate: could not run: ${known ? error.message : String(error?.stack ?? error)}\n`);
|
|
442
|
+
return 2;
|
|
443
|
+
}
|
|
444
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosyte/vitest-config",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Shared Vitest config (v8 coverage, per-directory >=90 gates) for @cosyte/* packages.",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Shared Vitest config (v8 coverage, per-directory >=90 gates) and StrykerJS mutation testing config for @cosyte/* packages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vitest",
|
|
7
7
|
"coverage",
|
|
8
8
|
"test",
|
|
9
9
|
"doctest",
|
|
10
10
|
"documentation",
|
|
11
|
-
"cosyte"
|
|
11
|
+
"cosyte",
|
|
12
|
+
"stryker",
|
|
13
|
+
"mutation-testing"
|
|
12
14
|
],
|
|
13
15
|
"homepage": "https://github.com/cosyte/config/tree/main/packages/vitest-config#readme",
|
|
14
16
|
"repository": {
|
|
@@ -21,6 +23,9 @@
|
|
|
21
23
|
"type": "module",
|
|
22
24
|
"main": "index.js",
|
|
23
25
|
"types": "index.d.ts",
|
|
26
|
+
"bin": {
|
|
27
|
+
"cosyte-mutate": "bin/cosyte-mutate.js"
|
|
28
|
+
},
|
|
24
29
|
"exports": {
|
|
25
30
|
".": {
|
|
26
31
|
"types": "./index.d.ts",
|
|
@@ -29,6 +34,14 @@
|
|
|
29
34
|
"./snippets": {
|
|
30
35
|
"types": "./snippets.d.ts",
|
|
31
36
|
"default": "./snippets.js"
|
|
37
|
+
},
|
|
38
|
+
"./stryker": {
|
|
39
|
+
"types": "./stryker.d.ts",
|
|
40
|
+
"default": "./stryker.js"
|
|
41
|
+
},
|
|
42
|
+
"./mutate": {
|
|
43
|
+
"types": "./mutate.d.ts",
|
|
44
|
+
"default": "./mutate.js"
|
|
32
45
|
}
|
|
33
46
|
},
|
|
34
47
|
"files": [
|
|
@@ -36,6 +49,11 @@
|
|
|
36
49
|
"index.d.ts",
|
|
37
50
|
"snippets.js",
|
|
38
51
|
"snippets.d.ts",
|
|
52
|
+
"stryker.js",
|
|
53
|
+
"stryker.d.ts",
|
|
54
|
+
"mutate.js",
|
|
55
|
+
"mutate.d.ts",
|
|
56
|
+
"bin",
|
|
39
57
|
"README.md",
|
|
40
58
|
"CHANGELOG.md"
|
|
41
59
|
],
|
|
@@ -43,8 +61,38 @@
|
|
|
43
61
|
"access": "public"
|
|
44
62
|
},
|
|
45
63
|
"peerDependencies": {
|
|
64
|
+
"@stryker-mutator/core": "^10.0.0",
|
|
65
|
+
"@stryker-mutator/vitest-runner": "^10.0.0",
|
|
46
66
|
"@vitest/coverage-v8": "^4.0.0 || ^5.0.0",
|
|
47
67
|
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0",
|
|
48
|
-
"vitest": "^4.0.0 || ^5.0.0"
|
|
68
|
+
"vitest": "^4.0.0 || ^5.0.0",
|
|
69
|
+
"typescript": ">=5.0.0",
|
|
70
|
+
"@cosyte/script-utils": "^0.1.10"
|
|
71
|
+
},
|
|
72
|
+
"peerDependenciesMeta": {
|
|
73
|
+
"@cosyte/script-utils": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@stryker-mutator/core": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@stryker-mutator/vitest-runner": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"typescript": {
|
|
83
|
+
"optional": true
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@stryker-mutator/core": "10.1.1",
|
|
88
|
+
"@stryker-mutator/vitest-runner": "10.1.1",
|
|
89
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
90
|
+
"vite": "8.3.6",
|
|
91
|
+
"vitest": "5.1.0",
|
|
92
|
+
"typescript": "5.9.3",
|
|
93
|
+
"@cosyte/script-utils": "^0.1.10"
|
|
94
|
+
},
|
|
95
|
+
"scripts": {
|
|
96
|
+
"test": "vitest run --coverage"
|
|
49
97
|
}
|
|
50
98
|
}
|
package/snippets.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
/** One extracted runnable snippet, in document order. */
|
|
11
11
|
export interface RunnableSnippet {
|
|
12
|
-
/** The fence language (`ts` / `typescript` / `tsx`). */
|
|
12
|
+
/** The fence language (`ts` / `typescript` / `tsx`; a sample may also be `js` / `javascript` / `mjs`). */
|
|
13
13
|
lang: string;
|
|
14
14
|
/** Fence info-string tokens after the language (e.g. `["runnable", "throws"]`). */
|
|
15
15
|
meta: string[];
|
|
@@ -19,6 +19,79 @@ export interface RunnableSnippet {
|
|
|
19
19
|
line: number;
|
|
20
20
|
/** Whether the block is tagged `throws` (expected to throw rather than run clean). */
|
|
21
21
|
throws: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The extension of the module {@link runSnippet} writes the snippet to. Absent, it is `.ts`, which
|
|
24
|
+
* is what every snippet {@link extractRunnableSnippets} returns runs as.
|
|
25
|
+
*/
|
|
26
|
+
extension?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One extracted code sample, in document order: a TypeScript or JavaScript block, tagged or not. */
|
|
30
|
+
export interface Sample extends RunnableSnippet {
|
|
31
|
+
/**
|
|
32
|
+
* The reason the block is not run: the words after its `no-run` tag (empty when the tag has no
|
|
33
|
+
* reason, which the suite reports as an error), or `null` for a block that runs.
|
|
34
|
+
*/
|
|
35
|
+
skip: string | null;
|
|
36
|
+
/**
|
|
37
|
+
* The reason the sample is run but not type-checked: the words after its `no-typecheck` tag
|
|
38
|
+
* (empty when the tag has no reason, which the suite reports as an error), or `null` otherwise.
|
|
39
|
+
* Of `no-run` and `no-typecheck`, the first in the fence counts, and what follows it is its reason.
|
|
40
|
+
*/
|
|
41
|
+
noTypecheck: string | null;
|
|
42
|
+
/** The module it runs as: `.mjs` for a `js`, `javascript` or `mjs` sample, `.tsx` for `tsx`, else `.ts`. */
|
|
43
|
+
extension: ".ts" | ".tsx" | ".mjs";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One type error in a sample. */
|
|
47
|
+
export interface SampleTypeError {
|
|
48
|
+
/** The markdown line it is on. */
|
|
49
|
+
line: number;
|
|
50
|
+
/** TypeScript's error code, as in `TS2322`. */
|
|
51
|
+
code: number;
|
|
52
|
+
/** TypeScript's message, on one line. */
|
|
53
|
+
message: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Options for {@link typecheckSamples}. */
|
|
57
|
+
export interface TypecheckSamplesOptions {
|
|
58
|
+
/** The `tsconfig.json` to compile under. Default: the one in the working directory, if any. */
|
|
59
|
+
tsconfig?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Where each sample is written to be compiled; it must sit inside the project, so the package's
|
|
62
|
+
* name resolves. Default `.cosyte-doc-snippets/typecheck` in the working directory.
|
|
63
|
+
*/
|
|
64
|
+
dir?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The fence languages that make a block a code sample: TypeScript and JavaScript. */
|
|
68
|
+
export declare const SAMPLE_LANGS: ReadonlySet<string>;
|
|
69
|
+
|
|
70
|
+
/** What a suite does with one sample. */
|
|
71
|
+
export interface SamplePlanStep {
|
|
72
|
+
/** The test name: the file, relative to the root, and the sample's first code line. */
|
|
73
|
+
label: string;
|
|
74
|
+
/** Run it, skip it with its reason, or report a `no-run` or `no-typecheck` that gives no reason. */
|
|
75
|
+
kind: "run" | "skip" | "invalid";
|
|
76
|
+
/**
|
|
77
|
+
* Why it is skipped or invalid; for a run, why it is not type-checked, or `null` when nothing is
|
|
78
|
+
* left out.
|
|
79
|
+
*/
|
|
80
|
+
reason: string | null;
|
|
81
|
+
/** Whether the sample is type-checked before it runs: a TypeScript sample, in every-sample mode. */
|
|
82
|
+
typecheck: boolean;
|
|
83
|
+
/** The sample. */
|
|
84
|
+
snippet: Sample;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Options for {@link planSamples}. */
|
|
88
|
+
export interface PlanSamplesOptions {
|
|
89
|
+
/** Take every sample rather than only the `runnable` ones. Default `false`. */
|
|
90
|
+
every?: boolean;
|
|
91
|
+
/** What labels are relative to. Default: the working directory. */
|
|
92
|
+
root?: string;
|
|
93
|
+
/** Fence opt-in tag when `every` is off. Default `"runnable"`. */
|
|
94
|
+
runnableTag?: string;
|
|
22
95
|
}
|
|
23
96
|
|
|
24
97
|
/** Options for {@link extractRunnableSnippets}. */
|
|
@@ -35,6 +108,8 @@ export interface RunSnippetOptions {
|
|
|
35
108
|
tmpDir?: string;
|
|
36
109
|
/** Force the expected-to-throw expectation even for a raw-string snippet. */
|
|
37
110
|
throws?: boolean;
|
|
111
|
+
/** The snippet's type errors, from {@link typecheckSamples}: when any, it fails before it runs. */
|
|
112
|
+
typeErrors?: SampleTypeError[];
|
|
38
113
|
}
|
|
39
114
|
|
|
40
115
|
/** Options for {@link docSnippetSuite}. */
|
|
@@ -55,8 +130,55 @@ export interface DocSnippetSuiteOptions {
|
|
|
55
130
|
requireSnippet?: boolean;
|
|
56
131
|
/** Where temp modules are written (must be inside the project root). */
|
|
57
132
|
tmpDir?: string;
|
|
133
|
+
/**
|
|
134
|
+
* Run every TypeScript and JavaScript sample, not only the `runnable` ones; a `no-run <reason>`
|
|
135
|
+
* sample is listed as skipped with its reason, and a `no-run` with no reason fails. Each
|
|
136
|
+
* TypeScript sample is type-checked before it runs, unless it carries `no-typecheck <reason>`.
|
|
137
|
+
* Walking `docsDir`, the generated `api/` folder is left out. Default `false`.
|
|
138
|
+
*/
|
|
139
|
+
every?: boolean;
|
|
140
|
+
/** In every-sample mode, walk the generated `api/` folder of `docsDir` too. Default `false`. */
|
|
141
|
+
includeGenerated?: boolean;
|
|
142
|
+
/** The `tsconfig.json` samples are type-checked under. Default: the one in the working directory. */
|
|
143
|
+
tsconfig?: string;
|
|
58
144
|
}
|
|
59
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Extract every code sample from a markdown string: each `ts`, `typescript`, `tsx`, `js`,
|
|
148
|
+
* `javascript` or `mjs` block, tagged or not, in document order.
|
|
149
|
+
*
|
|
150
|
+
* @param markdown - The markdown source.
|
|
151
|
+
* @returns One entry per sample.
|
|
152
|
+
*/
|
|
153
|
+
export declare function extractSamples(markdown: string): Sample[];
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Decide what a suite does with each sample in a set of markdown files.
|
|
157
|
+
*
|
|
158
|
+
* @param files - Markdown file paths.
|
|
159
|
+
* @param options - Options.
|
|
160
|
+
* @returns One step per sample, in file and document order.
|
|
161
|
+
*/
|
|
162
|
+
export declare function planSamples(
|
|
163
|
+
files: string[],
|
|
164
|
+
options?: PlanSamplesOptions,
|
|
165
|
+
): SamplePlanStep[];
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Type-check TypeScript samples against the package's types, each as its own module, under the
|
|
169
|
+
* project's `tsconfig.json` with emit and the unused-name checks off. Needs `typescript`.
|
|
170
|
+
*
|
|
171
|
+
* @param samples - The samples: their code, the markdown line of their first code line, and the
|
|
172
|
+
* extension they compile as (`.ts` when absent).
|
|
173
|
+
* @param options - Options.
|
|
174
|
+
* @returns For each sample, in order, its errors; empty for one that compiles.
|
|
175
|
+
* @throws When the config cannot be read or the program as a whole has an error.
|
|
176
|
+
*/
|
|
177
|
+
export declare function typecheckSamples(
|
|
178
|
+
samples: Array<Pick<RunnableSnippet, "code" | "line" | "extension">>,
|
|
179
|
+
options?: TypecheckSamplesOptions,
|
|
180
|
+
): Promise<SampleTypeError[][]>;
|
|
181
|
+
|
|
60
182
|
/**
|
|
61
183
|
* Extract the opt-in runnable snippets from a markdown string, in document order.
|
|
62
184
|
*
|