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

Standardise usage of #[should_panic] vs panic::catch_unwind in tests #715

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

edmorley
Copy link
Member

@edmorley edmorley commented Nov 2, 2023

The #[should_panic] annotation allows marking tests that are expected to panic. It accepts an expected 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 use panic::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:

  • For the tests address_for_port_when_container_crashed and app_dir_invalid_path, the tests now uses panic::catch_unwind allowing the assertions to match against the whole dynamic message, improving coverage.
  • For 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.

The `#[should_panic]` annotation allows marking tests that are
expected to panic. It accepts an `expected` 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 use
`panic::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:
- For the tests `address_for_port_when_container_crashed` and
  `app_dir_invalid_path`, the tests now uses `panic::catch_unwind`
  allowing the assertions to match against the whole dynamic message,
  improving coverage.
- For `expected_pack_failure_still_panics_for_non_pack_failure`, the
  test has been switched back to using `#[should_panic]` (as it was before
  #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
@edmorley edmorley self-assigned this Nov 2, 2023
@edmorley edmorley marked this pull request as ready for review November 2, 2023 13:59
@edmorley edmorley requested a review from a team as a code owner November 2, 2023 13:59
@edmorley edmorley merged commit a9759ee into main Nov 2, 2023
6 checks passed
@edmorley edmorley deleted the edmorley/catch-unwind branch November 2, 2023 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants