Skip to content

Commit 35f6d7c

Browse files
ojedaintel-lab-lkp
authored andcommitted
rust: kunit: support KUnit-mapped assert! macros in #[test]s
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: rust-lang/rust#140514 [1] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent b443265 commit 35f6d7c

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

init/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ config LD_CAN_USE_KEEP_IN_OVERLAY
140140
config RUSTC_HAS_COERCE_POINTEE
141141
def_bool RUSTC_VERSION >= 108400
142142

143+
config RUSTC_HAS_SPAN_FILE
144+
def_bool RUSTC_VERSION >= 108800
145+
143146
config PAHOLE_VERSION
144147
int
145148
default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))

rust/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
402402
-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
403403
--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
404404
--crate-type proc-macro \
405-
--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) $<
405+
--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
406+
@$(objtree)/include/generated/rustc_cfg $<
406407

407408
# Procedural macros can only be used with the `rustc` that compiled it.
408409
$(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE

rust/kernel/kunit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ mod tests {
323323

324324
#[test]
325325
fn rust_test_kunit_example_test() {
326-
#![expect(clippy::eq_op)]
327326
assert_eq!(1 + 1, 2);
328327
}
329328

rust/macros/helpers.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ pub(crate) fn function_name(input: TokenStream) -> Option<Ident> {
8686
}
8787
None
8888
}
89+
90+
pub(crate) fn file() -> String {
91+
#[cfg(not(CONFIG_RUSTC_HAS_SPAN_FILE))]
92+
{
93+
proc_macro::Span::call_site()
94+
.source_file()
95+
.path()
96+
.to_string_lossy()
97+
.into_owned()
98+
}
99+
100+
#[cfg(CONFIG_RUSTC_HAS_SPAN_FILE)]
101+
{
102+
proc_macro::Span::call_site().file()
103+
}
104+
}

rust/macros/kunit.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
101101
// ```
102102
let mut kunit_macros = "".to_owned();
103103
let mut test_cases = "".to_owned();
104+
let mut assert_macros = "".to_owned();
105+
let path = crate::helpers::file();
104106
for test in &tests {
105107
let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{}", test);
106108
let kunit_wrapper = format!(
@@ -114,6 +116,27 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
114116
test, kunit_wrapper_fn_name
115117
)
116118
.unwrap();
119+
writeln!(
120+
assert_macros,
121+
r#"
122+
/// Overrides the usual [`assert!`] macro with one that calls KUnit instead.
123+
#[allow(unused)]
124+
macro_rules! assert {{
125+
($cond:expr $(,)?) => {{{{
126+
kernel::kunit_assert!("{test}", "{path}", 0, $cond);
127+
}}}}
128+
}}
129+
130+
/// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead.
131+
#[allow(unused)]
132+
macro_rules! assert_eq {{
133+
($left:expr, $right:expr $(,)?) => {{{{
134+
kernel::kunit_assert_eq!("{test}", "{path}", 0, $left, $right);
135+
}}}}
136+
}}
137+
"#
138+
)
139+
.unwrap();
117140
}
118141

119142
writeln!(kunit_macros).unwrap();
@@ -152,7 +175,10 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
152175
}
153176
}
154177

155-
let mut new_body = TokenStream::from_iter(new_body);
178+
let body = new_body;
179+
let mut new_body = TokenStream::new();
180+
new_body.extend::<TokenStream>(assert_macros.parse().unwrap());
181+
new_body.extend(body);
156182
new_body.extend::<TokenStream>(kunit_macros.parse().unwrap());
157183

158184
tokens.push(TokenTree::Group(Group::new(Delimiter::Brace, new_body)));

rust/macros/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
// and thus add a dependency on `include/config/RUSTC_VERSION_TEXT`, which is
77
// touched by Kconfig when the version string from the compiler changes.
88

9+
// TODO: check that when Rust 1.88.0 is released, this would be enough:
10+
// #![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))]
11+
#![feature(proc_macro_span)]
12+
913
#[macro_use]
1014
mod quote;
1115
mod concat_idents;

0 commit comments

Comments
 (0)