Skip to content

Commit 5e4a967

Browse files
feat: Feature/cli support multiple reporters (usebruno#2911)
* Support multiple reporters at once in the CLI * Typos * Better logging string after writing file * Remove double blank line * More double blank lines * Switch reporter schema to one from discussion * Typo * Add comment
1 parent 4419634 commit 5e4a967

File tree

1 file changed

+70
-15
lines changed
  • packages/bruno-cli/src/commands

1 file changed

+70
-15
lines changed

Diff for: packages/bruno-cli/src/commands/run.js

+70-15
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ const builder = async (yargs) => {
235235
default: 'json',
236236
type: 'string'
237237
})
238+
.option('reporter-json', {
239+
describe: 'Path to write json file results to',
240+
type: 'string'
241+
})
242+
.option('reporter-junit', {
243+
describe: 'Path to write junit file results to',
244+
type: 'string'
245+
})
246+
.option('reporter-html', {
247+
describe: 'Path to write html file results to',
248+
type: 'string'
249+
})
238250
.option('insecure', {
239251
type: 'boolean',
240252
description: 'Allow insecure server connections'
@@ -267,6 +279,10 @@ const builder = async (yargs) => {
267279
'$0 run request.bru --output results.html --format html',
268280
'Run a request and write the results to results.html in html format in the current directory'
269281
)
282+
.example(
283+
'$0 run request.bru --reporter-junit results.xml --reporter-html results.html',
284+
'Run a request and write the results to results.html in html format and results.xml in junit format in the current directory'
285+
)
270286

271287
.example('$0 run request.bru --tests-only', 'Run all requests that have a test')
272288
.example(
@@ -291,6 +307,9 @@ const handler = async function (argv) {
291307
r: recursive,
292308
output: outputPath,
293309
format,
310+
reporterJson,
311+
reporterJunit,
312+
reporterHtml,
294313
sandbox,
295314
testsOnly,
296315
bail
@@ -392,6 +411,25 @@ const handler = async function (argv) {
392411
process.exit(constants.EXIT_STATUS.ERROR_INCORRECT_OUTPUT_FORMAT);
393412
}
394413

414+
let formats = {};
415+
416+
// Maintains back compat with --format and --output
417+
if (outputPath && outputPath.length) {
418+
formats[format] = outputPath;
419+
}
420+
421+
if (reporterHtml && reporterHtml.length) {
422+
formats['html'] = reporterHtml;
423+
}
424+
425+
if (reporterJson && reporterJson.length) {
426+
formats['json'] = reporterJson;
427+
}
428+
429+
if (reporterJunit && reporterJunit.length) {
430+
formats['junit'] = reporterJunit;
431+
}
432+
395433
// load .env file at root of collection if it exists
396434
const dotEnvPath = path.join(collectionPath, '.env');
397435
const dotEnvExists = await exists(dotEnvPath);
@@ -524,28 +562,45 @@ const handler = async function (argv) {
524562
const totalTime = results.reduce((acc, res) => acc + res.response.responseTime, 0);
525563
console.log(chalk.dim(chalk.grey(`Ran all requests - ${totalTime} ms`)));
526564

527-
if (outputPath && outputPath.length) {
528-
const outputDir = path.dirname(outputPath);
529-
const outputDirExists = await exists(outputDir);
530-
if (!outputDirExists) {
531-
console.error(chalk.red(`Output directory ${outputDir} does not exist`));
532-
process.exit(constants.EXIT_STATUS.ERROR_MISSING_OUTPUT_DIR);
533-
}
534-
565+
const formatKeys = Object.keys(formats);
566+
if (formatKeys && formatKeys.length > 0) {
535567
const outputJson = {
536568
summary,
537569
results
538570
};
539571

540-
if (format === 'json') {
541-
fs.writeFileSync(outputPath, JSON.stringify(outputJson, null, 2));
542-
} else if (format === 'junit') {
543-
makeJUnitOutput(results, outputPath);
544-
} else if (format === 'html') {
545-
makeHtmlOutput(outputJson, outputPath);
572+
const reporters = {
573+
'json': (path) => fs.writeFileSync(path, JSON.stringify(outputJson, null, 2)),
574+
'junit': (path) => makeJUnitOutput(results, path),
575+
'html': (path) => makeHtmlOutput(outputJson, path),
546576
}
547577

548-
console.log(chalk.dim(chalk.grey(`Wrote results to ${outputPath}`)));
578+
for (const formatter of Object.keys(formats))
579+
{
580+
const reportPath = formats[formatter];
581+
const reporter = reporters[formatter];
582+
583+
// Skip formatters lacking an output path.
584+
if (!reportPath || reportPath.length === 0) {
585+
continue;
586+
}
587+
588+
const outputDir = path.dirname(reportPath);
589+
const outputDirExists = await exists(outputDir);
590+
if (!outputDirExists) {
591+
console.error(chalk.red(`Output directory ${outputDir} does not exist`));
592+
process.exit(constants.EXIT_STATUS.ERROR_MISSING_OUTPUT_DIR);
593+
}
594+
595+
if (!reporter) {
596+
console.error(chalk.red(`Reporter ${formatter} does not exist`));
597+
process.exit(constants.EXIT_STATUS.ERROR_INCORRECT_OUTPUT_FORMAT);
598+
}
599+
600+
reporter(reportPath);
601+
602+
console.log(chalk.dim(chalk.grey(`Wrote ${formatter} results to ${reportPath}`)));
603+
}
549604
}
550605

551606
if (summary.failedAssertions + summary.failedTests + summary.failedRequests > 0) {

0 commit comments

Comments
 (0)