Skip to content

Commit 44d1d86

Browse files
committed
libtest: Pass the test's panic payload as Option instead of Result
Passing a `Result<(), &dyn Any>` to `calc_result` requires awkward code at both call sites, for no real benefit. It's much easier to just pass the payload as `Option<&dyn Any>`. No functional change, except that the owned payload is dropped slightly later.
1 parent c6c1796 commit 44d1d86

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

Diff for: library/test/src/lib.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,11 @@ fn run_test_in_process(
666666

667667
io::set_output_capture(None);
668668

669-
let test_result = match result {
670-
Ok(()) => calc_result(&desc, Ok(()), time_opts.as_ref(), exec_time.as_ref()),
671-
Err(e) => calc_result(&desc, Err(e.as_ref()), time_opts.as_ref(), exec_time.as_ref()),
672-
};
669+
// Determine whether the test passed or failed, by comparing its panic
670+
// payload (if any) with its `ShouldPanic` value, and by checking for
671+
// fatal timeout.
672+
let test_result =
673+
calc_result(&desc, result.err().as_deref(), time_opts.as_ref(), exec_time.as_ref());
673674
let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec();
674675
let message = CompletedTest::new(id, desc, test_result, exec_time, stdout);
675676
monitor_ch.send(message).unwrap();
@@ -741,10 +742,7 @@ fn spawn_test_subprocess(
741742
fn run_test_in_spawned_subprocess(desc: TestDesc, runnable_test: RunnableTest) -> ! {
742743
let builtin_panic_hook = panic::take_hook();
743744
let record_result = Arc::new(move |panic_info: Option<&'_ PanicHookInfo<'_>>| {
744-
let test_result = match panic_info {
745-
Some(info) => calc_result(&desc, Err(info.payload()), None, None),
746-
None => calc_result(&desc, Ok(()), None, None),
747-
};
745+
let test_result = calc_result(&desc, panic_info.map(|info| info.payload()), None, None);
748746

749747
// We don't support serializing TrFailedMsg, so just
750748
// print the message out to stderr.

Diff for: library/test/src/test_result.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ pub enum TestResult {
3939

4040
/// Creates a `TestResult` depending on the raw result of test execution
4141
/// and associated data.
42-
pub(crate) fn calc_result<'a>(
42+
pub(crate) fn calc_result(
4343
desc: &TestDesc,
44-
task_result: Result<(), &'a (dyn Any + 'static + Send)>,
44+
panic_payload: Option<&(dyn Any + Send)>,
4545
time_opts: Option<&time::TestTimeOptions>,
4646
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
48-
let result = match (&desc.should_panic, task_result) {
49-
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
50-
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
48+
let result = match (desc.should_panic, panic_payload) {
49+
// The test did or didn't panic, as expected.
50+
(ShouldPanic::No, None) | (ShouldPanic::Yes, Some(_)) => TestResult::TrOk,
51+
52+
// Check the actual panic message against the expected message.
53+
(ShouldPanic::YesWithMessage(msg), Some(err)) => {
5154
let maybe_panic_str = err
5255
.downcast_ref::<String>()
5356
.map(|e| &**e)
@@ -71,10 +74,14 @@ pub(crate) fn calc_result<'a>(
7174
))
7275
}
7376
}
74-
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
77+
78+
// The test should have panicked, but didn't panic.
79+
(ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
7580
TestResult::TrFailedMsg("test did not panic as expected".to_string())
7681
}
77-
_ => TestResult::TrFailed,
82+
83+
// The test should not have panicked, but did panic.
84+
(ShouldPanic::No, Some(_)) => TestResult::TrFailed,
7885
};
7986

8087
// If test is already failed (or allowed to fail), do not change the result.

0 commit comments

Comments
 (0)