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