@@ -39,6 +39,7 @@ impl DocTestRunner {
3939 doctest : & DocTestBuilder ,
4040 scraped_test : & ScrapedDocTest ,
4141 target_str : & str ,
42+ opts : & RustdocOptions ,
4243 ) {
4344 let ignore = match scraped_test. langstr . ignore {
4445 Ignore :: All => true ,
@@ -62,6 +63,7 @@ impl DocTestRunner {
6263 self . nb_tests,
6364 & mut self . output,
6465 & mut self . output_merged_tests,
66+ opts,
6567 ) ,
6668 ) ) ;
6769 self . supports_color &= doctest. supports_color ;
@@ -134,13 +136,20 @@ mod __doctest_mod {{
134136 }}
135137
136138 #[allow(unused)]
137- pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> ExitCode {{
139+ pub fn doctest_runner(bin: &std::path::Path, test_nb: usize, should_panic: bool ) -> ExitCode {{
138140 let out = std::process::Command::new(bin)
139141 .env(self::RUN_OPTION, test_nb.to_string())
140142 .args(std::env::args().skip(1).collect::<Vec<_>>())
141143 .output()
142144 .expect(\" failed to run command\" );
143- if !out.status.success() {{
145+ if should_panic {{
146+ if out.status.code() != Some(test::ERROR_EXIT_CODE) {{
147+ eprintln!(\" Test didn't panic, but it's marked `should_panic`.\" );
148+ ExitCode::FAILURE
149+ }} else {{
150+ ExitCode::SUCCESS
151+ }}
152+ }} else if !out.status.success() {{
144153 if let Some(code) = out.status.code() {{
145154 eprintln!(\" Test executable failed (exit status: {{code}}).\" );
146155 }} else {{
@@ -223,6 +232,7 @@ fn generate_mergeable_doctest(
223232 id : usize ,
224233 output : & mut String ,
225234 output_merged_tests : & mut String ,
235+ opts : & RustdocOptions ,
226236) -> String {
227237 let test_id = format ! ( "__doctest_{id}" ) ;
228238
@@ -256,22 +266,29 @@ fn main() {returns_result} {{
256266 )
257267 . unwrap ( ) ;
258268 }
259- let not_running = ignore || scraped_test. langstr . no_run ;
269+ let should_panic = scraped_test. langstr . should_panic ;
270+ // On some platforms, exit code can only be 0 or 1, preventing us to actually know if it's a
271+ // panic. In this case, we ignore the tests with `should_panic`.
272+ //
273+ // Emscripten can catch panics but other wasm targets cannot
274+ let ignore_because_no_process_support = should_panic
275+ && ( cfg ! ( target_family = "wasm" ) || cfg ! ( target_os = "zkvm" ) )
276+ && !cfg ! ( target_os = "emscripten" ) ;
277+ let not_running = ignore_because_no_process_support || ignore || scraped_test. no_run ( opts) ;
260278 writeln ! (
261279 output_merged_tests,
262280 "
263281mod {test_id} {{
264282pub const TEST: test::TestDescAndFn = test::TestDescAndFn::new_doctest(
265- {test_name:?}, {ignore}, {file:?}, {line}, {no_run}, {should_panic} ,
283+ {test_name:?}, {ignore}, {file:?}, {line}, {no_run}, false ,
266284test::StaticTestFn(
267285 || {{{runner}}},
268286));
269287}}" ,
270288 test_name = scraped_test. name,
271289 file = scraped_test. path( ) ,
272290 line = scraped_test. line,
273- no_run = scraped_test. langstr. no_run,
274- should_panic = !scraped_test. langstr. no_run && scraped_test. langstr. should_panic,
291+ no_run = scraped_test. no_run( opts) ,
275292 // Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
276293 // don't give it the function to run.
277294 runner = if not_running {
@@ -280,7 +297,7 @@ test::StaticTestFn(
280297 format!(
281298 "
282299if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
283- test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}))
300+ test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}, {should_panic} ))
284301}} else {{
285302 test::assert_test_result(doctest_bundle::{test_id}::__main_fn())
286303}}
0 commit comments