Skip to content

Commit 950b306

Browse files
committed
rust: kunit: support checked -> Results in KUnit #[test]s
Currently, return values of KUnit `#[test]` functions are ignored. Thus introduce support for `-> Result` functions by checking their returned values. At the same time, require that test functions return `()` or `Result<T, E>`, which should avoid mistakes, especially with non-`#[must_use]` types. Other types can be supported in the future if needed. With this, a failing test like: #[test] fn my_test() -> Result { f()?; Ok(()) } will output: [ 3.744214] KTAP version 1 [ 3.744287] # Subtest: my_test_suite [ 3.744378] # speed: normal [ 3.744399] 1..1 [ 3.745817] # my_test: ASSERTION FAILED at rust/kernel/lib.rs:321 [ 3.745817] Expected is_test_result_ok(my_test()) to be true, but is false [ 3.747152] # my_test.speed: normal [ 3.747199] not ok 1 my_test [ 3.747345] not ok 4 my_test_suite Reviewed-by: David Gow <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Used `::kernel` for paths. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 36174d1 commit 950b306

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

rust/kernel/kunit.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,31 @@ macro_rules! kunit_assert_eq {
164164
}};
165165
}
166166

167+
trait TestResult {
168+
fn is_test_result_ok(&self) -> bool;
169+
}
170+
171+
impl TestResult for () {
172+
fn is_test_result_ok(&self) -> bool {
173+
true
174+
}
175+
}
176+
177+
impl<T, E> TestResult for Result<T, E> {
178+
fn is_test_result_ok(&self) -> bool {
179+
self.is_ok()
180+
}
181+
}
182+
183+
/// Returns whether a test result is to be considered OK.
184+
///
185+
/// This will be `assert!`ed from the generated tests.
186+
#[doc(hidden)]
187+
#[expect(private_bounds)]
188+
pub fn is_test_result_ok(t: impl TestResult) -> bool {
189+
t.is_test_result_ok()
190+
}
191+
167192
/// Represents an individual test case.
168193
///
169194
/// The [`kunit_unsafe_test_suite!`] macro expects a NULL-terminated list of valid test cases.

rust/macros/kunit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
102102
let path = crate::helpers::file();
103103
for test in &tests {
104104
let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}");
105+
// An extra `use` is used here to reduce the length of the message.
105106
let kunit_wrapper = format!(
106-
"unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit) {{ {test}(); }}"
107+
"unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit) {{ use ::kernel::kunit::is_test_result_ok; assert!(is_test_result_ok({test}())); }}",
107108
);
108109
writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
109110
writeln!(

0 commit comments

Comments
 (0)