Skip to content

Commit a3f4954

Browse files
Merge pull request #7 from ctrf-io/feat/include-suite
fixes #5
2 parents 0f46a2a + 9cd66a3 commit a3f4954

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ npx junit-to-ctrf path/to/junit.xml
2222

2323
`-t`, `--tool` <toolName>: Tool name to include in the CTRF report.
2424

25+
`-u`, `--use-suite-name` <useSuiteName>: Use suite name in the test name, defaults to true.
26+
2527
`-e`, `--env` <envProperties>: Environment properties to include in the CTRF report. Accepts multiple properties in the format KEY=value.
2628

29+
2730
## Examples
2831

2932
Convert a JUnit XML report to the default CTRF report location (ctrf/ctrf-report.json):
@@ -58,6 +61,12 @@ npx junit-to-ctrf path/to/junit.xml -e appName=MyApp buildName=MyBuild
5861

5962
See [CTRF schema](https://www.ctrf.io/docs/schema/environment) for possible environment properties
6063

64+
### Exclude Suite Name
65+
66+
```sh
67+
npx junit-to-ctrf path/to/junit.xml -u false
68+
```
69+
6170
### Full Command
6271

6372
Combine all options in a single command:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "junit-to-ctrf",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/cli.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ yargs(hideBin(process.argv))
3030
alias: 'e',
3131
type: 'array',
3232
description: 'Environment properties',
33+
})
34+
.option('use-suite-name', {
35+
alias: 'u',
36+
type: 'boolean',
37+
default: true,
38+
description: 'Use suite name in the test name',
3339
});
3440
},
3541
async (argv) => {
3642
try {
37-
const { path, output, tool, env } = argv;
38-
await convertJUnitToCTRF(path as string, output as string, tool as string, env as string[]);
43+
const { path, output, tool, env, useSuiteName } = argv;
44+
await convertJUnitToCTRF(path as string, output as string, tool as string, env as string[], useSuiteName);
3945
console.log('Conversion completed successfully.');
4046
} catch (error: any) {
4147
console.error('Error:', error.message);

src/convert.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
6161
return testCases;
6262
}
6363

64-
function convertToCTRFTest(testCase: JUnitTestCase): CtrfTest {
64+
function convertToCTRFTest(testCase: JUnitTestCase, useSuiteName: boolean): CtrfTest {
6565
let status: CtrfTest['status'] = 'other';
6666

6767
if (testCase.failure) {
@@ -76,21 +76,27 @@ function convertToCTRFTest(testCase: JUnitTestCase): CtrfTest {
7676

7777
const durationMs = Math.round(parseFloat(testCase.time) * 1000);
7878

79+
const testName = useSuiteName
80+
? `${testCase.suite}: ${testCase.name}`
81+
: testCase.name;
82+
7983
return {
80-
name: `${testCase.suite}: ${testCase.name}`,
84+
name: testName,
8185
status,
8286
duration: durationMs,
8387
message: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
8488
trace: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
89+
suite: testCase.suite || ''
8590
};
8691
}
8792

8893
function createCTRFReport(
8994
testCases: JUnitTestCase[],
9095
toolName?: string,
91-
envProps?: Record<string, any>
96+
envProps?: Record<string, any>,
97+
useSuiteName?: boolean
9298
): CtrfReport {
93-
const ctrfTests = testCases.map(convertToCTRFTest);
99+
const ctrfTests = testCases.map(testCase => convertToCTRFTest(testCase, !!useSuiteName));
94100
const passed = ctrfTests.filter(test => test.status === 'passed').length;
95101
const failed = ctrfTests.filter(test => test.status === 'failed').length;
96102
const skipped = ctrfTests.filter(test => test.status === 'skipped').length;
@@ -131,11 +137,12 @@ export async function convertJUnitToCTRF(
131137
junitPath: string,
132138
outputPath?: string,
133139
toolName?: string,
134-
envProps?: string[]
140+
envProps?: string[],
141+
useSuiteName?: boolean
135142
): Promise<void> {
136143
const testCases = await parseJUnitReport(junitPath);
137144
const envPropsObj = envProps ? Object.fromEntries(envProps.map(prop => prop.split('='))) : {};
138-
const ctrfReport = createCTRFReport(testCases, toolName, envPropsObj);
145+
const ctrfReport = createCTRFReport(testCases, toolName, envPropsObj, useSuiteName);
139146

140147
const defaultOutputPath = path.join('ctrf', 'ctrf-report.json');
141148
const finalOutputPath = path.resolve(outputPath || defaultOutputPath);

0 commit comments

Comments
 (0)