fix(test): reject malformed group report numeric flags

This commit is contained in:
Vincent Koc
2026-06-02 21:31:16 +02:00
parent 661c763b28
commit 28b1ea7c0d
2 changed files with 25 additions and 11 deletions

View File

@@ -11,6 +11,7 @@ import {
renderGroupedTestComparison,
renderGroupedTestReport,
} from "./lib/test-group-report.mjs";
import { parsePositiveInt } from "./lib/numeric-options.mjs";
import { formatMs } from "./lib/vitest-report-cli-utils.mjs";
import { resolveVitestNodeArgs } from "./run-vitest.mjs";
import {
@@ -55,11 +56,6 @@ function usage() {
].join("\n");
}
function parsePositiveInt(value, fallback) {
const parsed = Number.parseInt(value ?? "", 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
export function parseTestGroupReportArgs(argv) {
const args = {
allowFailures: false,
@@ -130,32 +126,32 @@ export function parseTestGroupReportArgs(argv) {
continue;
}
if (arg === "--limit") {
args.limit = parsePositiveInt(argv[index + 1], args.limit);
args.limit = parsePositiveInt(argv[index + 1], "--limit");
index += 1;
continue;
}
if (arg === "--max-test-ms") {
args.maxTestMs = parsePositiveInt(argv[index + 1], args.maxTestMs);
args.maxTestMs = parsePositiveInt(argv[index + 1], "--max-test-ms");
index += 1;
continue;
}
if (arg === "--timeout-ms") {
args.timeoutMs = parsePositiveInt(argv[index + 1], args.timeoutMs);
args.timeoutMs = parsePositiveInt(argv[index + 1], "--timeout-ms");
index += 1;
continue;
}
if (arg === "--kill-grace-ms") {
args.killGraceMs = parsePositiveInt(argv[index + 1], args.killGraceMs);
args.killGraceMs = parsePositiveInt(argv[index + 1], "--kill-grace-ms");
index += 1;
continue;
}
if (arg === "--concurrency") {
args.concurrency = parsePositiveInt(argv[index + 1], args.concurrency);
args.concurrency = parsePositiveInt(argv[index + 1], "--concurrency");
index += 1;
continue;
}
if (arg === "--top-files") {
args.topFiles = parsePositiveInt(argv[index + 1], args.topFiles);
args.topFiles = parsePositiveInt(argv[index + 1], "--top-files");
index += 1;
continue;
}

View File

@@ -319,6 +319,24 @@ describe("scripts/test-group-report arg parsing", () => {
timeoutMs: 5000,
});
});
it("rejects malformed positive integer flags", () => {
for (const flag of [
"--limit",
"--top-files",
"--max-test-ms",
"--timeout-ms",
"--kill-grace-ms",
"--concurrency",
]) {
expect(() => parseTestGroupReportArgs([flag, "20x"])).toThrow(
`${flag} must be a positive integer`,
);
expect(() => parseTestGroupReportArgs([flag, "0"])).toThrow(
`${flag} must be a positive integer`,
);
}
});
});
describe("scripts/test-group-report child process guard", () => {