History

Merge tag 'linux_kselftest-kunit-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kunit updates from Shuah Khan: - New parameterized test features KUnit parameterized tests supported two primary methods for getting parameters: - Defining custom logic within a generate_params() function. - Using the KUNIT_ARRAY_PARAM() and KUNIT_ARRAY_PARAM_DESC() macros with a pre-defined static array and passing the created *_gen_params() to KUNIT_CASE_PARAM(). These methods present limitations when dealing with dynamically generated parameter arrays, or in scenarios where populating parameters sequentially via generate_params() is inefficient or overly complex. These limitations are fixed with a parameterized test method - Fix issues in kunit build artifacts cleanup - Fix parsing skipped test problem in kselftest framework - Enable PCI on UML without triggering WARN() - a few other fixes and adds support for new configs such as MIPS * tag 'linux_kselftest-kunit-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: Extend kconfig help text for KUNIT_UML_PCI rust: kunit: allow `cfg` on `test`s kunit: qemu_configs: Add MIPS configurations kunit: Enable PCI on UML without triggering WARN() Documentation: kunit: Document new parameterized test features kunit: Add example parameterized test with direct dynamic parameter array setup kunit: Add example parameterized test with shared resource management using the Resource API kunit: Enable direct registration of parameter arrays to a KUnit test kunit: Pass parameterized test context to generate_params() kunit: Introduce param_init/exit for parameterized test context management kunit: Add parent kunit for parameterized test context kunit: tool: Accept --raw_output=full as an alias of 'all' kunit: tool: Parse skipped tests from kselftest.h kunit: Always descend into kunit directory during build

Linus Torvalds committed 1 year ago

Commit 30bbcb42 parents d2b2fea 285cae520 files · +880 −63Browse files

Showing the changes to rust (2 of 20 files). Show all

Unified · Split

2 files
  1. rust/kernel/kunit.rs +11 −0
  2. rust/macros/kunit.rs +36 −12
rust/kernel/kunit.rs+11−0
@@ -210,6 +210,8 @@ pub const fn kunit_case( · 209 unmodified lines
210 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,210 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
211 module_name: core::ptr::null_mut(),211 module_name: core::ptr::null_mut(),
212 log: core::ptr::null_mut(),212 log: core::ptr::null_mut(),
213 param_init: None,
214 param_exit: None,
213 }215 }
214}216}
215 217
@@ -229,6 +231,8 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case { · 13 unmodified lines
229 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,231 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
230 module_name: core::ptr::null_mut(),232 module_name: core::ptr::null_mut(),
231 log: core::ptr::null_mut(),233 log: core::ptr::null_mut(),
234 param_init: None,
235 param_exit: None,
232 }236 }
233}237}
234 238
@@ -357,4 +361,11 @@ mod tests { · 122 unmodified lines
357 fn rust_test_kunit_in_kunit_test() {361 fn rust_test_kunit_in_kunit_test() {
358 assert!(in_kunit_test());362 assert!(in_kunit_test());
359 }363 }
364
365 #[test]
366 #[cfg(not(all()))]
367 fn rust_test_kunit_always_disabled_test() {
368 // This test should never run because of the `cfg`.
369 assert!(false);
370 }
360}371}
rust/macros/kunit.rs+36−12
@@ -5,6 +5,7 @@ · 4 unmodified lines
5//! Copyright (c) 2023 José Expósito <jose.exposito89@gmail.com>5//! Copyright (c) 2023 José Expósito <jose.exposito89@gmail.com>
6 6
7use proc_macro::{Delimiter, Group, TokenStream, TokenTree};7use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
8use std::collections::HashMap;
8use std::fmt::Write;9use std::fmt::Write;
9 10
10pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {11pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
@@ -41,20 +42,32 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream { · 30 unmodified lines
41 // Get the functions set as tests. Search for `[test]` -> `fn`.42 // Get the functions set as tests. Search for `[test]` -> `fn`.
42 let mut body_it = body.stream().into_iter();43 let mut body_it = body.stream().into_iter();
43 let mut tests = Vec::new();44 let mut tests = Vec::new();
45 let mut attributes: HashMap<String, TokenStream> = HashMap::new();
44 while let Some(token) = body_it.next() {46 while let Some(token) = body_it.next() {
45 match token {47 match token {
46 TokenTree::Group(ident) if ident.to_string() == "[test]" => match body_it.next() {48 TokenTree::Punct(ref p) if p.as_char() == '#' => match body_it.next() {
47 Some(TokenTree::Ident(ident)) if ident.to_string() == "fn" => {49 Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Bracket => {
48 let test_name = match body_it.next() {50 if let Some(TokenTree::Ident(name)) = g.stream().into_iter().next() {
49 Some(TokenTree::Ident(ident)) => ident.to_string(),51 // Collect attributes because we need to find which are tests. We also
50 _ => continue,52 // need to copy `cfg` attributes so tests can be conditionally enabled.
51 };53 attributes
52 tests.push(test_name);54 .entry(name.to_string())
55 .or_default()
56 .extend([token, TokenTree::Group(g)]);
57 }
58 continue;
53 }59 }
54 _ => continue,60 _ => (),
55 },61 },
62 TokenTree::Ident(i) if i.to_string() == "fn" && attributes.contains_key("test") => {
63 if let Some(TokenTree::Ident(test_name)) = body_it.next() {
64 tests.push((test_name, attributes.remove("cfg").unwrap_or_default()))
65 }
66 }
67
56 _ => (),68 _ => (),
57 }69 }
70 attributes.clear();
58 }71 }
59 72
60 // Add `#[cfg(CONFIG_KUNIT="y")]` before the module declaration.73 // Add `#[cfg(CONFIG_KUNIT="y")]` before the module declaration.
@@ -100,11 +113,22 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream { · 39 unmodified lines
100 let mut test_cases = "".to_owned();113 let mut test_cases = "".to_owned();
101 let mut assert_macros = "".to_owned();114 let mut assert_macros = "".to_owned();
102 let path = crate::helpers::file();115 let path = crate::helpers::file();
103 for test in &tests {116 let num_tests = tests.len();
117 for (test, cfg_attr) in tests {
104 let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}");118 let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}");
105 // An extra `use` is used here to reduce the length of the message.119 // Append any `cfg` attributes the user might have written on their tests so we don't
120 // attempt to call them when they are `cfg`'d out. An extra `use` is used here to reduce
121 // the length of the assert message.
106 let kunit_wrapper = format!(122 let kunit_wrapper = format!(
107 "unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit) {{ use ::kernel::kunit::is_test_result_ok; assert!(is_test_result_ok({test}())); }}",123 r#"unsafe extern "C" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit)
124 {{
125 (*_test).status = ::kernel::bindings::kunit_status_KUNIT_SKIPPED;
126 {cfg_attr} {{
127 (*_test).status = ::kernel::bindings::kunit_status_KUNIT_SUCCESS;
128 use ::kernel::kunit::is_test_result_ok;
129 assert!(is_test_result_ok({test}()));
130 }}
131 }}"#,
108 );132 );
109 writeln!(kunit_macros, "{kunit_wrapper}").unwrap();133 writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
110 writeln!(134 writeln!(
@@ -139,7 +163,7 @@ macro_rules! assert_eq {{ · 28 unmodified lines
139 writeln!(163 writeln!(
140 kunit_macros,164 kunit_macros,
141 "static mut TEST_CASES: [::kernel::bindings::kunit_case; {}] = [\n{test_cases} ::kernel::kunit::kunit_case_null(),\n];",165 "static mut TEST_CASES: [::kernel::bindings::kunit_case; {}] = [\n{test_cases} ::kernel::kunit::kunit_case_null(),\n];",
142 tests.len() + 1166 num_tests + 1
143 )167 )
144 .unwrap();168 .unwrap();
145 169

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t