Skip to content

Commit

Permalink
handle file based formatter implemenation
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 22, 2024
1 parent 850315d commit 783703d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/cucumber-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,58 @@ export function buildArgs(runCfg: CucumberRunnerConfig, cucumberBin: string) {
/**
* Normalizes a Cucumber-js format string.
*
* For structured inputs (`key:value` or `"key:value"`), returns a string in the
* form `"key":"value"`. If the value starts with `file://`, it is treated as an
* absolute path and no asset directory is prepended. Otherwise, the asset
* directory is prepended to relative paths.
* For structured inputs (`key:value`, `"key:value"`, or `"key":"value"`), returns a string
* in the form `"key":"value"`. If the value starts with `file://`, it is treated as an
* absolute path, and no asset directory is prepended. Otherwise, the asset directory
* is prepended to relative paths.
*
* For simple inputs (e.g., `usage`), the input is returned unchanged.
* For simple inputs (e.g., `usage`) or other unstructured formats, the input is returned unchanged.
*
* @param {string} format - The input format string. Examples include:
* - `"key:value"`
* - `"key":"value"`
* - `key:value`
* - `usage`
* - `"file://implementation":"output_file"`
* @param {string} assetDir - The directory to prepend to the value for relative paths.
* @returns {string} The normalized format string.
*
* Example:
* - Input: `"html":"formatter/report.html"`, `"/project/assets"`
* Examples:
* - Input: `"html:formatter/report.html"`, `"/project/assets"`
* Output: `"html":"/project/assets/formatter/report.html"`
* - Input: `"html":"file://formatter/report.html"`, `"/project/assets"`
* Output: `"html":"file://formatter/report.html"`
* - Input: `"usage"`, `"/project/assets"`
* Output: `"usage"`
* - Input: `"file://implementation":"output_file"`, `"/project/assets"`
* Output: `"file://implementation":"/project/assets/output_file"`
*/
export function normalizeFormat(format: string, assetDir: string): string {
// Checks if the format is structured; if not, returns it unchanged.
const match = format.match(/^"?([^:]+):"?([^"]+)"?$/);
// Try to match structured inputs in the format key:value, "key:value", or "key":"value".
let match = format.match(/^"?([^:]+):"?([^"]+)"?$/);

if (!match) {
// Check if the format uses a file path starting with "file://".
if (!format.startsWith('"file://')) {
return format;
}

// Match file-based structured inputs like "file://implementation":"output_file".
match = format.match(/^"([^"]+)":"([^"]+)"$/);
}

if (!match) {
return format;
}

let [, key, value] = match;
key = key.replaceAll('"', '');
value = value.replaceAll('"', '');

if (value.startsWith('file://')) {
return `"${key}":"${value}"`;
}

return `"${key}":"${path.join(assetDir, value)}"`;
}

Expand Down
9 changes: 9 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,15 @@ describe('normalizeFormat', () => {
).toBe(`"html":"file:///tmp/formatter/report.html"`);
});

it('should normalize format with file path type', () => {
expect(
normalizeFormat(
`"file://formatter/implementation":"report.json"`,
assetDir,
),
).toBe(`"file://formatter/implementation":"/project/assets/report.json"`);
});

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 783703d

Please sign in to comment.