Skip to content

Commit

Permalink
support simple format strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 21, 2024
1 parent aa780f4 commit 3cd800e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
25 changes: 16 additions & 9 deletions src/cucumber-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,32 @@ export function buildArgs(runCfg: CucumberRunnerConfig, cucumberBin: string) {
}

/**
* Normalizes a Cucumber format string by ensuring it is in the form of `"key":"value"`.
* Normalizes a Cucumber-js format string.
*
* @param {string} format - The input format string, which can be in various forms:
* For structured inputs (`key:value` or `"key:value"`), returns a string in the
* form `"key":"value"`, with the asset directory prepended to relative paths.
* For simple inputs (e.g., `progress-bar`), returns the input as-is.
*
* @param {string} format - The input format string. Examples include:
* - `"key:value"`
* - `"key":"value"`
* - `key:value`
* @param {string} assetDir - The asset directory.
* @throws {Error} If the input format is invalid (e.g., missing a colon separator).
* @returns {string} The normalized format string in the form of `"key":"value"`,
* with the asset directory prepended to relative paths.
* - `progress-bar`
* @param {string} assetDir - The directory to prepend to the value for relative paths.
* @returns {string} The normalized format string. For structured inputs, it returns
* a string in the form `"key":"value"`. For simple inputs, it
* returns the input unchanged.
*
* Example:
* Input: `"html:formatter/report.html"`, `"/project/assets"`
* Output: `"html":"/project/assets/formatter/report.html"`
* - Input: `"html":"formatter/report.html"`, `"/project/assets"`
* Output: `"html":"/project/assets/formatter/report.html"`
* - Input: `"progress-bar"`, `"/project/assets"`
* Output: `"progress-bar"`
*/
export function normalizeFormat(format: string, assetDir: string): string {
const match = format.match(/^"?([^:]+):"?([^"]+)"?$/);
if (!match) {
throw new Error(`Invalid format: ${format}`);
return format;
}

let [, key, value] = match;
Expand Down
10 changes: 3 additions & 7 deletions tests/unit/src/cucumber-runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ describe('normalizeFormat', () => {
);
});

it('should throw an error for invalid formats', () => {
expect(() =>
normalizeFormat(`html-formatter/report.html`, assetDir),
).toThrow('Invalid format: html-formatter/report.html');
expect(() =>
normalizeFormat(`"htmlformatter/report.html"`, assetDir),
).toThrow('Invalid format: "htmlformatter/report.html"');
it('should return simple strings as-is', () => {
expect(normalizeFormat(`"progress-bar"`, assetDir)).toBe('"progress-bar"');
expect(normalizeFormat(`progress-bar`, assetDir)).toBe('progress-bar');
});
});

0 comments on commit 3cd800e

Please sign in to comment.