Skip to content

Commit b292354

Browse files
Add regression tests for no_run and compile_fail
1 parent 630702b commit b292354

File tree

7 files changed

+150
-10
lines changed

7 files changed

+150
-10
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
//@ ignore-cross-compile
5+
6+
use run_make_support::rustdoc;
7+
8+
fn check_output(edition: &str, panic_abort: bool) {
9+
let mut rustdoc_cmd = rustdoc();
10+
rustdoc_cmd.input("test.rs").arg("--test").edition(edition);
11+
if panic_abort {
12+
rustdoc_cmd.args(["-C", "panic=abort"]);
13+
}
14+
let output = rustdoc_cmd.run_fail().stdout_utf8();
15+
let should_contain = &[
16+
"test test.rs - bad_exit_code (line 1) ... FAILED",
17+
"test test.rs - did_not_panic (line 6) ... FAILED",
18+
"test test.rs - did_panic (line 11) ... ok",
19+
"---- test.rs - bad_exit_code (line 1) stdout ----
20+
Test executable failed (exit status: 1).",
21+
"---- test.rs - did_not_panic (line 6) stdout ----
22+
Test didn't panic, but it's marked `should_panic` (got unexpected return code 1).",
23+
"test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out;",
24+
];
25+
for text in should_contain {
26+
assert!(
27+
output.contains(text),
28+
"output (edition: {edition}) doesn't contain {:?}\nfull output: {output}",
29+
text
30+
);
31+
}
32+
}
33+
34+
fn main() {
35+
check_output("2015", false);
36+
37+
// Same check with the merged doctest feature (enabled with the 2024 edition).
38+
check_output("2024", false);
39+
40+
// Checking that `-C panic=abort` is working too.
41+
check_output("2015", true);
42+
check_output("2024", true);
43+
}
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() {}

tests/rustdoc-ui/doctest/failed-doctest-should-panic.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// adapted to use that, and that normalize line can go away
33

44
//@ edition: 2024
5-
//@ compile-flags:--test
5+
//@ compile-flags:--test --test-args=--test-threads=1
66
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
77
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
88
//@ normalize-stdout: "ran in \d+\.\d+s" -> "ran in $$TIME"
99
//@ normalize-stdout: "compilation took \d+\.\d+s" -> "compilation took $$TIME"
1010
//@ failure-status: 101
1111

12-
/// ```should_panic
13-
/// println!("Hello, world!");
14-
/// ```
15-
pub struct Foo;
12+
//! ```should_panic
13+
//! println!("Hello, world!");
14+
//! ```
15+
//!
16+
//! ```should_panic
17+
//! std::process::exit(2);
18+
//! ```
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11

2-
running 1 test
3-
test $DIR/failed-doctest-should-panic.rs - Foo (line 12) ... FAILED
2+
running 2 tests
3+
test $DIR/failed-doctest-should-panic.rs - (line 12) ... FAILED
4+
test $DIR/failed-doctest-should-panic.rs - (line 16) ... FAILED
45

56
failures:
67

7-
---- $DIR/failed-doctest-should-panic.rs - Foo (line 12) stdout ----
8+
---- $DIR/failed-doctest-should-panic.rs - (line 12) stdout ----
89
Test didn't panic, but it's marked `should_panic`.
10+
---- $DIR/failed-doctest-should-panic.rs - (line 16) stdout ----
11+
Test didn't panic, but it's marked `should_panic` (got unexpected return code 2).
912

1013
failures:
11-
$DIR/failed-doctest-should-panic.rs - Foo (line 12)
14+
$DIR/failed-doctest-should-panic.rs - (line 12)
15+
$DIR/failed-doctest-should-panic.rs - (line 16)
1216

13-
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
17+
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
1418

1519
all doctests ran in $TIME; merged doctests compilation took $TIME
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
running 7 tests
3+
test $DIR/no-run.rs - f (line 14) - compile ... ok
4+
test $DIR/no-run.rs - f (line 17) - compile ... ok
5+
test $DIR/no-run.rs - f (line 20) ... ignored
6+
test $DIR/no-run.rs - f (line 23) - compile ... ok
7+
test $DIR/no-run.rs - f (line 31) - compile fail ... ok
8+
test $DIR/no-run.rs - f (line 36) - compile ... ok
9+
test $DIR/no-run.rs - f (line 40) - compile ... ok
10+
11+
test result: ok. 6 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in $TIME
12+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
running 5 tests
3+
test $DIR/no-run.rs - f (line 14) - compile ... ok
4+
test $DIR/no-run.rs - f (line 17) - compile ... ok
5+
test $DIR/no-run.rs - f (line 23) - compile ... ok
6+
test $DIR/no-run.rs - f (line 36) - compile ... ok
7+
test $DIR/no-run.rs - f (line 40) - compile ... ok
8+
9+
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
10+
11+
12+
running 2 tests
13+
test $DIR/no-run.rs - f (line 20) ... ignored
14+
test $DIR/no-run.rs - f (line 31) - compile fail ... ok
15+
16+
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in $TIME
17+
18+
all doctests ran in $TIME; merged doctests compilation took $TIME

tests/rustdoc-ui/doctest/no-run.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This test ensures that the `--no-run` flag works the same between normal and merged doctests.
2+
// Regression test for <https://github.com/rust-lang/rust/issues/143858>.
3+
4+
//@ check-pass
5+
//@ revisions: edition2021 edition2024
6+
//@ [edition2021]edition:2021
7+
//@ [edition2024]edition:2024
8+
//@ compile-flags:-Z unstable-options --test --no-run --test-args=--test-threads=1
9+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
10+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
11+
//@ normalize-stdout: "ran in \d+\.\d+s" -> "ran in $$TIME"
12+
//@ normalize-stdout: "compilation took \d+\.\d+s" -> "compilation took $$TIME"
13+
14+
/// ```
15+
/// let a = true;
16+
/// ```
17+
/// ```should_panic
18+
/// panic!()
19+
/// ```
20+
/// ```ignore (incomplete-code)
21+
/// fn foo() {
22+
/// ```
23+
/// ```no_run
24+
/// loop {
25+
/// println!("Hello, world");
26+
/// }
27+
/// ```
28+
///
29+
/// fails to compile
30+
///
31+
/// ```compile_fail
32+
/// let x = 5;
33+
/// x += 2; // shouldn't compile!
34+
/// ```
35+
/// Ok the test does not run
36+
/// ```
37+
/// panic!()
38+
/// ```
39+
/// Ok the test does not run
40+
/// ```should_panic
41+
/// loop {
42+
/// println!("Hello, world");
43+
/// panic!()
44+
/// }
45+
/// ```
46+
pub fn f() {}

0 commit comments

Comments
 (0)