Skip to content

rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]

Nicolás Antinori linux-kernel-mentees lkml rust-for-linux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 of 3 patches 2 repliesIn review

Changelog from the cover letter

- RFC patch: [3]

1. Supporting #[should_panic] [2]:

2 of 3 patches are in this archive. The cover letter announces 3.

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
2/3rust: kunit: allow same test name with different #[cfg(...)]rust/kernel/kunit.rs rust/macros/kunit.rs+37 -50 replies
3/3rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic caserust/kernel/bitmap.rs+18 -220 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]

This patch series intends to implement two features for KUnit tests written in Rust. The work is based on a TODO comment made in the bitmap.rs module [1].

Changelog:
- RFC patch: [3]

1. Supporting #[should_panic] [2]:

KUnit tests in Rust follow the user-space syntax, but at the moment #[should_panic] is not supported. The first patch of this series adds support for the attribute (only in its basic form, #[should_panic = "message"] is not supported yet).

The way it is supported is by adding a new boolean property called rust_should_panic to kunit_attributes struct in kunit/test.h and check for the try_catch->try_result code and the named flag to detect if there was a panic in lib/kunit/test.c::kunit_run_case_catch_errors function.

The difference with the previous version [3] is that now, almost everything is handled in the C side, meaning that we don't have a separate #[panic_handler] when KUnit tests are active. In other words there's no need of a Rust side panic handler because the C side catches Rust's panics too, since the #[panic_hanlder] executes a bindings::BUG().

2. Allow same test name with different #[cfg(...)]:

When testing #[should_panic] in bitmap.rs (check the last patch of the series) I found that the test that was supposed to panic had the same name as another one, but they were run on different configurations. This caused the following compilation error:

ERROR:root:error[E0428]: the name kunit_rust_wrapper_owned_bitmap_out_of_bounds is defined multiple times
--> ../rust/kernel/bitmap.rs:503:1

    |
503 | #[macros::kunit_tests(rust_kernel_bitmap)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `kunit_rust_wrapper_owned_bitmap_out_of_bounds` redefined here
    |

= note: kunit_rust_wrapper_owned_bitmap_out_of_bounds must be defined only once in the value namespace of this module = note: this error originates in the attribute macro macros::kunit_tests (in Nightly builds, run with -Z macro-backtrace for more info)

Now, only the tests with an active #[cfg(...)] will be executed by the runner. Tests with an inactive #[cfg(...)] will not appear in the report since they are not added to the TEST_CASES array.

[1] https://github.com/Rust-for-Linux/linux/blob/fd73f4a6659897191fa0d40695fe370925dd3780/rust/kernel/bitmap.rs#L592-L600
[2] https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html#testing-panics
[3] https://lore.kernel.org/rust-for-linux/[email protected]/

Nicolás Antinori (3):
rust: kunit: add #[should_panic] support
rust: kunit: allow same test name with different #[cfg(...)]
rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case

 include/kunit/test.h   |  1 +
 lib/kunit/attributes.c | 18 ++++++++++++++++
 lib/kunit/test.c       |  5 +++++
 rust/kernel/bitmap.rs  | 40 ++++++++++++++++-------------------
 rust/kernel/kunit.rs   | 37 ++++++++++++++++++++++++++++++++
 rust/macros/kunit.rs   | 48 +++++++++++++++++++++++++++++++++---------
 6 files changed, 117 insertions(+), 32 deletions(-)

--
2.47.3