From 28b1ea7c0d6e2dcf71256e2afd6971aa35065cce Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 2 Jun 2026 21:31:16 +0200 Subject: [PATCH] fix(test): reject malformed group report numeric flags --- scripts/test-group-report.mjs | 18 +++++++----------- test/scripts/test-group-report.test.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/scripts/test-group-report.mjs b/scripts/test-group-report.mjs index 67a8c36153a2..1b4996528221 100644 --- a/scripts/test-group-report.mjs +++ b/scripts/test-group-report.mjs @@ -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; } diff --git a/test/scripts/test-group-report.test.ts b/test/scripts/test-group-report.test.ts index 270e8ffc83a1..82da1ea1269b 100644 --- a/test/scripts/test-group-report.test.ts +++ b/test/scripts/test-group-report.test.ts @@ -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", () => {