@@ -235,6 +235,18 @@ const builder = async (yargs) => {
235
235
default : 'json' ,
236
236
type : 'string'
237
237
} )
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
+ } )
238
250
. option ( 'insecure' , {
239
251
type : 'boolean' ,
240
252
description : 'Allow insecure server connections'
@@ -267,6 +279,10 @@ const builder = async (yargs) => {
267
279
'$0 run request.bru --output results.html --format html' ,
268
280
'Run a request and write the results to results.html in html format in the current directory'
269
281
)
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
+ )
270
286
271
287
. example ( '$0 run request.bru --tests-only' , 'Run all requests that have a test' )
272
288
. example (
@@ -291,6 +307,9 @@ const handler = async function (argv) {
291
307
r : recursive ,
292
308
output : outputPath ,
293
309
format,
310
+ reporterJson,
311
+ reporterJunit,
312
+ reporterHtml,
294
313
sandbox,
295
314
testsOnly,
296
315
bail
@@ -392,6 +411,25 @@ const handler = async function (argv) {
392
411
process . exit ( constants . EXIT_STATUS . ERROR_INCORRECT_OUTPUT_FORMAT ) ;
393
412
}
394
413
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
+
395
433
// load .env file at root of collection if it exists
396
434
const dotEnvPath = path . join ( collectionPath , '.env' ) ;
397
435
const dotEnvExists = await exists ( dotEnvPath ) ;
@@ -524,28 +562,45 @@ const handler = async function (argv) {
524
562
const totalTime = results . reduce ( ( acc , res ) => acc + res . response . responseTime , 0 ) ;
525
563
console . log ( chalk . dim ( chalk . grey ( `Ran all requests - ${ totalTime } ms` ) ) ) ;
526
564
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 ) {
535
567
const outputJson = {
536
568
summary,
537
569
results
538
570
} ;
539
571
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 ) ,
546
576
}
547
577
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
+ }
549
604
}
550
605
551
606
if ( summary . failedAssertions + summary . failedTests + summary . failedRequests > 0 ) {
0 commit comments