diff --git a/.changeset/cli-strict-integer-flags.md b/.changeset/cli-strict-integer-flags.md new file mode 100644 index 0000000000..559c7a47ba --- /dev/null +++ b/.changeset/cli-strict-integer-flags.md @@ -0,0 +1,5 @@ +--- +'e2e': patch +--- + +The numeric flags of `e2e run`, `e2e mcp`, and `e2e explore` take decimal digits only. An empty value, such as `--retries "$RETRIES"` with the variable unset, used to read as 0 and turn retries off without a word; it is now a usage error, exit 2, as an empty `--tag` is. `0x1` and `1e3` are refused the same way. diff --git a/docs/reference/cli.mdx b/docs/reference/cli.mdx index c760702368..d033e1e4cf 100644 --- a/docs/reference/cli.mdx +++ b/docs/reference/cli.mdx @@ -861,7 +861,10 @@ anything starts, as the config value would. `--target`, `--tag`, `--exclude-tag`, and `--agent` take each name once. An empty value (`--tag ''`, or `--tag "$TAGS"` with the variable unset) is a usage error, exit 2, never a wider selection. An empty `--reporter` is the same usage error, never a run -with no reporters. +with no reporters. A numeric flag (`--retries`, `--workers`, +`--max-failures`, a timeout) takes decimal digits only: an empty value +(`--retries "$RETRIES"` with the variable unset), `0x1`, or `1e3` is the same +usage error, never read as some other number. Filters only narrow, and each applies after the one before: positionals, then `.only`, `--tag`, `--exclude-tag`, `--grep`, `--grep-invert`, `--last-failed`, diff --git a/packages/e2e/src/cli/index.ts b/packages/e2e/src/cli/index.ts index c102f7dff4..4590c8e412 100644 --- a/packages/e2e/src/cli/index.ts +++ b/packages/e2e/src/cli/index.ts @@ -49,9 +49,18 @@ import { telemetry as telemetryCommand, TELEMETRY_ACTIONS, type TelemetryAction import { parseUpgradeTarget, type UpgradeTarget } from './upgrade-target.ts'; import { wordmarkBanner } from './wordmark.ts'; +/** + * The decimal digits of a flag value as a number, else NaN. `Number` alone + * reads `''` and `' '` as 0 and takes `0x10` and `1e3`, so an unset + * `--retries "$RETRIES"` would turn retries off instead of failing. + */ +function integerOf(value: string): number { + return /^\d+$/u.test(value) ? Number(value) : Number.NaN; +} + /** An integer of at least one, for `--max-failures` and `--repeat-each`. */ function parsePositiveInt(value: string): number { - const parsed = Number(value); + const parsed = integerOf(value); if (!Number.isSafeInteger(parsed) || parsed < 1) throw new InvalidArgumentError('must be a positive integer'); return parsed; } @@ -65,7 +74,7 @@ function parseDevice(value: string): string { /** Shape check only; the config resolver applies each flag's bounds. */ function parseNonNegativeInt(value: string): number { - const parsed = Number(value); + const parsed = integerOf(value); if (!Number.isSafeInteger(parsed) || parsed < 0) { throw new InvalidArgumentError('must be a nonnegative integer'); } @@ -99,7 +108,7 @@ function parseShard(value: string): Shard { /** An integer inside a closed range, for the explore budgets, `--max-duration`, and the timeout overrides. */ function parseBoundedInt(bounds: { readonly min: number; readonly max: number }): (value: string) => number { return (value) => { - const parsed = Number(value); + const parsed = integerOf(value); if (!Number.isSafeInteger(parsed) || parsed < bounds.min || parsed > bounds.max) { throw new InvalidArgumentError(`must be an integer from ${bounds.min} through ${bounds.max}`); } diff --git a/packages/e2e/tests/unit/cli.test.ts b/packages/e2e/tests/unit/cli.test.ts index ef7218ef34..bf211d4265 100644 --- a/packages/e2e/tests/unit/cli.test.ts +++ b/packages/e2e/tests/unit/cli.test.ts @@ -213,7 +213,7 @@ describe('e2e run argument parsing', () => { await invoke('run', '--max-failures', '3'); expect(lastRunOptions().maxFailures).toBe(3); runMock.mockClear(); - for (const value of ['0', '-1', '2.5', 'many']) { + for (const value of ['0', '-1', '2.5', 'many', '', '1e1']) { stderrSpy.mockClear(); process.exitCode = undefined; await invoke('run', '--max-failures', value); @@ -230,7 +230,7 @@ describe('e2e run argument parsing', () => { await invoke('run', '--max-duration', '2147483647'); expect(lastRunOptions().maxDuration).toBe(2_147_483_647); runMock.mockClear(); - for (const value of ['0', '-1', '2.5', '15m', '2147483648']) { + for (const value of ['0', '-1', '2.5', '15m', '2147483648', '', '0x10']) { stderrSpy.mockClear(); process.exitCode = undefined; await invoke('run', '--max-duration', value); @@ -467,12 +467,14 @@ describe('e2e run argument parsing', () => { }); it('rejects negative and non-integer numeric flags with exit code 2', async () => { - for (const value of ['-1', '1.5', 'abc']) { - runMock.mockClear(); - process.exitCode = undefined; - await invoke('run', '--retries', value); - expect(runMock).not.toHaveBeenCalled(); - expect(process.exitCode).toBe(2); + for (const flag of ['--retries', '--workers']) { + for (const value of ['-1', '1.5', 'abc', '', ' ', '0x1', '1e1']) { + runMock.mockClear(); + process.exitCode = undefined; + await invoke('run', flag, value); + expect(runMock, `${flag} '${value}'`).not.toHaveBeenCalled(); + expect(process.exitCode, `${flag} '${value}'`).toBe(2); + } } });