@@ -337,6 +337,13 @@ pub enum ColorConfig {
337
337
NeverColor ,
338
338
}
339
339
340
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
341
+ pub enum OutputFormat {
342
+ Pretty ,
343
+ Terse ,
344
+ Json
345
+ }
346
+
340
347
#[ derive( Debug ) ]
341
348
pub struct TestOpts {
342
349
pub list : bool ,
@@ -348,7 +355,7 @@ pub struct TestOpts {
348
355
pub logfile : Option < PathBuf > ,
349
356
pub nocapture : bool ,
350
357
pub color : ColorConfig ,
351
- pub quiet : bool ,
358
+ pub format : OutputFormat ,
352
359
pub test_threads : Option < usize > ,
353
360
pub skip : Vec < String > ,
354
361
pub options : Options ,
@@ -367,7 +374,7 @@ impl TestOpts {
367
374
logfile : None ,
368
375
nocapture : false ,
369
376
color : AutoColor ,
370
- quiet : false ,
377
+ format : OutputFormat ,
371
378
test_threads : None ,
372
379
skip : vec ! [ ] ,
373
380
options : Options :: new ( ) ,
@@ -399,7 +406,11 @@ fn optgroups() -> getopts::Options {
399
406
. optopt ( "" , "color" , "Configure coloring of output:
400
407
auto = colorize if stdout is a tty and tests are run on serially (default);
401
408
always = always colorize output;
402
- never = never colorize output;" , "auto|always|never" ) ;
409
+ never = never colorize output;" , "auto|always|never" )
410
+ . optopt ( "" , "format" , "Configure formatting of output:
411
+ pretty = Print verbose output;
412
+ terse = Display one character per test;
413
+ json = Output a json document" , "pretty|terse|json" ) ;
403
414
return opts
404
415
}
405
416
@@ -499,6 +510,19 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
499
510
}
500
511
} ;
501
512
513
+ let format = match matches. opt_str ( "format" ) . as_ref ( ) . map ( |s| & * * s) {
514
+ None if quiet => OutputFormat :: Terse ,
515
+ Some ( "pretty" ) | None => OutputFormat :: Pretty ,
516
+ Some ( "terse" ) => OutputFormat :: Terse ,
517
+ Some ( "json" ) => OutputFormat :: Json ,
518
+
519
+ Some ( v) => {
520
+ return Some ( Err ( format ! ( "argument for --format must be pretty, terse, or json (was \
521
+ {})",
522
+ v) ) )
523
+ }
524
+ } ;
525
+
502
526
let test_opts = TestOpts {
503
527
list,
504
528
filter,
@@ -509,7 +533,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
509
533
logfile,
510
534
nocapture,
511
535
color,
512
- quiet ,
536
+ format ,
513
537
test_threads,
514
538
skip : matches. opt_strs ( "skip" ) ,
515
539
options : Options :: new ( ) ,
@@ -673,7 +697,9 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
673
697
None => Raw ( io:: stdout ( ) ) ,
674
698
Some ( t) => Pretty ( t) ,
675
699
} ;
676
- let mut out = HumanFormatter :: new ( output, use_color ( opts) , opts. quiet ) ;
700
+
701
+ let quiet = opts. format == OutputFormat :: Terse ;
702
+ let mut out = HumanFormatter :: new ( output, use_color ( opts) , quiet) ;
677
703
let mut st = ConsoleTestState :: new ( opts) ?;
678
704
679
705
let mut ntest = 0 ;
@@ -702,7 +728,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
702
728
}
703
729
}
704
730
705
- if !opts . quiet {
731
+ if !quiet {
706
732
if ntest != 0 || nbench != 0 || nmetric != 0 {
707
733
out. write_plain ( "\n " ) ?;
708
734
}
@@ -732,7 +758,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
732
758
TeTimeout ( ref test) => out. write_timeout ( test) ,
733
759
TeResult ( test, result, stdout) => {
734
760
st. write_log_result ( & test, & result) ?;
735
- out. write_result ( & result) ?;
761
+ out. write_result ( & test , & result) ?;
736
762
match result {
737
763
TrOk => {
738
764
st. passed += 1 ;
@@ -778,8 +804,11 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
778
804
Some ( t) => Pretty ( t) ,
779
805
} ;
780
806
781
- let mut out = HumanFormatter :: new ( output, use_color ( opts) , opts. quiet ) ;
782
-
807
+ let mut out: Box < OutputFormatter > = match opts. format {
808
+ OutputFormat :: Pretty => Box :: new ( HumanFormatter :: new ( output, use_color ( opts) , false ) ) ,
809
+ OutputFormat :: Terse => Box :: new ( HumanFormatter :: new ( output, use_color ( opts) , true ) ) ,
810
+ OutputFormat :: Json => Box :: new ( JsonFormatter :: new ( output) ) ,
811
+ } ;
783
812
let mut st = ConsoleTestState :: new ( opts) ?;
784
813
fn len_if_padded ( t : & TestDescAndFn ) -> usize {
785
814
match t. testfn . padding ( ) {
@@ -791,7 +820,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
791
820
let n = t. desc . name . as_slice ( ) ;
792
821
st. max_name_len = n. len ( ) ;
793
822
}
794
- run_tests ( opts, tests, |x| callback ( & x, & mut st, & mut out) ) ?;
823
+ run_tests ( opts, tests, |x| callback ( & x, & mut st, & mut * out) ) ?;
795
824
796
825
assert ! ( st. current_test_count( ) == st. total) ;
797
826
0 commit comments