From c73598f0fb0ef2cfcdc784e577a96bb8041445ba Mon Sep 17 00:00:00 2001 From: xizheyin Date: Mon, 17 Mar 2025 22:22:32 +0800 Subject: [PATCH 1/2] Report span of test when should_panic test failed Signed-off-by: xizheyin --- library/test/src/test_result.rs | 7 +++- .../failed-doctest-should-panic.stdout | 2 +- .../test-should-panic-failed-show-span.rs | 42 +++++++++++++++++++ ...t-should-panic-failed-show-span.run.stdout | 38 +++++++++++++++++ .../test-should-panic-failed-show-span.stderr | 21 ++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 tests/ui/test-attrs/test-should-panic-failed-show-span.rs create mode 100644 tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout create mode 100644 tests/ui/test-attrs/test-should-panic-failed-show-span.stderr diff --git a/library/test/src/test_result.rs b/library/test/src/test_result.rs index 959cd730fa4..a312894c25c 100644 --- a/library/test/src/test_result.rs +++ b/library/test/src/test_result.rs @@ -77,7 +77,12 @@ pub(crate) fn calc_result( // The test should have panicked, but didn't panic. (ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => { - TestResult::TrFailedMsg("test did not panic as expected".to_string()) + let fn_location = if !desc.source_file.is_empty() { + &format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col) + } else { + "" + }; + TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location)) } // The test should not have panicked, but did panic. diff --git a/tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout b/tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout index 90c0463d832..2b04b77c9dc 100644 --- a/tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout +++ b/tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout @@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL failures: ---- $DIR/failed-doctest-should-panic.rs - Foo (line 10) stdout ---- -note: test did not panic as expected +note: test did not panic as expected at $DIR/failed-doctest-should-panic.rs:10:0 failures: $DIR/failed-doctest-should-panic.rs - Foo (line 10) diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.rs b/tests/ui/test-attrs/test-should-panic-failed-show-span.rs new file mode 100644 index 00000000000..960673bcc0d --- /dev/null +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.rs @@ -0,0 +1,42 @@ +//@ run-fail +//@ check-run-results +//@ compile-flags: --test +//@ exec-env:RUST_BACKTRACE=0 +//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" +//@ run-flags: --test-threads=1 + +#[test] +#[should_panic] +fn should_panic_with_any_message() { + panic!("Panic!"); +} + +#[test] +#[should_panic = "message"] +fn should_panic_with_message() { + panic!("message"); +} + +#[test] +#[should_panic] +fn should_panic_with_any_message_does_not_panic() { + // DON'T PANIC +} + +#[test] +#[should_panic = "message"] +fn should_panic_with_message_does_not_panic() { + // DON'T PANIC +} + +#[test] +#[should_panic = "message"] +fn should_panic_with_substring_panics_with_incorrect_string() { + panic!("ZOMGWTFBBQ"); +} + +#[test] +#[should_panic = "message"] +fn should_panic_with_substring_panics_with_non_string_value() { + panic!(123); //~ WARNING panic message is not a string literal +} diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout new file mode 100644 index 00000000000..4edc67694b9 --- /dev/null +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout @@ -0,0 +1,38 @@ + +running 6 tests +test should_panic_with_any_message - should panic ... ok +test should_panic_with_any_message_does_not_panic - should panic ... FAILED +test should_panic_with_message - should panic ... ok +test should_panic_with_message_does_not_panic - should panic ... FAILED +test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED +test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED + +failures: + +---- should_panic_with_any_message_does_not_panic stdout ---- +note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:22:4 +---- should_panic_with_message_does_not_panic stdout ---- +note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:28:4 +---- should_panic_with_substring_panics_with_incorrect_string stdout ---- + +thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:35:5: +ZOMGWTFBBQ +note: panic did not contain expected string + panic message: `"ZOMGWTFBBQ"`, + expected substring: `"message"` +---- should_panic_with_substring_panics_with_non_string_value stdout ---- + +thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:41:5: +Box +note: expected panic with string value, + found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)` + expected substring: `"message"` + +failures: + should_panic_with_any_message_does_not_panic + should_panic_with_message_does_not_panic + should_panic_with_substring_panics_with_incorrect_string + should_panic_with_substring_panics_with_non_string_value + +test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr b/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr new file mode 100644 index 00000000000..e803ff5513c --- /dev/null +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr @@ -0,0 +1,21 @@ +warning: panic message is not a string literal + --> $DIR/test-should-panic-failed-show-span.rs:41:12 + | +LL | panic!(123); + | ^^^ + | + = note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021 + = note: for more information, see + = note: `#[warn(non_fmt_panics)]` on by default +help: add a "{}" format string to `Display` the message + | +LL | panic!("{}", 123); + | +++++ +help: or use std::panic::panic_any instead + | +LL - panic!(123); +LL + std::panic::panic_any(123); + | + +warning: 1 warning emitted + From dc3a586eed198548316fcbfff2d5c28df0728750 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Tue, 8 Apr 2025 09:40:37 +0800 Subject: [PATCH 2/2] Adjust test directives Signed-off-by: xizheyin --- .../test-should-panic-failed-show-span.rs | 10 ++++++--- ...t-should-panic-failed-show-span.run.stderr | 13 ++++++++++++ ...t-should-panic-failed-show-span.run.stdout | 12 +++-------- .../test-should-panic-failed-show-span.stderr | 21 ------------------- 4 files changed, 23 insertions(+), 33 deletions(-) create mode 100644 tests/ui/test-attrs/test-should-panic-failed-show-span.run.stderr delete mode 100644 tests/ui/test-attrs/test-should-panic-failed-show-span.stderr diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.rs b/tests/ui/test-attrs/test-should-panic-failed-show-span.rs index 960673bcc0d..f400f614142 100644 --- a/tests/ui/test-attrs/test-should-panic-failed-show-span.rs +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.rs @@ -1,9 +1,12 @@ +//@ compile-flags: --test +//@ run-flags: --test-threads=1 --nocapture //@ run-fail //@ check-run-results -//@ compile-flags: --test //@ exec-env:RUST_BACKTRACE=0 //@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" -//@ run-flags: --test-threads=1 +//@ normalize-stdout: "TypeId\(0x[0-9a-f]+\)" -> "TypeId($$HEX)" +//@ needs-threads +//@ needs-unwind (panic) #[test] #[should_panic] @@ -37,6 +40,7 @@ fn should_panic_with_substring_panics_with_incorrect_string() { #[test] #[should_panic = "message"] +#[expect(non_fmt_panics)] fn should_panic_with_substring_panics_with_non_string_value() { - panic!(123); //~ WARNING panic message is not a string literal + panic!(123); } diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stderr b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stderr new file mode 100644 index 00000000000..db379a16b52 --- /dev/null +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stderr @@ -0,0 +1,13 @@ + +thread 'should_panic_with_any_message' panicked at $DIR/test-should-panic-failed-show-span.rs:14:5: +Panic! +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + +thread 'should_panic_with_message' panicked at $DIR/test-should-panic-failed-show-span.rs:20:5: +message + +thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:38:5: +ZOMGWTFBBQ + +thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:45:5: +Box diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout index 4edc67694b9..75600b4d3d6 100644 --- a/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout +++ b/tests/ui/test-attrs/test-should-panic-failed-show-span.run.stdout @@ -10,22 +10,16 @@ test should_panic_with_substring_panics_with_non_string_value - should panic ... failures: ---- should_panic_with_any_message_does_not_panic stdout ---- -note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:22:4 +note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:25:4 ---- should_panic_with_message_does_not_panic stdout ---- -note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:28:4 +note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:31:4 ---- should_panic_with_substring_panics_with_incorrect_string stdout ---- - -thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:35:5: -ZOMGWTFBBQ note: panic did not contain expected string panic message: `"ZOMGWTFBBQ"`, expected substring: `"message"` ---- should_panic_with_substring_panics_with_non_string_value stdout ---- - -thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:41:5: -Box note: expected panic with string value, - found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)` + found non-string value: `TypeId($HEX)` expected substring: `"message"` failures: diff --git a/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr b/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr deleted file mode 100644 index e803ff5513c..00000000000 --- a/tests/ui/test-attrs/test-should-panic-failed-show-span.stderr +++ /dev/null @@ -1,21 +0,0 @@ -warning: panic message is not a string literal - --> $DIR/test-should-panic-failed-show-span.rs:41:12 - | -LL | panic!(123); - | ^^^ - | - = note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021 - = note: for more information, see - = note: `#[warn(non_fmt_panics)]` on by default -help: add a "{}" format string to `Display` the message - | -LL | panic!("{}", 123); - | +++++ -help: or use std::panic::panic_any instead - | -LL - panic!(123); -LL + std::panic::panic_any(123); - | - -warning: 1 warning emitted -