]> git.lizzy.rs Git - rust.git/blob - library/test/src/lib.rs
Rollup merge of #82093 - bjorn3:more_atomic_tests, r=kennytm
[rust.git] / library / test / src / lib.rs
1 //! Support code for rustc's built in unit-test and micro-benchmarking
2 //! framework.
3 //!
4 //! Almost all user code will only be interested in `Bencher` and
5 //! `black_box`. All other interactions (such as writing tests and
6 //! benchmarks themselves) should be done via the `#[test]` and
7 //! `#[bench]` attributes.
8 //!
9 //! See the [Testing Chapter](../book/ch11-00-testing.html) of the book for more details.
10
11 // Currently, not much of this is meant for users. It is intended to
12 // support the simplest interface possible for representing and
13 // running tests while providing a base that other test frameworks may
14 // build off of.
15
16 // N.B., this is also specified in this crate's Cargo.toml, but librustc_ast contains logic specific to
17 // this crate, which relies on this attribute (rather than the value of `--crate-name` passed by
18 // cargo) to detect this crate.
19
20 #![crate_name = "test"]
21 #![unstable(feature = "test", issue = "50297")]
22 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
23 #![cfg_attr(unix, feature(libc))]
24 #![feature(rustc_private)]
25 #![feature(nll)]
26 #![feature(available_concurrency)]
27 #![feature(internal_output_capture)]
28 #![feature(option_unwrap_none)]
29 #![feature(panic_unwind)]
30 #![feature(staged_api)]
31 #![feature(termination_trait_lib)]
32 #![feature(test)]
33 #![feature(total_cmp)]
34 #![feature(str_split_once)]
35
36 // Public reexports
37 pub use self::bench::{black_box, Bencher};
38 pub use self::console::run_tests_console;
39 pub use self::options::{ColorConfig, Options, OutputFormat, RunIgnored, ShouldPanic};
40 pub use self::types::TestName::*;
41 pub use self::types::*;
42 pub use self::ColorConfig::*;
43 pub use cli::TestOpts;
44
45 // Module to be used by rustc to compile tests in libtest
46 pub mod test {
47     pub use crate::{
48         assert_test_result,
49         bench::Bencher,
50         cli::{parse_opts, TestOpts},
51         filter_tests,
52         helpers::metrics::{Metric, MetricMap},
53         options::{Options, RunIgnored, RunStrategy, ShouldPanic},
54         run_test, test_main, test_main_static,
55         test_result::{TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk},
56         time::{TestExecTime, TestTimeOptions},
57         types::{
58             DynTestFn, DynTestName, StaticBenchFn, StaticTestFn, StaticTestName, TestDesc,
59             TestDescAndFn, TestName, TestType,
60         },
61     };
62 }
63
64 use std::{
65     collections::VecDeque,
66     env, io,
67     io::prelude::Write,
68     panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo},
69     process::{self, Command, Termination},
70     sync::mpsc::{channel, Sender},
71     sync::{Arc, Mutex},
72     thread,
73     time::{Duration, Instant},
74 };
75
76 pub mod bench;
77 mod cli;
78 mod console;
79 mod event;
80 mod formatters;
81 mod helpers;
82 mod options;
83 pub mod stats;
84 mod test_result;
85 mod time;
86 mod types;
87
88 #[cfg(test)]
89 mod tests;
90
91 use event::{CompletedTest, TestEvent};
92 use helpers::concurrency::get_concurrency;
93 use helpers::exit_code::get_exit_code;
94 use options::{Concurrent, RunStrategy};
95 use test_result::*;
96 use time::TestExecTime;
97
98 // Process exit code to be used to indicate test failures.
99 const ERROR_EXIT_CODE: i32 = 101;
100
101 const SECONDARY_TEST_INVOKER_VAR: &str = "__RUST_TEST_INVOKE";
102
103 // The default console test runner. It accepts the command line
104 // arguments and a vector of test_descs.
105 pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Options>) {
106     let mut opts = match cli::parse_opts(args) {
107         Some(Ok(o)) => o,
108         Some(Err(msg)) => {
109             eprintln!("error: {}", msg);
110             process::exit(ERROR_EXIT_CODE);
111         }
112         None => return,
113     };
114     if let Some(options) = options {
115         opts.options = options;
116     }
117     if opts.list {
118         if let Err(e) = console::list_tests_console(&opts, tests) {
119             eprintln!("error: io error when listing tests: {:?}", e);
120             process::exit(ERROR_EXIT_CODE);
121         }
122     } else {
123         match console::run_tests_console(&opts, tests) {
124             Ok(true) => {}
125             Ok(false) => process::exit(ERROR_EXIT_CODE),
126             Err(e) => {
127                 eprintln!("error: io error when listing tests: {:?}", e);
128                 process::exit(ERROR_EXIT_CODE);
129             }
130         }
131     }
132 }
133
134 /// A variant optimized for invocation with a static test vector.
135 /// This will panic (intentionally) when fed any dynamic tests.
136 ///
137 /// This is the entry point for the main function generated by `rustc --test`
138 /// when panic=unwind.
139 pub fn test_main_static(tests: &[&TestDescAndFn]) {
140     let args = env::args().collect::<Vec<_>>();
141     let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
142     test_main(&args, owned_tests, None)
143 }
144
145 /// A variant optimized for invocation with a static test vector.
146 /// This will panic (intentionally) when fed any dynamic tests.
147 ///
148 /// Runs tests in panic=abort mode, which involves spawning subprocesses for
149 /// tests.
150 ///
151 /// This is the entry point for the main function generated by `rustc --test`
152 /// when panic=abort.
153 pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
154     // If we're being run in SpawnedSecondary mode, run the test here. run_test
155     // will then exit the process.
156     if let Ok(name) = env::var(SECONDARY_TEST_INVOKER_VAR) {
157         env::remove_var(SECONDARY_TEST_INVOKER_VAR);
158         let test = tests
159             .iter()
160             .filter(|test| test.desc.name.as_slice() == name)
161             .map(make_owned_test)
162             .next()
163             .unwrap_or_else(|| panic!("couldn't find a test with the provided name '{}'", name));
164         let TestDescAndFn { desc, testfn } = test;
165         let testfn = match testfn {
166             StaticTestFn(f) => f,
167             _ => panic!("only static tests are supported"),
168         };
169         run_test_in_spawned_subprocess(desc, Box::new(testfn));
170     }
171
172     let args = env::args().collect::<Vec<_>>();
173     let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
174     test_main(&args, owned_tests, Some(Options::new().panic_abort(true)))
175 }
176
177 /// Clones static values for putting into a dynamic vector, which test_main()
178 /// needs to hand out ownership of tests to parallel test runners.
179 ///
180 /// This will panic when fed any dynamic tests, because they cannot be cloned.
181 fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
182     match test.testfn {
183         StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: test.desc.clone() },
184         StaticBenchFn(f) => TestDescAndFn { testfn: StaticBenchFn(f), desc: test.desc.clone() },
185         _ => panic!("non-static tests passed to test::test_main_static"),
186     }
187 }
188
189 /// Invoked when unit tests terminate. Should panic if the unit
190 /// Tests is considered a failure. By default, invokes `report()`
191 /// and checks for a `0` result.
192 pub fn assert_test_result<T: Termination>(result: T) {
193     let code = result.report();
194     assert_eq!(
195         code, 0,
196         "the test returned a termination value with a non-zero status code ({}) \
197          which indicates a failure",
198         code
199     );
200 }
201
202 pub fn run_tests<F>(
203     opts: &TestOpts,
204     tests: Vec<TestDescAndFn>,
205     mut notify_about_test_event: F,
206 ) -> io::Result<()>
207 where
208     F: FnMut(TestEvent) -> io::Result<()>,
209 {
210     use std::collections::{self, HashMap};
211     use std::hash::BuildHasherDefault;
212     use std::sync::mpsc::RecvTimeoutError;
213
214     struct RunningTest {
215         join_handle: Option<thread::JoinHandle<()>>,
216     }
217
218     // Use a deterministic hasher
219     type TestMap =
220         HashMap<TestDesc, RunningTest, BuildHasherDefault<collections::hash_map::DefaultHasher>>;
221
222     struct TimeoutEntry {
223         desc: TestDesc,
224         timeout: Instant,
225     }
226
227     let tests_len = tests.len();
228
229     let mut filtered_tests = filter_tests(opts, tests);
230     if !opts.bench_benchmarks {
231         filtered_tests = convert_benchmarks_to_tests(filtered_tests);
232     }
233
234     let filtered_tests = {
235         let mut filtered_tests = filtered_tests;
236         for test in filtered_tests.iter_mut() {
237             test.desc.name = test.desc.name.with_padding(test.testfn.padding());
238         }
239
240         filtered_tests
241     };
242
243     let filtered_out = tests_len - filtered_tests.len();
244     let event = TestEvent::TeFilteredOut(filtered_out);
245     notify_about_test_event(event)?;
246
247     let filtered_descs = filtered_tests.iter().map(|t| t.desc.clone()).collect();
248
249     let event = TestEvent::TeFiltered(filtered_descs);
250     notify_about_test_event(event)?;
251
252     let (filtered_tests, filtered_benchs): (Vec<_>, _) = filtered_tests
253         .into_iter()
254         .partition(|e| matches!(e.testfn, StaticTestFn(_) | DynTestFn(_)));
255
256     let concurrency = opts.test_threads.unwrap_or_else(get_concurrency);
257
258     let mut remaining = filtered_tests;
259     remaining.reverse();
260     let mut pending = 0;
261
262     let (tx, rx) = channel::<CompletedTest>();
263     let run_strategy = if opts.options.panic_abort && !opts.force_run_in_process {
264         RunStrategy::SpawnPrimary
265     } else {
266         RunStrategy::InProcess
267     };
268
269     let mut running_tests: TestMap = HashMap::default();
270     let mut timeout_queue: VecDeque<TimeoutEntry> = VecDeque::new();
271
272     fn get_timed_out_tests(
273         running_tests: &TestMap,
274         timeout_queue: &mut VecDeque<TimeoutEntry>,
275     ) -> Vec<TestDesc> {
276         let now = Instant::now();
277         let mut timed_out = Vec::new();
278         while let Some(timeout_entry) = timeout_queue.front() {
279             if now < timeout_entry.timeout {
280                 break;
281             }
282             let timeout_entry = timeout_queue.pop_front().unwrap();
283             if running_tests.contains_key(&timeout_entry.desc) {
284                 timed_out.push(timeout_entry.desc);
285             }
286         }
287         timed_out
288     }
289
290     fn calc_timeout(timeout_queue: &VecDeque<TimeoutEntry>) -> Option<Duration> {
291         timeout_queue.front().map(|&TimeoutEntry { timeout: next_timeout, .. }| {
292             let now = Instant::now();
293             if next_timeout >= now { next_timeout - now } else { Duration::new(0, 0) }
294         })
295     }
296
297     if concurrency == 1 {
298         while !remaining.is_empty() {
299             let test = remaining.pop().unwrap();
300             let event = TestEvent::TeWait(test.desc.clone());
301             notify_about_test_event(event)?;
302             run_test(opts, !opts.run_tests, test, run_strategy, tx.clone(), Concurrent::No)
303                 .unwrap_none();
304             let completed_test = rx.recv().unwrap();
305
306             let event = TestEvent::TeResult(completed_test);
307             notify_about_test_event(event)?;
308         }
309     } else {
310         while pending > 0 || !remaining.is_empty() {
311             while pending < concurrency && !remaining.is_empty() {
312                 let test = remaining.pop().unwrap();
313                 let timeout = time::get_default_test_timeout();
314                 let desc = test.desc.clone();
315
316                 let event = TestEvent::TeWait(desc.clone());
317                 notify_about_test_event(event)?; //here no pad
318                 let join_handle = run_test(
319                     opts,
320                     !opts.run_tests,
321                     test,
322                     run_strategy,
323                     tx.clone(),
324                     Concurrent::Yes,
325                 );
326                 running_tests.insert(desc.clone(), RunningTest { join_handle });
327                 timeout_queue.push_back(TimeoutEntry { desc, timeout });
328                 pending += 1;
329             }
330
331             let mut res;
332             loop {
333                 if let Some(timeout) = calc_timeout(&timeout_queue) {
334                     res = rx.recv_timeout(timeout);
335                     for test in get_timed_out_tests(&running_tests, &mut timeout_queue) {
336                         let event = TestEvent::TeTimeout(test);
337                         notify_about_test_event(event)?;
338                     }
339
340                     match res {
341                         Err(RecvTimeoutError::Timeout) => {
342                             // Result is not yet ready, continue waiting.
343                         }
344                         _ => {
345                             // We've got a result, stop the loop.
346                             break;
347                         }
348                     }
349                 } else {
350                     res = rx.recv().map_err(|_| RecvTimeoutError::Disconnected);
351                     break;
352                 }
353             }
354
355             let mut completed_test = res.unwrap();
356             let running_test = running_tests.remove(&completed_test.desc).unwrap();
357             if let Some(join_handle) = running_test.join_handle {
358                 if let Err(_) = join_handle.join() {
359                     if let TrOk = completed_test.result {
360                         completed_test.result =
361                             TrFailedMsg("panicked after reporting success".to_string());
362                     }
363                 }
364             }
365
366             let event = TestEvent::TeResult(completed_test);
367             notify_about_test_event(event)?;
368             pending -= 1;
369         }
370     }
371
372     if opts.bench_benchmarks {
373         // All benchmarks run at the end, in serial.
374         for b in filtered_benchs {
375             let event = TestEvent::TeWait(b.desc.clone());
376             notify_about_test_event(event)?;
377             run_test(opts, false, b, run_strategy, tx.clone(), Concurrent::No);
378             let completed_test = rx.recv().unwrap();
379
380             let event = TestEvent::TeResult(completed_test);
381             notify_about_test_event(event)?;
382         }
383     }
384     Ok(())
385 }
386
387 pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
388     let mut filtered = tests;
389     let matches_filter = |test: &TestDescAndFn, filter: &str| {
390         let test_name = test.desc.name.as_slice();
391
392         match opts.filter_exact {
393             true => test_name == filter,
394             false => test_name.contains(filter),
395         }
396     };
397
398     // Remove tests that don't match the test filter
399     if !opts.filters.is_empty() {
400         filtered.retain(|test| opts.filters.iter().any(|filter| matches_filter(test, filter)));
401     }
402
403     // Skip tests that match any of the skip filters
404     filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
405
406     // Excludes #[should_panic] tests
407     if opts.exclude_should_panic {
408         filtered.retain(|test| test.desc.should_panic == ShouldPanic::No);
409     }
410
411     // maybe unignore tests
412     match opts.run_ignored {
413         RunIgnored::Yes => {
414             filtered.iter_mut().for_each(|test| test.desc.ignore = false);
415         }
416         RunIgnored::Only => {
417             filtered.retain(|test| test.desc.ignore);
418             filtered.iter_mut().for_each(|test| test.desc.ignore = false);
419         }
420         RunIgnored::No => {}
421     }
422
423     // Sort the tests alphabetically
424     filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));
425
426     filtered
427 }
428
429 pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
430     // convert benchmarks to tests, if we're not benchmarking them
431     tests
432         .into_iter()
433         .map(|x| {
434             let testfn = match x.testfn {
435                 DynBenchFn(bench) => DynTestFn(Box::new(move || {
436                     bench::run_once(|b| __rust_begin_short_backtrace(|| bench.run(b)))
437                 })),
438                 StaticBenchFn(benchfn) => DynTestFn(Box::new(move || {
439                     bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
440                 })),
441                 f => f,
442             };
443             TestDescAndFn { desc: x.desc, testfn }
444         })
445         .collect()
446 }
447
448 pub fn run_test(
449     opts: &TestOpts,
450     force_ignore: bool,
451     test: TestDescAndFn,
452     strategy: RunStrategy,
453     monitor_ch: Sender<CompletedTest>,
454     concurrency: Concurrent,
455 ) -> Option<thread::JoinHandle<()>> {
456     let TestDescAndFn { desc, testfn } = test;
457
458     // Emscripten can catch panics but other wasm targets cannot
459     let ignore_because_no_process_support = desc.should_panic != ShouldPanic::No
460         && cfg!(target_arch = "wasm32")
461         && !cfg!(target_os = "emscripten");
462
463     if force_ignore || desc.ignore || ignore_because_no_process_support {
464         let message = CompletedTest::new(desc, TrIgnored, None, Vec::new());
465         monitor_ch.send(message).unwrap();
466         return None;
467     }
468
469     struct TestRunOpts {
470         pub strategy: RunStrategy,
471         pub nocapture: bool,
472         pub concurrency: Concurrent,
473         pub time: Option<time::TestTimeOptions>,
474     }
475
476     fn run_test_inner(
477         desc: TestDesc,
478         monitor_ch: Sender<CompletedTest>,
479         testfn: Box<dyn FnOnce() + Send>,
480         opts: TestRunOpts,
481     ) -> Option<thread::JoinHandle<()>> {
482         let concurrency = opts.concurrency;
483         let name = desc.name.clone();
484
485         let runtest = move || match opts.strategy {
486             RunStrategy::InProcess => run_test_in_process(
487                 desc,
488                 opts.nocapture,
489                 opts.time.is_some(),
490                 testfn,
491                 monitor_ch,
492                 opts.time,
493             ),
494             RunStrategy::SpawnPrimary => spawn_test_subprocess(
495                 desc,
496                 opts.nocapture,
497                 opts.time.is_some(),
498                 monitor_ch,
499                 opts.time,
500             ),
501         };
502
503         // If the platform is single-threaded we're just going to run
504         // the test synchronously, regardless of the concurrency
505         // level.
506         let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32");
507         if concurrency == Concurrent::Yes && supports_threads {
508             let cfg = thread::Builder::new().name(name.as_slice().to_owned());
509             let mut runtest = Arc::new(Mutex::new(Some(runtest)));
510             let runtest2 = runtest.clone();
511             match cfg.spawn(move || runtest2.lock().unwrap().take().unwrap()()) {
512                 Ok(handle) => Some(handle),
513                 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
514                     // `ErrorKind::WouldBlock` means hitting the thread limit on some
515                     // platforms, so run the test synchronously here instead.
516                     Arc::get_mut(&mut runtest).unwrap().get_mut().unwrap().take().unwrap()();
517                     None
518                 }
519                 Err(e) => panic!("failed to spawn thread to run test: {}", e),
520             }
521         } else {
522             runtest();
523             None
524         }
525     }
526
527     let test_run_opts =
528         TestRunOpts { strategy, nocapture: opts.nocapture, concurrency, time: opts.time_options };
529
530     match testfn {
531         DynBenchFn(bencher) => {
532             // Benchmarks aren't expected to panic, so we run them all in-process.
533             crate::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
534                 bencher.run(harness)
535             });
536             None
537         }
538         StaticBenchFn(benchfn) => {
539             // Benchmarks aren't expected to panic, so we run them all in-process.
540             crate::bench::benchmark(desc, monitor_ch, opts.nocapture, benchfn);
541             None
542         }
543         DynTestFn(f) => {
544             match strategy {
545                 RunStrategy::InProcess => (),
546                 _ => panic!("Cannot run dynamic test fn out-of-process"),
547             };
548             run_test_inner(
549                 desc,
550                 monitor_ch,
551                 Box::new(move || __rust_begin_short_backtrace(f)),
552                 test_run_opts,
553             )
554         }
555         StaticTestFn(f) => run_test_inner(
556             desc,
557             monitor_ch,
558             Box::new(move || __rust_begin_short_backtrace(f)),
559             test_run_opts,
560         ),
561     }
562 }
563
564 /// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
565 #[inline(never)]
566 fn __rust_begin_short_backtrace<F: FnOnce()>(f: F) {
567     f();
568
569     // prevent this frame from being tail-call optimised away
570     black_box(());
571 }
572
573 fn run_test_in_process(
574     desc: TestDesc,
575     nocapture: bool,
576     report_time: bool,
577     testfn: Box<dyn FnOnce() + Send>,
578     monitor_ch: Sender<CompletedTest>,
579     time_opts: Option<time::TestTimeOptions>,
580 ) {
581     // Buffer for capturing standard I/O
582     let data = Arc::new(Mutex::new(Vec::new()));
583
584     if !nocapture {
585         io::set_output_capture(Some(data.clone()));
586     }
587
588     let start = report_time.then(Instant::now);
589     let result = catch_unwind(AssertUnwindSafe(testfn));
590     let exec_time = start.map(|start| {
591         let duration = start.elapsed();
592         TestExecTime(duration)
593     });
594
595     io::set_output_capture(None);
596
597     let test_result = match result {
598         Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time),
599         Err(e) => calc_result(&desc, Err(e.as_ref()), &time_opts, &exec_time),
600     };
601     let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec();
602     let message = CompletedTest::new(desc, test_result, exec_time, stdout);
603     monitor_ch.send(message).unwrap();
604 }
605
606 fn spawn_test_subprocess(
607     desc: TestDesc,
608     nocapture: bool,
609     report_time: bool,
610     monitor_ch: Sender<CompletedTest>,
611     time_opts: Option<time::TestTimeOptions>,
612 ) {
613     let (result, test_output, exec_time) = (|| {
614         let args = env::args().collect::<Vec<_>>();
615         let current_exe = &args[0];
616
617         let mut command = Command::new(current_exe);
618         command.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice());
619         if nocapture {
620             command.stdout(process::Stdio::inherit());
621             command.stderr(process::Stdio::inherit());
622         }
623
624         let start = report_time.then(Instant::now);
625         let output = match command.output() {
626             Ok(out) => out,
627             Err(e) => {
628                 let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e);
629                 return (TrFailed, err.into_bytes(), None);
630             }
631         };
632         let exec_time = start.map(|start| {
633             let duration = start.elapsed();
634             TestExecTime(duration)
635         });
636
637         let std::process::Output { stdout, stderr, status } = output;
638         let mut test_output = stdout;
639         formatters::write_stderr_delimiter(&mut test_output, &desc.name);
640         test_output.extend_from_slice(&stderr);
641
642         let result = match (|| -> Result<TestResult, String> {
643             let exit_code = get_exit_code(status)?;
644             Ok(get_result_from_exit_code(&desc, exit_code, &time_opts, &exec_time))
645         })() {
646             Ok(r) => r,
647             Err(e) => {
648                 write!(&mut test_output, "Unexpected error: {}", e).unwrap();
649                 TrFailed
650             }
651         };
652
653         (result, test_output, exec_time)
654     })();
655
656     let message = CompletedTest::new(desc, result, exec_time, test_output);
657     monitor_ch.send(message).unwrap();
658 }
659
660 fn run_test_in_spawned_subprocess(desc: TestDesc, testfn: Box<dyn FnOnce() + Send>) -> ! {
661     let builtin_panic_hook = panic::take_hook();
662     let record_result = Arc::new(move |panic_info: Option<&'_ PanicInfo<'_>>| {
663         let test_result = match panic_info {
664             Some(info) => calc_result(&desc, Err(info.payload()), &None, &None),
665             None => calc_result(&desc, Ok(()), &None, &None),
666         };
667
668         // We don't support serializing TrFailedMsg, so just
669         // print the message out to stderr.
670         if let TrFailedMsg(msg) = &test_result {
671             eprintln!("{}", msg);
672         }
673
674         if let Some(info) = panic_info {
675             builtin_panic_hook(info);
676         }
677
678         if let TrOk = test_result {
679             process::exit(test_result::TR_OK);
680         } else {
681             process::exit(test_result::TR_FAILED);
682         }
683     });
684     let record_result2 = record_result.clone();
685     panic::set_hook(Box::new(move |info| record_result2(Some(&info))));
686     testfn();
687     record_result(None);
688     unreachable!("panic=abort callback should have exited the process")
689 }