Skip to content

Commit 1fa8aae

Browse files
committed
Auto merge of rust-lang#138603 - xizheyin:issue-137405, r=<try>
Report line number of test when should_panic test failed Closes rust-lang#137405 --- try-job: x86_64-gnu-llvm-19-3 try-job: test-various
2 parents c6c1796 + 4f5edfd commit 1fa8aae

6 files changed

+118
-2
lines changed

library/test/src/test_result.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ pub(crate) fn calc_result<'a>(
4545
time_opts: Option<&time::TestTimeOptions>,
4646
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
48+
let fn_location = if !desc.source_file.is_empty() {
49+
&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
50+
} else {
51+
""
52+
};
53+
4854
let result = match (&desc.should_panic, task_result) {
4955
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
5056
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
@@ -72,7 +78,7 @@ pub(crate) fn calc_result<'a>(
7278
}
7379
}
7480
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
81+
TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
7682
}
7783
_ => TestResult::TrFailed,
7884
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL
55
failures:
66

77
---- $DIR/failed-doctest-should-panic.rs - Foo (line 10) stdout ----
8-
note: test did not panic as expected
8+
note: test did not panic as expected at $DIR/failed-doctest-should-panic.rs:10:0
99

1010
failures:
1111
$DIR/failed-doctest-should-panic.rs - Foo (line 10)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ no-prefer-dynamic
2+
//@ compile-flags: --test
3+
//@ run-flags: --test-threads=1 --nocapture
4+
//@ run-fail
5+
//@ check-run-results
6+
//@ exec-env:RUST_BACKTRACE=0
7+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
8+
//@ needs-threads
9+
10+
#[test]
11+
#[should_panic]
12+
fn should_panic_with_any_message() {
13+
panic!("Panic!");
14+
}
15+
16+
#[test]
17+
#[should_panic = "message"]
18+
fn should_panic_with_message() {
19+
panic!("message");
20+
}
21+
22+
#[test]
23+
#[should_panic]
24+
fn should_panic_with_any_message_does_not_panic() {
25+
// DON'T PANIC
26+
}
27+
28+
#[test]
29+
#[should_panic = "message"]
30+
fn should_panic_with_message_does_not_panic() {
31+
// DON'T PANIC
32+
}
33+
34+
#[test]
35+
#[should_panic = "message"]
36+
fn should_panic_with_substring_panics_with_incorrect_string() {
37+
panic!("ZOMGWTFBBQ");
38+
}
39+
40+
#[test]
41+
#[should_panic = "message"]
42+
fn should_panic_with_substring_panics_with_non_string_value() {
43+
panic!(123); //~ WARNING panic message is not a string literal
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
thread 'should_panic_with_any_message' panicked at $DIR/test-should-panic-failed-show-span.rs:13:5:
3+
Panic!
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
6+
thread 'should_panic_with_message' panicked at $DIR/test-should-panic-failed-show-span.rs:19:5:
7+
message
8+
9+
thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:37:5:
10+
ZOMGWTFBBQ
11+
12+
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:43:5:
13+
Box<dyn Any>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
running 6 tests
3+
test should_panic_with_any_message - should panic ... ok
4+
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
5+
test should_panic_with_message - should panic ... ok
6+
test should_panic_with_message_does_not_panic - should panic ... FAILED
7+
test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED
8+
test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED
9+
10+
failures:
11+
12+
---- should_panic_with_any_message_does_not_panic stdout ----
13+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:24:4
14+
---- should_panic_with_message_does_not_panic stdout ----
15+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:30:4
16+
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
17+
note: panic did not contain expected string
18+
panic message: `"ZOMGWTFBBQ"`,
19+
expected substring: `"message"`
20+
---- should_panic_with_substring_panics_with_non_string_value stdout ----
21+
note: expected panic with string value,
22+
found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)`
23+
expected substring: `"message"`
24+
25+
failures:
26+
should_panic_with_any_message_does_not_panic
27+
should_panic_with_message_does_not_panic
28+
should_panic_with_substring_panics_with_incorrect_string
29+
should_panic_with_substring_panics_with_non_string_value
30+
31+
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
32+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: panic message is not a string literal
2+
--> $DIR/test-should-panic-failed-show-span.rs:43:12
3+
|
4+
LL | panic!(123);
5+
| ^^^
6+
|
7+
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
9+
= note: `#[warn(non_fmt_panics)]` on by default
10+
help: add a "{}" format string to `Display` the message
11+
|
12+
LL | panic!("{}", 123);
13+
| +++++
14+
help: or use std::panic::panic_any instead
15+
|
16+
LL - panic!(123);
17+
LL + std::panic::panic_any(123);
18+
|
19+
20+
warning: 1 warning emitted
21+

0 commit comments

Comments
 (0)