On Wed, 30 Oct 2024 at 13:11, Boqun Feng boqun.feng@gmail.com wrote:
On Wed, Oct 30, 2024 at 12:57:14PM +0800, David Gow wrote:
From: José Expósito jose.exposito89@gmail.com Add a new procedural macro (`#[kunit_tests(kunit_test_suit_name)]`) to run KUnit tests using a user-space like syntax.
The macro, that should be used on modules, transforms every `#[test]` in a `kunit_case!` and adds a `kunit_unsafe_test_suite!` registering all of them.
The only difference with user-space tests is that instead of using `#[cfg(test)]`, `#[kunit_tests(kunit_test_suit_name)]` is used.
Note that `#[cfg(CONFIG_KUNIT)]` is added so the test module is not compiled when `CONFIG_KUNIT` is set to `n`.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: José Expósito jose.exposito89@gmail.com [Updated to use new const fn.] Signed-off-by: David Gow davidgow@google.com
Changes since v2: https://lore.kernel.org/linux-kselftest/20241029092422.2884505-3-davidgow@go...
- Include missing rust/macros/kunit.rs file from v2. (Thanks Boqun!)
- The proc macro now emits an error if the suite name is too long.
Changes since v1: https://lore.kernel.org/lkml/20230720-rustbind-v1-2-c80db349e3b5@google.com/
- Rebased on top of rust-next
- Make use of the new const functions, rather than the kunit_case!() macro.
MAINTAINERS | 1 + rust/kernel/kunit.rs | 11 ++++ rust/macros/kunit.rs | 153 +++++++++++++++++++++++++++++++++++++++++++ rust/macros/lib.rs | 29 ++++++++ 4 files changed, 194 insertions(+) create mode 100644 rust/macros/kunit.rs
(...snip...)
- let new_body: TokenStream = vec![body.stream(), kunit_macros.parse().unwrap()]
.into_iter()
.collect();
- // Remove the `#[test]` macros.
- let new_body = new_body.to_string().replace("#[test]", "");
Yeah, I want to see how you do it this time ;-) So if you do a `.to_string()` on a `TokenStream`, you lose all the span [1] information ("span information" is a term invited by me, hope I get it right ;-)) e.g. if there is a compile error in the test code, the compiler cannot report the exact line of the error, it can only report there is an error.
Last time I find how to preserve the Span:
https://lore.kernel.org/rust-for-linux/ZMba0_XXZuTgWyWY@boqun-archlinux/
Hope it helps!
Regards, Boqun
Thanks. I managed to get this working, but just ended up with an uglier version of your change, so I've copied it in and added you as a co-developer for this patch in v4.
It made clippy catch a couple of warnings in the example tests, too, so it's clearly working well.
Cheers, -- David