Skip to content

Commit

Permalink
add an invalid case
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 25, 2024
1 parent e1bcfb1 commit a2248ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cucumber-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export function buildArgs(runCfg: CucumberRunnerConfig, cucumberBin: string) {
* For simple inputs (e.g., `usage`) or unstructured formats, the function returns the
* input unchanged.
*
* If the input starts with `file://`, an error is thrown to indicate an invalid format.
*
* @param {string} format - The input format string. Examples include:
* - `"key:value"`
* - `"key":"value"`
Expand All @@ -102,6 +104,13 @@ export function buildArgs(runCfg: CucumberRunnerConfig, cucumberBin: string) {
* Output: `"file://implementation":"output_file"` (unchanged)
*/
export function normalizeFormat(format: string, assetDir: string): string {
// Formats starting with file:// are not supported by the current implementation.
// Restrict users from using this format.
if (format.startsWith('file://')) {
throw new Error(
`Invalid format setting detected. The provided format "${format}" is not allowed.`,
);
}
// Try to match structured inputs in the format key:value, "key:value", or "key":"value".
let match = format.match(/^"?([^:]+):"?([^"]+)"?$/);

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/src/cucumber-runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('normalizeFormat', () => {
).toBe(`"file://formatter/implementation":"/project/assets/report.json"`);
});

it('should throw an error for an invalid file path type', () => {
expect(() => {
normalizeFormat(`file://formatter/implementation:report.json`, assetDir);
}).toThrow('Invalid format setting detected');
});

it('should return simple strings as-is', () => {
expect(normalizeFormat(`"usage"`, assetDir)).toBe('"usage"');
expect(normalizeFormat(`usage`, assetDir)).toBe('usage');
Expand Down

0 comments on commit a2248ab

Please sign in to comment.