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

Improvements to the #[should_panic] feature #37749

Merged
merged 2 commits into from
Nov 18, 2016

Conversation

keeperofdakeys
Copy link
Contributor

Add more error checking for the #[should_panic] attribute, and print the expected panic string when it does not match.

Fixes #29000

Eg:

test test2 ... ok
test test1 ... FAILED
: Panic did not include expected string 'foo'
test test3 ... FAILED

failures:

---- test1 stdout ----
	thread 'test1' panicked at 'bar', test.rs:7
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- test3 stdout ----
	thread 'test3' panicked at 'bar', test.rs:18

@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 @nrc (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@GuillaumeGomez
Copy link
Member

I restarted the test which failed (for some reasons...).

@est31
Copy link
Member

est31 commented Nov 13, 2016

You should also add tests to check

  • when the panic message doesn't match, the test fails
  • when the panic message matches, the test succeeds

@keeperofdakeys
Copy link
Contributor Author

You should also add tests to check

when the panic message doesn't match, the test fails
when the panic message matches, the test succeeds

Aren't the tests that are already present covering this?
https://github.com/rust-lang/rust/blob/master/src/libtest/lib.rs#L1526-L1597

@est31
Copy link
Member

est31 commented Nov 13, 2016

@keeperofdakeys yes, but this only tests part of the code. The parts that you added aren't tested (extracting the string from the expected attribute). It would be nicer to test all of it.

@keeperofdakeys
Copy link
Contributor Author

I've updated the test_should_panic_bad_message test so that it also checks the failed message.

@nrc
Copy link
Member

nrc commented Nov 14, 2016

Hmm, I suppose this is a back-compat breaking change because people who used the bad forms previously will get an error when there was none previously. Annoyingly, Crater will not pick up such errors. I think therefore that this needs to be a warning, rather than an error for one cycle.

@nrc
Copy link
Member

nrc commented Nov 14, 2016

LGTM, otherwise

@keeperofdakeys
Copy link
Contributor Author

Since it seems you can't insert lints in libsyntax, I've changed the errors into warnings, and added notes about future changes.

@est31
Copy link
Member

est31 commented Nov 14, 2016

@keeperofdakeys that's still not fullfilling the complete potential of the test, It still doesn't test whether the thing you specify in expected is actually the string that's being used for the panic message, and not something else.

@keeperofdakeys
Copy link
Contributor Author

@est31 I don't understand the exact point you're trying to make, could you elaborate?

@brson
Copy link
Contributor

brson commented Nov 15, 2016

This looks good, except that I'm not sure about the new output:

test test1 ... FAILED
: Panic did not include expected string 'foo'

I don't think there's any other such inline output about what happened in the test for any other sort of test, and I'm reluctant to start the precedent here. Usually this type of information is printed to stderr, captured by the test runner, and printed as part of the final summary list of failures, and I'd prefer if this information could be presented similarly.

@est31
Copy link
Member

est31 commented Nov 15, 2016

@keeperofdakeys the unit tests you linked to use internal testing functions, and don't use the #[should_panic] attribute. If there is a bug in parsing of that attribute (the code you add in this PR), they won't detect it.

@keeperofdakeys keeperofdakeys force-pushed the should-panic branch 2 times, most recently from 1eee8e3 to 72ee3e6 Compare November 15, 2016 12:13
@keeperofdakeys
Copy link
Contributor Author

keeperofdakeys commented Nov 15, 2016

I've updated the output to:

running 3 tests
test test1 ... FAILED
test test2 ... FAILED
test test3 ... ok

failures:

---- test1 stdout ----
    thread 'test1' panicked at 'explicit panic', ./test.rs:6
note: Run with `RUST_BACKTRACE=1` for a backtrace.
note: Panic did not include expected string 'foo'
---- test2 stdout ----
    thread 'test2' panicked at 'bar', ./test.rs:12
note: Panic did not include expected string 'foo'

failures:
    test1
    test2

test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured

@est31 Ah, thanks for clarifying. Since writing a unit test for fn should_panic in libsyntax/test.rs seems non-trivial, I'm thinking that a test in src/test/run-fail with a panic! that doesn't match the expected value would be the best.

@nrc
Copy link
Member

nrc commented Nov 18, 2016

@bors: r+

@bors
Copy link
Contributor

bors commented Nov 18, 2016

📌 Commit 72ee3e6 has been approved by nrc

@bors
Copy link
Contributor

bors commented Nov 18, 2016

⌛ Testing commit 72ee3e6 with merge 0a1b0f4...

@bors
Copy link
Contributor

bors commented Nov 18, 2016

💔 Test failed - auto-linux-64-opt

@keeperofdakeys
Copy link
Contributor Author

Whoops, looks like I accidentally made a heisentest. I've split it out into two files now.

@keeperofdakeys
Copy link
Contributor Author

Strange, travis-ci failed before compilation.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Nov 18, 2016

📌 Commit fb5ccf8 has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Nov 18, 2016

⌛ Testing commit fb5ccf8 with merge 195c42c...

bors added a commit that referenced this pull request Nov 18, 2016
Improvements to the #[should_panic] feature

Add more error checking for the `#[should_panic]` attribute, and print the expected panic string when it does not match.

Fixes #29000

Eg:
```running 3 tests
test test2 ... ok
test test1 ... FAILED
: Panic did not include expected string 'foo'
test test3 ... FAILED

failures:

---- test1 stdout ----
	thread 'test1' panicked at 'bar', test.rs:7
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- test3 stdout ----
	thread 'test3' panicked at 'bar', test.rs:18

```
@bors bors merged commit fb5ccf8 into rust-lang:master Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants