Repository navigation
[Hackathon] fix: reject empty and whitespace ids with VALIDATION_ERROR in read-by-id commands #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grzdev
wants to merge
1
commit into
TestSprite:main
Choose a base branch
from
grzdev:fix/read-by-id-require-non-empty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { Command, Argument } from 'commander'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { formatArgumentName, validatePositionalArgs } from './args.js'; | ||
| import { createProjectCommand } from '../commands/project.js'; | ||
| import { createTestCommand } from '../commands/test.js'; | ||
|
|
||
| describe('formatArgumentName', () => { | ||
| it('formats required positional argument as <name>', () => { | ||
| const arg = new Argument('<project-id>'); | ||
| expect(formatArgumentName(arg)).toBe('<project-id>'); | ||
| }); | ||
|
|
||
| it('formats optional positional argument as [name]', () => { | ||
| const arg = new Argument('[environment]'); | ||
| expect(formatArgumentName(arg)).toBe('[environment]'); | ||
| }); | ||
|
|
||
| it('formats required variadic argument as <name...>', () => { | ||
| const arg = new Argument('<run-id...>'); | ||
| expect(formatArgumentName(arg)).toBe('<run-id...>'); | ||
| }); | ||
|
|
||
| it('formats optional variadic argument as [name...]', () => { | ||
| const arg = new Argument('[test-ids...]'); | ||
| expect(formatArgumentName(arg)).toBe('[test-ids...]'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validatePositionalArgs with throwaway Commander program', () => { | ||
| function makeProgram(actionFn = vi.fn()) { | ||
| const program = new Command(); | ||
| program.exitOverride(); | ||
| program.hook('preAction', (_thisCmd, actionCmd) => { | ||
| validatePositionalArgs(actionCmd); | ||
| }); | ||
|
|
||
| program.command('test-req <id>').action(actionFn); | ||
|
|
||
| program.command('test-opt [opt]').action(actionFn); | ||
|
|
||
| program.command('test-var <first> [rest...]').action(actionFn); | ||
|
|
||
| return { program, actionFn }; | ||
| } | ||
|
|
||
| it('passes when valid positional arguments are provided', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await program.parseAsync(['node', 'test', 'test-req', 'valid-id']); | ||
| expect(actionFn).toHaveBeenCalledWith('valid-id', expect.anything(), expect.anything()); | ||
| }); | ||
|
|
||
| it('rejects empty string required positional with VALIDATION_ERROR (exit 5)', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await expect(program.parseAsync(['node', 'test', 'test-req', ''])).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `<id>` is invalid: cannot be empty or whitespace-only.', | ||
| details: { | ||
| field: '<id>', | ||
| reason: 'cannot be empty or whitespace-only', | ||
| }, | ||
| }); | ||
| expect(actionFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects whitespace-only required positional with VALIDATION_ERROR (exit 5)', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await expect(program.parseAsync(['node', 'test', 'test-req', ' \t '])).rejects.toMatchObject( | ||
| { | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `<id>` is invalid: cannot be empty or whitespace-only.', | ||
| }, | ||
| ); | ||
| expect(actionFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('passes when optional positional is omitted', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await program.parseAsync(['node', 'test', 'test-opt']); | ||
| expect(actionFn).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects empty string optional positional when passed explicitly', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await expect(program.parseAsync(['node', 'test', 'test-opt', ''])).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `[opt]` is invalid: cannot be empty or whitespace-only.', | ||
| }); | ||
| expect(actionFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects whitespace-only optional positional when passed explicitly', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await expect(program.parseAsync(['node', 'test', 'test-opt', ' '])).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `[opt]` is invalid: cannot be empty or whitespace-only.', | ||
| }); | ||
| expect(actionFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects empty or whitespace elements in variadic positional arguments', async () => { | ||
| const { program, actionFn } = makeProgram(); | ||
| await expect( | ||
| program.parseAsync(['node', 'test', 'test-var', 'ok-first', 'valid-item', ' ']), | ||
| ).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `[rest...]` is invalid: cannot be empty or whitespace-only.', | ||
| }); | ||
| expect(actionFn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('command validation via parseAsync with real commands', () => { | ||
| function makeCliProgram(cmd: Command) { | ||
| const program = new Command(); | ||
| program.option('--dry-run', 'dry run mode'); | ||
| program.hook('preAction', (_thisCmd, actionCmd) => { | ||
| validatePositionalArgs(actionCmd); | ||
| }); | ||
| program.addCommand(cmd); | ||
|
|
||
| function applyExitOverride(c: Command): void { | ||
| c.exitOverride(); | ||
| for (const sub of c.commands) { | ||
| applyExitOverride(sub); | ||
| } | ||
| } | ||
| applyExitOverride(program); | ||
| return program; | ||
| } | ||
|
|
||
| it('rejects project get with whitespace project id', async () => { | ||
| const program = makeCliProgram(createProjectCommand()); | ||
|
|
||
| await expect( | ||
| program.parseAsync(['node', 'testsprite', 'project', 'get', ' ']), | ||
| ).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `<project-id>` is invalid: cannot be empty or whitespace-only.', | ||
| details: { | ||
| field: '<project-id>', | ||
| reason: 'cannot be empty or whitespace-only', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects test get with empty test id under --dry-run', async () => { | ||
| const program = makeCliProgram(createTestCommand()); | ||
|
|
||
| await expect( | ||
| program.parseAsync(['node', 'testsprite', 'test', 'get', '', '--dry-run']), | ||
| ).rejects.toMatchObject({ | ||
| code: 'VALIDATION_ERROR', | ||
| exitCode: 5, | ||
| nextAction: 'Argument `<test-id>` is invalid: cannot be empty or whitespace-only.', | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { Argument, Command } from 'commander'; | ||
| import { localValidationError } from './errors.js'; | ||
|
|
||
| /** | ||
| * Returns the formatted argument label matching Commander's display convention: | ||
| * `<name>` for required arguments, `[name]` for optional arguments, with `...` | ||
| * appended for variadic arguments. | ||
| */ | ||
| export function formatArgumentName(arg: Argument): string { | ||
| const name = arg.name() + (arg.variadic ? '...' : ''); | ||
| return arg.required ? `<${name}>` : `[${name}]`; | ||
| } | ||
|
|
||
| /** | ||
| * Validates positional arguments on a Commander command before its action executes. | ||
| * Rejects any empty string or whitespace-only positional argument (including variadics) | ||
| * with exit 5 VALIDATION_ERROR. | ||
| */ | ||
| export function validatePositionalArgs(command: Command): void { | ||
| const registeredArgs = command.registeredArguments; | ||
| if (!registeredArgs || registeredArgs.length === 0) { | ||
| return; | ||
| } | ||
| const processedArgs = command.processedArgs; | ||
| if (!processedArgs || processedArgs.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| for (let i = 0; i < registeredArgs.length; i++) { | ||
| const argDef = registeredArgs[i]; | ||
| if (!argDef) { | ||
| continue; | ||
| } | ||
| const val = processedArgs[i]; | ||
|
|
||
| if (val === undefined) { | ||
| continue; | ||
| } | ||
|
|
||
| const argLabel = formatArgumentName(argDef); | ||
|
|
||
| if (typeof val === 'string') { | ||
| if (val.trim().length === 0) { | ||
| throw localValidationError( | ||
| argLabel, | ||
| 'cannot be empty or whitespace-only', | ||
| undefined, | ||
| 'argument', | ||
| ); | ||
| } | ||
| } else if (Array.isArray(val)) { | ||
| for (const item of val) { | ||
| if (typeof item === 'string' && item.trim().length === 0) { | ||
| throw localValidationError( | ||
| argLabel, | ||
| 'cannot be empty or whitespace-only', | ||
| undefined, | ||
| 'argument', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.