@@ -321,6 +321,13 @@ pub enum ColorConfig {
321
321
NeverColor ,
322
322
}
323
323
324
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
325
+ pub enum OutputFormat {
326
+ Pretty ,
327
+ Terse ,
328
+ Json
329
+ }
330
+
324
331
#[ derive( Debug ) ]
325
332
pub struct TestOpts {
326
333
pub list : bool ,
@@ -332,7 +339,7 @@ pub struct TestOpts {
332
339
pub logfile : Option < PathBuf > ,
333
340
pub nocapture : bool ,
334
341
pub color : ColorConfig ,
335
- pub quiet : bool ,
342
+ pub format : OutputFormat ,
336
343
pub test_threads : Option < usize > ,
337
344
pub skip : Vec < String > ,
338
345
pub options : Options ,
@@ -351,7 +358,7 @@ impl TestOpts {
351
358
logfile : None ,
352
359
nocapture : false ,
353
360
color : AutoColor ,
354
- quiet : false ,
361
+ format : OutputFormat ,
355
362
test_threads : None ,
356
363
skip : vec ! [ ] ,
357
364
options : Options :: new ( ) ,
@@ -383,7 +390,11 @@ fn optgroups() -> getopts::Options {
383
390
. optopt ( "" , "color" , "Configure coloring of output:
384
391
auto = colorize if stdout is a tty and tests are run on serially (default);
385
392
always = always colorize output;
386
- never = never colorize output;" , "auto|always|never" ) ;
393
+ never = never colorize output;" , "auto|always|never" )
394
+ . optopt ( "" , "format" , "Configure formatting of output:
395
+ pretty = Print verbose output;
396
+ terse = Display one character per test;
397
+ json = Output a json document" , "pretty|terse|json" ) ;
387
398
return opts
388
399
}
389
400
@@ -484,6 +495,19 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
484
495
}
485
496
} ;
486
497
498
+ let format = match matches. opt_str ( "format" ) . as_ref ( ) . map ( |s| & * * s) {
499
+ None if quiet => OutputFormat :: Terse ,
500
+ Some ( "pretty" ) | None => OutputFormat :: Pretty ,
501
+ Some ( "terse" ) => OutputFormat :: Terse ,
502
+ Some ( "json" ) => OutputFormat :: Json ,
503
+
504
+ Some ( v) => {
505
+ return Some ( Err ( format ! ( "argument for --format must be pretty, terse, or json (was \
506
+ {})",
507
+ v) ) )
508
+ }
509
+ } ;
510
+
487
511
let test_opts = TestOpts {
488
512
list,
489
513
filter,
@@ -494,7 +518,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
494
518
logfile,
495
519
nocapture,
496
520
color,
497
- quiet ,
521
+ format ,
498
522
test_threads,
499
523
skip : matches. opt_strs ( "skip" ) ,
500
524
options : Options :: new ( ) ,
@@ -656,7 +680,9 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
656
680
None => Raw ( io:: stdout ( ) ) ,
657
681
Some ( t) => Pretty ( t) ,
658
682
} ;
659
- let mut out = HumanFormatter :: new ( output, use_color ( opts) , opts. quiet ) ;
683
+
684
+ let quiet = opts. format == OutputFormat :: Terse ;
685
+ let mut out = HumanFormatter :: new ( output, use_color ( opts) , quiet) ;
660
686
let mut st = ConsoleTestState :: new ( opts) ?;
661
687
662
688
let mut ntest = 0 ;
@@ -683,9 +709,9 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
683
709
}
684
710
}
685
711
686
- if !opts . quiet {
712
+ if !quiet {
687
713
if ntest != 0 || nbench != 0 {
688
- st . write_plain ( "\n " ) ?;
714
+ out . write_plain ( "\n " ) ?;
689
715
}
690
716
st. write_plain ( format ! ( "{}, {}\n " ,
691
717
plural( ntest, "test" ) ,
@@ -712,7 +738,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
712
738
TeTimeout ( ref test) => out. write_timeout ( test) ,
713
739
TeResult ( test, result, stdout) => {
714
740
st. write_log_result ( & test, & result) ?;
715
- out. write_result ( & result) ?;
741
+ out. write_result ( & test , & result) ?;
716
742
match result {
717
743
TrOk => {
718
744
st. passed += 1 ;
@@ -749,8 +775,11 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
749
775
Some ( t) => Pretty ( t) ,
750
776
} ;
751
777
752
- let mut out = HumanFormatter :: new ( output, use_color ( opts) , opts. quiet ) ;
753
-
778
+ let mut out: Box < OutputFormatter > = match opts. format {
779
+ OutputFormat :: Pretty => Box :: new ( HumanFormatter :: new ( output, use_color ( opts) , false ) ) ,
780
+ OutputFormat :: Terse => Box :: new ( HumanFormatter :: new ( output, use_color ( opts) , true ) ) ,
781
+ OutputFormat :: Json => Box :: new ( JsonFormatter :: new ( output) ) ,
782
+ } ;
754
783
let mut st = ConsoleTestState :: new ( opts) ?;
755
784
fn len_if_padded ( t : & TestDescAndFn ) -> usize {
756
785
match t. testfn . padding ( ) {
@@ -762,7 +791,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
762
791
let n = t. desc . name . as_slice ( ) ;
763
792
st. max_name_len = n. len ( ) ;
764
793
}
765
- run_tests ( opts, tests, |x| callback ( & x, & mut st, & mut out) ) ?;
794
+ run_tests ( opts, tests, |x| callback ( & x, & mut st, & mut * out) ) ?;
766
795
767
796
assert ! ( st. current_test_count( ) == st. total) ;
768
797
0 commit comments