Skip to content

Commit 906a540

Browse files
committed
chore: add option for log
1 parent bec508e commit 906a540

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ yargs(hideBin(process.argv))
4242
async (argv) => {
4343
try {
4444
const { pattern, output, tool, env, useSuiteName } = argv;
45-
await convertJUnitToCTRFReport(pattern as string, { outputPath: output as string, toolName: tool as string, envProps: env as string[], useSuiteName: useSuiteName as boolean });
45+
await convertJUnitToCTRFReport(pattern as string, { outputPath: output as string, toolName: tool as string, envProps: env as string[], useSuiteName: useSuiteName as boolean, log: true });
4646
console.log('Conversion completed successfully.');
4747
} catch (error: any) {
4848
console.error('Error:', error.message);

src/convert.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface ConvertOptions {
88
toolName?: string;
99
envProps?: string[];
1010
useSuiteName?: boolean;
11+
log?: boolean;
1112
}
1213

1314
/**
@@ -21,25 +22,25 @@ export async function convertJUnitToCTRFReport(
2122
options: ConvertOptions = {}
2223
): Promise<CtrfReport | null> {
2324
const { outputPath, toolName, envProps, useSuiteName } = options;
24-
const testCases = await readJUnitReportsFromGlob(pattern);
25+
const testCases = await readJUnitReportsFromGlob(pattern, { log: options.log });
2526
const envPropsObj = envProps ? Object.fromEntries(envProps.map(prop => prop.split('='))) : {};
2627

2728
if (testCases.length === 0) {
2829
console.warn('No test cases found in the provided path. No CTRF report generated.');
2930
return null;
3031
}
3132

32-
console.log(`Converting ${testCases.length} test cases to CTRF format`);
33+
if (options.log) console.log(`Converting ${testCases.length} test cases to CTRF format`);
3334
const ctrfReport = createCTRFReport(testCases, toolName, envPropsObj, useSuiteName);
3435

3536
if (outputPath) {
3637
const finalOutputPath = path.resolve(outputPath)
3738
const outputDir = path.dirname(finalOutputPath);
3839
await fs.ensureDir(outputDir);
3940

40-
console.log('Writing CTRF report to:', finalOutputPath);
41+
if (options.log) console.log('Writing CTRF report to:', finalOutputPath);
4142
await fs.outputJson(finalOutputPath, ctrfReport, { spaces: 2 });
42-
console.log(`CTRF report written to ${outputPath}`);
43+
if (options.log) console.log(`CTRF report written to ${outputPath}`);
4344
}
4445
return ctrfReport;
4546
}

src/read.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import { glob } from 'glob';
77
* @param globPattern - The glob pattern to match JUnit XML files
88
* @returns Promise resolving to an array of all test cases from all matching files
99
*/
10-
export async function readJUnitReportsFromGlob(globPattern: string): Promise<JUnitTestCase[]> {
11-
console.log('Searching for JUnit reports matching pattern:', globPattern);
10+
export async function readJUnitReportsFromGlob(globPattern: string, options: { log?: boolean } = {}): Promise<JUnitTestCase[]> {
11+
if (options.log) console.log('Searching for JUnit reports matching pattern:', globPattern);
1212

1313
const files = await glob(globPattern);
1414

1515
if (files.length === 0) {
16-
console.warn('No files found matching the pattern:', globPattern);
16+
if (options.log) console.warn('No files found matching the pattern:', globPattern);
1717
return [];
1818
}
1919

20-
console.log(`Found ${files.length} JUnit report files`);
20+
if (options.log) console.log(`Found ${files.length} JUnit report files`);
2121

22-
const allTestCasesPromises = files.map(file => parseJUnitReport(file));
22+
const allTestCasesPromises = files.map(file => parseJUnitReport(file, options));
2323
const testCasesArrays = await Promise.all(allTestCasesPromises);
2424

2525
return testCasesArrays.flat();
@@ -30,8 +30,8 @@ export async function readJUnitReportsFromGlob(globPattern: string): Promise<JUn
3030
* @param filePath - Path to the JUnit XML file
3131
* @returns Promise resolving to an array of test cases
3232
*/
33-
export async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
34-
console.log('Reading JUnit report file:', filePath);
33+
export async function parseJUnitReport(filePath: string, options: { log?: boolean } = {}): Promise<JUnitTestCase[]> {
34+
if (options.log) console.log('Reading JUnit report file:', filePath);
3535
const xml = await fs.readFile(filePath, 'utf-8');
3636
const result = await xml2js.parseStringPromise(xml);
3737
const testCases: JUnitTestCase[] = [];
@@ -89,7 +89,7 @@ export async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[
8989
const suiteName = suite.$.name;
9090
parseTestSuite(suite, suiteName);
9191
} else {
92-
console.warn('No test suites found in the provided file.');
92+
if (options.log) console.warn('No test suites found in the provided file.');
9393
}
9494

9595
return testCases;

src/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function main() {
3636
// await convertAndSaveToFile();
3737

3838
const report = await convertJUnitToCTRFReport(
39-
'test-junit.xml',
39+
'./*.xml',
4040
{
4141
outputPath: 'ctrf/output-report.json',
4242
toolName: 'junit-test-runner',

0 commit comments

Comments
 (0)