mirror of
https://github.com/torvalds/linux.git
synced 2025-11-02 09:40:27 +02:00
The KUnit `#[test]` support that landed recently is very basic and does
not map the `assert*!` macros into KUnit like the doctests do, so they
panic at the moment.
Thus implement the custom mapping in a similar way to doctests, reusing
the infrastructure there.
In Rust 1.88.0, the `file()` method in `Span` may be stable [1]. However,
it was changed recently (from `SourceFile`), so we need to do something
different in previous versions. Thus create a helper for it and use it
to get the path.
With this, a failing test suite like:
#[kunit_tests(my_test_suite)]
mod tests {
use super::*;
#[test]
fn my_first_test() {
assert_eq!(42, 43);
}
#[test]
fn my_second_test() {
assert!(42 >= 43);
}
}
will properly map back to KUnit, printing something like:
[ 1.924325] KTAP version 1
[ 1.924421] # Subtest: my_test_suite
[ 1.924506] # speed: normal
[ 1.924525] 1..2
[ 1.926385] # my_first_test: ASSERTION FAILED at rust/kernel/lib.rs:251
[ 1.926385] Expected 42 == 43 to be true, but is false
[ 1.928026] # my_first_test.speed: normal
[ 1.928075] not ok 1 my_first_test
[ 1.928723] # my_second_test: ASSERTION FAILED at rust/kernel/lib.rs:256
[ 1.928723] Expected 42 >= 43 to be true, but is false
[ 1.929834] # my_second_test.speed: normal
[ 1.929868] not ok 2 my_second_test
[ 1.930032] # my_test_suite: pass:0 fail:2 skip:0 total:2
[ 1.930153] # Totals: pass:0 fail:2 skip:0 total
Link: https://github.com/rust-lang/rust/pull/140514 [1]
Reviewed-by: David Gow <davidgow@google.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250502215133.1923676-2-ojeda@kernel.org
[ Required `KUNIT=y` like for doctests. Used the `cfg_attr` from the
TODO comment and clarified its comment now that the stabilization is
in beta and thus quite likely stable in Rust 1.88.0. Simplified the
`new_body` code by introducing a new variable. Added
`#[allow(clippy::incompatible_msrv)]`. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
|
||
|---|---|---|
| .. | ||
| alloc | ||
| block | ||
| fs | ||
| list | ||
| net | ||
| sync | ||
| time | ||
| .gitignore | ||
| alloc.rs | ||
| block.rs | ||
| build_assert.rs | ||
| cred.rs | ||
| device.rs | ||
| device_id.rs | ||
| devres.rs | ||
| dma.rs | ||
| driver.rs | ||
| error.rs | ||
| faux.rs | ||
| firmware.rs | ||
| fs.rs | ||
| generated_arch_static_branch_asm.rs.S | ||
| init.rs | ||
| io.rs | ||
| ioctl.rs | ||
| jump_label.rs | ||
| kunit.rs | ||
| lib.rs | ||
| list.rs | ||
| miscdevice.rs | ||
| net.rs | ||
| of.rs | ||
| page.rs | ||
| pci.rs | ||
| pid_namespace.rs | ||
| platform.rs | ||
| prelude.rs | ||
| print.rs | ||
| rbtree.rs | ||
| revocable.rs | ||
| security.rs | ||
| seq_file.rs | ||
| sizes.rs | ||
| static_assert.rs | ||
| std_vendor.rs | ||
| str.rs | ||
| sync.rs | ||
| task.rs | ||
| time.rs | ||
| tracepoint.rs | ||
| transmute.rs | ||
| types.rs | ||
| uaccess.rs | ||
| workqueue.rs | ||
| xarray.rs | ||