@mizchi/mutator-bun 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/dist/config.d.ts +19 -0
- package/dist/config.js +103 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/junit.d.ts +16 -0
- package/dist/junit.js +81 -0
- package/dist/junit.js.map +1 -0
- package/dist/session.d.ts +34 -0
- package/dist/session.js +471 -0
- package/dist/session.js.map +1 -0
- package/package.json +38 -3
- package/src/config.ts +110 -0
- package/src/index.ts +2 -0
- package/src/junit.ts +88 -0
- package/src/preload.js +153 -0
- package/src/session.ts +541 -0
- package/README.md +0 -3
package/src/config.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// What the session needs from the project's Bun setup: the bunfig.toml (`[test]` root,
|
|
2
|
+
// preload and pathIgnorePatterns), and the test files `bun test` would collect. Bun has no
|
|
3
|
+
// list mode, so the files are found with Bun's own rules: `*.test.*`, `*_test.*`, `*.spec.*`
|
|
4
|
+
// and `*_spec.*` with a JavaScript / TypeScript extension, outside node_modules and hidden
|
|
5
|
+
// directories.
|
|
6
|
+
import { existsSync, globSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { dirname, join, matchesGlob, relative, resolve } from 'node:path';
|
|
8
|
+
|
|
9
|
+
export interface Bunfig {
|
|
10
|
+
/** The bunfig.toml in effect, if any (absolute). */
|
|
11
|
+
path: string | undefined;
|
|
12
|
+
/** `[test] root`: where Bun looks for test files (absolute). */
|
|
13
|
+
testRoot: string | undefined;
|
|
14
|
+
/** Preload modules, from `preload` and `[test] preload` (absolute). */
|
|
15
|
+
preload: string[];
|
|
16
|
+
/** `[test] pathIgnorePatterns`: globs of test files Bun leaves out, relative to the project root. */
|
|
17
|
+
ignore: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Reads the keys the session uses from bunfig.toml: an explicit file, else `<root>/bunfig.toml`. */
|
|
21
|
+
export function readBunfig(root: string, configFile?: string): Bunfig {
|
|
22
|
+
const path = configFile ? resolve(root, configFile) : join(root, 'bunfig.toml');
|
|
23
|
+
if (!existsSync(path)) {
|
|
24
|
+
if (configFile) throw new Error(`mutator: bun config ${path} does not exist`);
|
|
25
|
+
return { path: undefined, testRoot: undefined, preload: [], ignore: [] };
|
|
26
|
+
}
|
|
27
|
+
const values = tomlValues(readFileSync(path, 'utf8'));
|
|
28
|
+
const base = dirname(path);
|
|
29
|
+
const testRoot = values.get('test.root')?.[0];
|
|
30
|
+
const preload = [...(values.get('preload') ?? []), ...(values.get('test.preload') ?? [])];
|
|
31
|
+
return {
|
|
32
|
+
path,
|
|
33
|
+
testRoot: testRoot === undefined ? undefined : resolve(base, testRoot),
|
|
34
|
+
// Package names (no leading `.` or `/`) are left out: they are not project files.
|
|
35
|
+
preload: preload.filter((p) => /^(?:\.{1,2}[\\/]|[\\/]|[A-Za-z]:[\\/])/.test(p)).map((p) => resolve(base, p)),
|
|
36
|
+
ignore: values.get('test.pathIgnorePatterns') ?? [],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The string values of `key = "..."` and `key = ["...", ...]` entries, by `table.key` (top-level
|
|
42
|
+
* keys without a table). Enough for the keys read here, not a TOML parser.
|
|
43
|
+
*/
|
|
44
|
+
export function tomlValues(text: string): Map<string, string[]> {
|
|
45
|
+
const values = new Map<string, string[]>();
|
|
46
|
+
let table = '';
|
|
47
|
+
let pending: { key: string; text: string } | undefined;
|
|
48
|
+
const STRING = /"((?:[^"\\]|\\.)*)"|'([^']*)'/g;
|
|
49
|
+
const strings = (s: string) => [...s.matchAll(STRING)].map((m) => m[2] ?? decodeEscapes(m[1]!));
|
|
50
|
+
// A `]` inside a string (a glob like `[ab]`) does not close the array.
|
|
51
|
+
const closes = (s: string) => s.replace(STRING, '').includes(']');
|
|
52
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
53
|
+
const line = stripComment(raw).trim();
|
|
54
|
+
if (pending) {
|
|
55
|
+
pending.text += ` ${line}`;
|
|
56
|
+
if (closes(line)) {
|
|
57
|
+
values.set(pending.key, strings(pending.text));
|
|
58
|
+
pending = undefined;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const header = /^\[\s*([^\]]+?)\s*\]$/.exec(line);
|
|
63
|
+
if (header) {
|
|
64
|
+
table = header[1]!;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const entry = /^([\w.-]+)\s*=\s*(.*)$/.exec(line);
|
|
68
|
+
if (!entry) continue;
|
|
69
|
+
const key = table ? `${table}.${entry[1]}` : entry[1]!;
|
|
70
|
+
const value = entry[2]!;
|
|
71
|
+
if (value.startsWith('[') && !closes(value)) pending = { key, text: value };
|
|
72
|
+
else values.set(key, strings(value));
|
|
73
|
+
}
|
|
74
|
+
return values;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const ESCAPES: Record<string, string> = { b: '\b', t: '\t', n: '\n', f: '\f', r: '\r', e: '\x1b', '"': '"', '\\': '\\' };
|
|
78
|
+
|
|
79
|
+
/** A basic string's escapes decoded: TOML's \uXXXX, \UXXXXXXXX and \xXX too. An invalid one is kept as written. */
|
|
80
|
+
function decodeEscapes(s: string): string {
|
|
81
|
+
return s.replace(/\\(?:u([0-9A-Fa-f]{4})|U([0-9A-Fa-f]{8})|x([0-9A-Fa-f]{2})|(.))/g, (escape, u?: string, U?: string, x?: string, c?: string) => {
|
|
82
|
+
const hex = u ?? U ?? x;
|
|
83
|
+
if (hex === undefined) return ESCAPES[c!] ?? escape;
|
|
84
|
+
const code = Number.parseInt(hex, 16);
|
|
85
|
+
return code <= 0x10ffff ? String.fromCodePoint(code) : escape;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function stripComment(line: string): string {
|
|
90
|
+
let quote: string | undefined;
|
|
91
|
+
for (let i = 0; i < line.length; i++) {
|
|
92
|
+
const c = line[i]!;
|
|
93
|
+
if (quote) {
|
|
94
|
+
if (c === '\\' && quote === '"') i++;
|
|
95
|
+
else if (c === quote) quote = undefined;
|
|
96
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
97
|
+
else if (c === '#') return line.slice(0, i);
|
|
98
|
+
}
|
|
99
|
+
return line;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const TEST_FILES = '**/*{.test,_test,.spec,_spec}.{ts,tsx,mts,cts,js,jsx,mjs,cjs}';
|
|
103
|
+
|
|
104
|
+
/** The test files `bun test` collects under `dir` (absolute, sorted); `ignore` globs are relative to `root`. */
|
|
105
|
+
export function listTestFiles(dir: string, root = dir, ignore: readonly string[] = []): string[] {
|
|
106
|
+
return globSync(TEST_FILES, { cwd: dir, exclude: ['**/node_modules/**', '**/.*/**'] })
|
|
107
|
+
.map((file) => join(dir, file))
|
|
108
|
+
.filter((file) => !ignore.some((pattern) => matchesGlob(relative(root, file), pattern)))
|
|
109
|
+
.sort();
|
|
110
|
+
}
|
package/src/index.ts
ADDED
package/src/junit.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Reads the JUnit XML `bun test --reporter=junit` writes. Only the subset Bun emits is
|
|
2
|
+
// understood: nested <testsuite> elements (a file, then its describe blocks) holding
|
|
3
|
+
// <testcase> elements with optional <failure>, <error> and <skipped> children.
|
|
4
|
+
|
|
5
|
+
export interface JunitTest {
|
|
6
|
+
/** Describe blocks and test name joined by spaces: what `--test-name-pattern` matches. */
|
|
7
|
+
fullName: string;
|
|
8
|
+
state: 'passed' | 'failed' | 'skipped';
|
|
9
|
+
durationMs: number;
|
|
10
|
+
/** Failure and error messages. */
|
|
11
|
+
messages: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface JunitFile {
|
|
15
|
+
/** The test file as Bun names it (relative to the working directory). */
|
|
16
|
+
file: string;
|
|
17
|
+
/** In report order, which is declaration order. */
|
|
18
|
+
tests: JunitTest[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const TAG = /<(\/?)([A-Za-z][\w:-]*)((?:\s+[\w:-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>|<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!\[CDATA\[([\s\S]*?)\]\]>/g;
|
|
22
|
+
const ATTRIBUTE = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
23
|
+
const ENTITIES: Record<string, string> = { lt: '<', gt: '>', amp: '&', quot: '"', apos: "'" };
|
|
24
|
+
|
|
25
|
+
export function decodeEntities(text: string): string {
|
|
26
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#\d+|\w+);/g, (whole, entity: string) => {
|
|
27
|
+
if (entity.startsWith('#x')) return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
|
|
28
|
+
if (entity.startsWith('#')) return String.fromCodePoint(Number.parseInt(entity.slice(1), 10));
|
|
29
|
+
return ENTITIES[entity] ?? whole;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function attributes(source: string): Record<string, string> {
|
|
34
|
+
const out: Record<string, string> = {};
|
|
35
|
+
for (const m of source.matchAll(ATTRIBUTE)) out[m[1]!] = decodeEntities(m[2] ?? m[3] ?? '');
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseJunit(xml: string): JunitFile[] {
|
|
40
|
+
const files: JunitFile[] = [];
|
|
41
|
+
// Open <testsuite> names, outermost (the file) first.
|
|
42
|
+
const suites: string[] = [];
|
|
43
|
+
let file: JunitFile | undefined;
|
|
44
|
+
let test: JunitTest | undefined;
|
|
45
|
+
// The <failure> / <error> being read: its message attribute, then its text.
|
|
46
|
+
let failure: { message: string; text: string } | undefined;
|
|
47
|
+
let last = 0;
|
|
48
|
+
const close = (name: string) => {
|
|
49
|
+
if (name === 'testsuite') {
|
|
50
|
+
suites.pop();
|
|
51
|
+
if (suites.length === 0) file = undefined;
|
|
52
|
+
} else if (name === 'testcase') test = undefined;
|
|
53
|
+
else if ((name === 'failure' || name === 'error') && failure) {
|
|
54
|
+
test?.messages.push(failure.text.trim() || failure.message);
|
|
55
|
+
failure = undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
for (const m of xml.matchAll(TAG)) {
|
|
59
|
+
if (failure) failure.text += decodeEntities(xml.slice(last, m.index)) + (m[5] ?? '');
|
|
60
|
+
last = m.index + m[0].length;
|
|
61
|
+
const [, closing, name, attrs = '', selfClosing] = m;
|
|
62
|
+
if (!name) continue;
|
|
63
|
+
if (closing) {
|
|
64
|
+
close(name);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const a = attributes(attrs);
|
|
68
|
+
if (name === 'testsuite') {
|
|
69
|
+
if (suites.length === 0) {
|
|
70
|
+
file = { file: a.file ?? a.name ?? '', tests: [] };
|
|
71
|
+
files.push(file);
|
|
72
|
+
}
|
|
73
|
+
suites.push(a.name ?? '');
|
|
74
|
+
} else if (name === 'testcase' && file) {
|
|
75
|
+
const fullName = [...suites.slice(1), a.name ?? ''].join(' ');
|
|
76
|
+
const seconds = Number(a.time);
|
|
77
|
+
test = { fullName, state: 'passed', durationMs: Number.isFinite(seconds) ? seconds * 1000 : 0, messages: [] };
|
|
78
|
+
file.tests.push(test);
|
|
79
|
+
} else if (name === 'skipped' && test) {
|
|
80
|
+
test.state = 'skipped';
|
|
81
|
+
} else if ((name === 'failure' || name === 'error') && test) {
|
|
82
|
+
test.state = 'failed';
|
|
83
|
+
failure = { message: a.message ?? '', text: '' };
|
|
84
|
+
}
|
|
85
|
+
if (selfClosing) close(name);
|
|
86
|
+
}
|
|
87
|
+
return files;
|
|
88
|
+
}
|
package/src/preload.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Preloaded by `bun test --isolate --preload` in front of every test file, each in a fresh
|
|
2
|
+
// global: installs the runtime state, serves the session's instrumented sources through a
|
|
3
|
+
// runtime plugin, attributes mutant hits to the running test, and writes the file's record
|
|
4
|
+
// for the session to read. Plain JavaScript, because Bun loads it as is from published builds.
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { createRequire, syncBuiltinESMExports } from 'node:module';
|
|
8
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import { afterAll, afterEach, beforeEach } from 'bun:test';
|
|
11
|
+
|
|
12
|
+
const env = process.env;
|
|
13
|
+
const limit = Number(env.MUTATOR_HIT_LIMIT);
|
|
14
|
+
const active = env.MUTATOR_ACTIVE || null;
|
|
15
|
+
|
|
16
|
+
// Core's RUNTIME_GLOBAL and RuntimeState, written out (test/preload.test.ts keeps them equal).
|
|
17
|
+
// Set before any test file or source module loads, so static mutants see the active key.
|
|
18
|
+
const ns = {
|
|
19
|
+
active,
|
|
20
|
+
// Coverage is only needed by the dry run; mutant runs skip the counters entirely.
|
|
21
|
+
collect: active === null,
|
|
22
|
+
modules: [],
|
|
23
|
+
cov: { static: {}, perTest: {} },
|
|
24
|
+
inf: { static: {}, perTest: {} },
|
|
25
|
+
testId: null,
|
|
26
|
+
hits: 0,
|
|
27
|
+
hitLimit: Number.isFinite(limit) && limit > 0 ? limit : Infinity,
|
|
28
|
+
};
|
|
29
|
+
globalThis.__mutator__ = ns;
|
|
30
|
+
|
|
31
|
+
// The session instruments the targets once and lists them in a manifest: `files` as
|
|
32
|
+
// [absolute source path, instrumented code path, Bun loader], and `filter`, the regex source
|
|
33
|
+
// matching those paths with either separator.
|
|
34
|
+
const manifest = env.MUTATOR_BUN_MANIFEST ? JSON.parse(readFileSync(env.MUTATOR_BUN_MANIFEST, 'utf8')) : { files: [] };
|
|
35
|
+
const served = new Map(manifest.files.map(([file, code, loader]) => [file, [code, loader]]));
|
|
36
|
+
if (served.size > 0) {
|
|
37
|
+
const filter = new RegExp(manifest.filter);
|
|
38
|
+
const lookup = (path) => served.get(path) ?? served.get(path.replaceAll('/', '\\')) ?? served.get(path.replaceAll('\\', '/'));
|
|
39
|
+
Bun.plugin({
|
|
40
|
+
name: 'mutator:instrument',
|
|
41
|
+
setup(build) {
|
|
42
|
+
// Synchronous: `require()` of a module whose onLoad returns a promise is unsupported.
|
|
43
|
+
build.onLoad({ filter }, (args) => {
|
|
44
|
+
const [code, loader] = lookup(args.path);
|
|
45
|
+
return { contents: readFileSync(code, 'utf8'), loader };
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Bun's hooks get no test context, so tests are numbered in the order they start: the
|
|
52
|
+
// session pairs the numbers with the tests its JUnit report lists, in declaration order.
|
|
53
|
+
// Overlapping tests (test.concurrent) or a count that does not match the report make the
|
|
54
|
+
// session credit the file's hits to every test of the file instead.
|
|
55
|
+
let started = 0;
|
|
56
|
+
let running = 0;
|
|
57
|
+
let overlap = false;
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
if (running > 0) overlap = true;
|
|
61
|
+
running++;
|
|
62
|
+
ns.testId = String(started++);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
running = Math.max(0, running - 1);
|
|
67
|
+
if (running === 0) ns.testId = null;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Dry run only: record the processes each test starts (mutants applied in memory are not
|
|
71
|
+
// seen there). node:child_process is implemented on Bun.spawn, so a call is recorded once.
|
|
72
|
+
const spawns = {};
|
|
73
|
+
if (ns.collect) {
|
|
74
|
+
const ORIGINAL = Symbol.for('mutator.original');
|
|
75
|
+
let inside = 0;
|
|
76
|
+
const record = (argv, cwd) => {
|
|
77
|
+
if (ns.testId === null) return;
|
|
78
|
+
(spawns[ns.testId] ??= []).push({ argv: argv.map(String), cwd: resolve(cwd instanceof URL ? fileURLToPath(cwd) : typeof cwd === 'string' ? cwd : process.cwd()) });
|
|
79
|
+
};
|
|
80
|
+
const childProcess = createRequire(import.meta.url)('node:child_process');
|
|
81
|
+
for (const name of ['spawn', 'spawnSync', 'exec', 'execSync', 'execFile', 'execFileSync', 'fork']) {
|
|
82
|
+
const original = childProcess[name][ORIGINAL] ?? childProcess[name];
|
|
83
|
+
const wrap = (fn) =>
|
|
84
|
+
function (...args) {
|
|
85
|
+
try {
|
|
86
|
+
const [command, second, third] = args;
|
|
87
|
+
const shell = name === 'exec' || name === 'execSync';
|
|
88
|
+
const list = !shell && Array.isArray(second) ? second : [];
|
|
89
|
+
const options = shell || !Array.isArray(second) ? second : third;
|
|
90
|
+
const argv = shell ? String(command).trim().split(/\s+/) : [...(name === 'fork' ? ['node'] : []), command, ...list];
|
|
91
|
+
record(argv, options && typeof options === 'object' ? options.cwd : undefined);
|
|
92
|
+
} catch {
|
|
93
|
+
// Recording must never break the test.
|
|
94
|
+
}
|
|
95
|
+
inside++;
|
|
96
|
+
try {
|
|
97
|
+
return fn.apply(this, args);
|
|
98
|
+
} finally {
|
|
99
|
+
inside--;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const wrapped = wrap(original);
|
|
103
|
+
wrapped[ORIGINAL] = original;
|
|
104
|
+
// util.promisify.custom (exec, execFile)
|
|
105
|
+
for (const key of Object.getOwnPropertySymbols(original)) wrapped[key] = typeof original[key] === 'function' ? wrap(original[key]) : original[key];
|
|
106
|
+
childProcess[name] = wrapped;
|
|
107
|
+
}
|
|
108
|
+
// ESM named imports of node:child_process see the wrappers too.
|
|
109
|
+
syncBuiltinESMExports();
|
|
110
|
+
for (const name of ['spawn', 'spawnSync']) {
|
|
111
|
+
const original = Bun[name][ORIGINAL] ?? Bun[name];
|
|
112
|
+
const wrapped = function (...args) {
|
|
113
|
+
if (inside === 0) {
|
|
114
|
+
try {
|
|
115
|
+
const [first, second] = args;
|
|
116
|
+
const cmd = Array.isArray(first) ? first : first?.cmd;
|
|
117
|
+
const options = Array.isArray(first) ? second : first;
|
|
118
|
+
if (Array.isArray(cmd)) record(cmd, options?.cwd);
|
|
119
|
+
} catch {
|
|
120
|
+
// Recording must never break the test.
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return original.apply(this, args);
|
|
124
|
+
};
|
|
125
|
+
wrapped[ORIGINAL] = original;
|
|
126
|
+
Bun[name] = wrapped;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Runs once the file's tests are done, also when the file failed to load (it then has no
|
|
131
|
+
// tests and is missing from the module registry). Not after --bail stopped the run.
|
|
132
|
+
afterAll(() => {
|
|
133
|
+
const dir = env.MUTATOR_BUN_OUT;
|
|
134
|
+
if (!dir) return;
|
|
135
|
+
const file = Bun.main;
|
|
136
|
+
const registry = createRequire(import.meta.url).cache;
|
|
137
|
+
const record = { file, loaded: registry[file] !== undefined, tests: started, overlap, hitLimit: ns.hits > ns.hitLimit };
|
|
138
|
+
if (ns.collect) {
|
|
139
|
+
// Hits per mutant summed over the modules this file loaded (sizes hit limits).
|
|
140
|
+
const counts = {};
|
|
141
|
+
for (const [keys, n] of ns.modules) for (let i = 0; i < keys.length; i++) if (n[i]) counts[keys[i]] = (counts[keys[i]] ?? 0) + n[i];
|
|
142
|
+
Object.assign(record, {
|
|
143
|
+
static: ns.cov.static,
|
|
144
|
+
perTest: ns.cov.perTest,
|
|
145
|
+
infected: ns.inf.perTest,
|
|
146
|
+
counts,
|
|
147
|
+
spawns,
|
|
148
|
+
// Local modules: the session reads them for test dependencies.
|
|
149
|
+
modules: Object.keys(registry).filter((m) => isAbsolute(m) && !/[\\/]node_modules[\\/]/.test(m)),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
writeFileSync(join(dir, `${createHash('sha256').update(file).digest('hex').slice(0, 32)}.json`), JSON.stringify(record));
|
|
153
|
+
});
|