[PATCH 0/7] Rust KUnit `#[test]` support improvements

Miguel Ojeda posted 7 patches 9 months, 1 week ago
Documentation/rust/testing.rst | 80 ++++++++++++++++++++++++++++++++--
init/Kconfig                   |  3 ++
rust/Makefile                  |  3 +-
rust/kernel/kunit.rs           | 29 ++++++++++--
rust/kernel/prelude.rs         |  2 +-
rust/kernel/str.rs             | 76 ++++++++++++++------------------
rust/macros/helpers.rs         | 16 +++++++
rust/macros/kunit.rs           | 31 ++++++++++++-
rust/macros/lib.rs             |  6 ++-
9 files changed, 191 insertions(+), 55 deletions(-)
[PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Miguel Ojeda 9 months, 1 week ago
Improvements that build on top of the very basic `#[test]` support merged in
v6.15.

They are fairly minimal changes, but they allow us to map `assert*!`s back to
KUnit, plus to add support for test functions that return `Result`s.

In essence, they get our `#[test]`s essentially on par with the documentation
tests.

I also took the chance to convert some host `#[test]`s we had to KUnit in order
to showcase the feature.

Finally, I added documentation that was lacking from the original submission.

I hope this helps.

Miguel Ojeda (7):
  rust: kunit: support KUnit-mapped `assert!` macros in `#[test]`s
  rust: kunit: support checked `-> Result`s in KUnit `#[test]`s
  rust: add `kunit_tests` to the prelude
  rust: str: convert `rusttest` tests into KUnit
  rust: str: take advantage of the `-> Result` support in KUnit
    `#[test]`'s
  Documentation: rust: rename `#[test]`s to "`rusttest` host tests"
  Documentation: rust: testing: add docs on the new KUnit `#[test]`
    tests

 Documentation/rust/testing.rst | 80 ++++++++++++++++++++++++++++++++--
 init/Kconfig                   |  3 ++
 rust/Makefile                  |  3 +-
 rust/kernel/kunit.rs           | 29 ++++++++++--
 rust/kernel/prelude.rs         |  2 +-
 rust/kernel/str.rs             | 76 ++++++++++++++------------------
 rust/macros/helpers.rs         | 16 +++++++
 rust/macros/kunit.rs           | 31 ++++++++++++-
 rust/macros/lib.rs             |  6 ++-
 9 files changed, 191 insertions(+), 55 deletions(-)


base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
--
2.49.0
Re: [PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Danilo Krummrich 9 months, 1 week ago
On Fri, May 02, 2025 at 11:51:25PM +0200, Miguel Ojeda wrote:
> Improvements that build on top of the very basic `#[test]` support merged in
> v6.15.
> 
> They are fairly minimal changes, but they allow us to map `assert*!`s back to
> KUnit, plus to add support for test functions that return `Result`s.
> 
> In essence, they get our `#[test]`s essentially on par with the documentation
> tests.
> 
> I also took the chance to convert some host `#[test]`s we had to KUnit in order
> to showcase the feature.
> 
> Finally, I added documentation that was lacking from the original submission.
> 
> I hope this helps.

It does -- thanks for this series!

	Acked-by: Danilo Krummrich <dakr@kernel.org>

>   rust: str: convert `rusttest` tests into KUnit

With that, do we still expose `alloc` primitives to userspace tests?
Re: [PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Miguel Ojeda 9 months, 1 week ago
On Mon, May 5, 2025 at 6:57 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> With that, do we still expose `alloc` primitives to userspace tests?

I considered removing a bunch of stuff (even the build support for
non-`macros` `rusttest`, to be honest) -- you are referring to the
`any(test, testlib)` bits, right?

I think we can wait to see if we need it, or we can also just remove
it and re-introduce later if needed.

Thanks for taking a look!

Cheers,
Miguel
Re: [PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Miguel Ojeda 8 months, 2 weeks ago
On Fri, May 2, 2025 at 11:51 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Improvements that build on top of the very basic `#[test]` support merged in
> v6.15.
>
> They are fairly minimal changes, but they allow us to map `assert*!`s back to
> KUnit, plus to add support for test functions that return `Result`s.
>
> In essence, they get our `#[test]`s essentially on par with the documentation
> tests.
>
> I also took the chance to convert some host `#[test]`s we had to KUnit in order
> to showcase the feature.
>
> Finally, I added documentation that was lacking from the original submission.
>
> I hope this helps.

Applied to `rust-next` -- thanks everyone!

    [ 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 ]

    [ Used `::kernel` for paths. - Miguel ]

    [ Split from the next commit as suggested by Tamir. - Miguel ]

    [ Split the `CString` simplification into a new commit. - Miguel ]

Cheers,
Miguel
Re: [PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Miguel Ojeda 8 months, 2 weeks ago
On Tue, May 27, 2025 at 2:10 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
>     [ 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 ]

One addition:

    Required `KUNIT=y` like for doctests.

The build error was reported by Stephen when merging -- thanks!

Cheers,
Miguel
Re: [PATCH 0/7] Rust KUnit `#[test]` support improvements
Posted by Miguel Ojeda 8 months, 2 weeks ago
On Tue, May 27, 2025 at 2:10 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
>     [ Split from the next commit as suggested by Tamir. - Miguel ]
>
>     [ Split the `CString` simplification into a new commit. - Miguel ]

By the way, I kept the tags from David and Danilo in that new commit,
since it was really a pure split, but of course let me know if someone
doesn't want that.

Cheers,
Miguel