Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less confusing message for failed test returning Result #97586

Closed
wants to merge 1 commit into from

Conversation

printercu
Copy link

@printercu printercu commented May 31, 2022

Failure message contained a part that is related to assert_eq! matcher.
This is confusing in the cases when test fails because of Err result.

Test:

#[test]
fn test() -> Result<(), &'static str> {
    Err("some failure")?;
    assert_eq!(1, 2);
    Ok(())
}

Before:

---- tests::test stdout ----
Error: "some failure"
thread 'tests::test' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/test/src/lib.rs:187:5

After:

 ---- tests::test stdout ----
Error: "some failure"
thread 'tests::test' panicked at 'the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/test/src/lib.rs:187:5

Fixes #69517

Failure message contained a part that is related to `assert_eq!` matcher.
This is confusing in the cases when test fails because of Err result.

Test:
```rust
#[test]
fn test() -> Result<(), &'static str> {
    Err("some failure")?;
    assert_eq!(1, 2);
    Ok(())
}
```

Before:
```
---- tests::test stdout ----
Error: "some failure"
thread 'tests::test' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/test/src/lib.rs:187:5
```

After:
```
 ---- tests::test stdout ----
Error: "some failure"
thread 'tests::test' panicked at 'the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/test/src/lib.rs:187:5
```
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 31, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @m-ou-se (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 31, 2022
@ehuss
Copy link
Contributor

ehuss commented May 31, 2022

This may close #69517.

@@ -182,8 +182,8 @@ fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
/// and checks for a `0` result.
pub fn assert_test_result<T: Termination>(result: T) {
let code = result.report().to_i32();
assert_eq!(
code, 0,
assert!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_eq also accepts an error message

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was assert_eq with error message. It results in confusing output, please see examples in initial comment or diff's comment

Copy link
Contributor

@ssomers ssomers Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert_eq here doesn't help, because the message already reports the code value, if it needs to be reported at all. Instead, it harms as shown in the description, in #69517 and in #59263.

In fact the fix was already approved as #59263 but eventually landed in an outdated repository.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 3, 2022
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jul 31, 2022

📌 Commit b4cc991 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2022
@klensy
Copy link
Contributor

klensy commented Jul 31, 2022

And without test and/or comment, this can be accidentally changed back.

@Mark-Simulacrum
Copy link
Member

@bors r-

Good call! We can add a test pretty easily along the lines of src/test/ui/test-attrs/test-panic-abort-nocapture.rs, which runs some tests and captures the stdout in src/test/ui/test-attrs/test-panic-abort-nocapture.run.stdout.

(Obviously, we don't need the panic=abort-ness for this test).

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 31, 2022
@bors
Copy link
Contributor

bors commented Oct 2, 2022

☔ The latest upstream changes (presumably #102586) made this pull request unmergeable. Please resolve the merge conflicts.

@printercu printercu closed this Oct 3, 2022
@bobhy
Copy link

bobhy commented Dec 18, 2022

It seems that this PR was unmergable (by the time it was accepted) and the issue was simply closed. Would a new PR allow the issue to be reopened?

The original problem is that test functions cannot report meaningful errors if they return a Err(E) from a ? operator (but can if same line uses .unwrap() instead), agreed?
That does force developers to use non-idiomatic syntax, but only in tests. That's kind of hard on our poor little brains. So, I would be glad to take a crack at a PR to fix this if it could be reviewed soonish.

@metasim
Copy link

metasim commented Dec 20, 2022

@printercu @Mark-Simulacrum Would you consider re-submitting this?

@printercu
Copy link
Author

@metasim #100451 made this pr obsolete.

@printercu printercu deleted the patch-1 branch December 20, 2022 21:02
@printercu
Copy link
Author

@bobhy As I understand tests will try to print error into stderr if it is fmt::Debug (https://doc.rust-lang.org/src/std/process.rs.html#2211). I'm not sure if it's possible to display any arbitrary type in the same way.

@cole-abbeduto-particle
Copy link

cole-abbeduto-particle commented Mar 17, 2023

@bobhy As I understand tests will try to print error into stderr if it is fmt::Debug (https://doc.rust-lang.org/src/std/process.rs.html#2211). I'm not sure if it's possible to display any arbitrary type in the same way.

That is true and a slight improvement. But we've also lost the line number in the process.

unwrap:

---- test::will_this_fail stdout ----
thread 'test::will_this_fail' panicked at 'called `Result::unwrap()` on an `Err` value: Is this a failure', /src/bin/file.rs:179:65
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

return Err or ?:

---- test::will_this_fail stdout ----
Error: Is this a failure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clearer error for failed tests which returned a Result Err()