Skip to content

libtest: Pass the test's panic payload as Option instead of Result #139511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,11 @@ fn run_test_in_process(

io::set_output_capture(None);

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

// We don't support serializing TrFailedMsg, so just
// print the message out to stderr.
Expand Down
21 changes: 14 additions & 7 deletions library/test/src/test_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ pub enum TestResult {

/// Creates a `TestResult` depending on the raw result of test execution
/// and associated data.
pub(crate) fn calc_result<'a>(
pub(crate) fn calc_result(
desc: &TestDesc,
task_result: Result<(), &'a (dyn Any + 'static + Send)>,
panic_payload: Option<&(dyn Any + Send)>,
time_opts: Option<&time::TestTimeOptions>,
exec_time: Option<&time::TestExecTime>,
) -> TestResult {
let result = match (&desc.should_panic, task_result) {
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
let result = match (desc.should_panic, panic_payload) {
// The test did or didn't panic, as expected.
(ShouldPanic::No, None) | (ShouldPanic::Yes, Some(_)) => TestResult::TrOk,

// Check the actual panic message against the expected message.
(ShouldPanic::YesWithMessage(msg), Some(err)) => {
let maybe_panic_str = err
.downcast_ref::<String>()
.map(|e| &**e)
Expand All @@ -71,10 +74,14 @@ pub(crate) fn calc_result<'a>(
))
}
}
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {

// The test should have panicked, but didn't panic.
(ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
TestResult::TrFailedMsg("test did not panic as expected".to_string())
}
_ => TestResult::TrFailed,

// The test should not have panicked, but did panic.
(ShouldPanic::No, Some(_)) => TestResult::TrFailed,
};

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