Skip to content
19 changes: 3 additions & 16 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
"useTemplate": "error"
},
"suspicious": {
"noAssignInExpressions": "off",
"noAssignInExpressions": "error",
"noConsole": "error",
"noDoubleEquals": "error",
"noExplicitAny": "off",
"noFocusedTests": "error",
"noImplicitAnyLet": "off",
"noImplicitAnyLet": "error",
"noSkippedTests": "error",
"noVar": "error"
}
Expand All @@ -92,14 +92,6 @@
"includes": ["test/**/*.js", "test/**/*.ts", "test/**/*.tsx"],
"linter": {
"rules": {
"complexity": {
"noExcessiveCognitiveComplexity": {
"level": "error",
"options": {
"maxAllowedComplexity": 30
}
}
},
"correctness": {
"noUnusedFunctionParameters": "off",
"noUnusedVariables": "off"
Expand Down Expand Up @@ -129,13 +121,8 @@
"includes": ["scripts/**/*.ts"],
"linter": {
"rules": {
"complexity": {
"noExcessiveCognitiveComplexity": "off"
},
"suspicious": {
"noAssignInExpressions": "off",
"noConsole": "off",
"noImplicitAnyLet": "off"
"noConsole": "off"
Comment thread
stefan-burke marked this conversation as resolved.
Resolved
}
}
}
Expand Down
61 changes: 38 additions & 23 deletions scripts/compact-test-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,38 +124,53 @@ const readYamlBlockScalar = (
return stripCommonIndent(block).trimEnd();
};

const parseYamlTapDiagnostic = (text: string): TapDiagnostic | undefined => {
const lines = text.split(/\r?\n/);
const diagnostic: TapDiagnostic = {};

const parseYamlMessage = (lines: string[]): string | undefined => {
for (let index = 0; index < lines.length; index++) {
const match = lines[index]?.match(/^(\s*)message:\s*(.*)$/);
if (!match) continue;
const value = match[2]?.trim() ?? "";
const baseIndent = (match[1] ?? "").length;
diagnostic.message =
value.startsWith("|") || value.startsWith(">")
? readYamlBlockScalar(lines, index, baseIndent)
: parseYamlScalar(value);
break;
return value.startsWith("|") || value.startsWith(">")
? readYamlBlockScalar(lines, index, baseIndent)
: parseYamlScalar(value);
}
return undefined;
};

const assignAtField = (
at: NonNullable<TapDiagnostic["at"]>,
key: string,
value: string,
): void => {
if (key === "file") at.file = value;
if (key === "line") at.line = Number(value);
if (key === "column") at.column = Number(value);
};

const parseYamlAt = (lines: string[]): TapDiagnostic["at"] | undefined => {
const atIndex = lines.findIndex((line) => /^(\s*)at:\s*$/.test(line));
if (atIndex !== -1) {
const atIndent = leadingWhitespaceLength(lines[atIndex] ?? "");
const at: TapDiagnostic["at"] = {};
for (const line of lines.slice(atIndex + 1)) {
if (line.trim() && leadingWhitespaceLength(line) <= atIndent) break;
const match = line.match(/^\s*(file|line|column):\s*(.*)$/);
if (!match) continue;
const key = match[1];
const value = parseYamlScalar(match[2] ?? "");
if (key === "file") at.file = value;
if (key === "line") at.line = Number(value);
if (key === "column") at.column = Number(value);
}
diagnostic.at = at;
if (atIndex === -1) return undefined;

const atIndent = leadingWhitespaceLength(lines[atIndex] ?? "");
const at: NonNullable<TapDiagnostic["at"]> = {};
for (const line of lines.slice(atIndex + 1)) {
if (line.trim() && leadingWhitespaceLength(line) <= atIndent) break;
const match = line.match(/^\s*(file|line|column):\s*(.*)$/);
if (!match) continue;
assignAtField(at, match[1] ?? "", parseYamlScalar(match[2] ?? ""));
}
return at;
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

const parseYamlTapDiagnostic = (text: string): TapDiagnostic | undefined => {
const lines = text.split(/\r?\n/);
const diagnostic: TapDiagnostic = {};

const message = parseYamlMessage(lines);
if (message !== undefined) diagnostic.message = message;

const at = parseYamlAt(lines);
if (at !== undefined) diagnostic.at = at;

return diagnostic.message || diagnostic.at ? diagnostic : undefined;
};
Expand Down
124 changes: 78 additions & 46 deletions scripts/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,44 @@ interface ParsedArgs {
useHarness: boolean;
}

const parseArgs = (args: string[]): ParsedArgs => {
const parsed: ParsedArgs = {
error: null,
exhaustive: false,
help: false,
sources: [],
tests: [],
timeout: DEFAULT_TIMEOUT,
useHarness: false,
};
const positional: string[] = [];
let index = 0;
while (index < args.length) {
const arg = args[index];
const next = args[index + 1];
if (arg === "--exhaustive") parsed.exhaustive = true;
else if (arg === "--harness") parsed.useHarness = true;
else if (arg === "-h" || arg === "--help") parsed.help = true;
else if (arg === "--source" && next !== undefined) {
parsed.sources.push(next);
index += 1;
} else if (arg === "--test" && next !== undefined) {
parsed.tests.push(next);
index += 1;
} else if (arg === "--timeout" && next !== undefined) {
parsed.timeout = Number(next);
index += 1;
} else if (arg === "--jobs" && next !== undefined) {
parsed.batchJobs = Number(next);
index += 1;
} else if (arg !== undefined) positional.push(arg);
index += 1;
}
if (parsed.sources.length > 0 || parsed.tests.length > 0) {
type ValueFlagApply = (parsed: ParsedArgs, value: string) => void;

/** Options that take a value; maps the flag to how it records `next` on `parsed`. */
const VALUE_FLAGS: Record<string, ValueFlagApply | undefined> = {
"--jobs": (parsed, value) => {
parsed.batchJobs = Number(value);
},
"--source": (parsed, value) => parsed.sources.push(value),
"--test": (parsed, value) => parsed.tests.push(value),
"--timeout": (parsed, value) => {
parsed.timeout = Number(value);
},
};

/**
* Apply a single argument to `parsed` (or collect it as positional). Returns
* true when it also consumed `next` as the flag's value.
*/
const applyArg = (
parsed: ParsedArgs,
positional: string[],
arg: string,
next: string | undefined,
): boolean => {
if (arg === "--exhaustive") parsed.exhaustive = true;
else if (arg === "--harness") parsed.useHarness = true;
else if (arg === "-h" || arg === "--help") parsed.help = true;
else if (VALUE_FLAGS[arg] !== undefined && next !== undefined) {
VALUE_FLAGS[arg]?.(parsed, next);
return true;
} else positional.push(arg);
return false;
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/** Fold positional arguments into `parsed`, recording an error on misuse. */
const applyPositionals = (parsed: ParsedArgs, positional: string[]): void => {
const usedFlagForm = parsed.sources.length > 0 || parsed.tests.length > 0;
if (usedFlagForm) {
// Flag-form was used: positionals are not part of the grammar. Any leftover
// means a glob expanded past the single value --source/--test consumed
// (e.g. `--source src/*.ts` → src/a.ts src/b.ts …), which would silently
Expand All @@ -105,26 +109,54 @@ const parseArgs = (args: string[]): ParsedArgs => {
"A glob likely expanded to multiple files — quote it " +
`(e.g. --source 'src/lib/forms/*.ts') or pass repeated --source/--test flags.`;
}
} else {
if (positional[0] !== undefined) parsed.sources.push(positional[0]);
if (positional[1] !== undefined) parsed.tests.push(positional[1]);
if (positional.length > 2) {
parsed.error =
`Too many positional arguments (${positional.length}). Quote your globs ` +
`so the shell can't expand them — e.g. 'src/lib/forms/*.ts' ` +
`'test/lib/forms/*.test.ts' — or pass repeated --source/--test flags.`;
}
return;
}
if (positional[0] !== undefined) parsed.sources.push(positional[0]);
if (positional[1] !== undefined) parsed.tests.push(positional[1]);
if (positional.length > 2) {
parsed.error =
`Too many positional arguments (${positional.length}). Quote your globs ` +
`so the shell can't expand them — e.g. 'src/lib/forms/*.ts' ` +
`'test/lib/forms/*.test.ts' — or pass repeated --source/--test flags.`;
}
};

/** Validate the parsed numeric options, recording the first error found. */
const validateNumericArgs = (parsed: ParsedArgs): void => {
if (!Number.isFinite(parsed.timeout) || parsed.timeout < 0) {
parsed.error ??=
"Invalid --timeout: expected a non-negative number of milliseconds.";
}
if (
const invalidJobs =
parsed.batchJobs !== undefined &&
(!Number.isInteger(parsed.batchJobs) || parsed.batchJobs <= 0)
) {
(!Number.isInteger(parsed.batchJobs) || parsed.batchJobs <= 0);
if (invalidJobs) {
parsed.error ??= "Invalid --jobs: expected a positive integer.";
}
};

const parseArgs = (args: string[]): ParsedArgs => {
const parsed: ParsedArgs = {
error: null,
exhaustive: false,
help: false,
sources: [],
tests: [],
timeout: DEFAULT_TIMEOUT,
useHarness: false,
};
const positional: string[] = [];
let index = 0;
while (index < args.length) {
const arg = args[index];
if (arg !== undefined) {
const consumedNext = applyArg(parsed, positional, arg, args[index + 1]);
if (consumedNext) index += 1;
}
index += 1;
}
applyPositionals(parsed, positional);
validateNumericArgs(parsed);
return parsed;
};

Expand Down
42 changes: 25 additions & 17 deletions scripts/mutation/isolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ const childArgs = (
...rewriteMutationArgs(root, snapshotRoot, args),
];

const forceStopChild = (child: Deno.ChildProcess | null): never => {
if (child) stopProcessNow(child);
Deno.exit(130);
};

const killChildQuietly = (child: Deno.ChildProcess | null): void => {
if (!child) return;
try {
child.kill();
} catch {
// It may already have exited.
}
};

const settleRecord = (
record: MutationRunRecord,
interrupted: boolean,
code: number,
): MutationRunRecord =>
interrupted ? markInterrupted(record) : markFinished(record, code);

export const runMutationInSnapshot = async (
args: string[],
root = projectRoot,
Expand All @@ -160,18 +181,9 @@ export const runMutationInSnapshot = async (
let child: Deno.ChildProcess | null = null;
let interrupted = false;
const stopChild = (): void => {
if (interrupted) {
if (child) stopProcessNow(child);
Deno.exit(130);
}
if (interrupted) forceStopChild(child);
interrupted = true;
if (child) {
try {
child.kill();
} catch {
// It may already have exited.
}
}
killChildQuietly(child);
};
onTerminationSignals(stopChild);

Expand Down Expand Up @@ -205,18 +217,14 @@ export const runMutationInSnapshot = async (

if (child !== null) {
const status = await child.status;
record = interrupted
? markInterrupted(record)
: markFinished(record, status.code);
record = settleRecord(record, interrupted, status.code);
await writeRunRecord(record);
exitCode = interrupted ? 130 : status.code;
}
} catch (error) {
if (child !== null) await stopProcess(child, 250);
exitCode = interrupted ? 130 : 1;
record = interrupted
? markInterrupted(record)
: markFinished(record, exitCode);
record = settleRecord(record, interrupted, exitCode);
try {
await writeRunRecord(record);
} catch {
Expand Down
Loading