@@ -178,11 +178,17 @@ pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
178
178
. next ( )
179
179
. unwrap_or_else ( || panic ! ( "couldn't find a test with the provided name '{name}'" ) ) ;
180
180
let TestDescAndFn { desc, testfn } = test;
181
- let testfn = match testfn {
182
- StaticTestFn ( f) => f,
183
- _ => panic ! ( "only static tests are supported" ) ,
184
- } ;
185
- run_test_in_spawned_subprocess ( desc, Box :: new ( testfn) ) ;
181
+ match testfn. into_runnable ( ) {
182
+ Runnable :: Test ( runnable_test) => {
183
+ if runnable_test. is_dynamic ( ) {
184
+ panic ! ( "only static tests are supported" ) ;
185
+ }
186
+ run_test_in_spawned_subprocess ( desc, runnable_test) ;
187
+ }
188
+ Runnable :: Bench ( _) => {
189
+ panic ! ( "benchmarks should not be executed into child processes" )
190
+ }
191
+ }
186
192
}
187
193
188
194
let args = env:: args ( ) . collect :: < Vec < _ > > ( ) ;
@@ -563,7 +569,7 @@ pub fn run_test(
563
569
id : TestId ,
564
570
desc : TestDesc ,
565
571
monitor_ch : Sender < CompletedTest > ,
566
- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
572
+ runnable_test : RunnableTest ,
567
573
opts : TestRunOpts ,
568
574
) -> Option < thread:: JoinHandle < ( ) > > {
569
575
let name = desc. name . clone ( ) ;
@@ -574,7 +580,7 @@ pub fn run_test(
574
580
desc,
575
581
opts. nocapture ,
576
582
opts. time . is_some ( ) ,
577
- testfn ,
583
+ runnable_test ,
578
584
monitor_ch,
579
585
opts. time ,
580
586
) ,
@@ -615,37 +621,21 @@ pub fn run_test(
615
621
let test_run_opts =
616
622
TestRunOpts { strategy, nocapture : opts. nocapture , time : opts. time_options } ;
617
623
618
- match testfn {
619
- DynBenchFn ( benchfn) => {
620
- // Benchmarks aren't expected to panic, so we run them all in-process.
621
- crate :: bench:: benchmark ( id, desc, monitor_ch, opts. nocapture , benchfn) ;
622
- None
624
+ match testfn. into_runnable ( ) {
625
+ Runnable :: Test ( runnable_test) => {
626
+ if runnable_test. is_dynamic ( ) {
627
+ match strategy {
628
+ RunStrategy :: InProcess => ( ) ,
629
+ _ => panic ! ( "Cannot run dynamic test fn out-of-process" ) ,
630
+ } ;
631
+ }
632
+ run_test_inner ( id, desc, monitor_ch, runnable_test, test_run_opts)
623
633
}
624
- StaticBenchFn ( benchfn ) => {
634
+ Runnable :: Bench ( runnable_bench ) => {
625
635
// Benchmarks aren't expected to panic, so we run them all in-process.
626
- crate :: bench :: benchmark ( id, desc, monitor_ch, opts. nocapture , benchfn ) ;
636
+ runnable_bench . run ( id, & desc, & monitor_ch, opts. nocapture ) ;
627
637
None
628
638
}
629
- DynTestFn ( f) => {
630
- match strategy {
631
- RunStrategy :: InProcess => ( ) ,
632
- _ => panic ! ( "Cannot run dynamic test fn out-of-process" ) ,
633
- } ;
634
- run_test_inner (
635
- id,
636
- desc,
637
- monitor_ch,
638
- Box :: new ( move || __rust_begin_short_backtrace ( f) ) ,
639
- test_run_opts,
640
- )
641
- }
642
- StaticTestFn ( f) => run_test_inner (
643
- id,
644
- desc,
645
- monitor_ch,
646
- Box :: new ( move || __rust_begin_short_backtrace ( f) ) ,
647
- test_run_opts,
648
- ) ,
649
639
}
650
640
}
651
641
@@ -663,7 +653,7 @@ fn run_test_in_process(
663
653
desc : TestDesc ,
664
654
nocapture : bool ,
665
655
report_time : bool ,
666
- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
656
+ runnable_test : RunnableTest ,
667
657
monitor_ch : Sender < CompletedTest > ,
668
658
time_opts : Option < time:: TestTimeOptions > ,
669
659
) {
@@ -675,7 +665,7 @@ fn run_test_in_process(
675
665
}
676
666
677
667
let start = report_time. then ( Instant :: now) ;
678
- let result = fold_err ( catch_unwind ( AssertUnwindSafe ( testfn ) ) ) ;
668
+ let result = fold_err ( catch_unwind ( AssertUnwindSafe ( || runnable_test . run ( ) ) ) ) ;
679
669
let exec_time = start. map ( |start| {
680
670
let duration = start. elapsed ( ) ;
681
671
TestExecTime ( duration)
@@ -760,10 +750,7 @@ fn spawn_test_subprocess(
760
750
monitor_ch. send ( message) . unwrap ( ) ;
761
751
}
762
752
763
- fn run_test_in_spawned_subprocess (
764
- desc : TestDesc ,
765
- testfn : Box < dyn FnOnce ( ) -> Result < ( ) , String > + Send > ,
766
- ) -> ! {
753
+ fn run_test_in_spawned_subprocess ( desc : TestDesc , runnable_test : RunnableTest ) -> ! {
767
754
let builtin_panic_hook = panic:: take_hook ( ) ;
768
755
let record_result = Arc :: new ( move |panic_info : Option < & ' _ PanicInfo < ' _ > > | {
769
756
let test_result = match panic_info {
@@ -789,7 +776,7 @@ fn run_test_in_spawned_subprocess(
789
776
} ) ;
790
777
let record_result2 = record_result. clone ( ) ;
791
778
panic:: set_hook ( Box :: new ( move |info| record_result2 ( Some ( info) ) ) ) ;
792
- if let Err ( message) = testfn ( ) {
779
+ if let Err ( message) = runnable_test . run ( ) {
793
780
panic ! ( "{}" , message) ;
794
781
}
795
782
record_result ( None ) ;
0 commit comments