Standardise usage of #[should_panic]
vs panic::catch_unwind
in tests
#715
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
#[should_panic]
annotation allows marking tests that are expected to panic. It accepts anexpected
field which allows for substring matching against the panic message.panic::catch_unwind
allows doing something similar, but requires a lot more boilerplate. However, it can be used in cases where the panic message contains dynamic values and substring matching isn't adequate for the purpose of what the tests intends to test.In general I believe we should prefer
#[should_panic]
and only usepanic::catch_unwind
when it benefits the test.I've added this recommendation to the top of the integration tests file, and updated several of the tests to follow it.
Notably:
address_for_port_when_container_crashed
andapp_dir_invalid_path
, the tests now usespanic::catch_unwind
allowing the assertions to match against the whole dynamic message, improving coverage.expected_pack_failure_still_panics_for_non_pack_failure
, the test has been switched back to using#[should_panic]
(as it was before Support local buildpacks and meta-buildpacks in libcnb-test #666), since the purpose of the test is not to check the full error message of that specific failure mode, but instead to confirm that a non-pack failure isn't marked as a success when using.expected_pack_result()
.See:
https://doc.rust-lang.org/book/ch11-01-writing-tests.html#checking-for-panics-with-should_panic
https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
https://doc.rust-lang.org/std/panic/struct.AssertUnwindSafe.html
GUS-W-14445053.