Skip to content

Commit 641f107

Browse files
Correcty handle should_panic on unsupported targets
1 parent adaf3e0 commit 641f107

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

library/test/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd
570570
.collect()
571571
}
572572

573+
pub fn cannot_handle_should_panic() -> bool {
574+
(cfg!(target_family = "wasm") || cfg!(target_os = "zkvm")) && !cfg!(target_os = "emscripten")
575+
}
576+
573577
pub fn run_test(
574578
opts: &TestOpts,
575579
force_ignore: bool,
@@ -581,9 +585,8 @@ pub fn run_test(
581585
let TestDescAndFn { desc, testfn } = test;
582586

583587
// Emscripten can catch panics but other wasm targets cannot
584-
let ignore_because_no_process_support = desc.should_panic != ShouldPanic::No
585-
&& (cfg!(target_family = "wasm") || cfg!(target_os = "zkvm"))
586-
&& !cfg!(target_os = "emscripten");
588+
let ignore_because_no_process_support =
589+
desc.should_panic != ShouldPanic::No && cannot_handle_should_panic();
587590

588591
if force_ignore || desc.ignore || ignore_because_no_process_support {
589592
let message = CompletedTest::new(id, desc, TrIgnored, None, Vec::new());

src/librustdoc/doctest.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,17 @@ impl CreateRunnableDocTests {
10711071
}
10721072
}
10731073

1074+
fn compute_ignore(langstr: &LangString, target_str: String) -> bool {
1075+
if langstr.should_panic && test::cannot_handle_should_panic() {
1076+
return true;
1077+
}
1078+
match langstr.ignore {
1079+
Ignore::All => true,
1080+
Ignore::None => false,
1081+
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
1082+
}
1083+
}
1084+
10741085
fn generate_test_desc_and_fn(
10751086
test: DocTestBuilder,
10761087
scraped_test: ScrapedDocTest,
@@ -1086,11 +1097,7 @@ fn generate_test_desc_and_fn(
10861097
test::TestDescAndFn {
10871098
desc: test::TestDesc {
10881099
name: test::DynTestName(scraped_test.name.clone()),
1089-
ignore: match scraped_test.langstr.ignore {
1090-
Ignore::All => true,
1091-
Ignore::None => false,
1092-
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
1093-
},
1100+
ignore: compute_ignore(&scraped_test.langstr, target_str),
10941101
ignore_message: None,
10951102
source_file: "",
10961103
start_line: 0,

src/librustdoc/doctest/runner.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,13 @@ test::StaticTestFn(
267267
runner = if not_running {
268268
"test::assert_test_result(Ok::<(), String>(()))".to_string()
269269
} else {
270+
// One case to consider: if this is a `should_panic` doctest, on some targets, libtest
271+
// ignores such tests because it's not supported. The first `if` checks exactly that.
270272
format!(
271273
"
272-
if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
274+
if {should_panic} && test::cannot_handle_should_panic() {{
275+
test::assert_test_result(Ok::<(), String>(()))
276+
}} else if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
273277
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}, {should_panic}))
274278
}} else {{
275279
test::assert_test_result(doctest_bundle::{test_id}::__main_fn())

0 commit comments

Comments
 (0)