@@ -490,7 +490,7 @@ impl TimeThreshold {
490
490
let durations_str = env:: var ( env_var_name) . ok ( ) ?;
491
491
492
492
// Split string into 2 substrings by comma and try to parse numbers.
493
- let durations : Vec < u64 > = durations_str
493
+ let mut durations = durations_str
494
494
. splitn ( 2 , ',' )
495
495
. map ( |v| {
496
496
u64:: from_str ( v) . unwrap_or_else ( |_| {
@@ -499,18 +499,20 @@ impl TimeThreshold {
499
499
env_var_name, v
500
500
)
501
501
} )
502
- } )
503
- . collect ( ) ;
502
+ } ) ;
504
503
505
- // Check that we have exactly 2 numbers .
506
- if durations . len ( ) != 2 {
504
+ // Callback to be called if the environment variable has unexpected structure .
505
+ let panic_on_incorrect_value = || {
507
506
panic ! (
508
507
"Duration variable {} expected to have 2 numbers separated by comma, but got {}" ,
509
- env_var_name, durations . len ( )
508
+ env_var_name, durations_str
510
509
) ;
511
- }
510
+ } ;
512
511
513
- let ( warn, critical) = ( durations[ 0 ] , durations[ 1 ] ) ;
512
+ let ( warn, critical) = (
513
+ durations. next ( ) . unwrap_or_else ( panic_on_incorrect_value) ,
514
+ durations. next ( ) . unwrap_or_else ( panic_on_incorrect_value)
515
+ ) ;
514
516
515
517
Some ( Self :: new ( Duration :: from_millis ( warn) , Duration :: from_millis ( critical) ) )
516
518
}
@@ -519,6 +521,8 @@ impl TimeThreshold {
519
521
/// Structure with parameters for calculating test execution time.
520
522
#[ derive( Copy , Clone , Debug , Default , PartialEq , Eq ) ]
521
523
pub struct TestTimeOptions {
524
+ /// Denotes if the test critical execution time limit excess should be considered
525
+ /// a test failure.
522
526
pub error_on_excess : bool ,
523
527
pub colored : bool ,
524
528
pub unit_threshold : TimeThreshold ,
@@ -609,6 +613,8 @@ pub struct TestOpts {
609
613
610
614
/// Result of parsing the options.
611
615
pub type OptRes = Result < TestOpts , String > ;
616
+ /// Result of parsing the option part.
617
+ type OptPartRes < T > = Result < Option < T > , String > ;
612
618
613
619
fn optgroups ( ) -> getopts:: Options {
614
620
let mut opts = getopts:: Options :: new ( ) ;
@@ -772,6 +778,27 @@ macro_rules! unstable_optflag {
772
778
} } ;
773
779
}
774
780
781
+ // Gets the CLI options assotiated with `report-time` feature.
782
+ fn get_time_options (
783
+ matches : & getopts:: Matches ,
784
+ allow_unstable : bool )
785
+ -> Option < OptPartRes < TestTimeOptions > > {
786
+ let report_time = unstable_optflag ! ( matches, allow_unstable, "report-time" ) ;
787
+ let colored_opt_str = matches. opt_str ( "report-time" ) ;
788
+ let report_time_colored = report_time && colored_opt_str == Some ( "colored" . into ( ) ) ;
789
+ let ensure_test_time = unstable_optflag ! ( matches, allow_unstable, "ensure-test-time" ) ;
790
+
791
+ // If `ensure-test-time` option is provided, time output is enforced,
792
+ // so user won't be confused if any of tests will silently fail.
793
+ let options = if report_time || ensure_test_time {
794
+ Some ( TestTimeOptions :: new_from_env ( ensure_test_time, report_time_colored) )
795
+ } else {
796
+ None
797
+ } ;
798
+
799
+ Some ( Ok ( options) )
800
+ }
801
+
775
802
// Parses command line arguments into test options
776
803
pub fn parse_opts ( args : & [ String ] ) -> Option < OptRes > {
777
804
let mut allow_unstable = false ;
@@ -842,17 +869,10 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
842
869
} ;
843
870
}
844
871
845
- let report_time = unstable_optflag ! ( matches, allow_unstable, "report-time" ) ;
846
- let colored_opt_str = matches. opt_str ( "report-time" ) ;
847
- let report_time_colored = report_time && colored_opt_str == Some ( "colored" . into ( ) ) ;
848
- let ensure_test_time = unstable_optflag ! ( matches, allow_unstable, "ensure-test-time" ) ;
849
-
850
- // If `ensure-test-time` option is provided, time output is enforced,
851
- // so user won't be confused if any of tests will silently fail.
852
- let time_options = if report_time || ensure_test_time {
853
- Some ( TestTimeOptions :: new_from_env ( ensure_test_time, report_time_colored) )
854
- } else {
855
- None
872
+ let time_options = match get_time_options ( & matches, allow_unstable) {
873
+ Some ( Ok ( val) ) => val,
874
+ Some ( Err ( e) ) => return Some ( Err ( e) ) ,
875
+ x => panic ! ( "Unexpected output from `get_time_options`: {:?}" , x) ,
856
876
} ;
857
877
858
878
let test_threads = match matches. opt_str ( "test-threads" ) {
0 commit comments