diff --git a/scripts/kova-ci-summary.mjs b/scripts/kova-ci-summary.mjs index b7713d4f2e2..2af9d039fae 100644 --- a/scripts/kova-ci-summary.mjs +++ b/scripts/kova-ci-summary.mjs @@ -3,8 +3,9 @@ import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; +const knownArgKeys = new Set(["report", "output", "lane", "reporturl", "artifacturl"]); const rawArgs = process.argv.slice(2); -if (rawArgs.includes("--help") || rawArgs.includes("-h")) { +if (shouldPrintHelp(rawArgs)) { usage("", 0); } @@ -245,18 +246,17 @@ function value(input) { function parseArgs(argv) { const parsed = {}; - const knownKeys = new Set(["report", "output", "lane", "reporturl", "artifacturl"]); for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]; if (!arg.startsWith("--")) { usage(`unexpected argument: ${arg}`); } const key = arg.slice(2).replaceAll("-", ""); - if (!knownKeys.has(key)) { + if (!knownArgKeys.has(key)) { usage(`unknown argument: ${arg}`); } const valueLocal = argv[index + 1]; - if (!valueLocal || valueLocal.startsWith("--")) { + if (!valueLocal || valueLocal.startsWith("-")) { usage(`${arg} requires a value`); } parsed[key] = valueLocal; @@ -271,6 +271,28 @@ function parseArgs(argv) { }; } +function shouldPrintHelp(argv) { + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + if (arg === "--help" || arg === "-h") { + return true; + } + if (!arg.startsWith("--")) { + return false; + } + const key = arg.slice(2).replaceAll("-", ""); + if (!knownArgKeys.has(key)) { + return false; + } + const optionValue = argv[index + 1]; + if (!optionValue || optionValue.startsWith("-")) { + return false; + } + index += 1; + } + return false; +} + function usage(message, status = 2) { const text = "usage: node scripts/kova-ci-summary.mjs --report [--output ] [--lane ] [--report-url ] [--artifact-url ]\n"; diff --git a/test/scripts/kova-ci-summary.test.ts b/test/scripts/kova-ci-summary.test.ts index 348fffbee09..538118eee7b 100644 --- a/test/scripts/kova-ci-summary.test.ts +++ b/test/scripts/kova-ci-summary.test.ts @@ -38,6 +38,20 @@ describe("scripts/kova-ci-summary", () => { expect(result.stdout).toContain("usage: node scripts/kova-ci-summary.mjs --report"); }); + it.each([ + ["flag-shaped value", ["--report", "-h"]], + ["option separator before help", ["--report", "--", "--help"]], + ])("rejects %s before help handling", (_name, args) => { + const result = spawnSync(process.execPath, ["scripts/kova-ci-summary.mjs", ...args], { + cwd: process.cwd(), + encoding: "utf8", + }); + + expect(result.status).toBe(2); + expect(result.stdout).toBe(""); + expect(result.stderr).toContain("error: --report requires a value"); + }); + it("rejects empty Kova reports instead of rendering unknown summaries", () => { const empty = runSummary({}); expect(empty.result.status).toBe(1);