Skip to content

Commit 63efcd4

Browse files
Add regression test for #143009
1 parent 3811364 commit 63efcd4

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/librustdoc/doctest/runner.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,13 @@ mod __doctest_mod {{
135135
.args(std::env::args().skip(1).collect::<Vec<_>>())
136136
.output()
137137
.expect(\"failed to run command\");
138-
if should_panic && out.status.code() != Some(101) {{
139-
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
138+
if should_panic {{
139+
if out.status.code() != Some(101) {{
140+
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
141+
ExitCode::FAILURE
142+
}} else {{
143+
ExitCode::SUCCESS
144+
}}
140145
}} else if !out.status.success() {{
141146
if let Some(code) = out.status.code() {{
142147
eprintln!(\"Test executable failed (exit status: {{code}}).\");
@@ -281,7 +286,7 @@ if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
281286
test::assert_test_result(doctest_bundle::{test_id}::__main_fn())
282287
}}
283288
",
284-
should_panic = !scraped_test.langstr.no_run && scraped_test.langstr.should_panic,
289+
should_panic = scraped_test.langstr.should_panic,
285290
)
286291
},
287292
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Ensure that `should_panic` doctests only succeed if the test actually panicked.
2+
// Regression test for <https://github.com/rust-lang/rust/issues/143009>.
3+
4+
//@ needs-target-std
5+
6+
use run_make_support::rustdoc;
7+
8+
fn check_output(output: String, edition: &str) {
9+
let should_contain = &[
10+
"test test.rs - bad_exit_code (line 1) ... FAILED",
11+
"test test.rs - did_not_panic (line 6) ... FAILED",
12+
"test test.rs - did_panic (line 11) ... ok",
13+
"---- test.rs - bad_exit_code (line 1) stdout ----
14+
Test executable failed (exit status: 1).",
15+
"---- test.rs - did_not_panic (line 6) stdout ----
16+
Test didn't panic, but it's marked `should_panic`.",
17+
"test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out;",
18+
];
19+
for text in should_contain {
20+
assert!(
21+
output.contains(text),
22+
"output doesn't contains (edition: {edition}) {:?}\nfull output: {output}",
23+
text
24+
);
25+
}
26+
}
27+
28+
fn main() {
29+
check_output(rustdoc().input("test.rs").arg("--test").run_fail().stdout_utf8(), "2015");
30+
31+
// Same check with the merged doctest feature (enabled with the 2024 edition).
32+
check_output(
33+
rustdoc().input("test.rs").arg("--test").edition("2024").run_fail().stdout_utf8(),
34+
"2024",
35+
);
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// ```
2+
/// std::process::exit(1);
3+
/// ```
4+
fn bad_exit_code() {}
5+
6+
/// ```should_panic
7+
/// std::process::exit(1);
8+
/// ```
9+
fn did_not_panic() {}
10+
11+
/// ```should_panic
12+
/// panic!("yeay");
13+
/// ```
14+
fn did_panic() {}

0 commit comments

Comments
 (0)