@@ -129,15 +129,19 @@ mod __doctest_mod {{
129129 }}
130130
131131 #[allow(unused)]
132- pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> ExitCode {{
132+ pub fn doctest_runner(bin: &std::path::Path, test_nb: usize, should_panic: bool ) -> ExitCode {{
133133 let out = std::process::Command::new(bin)
134134 .env(self::RUN_OPTION, test_nb.to_string())
135135 .args(std::env::args().skip(1).collect::<Vec<_>>())
136136 .output()
137137 .expect(\" failed to run command\" );
138138 if !out.status.success() {{
139139 if let Some(code) = out.status.code() {{
140- eprintln!(\" Test executable failed (exit status: {{code}}).\" );
140+ if should_panic {{
141+ eprintln!(\" Test didn't panic, but it's marked `should_panic`.\" );
142+ }} else {{
143+ eprintln!(\" Test executable failed (exit status: {{code}}).\" );
144+ }}
141145 }} else {{
142146 eprintln!(\" Test executable failed (terminated by signal).\" );
143147 }}
@@ -220,34 +224,40 @@ fn generate_mergeable_doctest(
220224 output_merged_tests : & mut String ,
221225) -> String {
222226 let test_id = format ! ( "__doctest_{id}" ) ;
227+ let should_panic = !scraped_test. langstr . no_run && scraped_test. langstr . should_panic ;
223228
224229 if ignore {
225230 // We generate nothing else.
226231 writeln ! ( output, "pub mod {test_id} {{}}\n " ) . unwrap ( ) ;
227232 } else {
228233 writeln ! ( output, "pub mod {test_id} {{\n {}{}" , doctest. crates, doctest. maybe_crate_attrs)
229234 . unwrap ( ) ;
235+ let mut returns_result = false ;
230236 if doctest. has_main_fn {
231237 output. push_str ( & doctest. everything_else ) ;
232238 } else {
233- let returns_result = if doctest. everything_else . trim_end ( ) . ends_with ( "(())" ) {
239+ let return_value = if doctest. everything_else . trim_end ( ) . ends_with ( "(())" ) {
240+ returns_result = true ;
234241 "-> Result<(), impl core::fmt::Debug>"
235242 } else {
236243 ""
237244 } ;
238245 write ! (
239246 output,
240247 "\
241- fn main() {returns_result } {{
248+ fn main() {return_value } {{
242249{}
243250}}" ,
244251 doctest. everything_else
245252 )
246253 . unwrap ( ) ;
247254 }
255+ let ( should_panic_pre, should_panic_post) =
256+ super :: make:: should_panic_wrapper ( should_panic, returns_result) ;
248257 writeln ! (
249258 output,
250- "\n pub fn __main_fn() -> impl std::process::Termination {{ main() }} \n }}\n "
259+ "\n pub fn __main_fn() -> impl std::process::Termination {{{should_panic_pre}main()\
260+ {should_panic_post}}} \n }}\n "
251261 )
252262 . unwrap ( ) ;
253263 }
@@ -257,7 +267,7 @@ fn main() {returns_result} {{
257267 "
258268mod {test_id} {{
259269pub const TEST: test::TestDescAndFn = test::TestDescAndFn::new_doctest(
260- {test_name:?}, {ignore}, {file:?}, {line}, {no_run}, {should_panic} ,
270+ {test_name:?}, {ignore}, {file:?}, {line}, {no_run}, false ,
261271test::StaticTestFn(
262272 || {{{runner}}},
263273));
@@ -266,7 +276,6 @@ test::StaticTestFn(
266276 file = scraped_test. path( ) ,
267277 line = scraped_test. line,
268278 no_run = scraped_test. langstr. no_run,
269- should_panic = !scraped_test. langstr. no_run && scraped_test. langstr. should_panic,
270279 // Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
271280 // don't give it the function to run.
272281 runner = if not_running {
@@ -275,7 +284,7 @@ test::StaticTestFn(
275284 format!(
276285 "
277286if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
278- test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}))
287+ test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}, {should_panic} ))
279288}} else {{
280289 test::assert_test_result(doctest_bundle::{test_id}::__main_fn())
281290}}
0 commit comments