Message ID | 20250502215133.1923676-5-ojeda@kernel.org |
---|---|
State | New |
Headers | show |
Series | Rust KUnit `#[test]` support improvements | expand |
On Sat, 3 May 2025 at 05:52, Miguel Ojeda <ojeda@kernel.org> wrote: > > In general, we should aim to test as much as possible within the actual > kernel, and not in the build host. > > Thus convert these `rusttest` tests into KUnit tests. > > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > --- I'm obviously a fan of this. Assuming no-one working on the str code strenuously objects, let's do it! Reviewed-by: David Gow <davidgow@google.com> Cheers, -- David > rust/kernel/str.rs | 10 ++++------ > 1 file changed, 4 insertions(+), 6 deletions(-) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index 878111cb77bc..cf2caa2db168 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -6,7 +6,7 @@ > use core::fmt::{self, Write}; > use core::ops::{self, Deref, DerefMut, Index}; > > -use crate::error::{code::*, Error}; > +use crate::prelude::*; > > /// Byte string without UTF-8 validity guarantee. > #[repr(transparent)] > @@ -572,8 +572,7 @@ macro_rules! c_str { > }}; > } > > -#[cfg(test)] > -#[expect(clippy::items_after_test_module)] > +#[kunit_tests(rust_kernel_str)] > mod tests { > use super::*; > > @@ -622,11 +621,10 @@ fn test_cstr_to_str() { > } > > #[test] > - #[should_panic] > - fn test_cstr_to_str_panic() { > + fn test_cstr_to_str_invalid_utf8() { > let bad_bytes = b"\xc3\x28\0"; > let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap(); > - checked_cstr.to_str().unwrap(); > + assert!(checked_cstr.to_str().is_err()); > } > > #[test] > -- > 2.49.0 >
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 878111cb77bc..cf2caa2db168 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -6,7 +6,7 @@ use core::fmt::{self, Write}; use core::ops::{self, Deref, DerefMut, Index}; -use crate::error::{code::*, Error}; +use crate::prelude::*; /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] @@ -572,8 +572,7 @@ macro_rules! c_str { }}; } -#[cfg(test)] -#[expect(clippy::items_after_test_module)] +#[kunit_tests(rust_kernel_str)] mod tests { use super::*; @@ -622,11 +621,10 @@ fn test_cstr_to_str() { } #[test] - #[should_panic] - fn test_cstr_to_str_panic() { + fn test_cstr_to_str_invalid_utf8() { let bad_bytes = b"\xc3\x28\0"; let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap(); - checked_cstr.to_str().unwrap(); + assert!(checked_cstr.to_str().is_err()); } #[test]
In general, we should aim to test as much as possible within the actual kernel, and not in the build host. Thus convert these `rusttest` tests into KUnit tests. Signed-off-by: Miguel Ojeda <ojeda@kernel.org> --- rust/kernel/str.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)