Re: [PATCH 2/3] rust: macros: add macro to easily run KUnit tests

Miguel Ojeda
2023-08-01T14:44:48Z

On Thu, Jul 20, 2023 at 8:40 AM David Gow <davidgow@google.com> wrote:
>
> The only difference with user-space tests is that instead of using
> `#[cfg(test)]`, `#[kunit_tests(kunit_test_suit_name)]` is used.

I may be missing something, but this does not appear to map the
`assert*!`s to the KUnit APIs, is that correct? (i.e. like we do for
`rustdoc`-tests).

I made an assertion fail, and it seems to use the standard library
macros, thus panicking and ending up in `BUG()` (rather than a failed
test):

rust_kernel: panicked at 'assertion failed: `(left == right)`
left: `true`,
right: `false`', rust/kernel/kunit.rs:329:1
------------[ cut here ]------------
kernel BUG at rust/helpers.c:34!

Then the test times out eventually and things break:

# rust_test_kunit_kunit_tests: try timed out
------------[ cut here ]------------
refcount_t: addition on 0; use-after-free.

> + // Add `#[cfg(CONFIG_KUNIT)]` before the module declaration.
> + let config_kunit = "#[cfg(CONFIG_KUNIT)]".to_owned().parse().unwrap();
> + tokens.insert(
> + 0,
> + TokenTree::Group(Group::new(Delimiter::None, config_kunit)),
> + );

I wonder about compile-time here with this approach. As far as I
understand, having the `cfg` explicitly outside the proc macro would
avoid invoking it.

Do we know the potential compile-time impact, especially when we will
have many tests?
ventually it would be ideal to have an approach closer to the
`rustdoc` one, where the compiler finds the tests for us and we
generate the needed code in the build system, i.e. outside a proc
macro.

Cheers,
Miguel