xref: /linux-6.15/scripts/rustdoc_test_gen.rs (revision fbefae55)
1a66d733dSMiguel Ojeda // SPDX-License-Identifier: GPL-2.0
2a66d733dSMiguel Ojeda 
3a66d733dSMiguel Ojeda //! Generates KUnit tests from saved `rustdoc`-generated tests.
4a66d733dSMiguel Ojeda //!
5a66d733dSMiguel Ojeda //! KUnit passes a context (`struct kunit *`) to each test, which should be forwarded to the other
6a66d733dSMiguel Ojeda //! KUnit functions and macros.
7a66d733dSMiguel Ojeda //!
8a66d733dSMiguel Ojeda //! However, we want to keep this as an implementation detail because:
9a66d733dSMiguel Ojeda //!
10a66d733dSMiguel Ojeda //!   - Test code should not care about the implementation.
11a66d733dSMiguel Ojeda //!
12a66d733dSMiguel Ojeda //!   - Documentation looks worse if it needs to carry extra details unrelated to the piece
13a66d733dSMiguel Ojeda //!     being described.
14a66d733dSMiguel Ojeda //!
15a66d733dSMiguel Ojeda //!   - Test code should be able to define functions and call them, without having to carry
16a66d733dSMiguel Ojeda //!     the context.
17a66d733dSMiguel Ojeda //!
18a66d733dSMiguel Ojeda //!   - Later on, we may want to be able to test non-kernel code (e.g. `core` or third-party
19a66d733dSMiguel Ojeda //!     crates) which likely use the standard library `assert*!` macros.
20a66d733dSMiguel Ojeda //!
21a66d733dSMiguel Ojeda //! For this reason, instead of the passed context, `kunit_get_current_test()` is used instead
22a66d733dSMiguel Ojeda //! (i.e. `current->kunit_test`).
23a66d733dSMiguel Ojeda //!
24a66d733dSMiguel Ojeda //! Note that this means other threads/tasks potentially spawned by a given test, if failing, will
25a66d733dSMiguel Ojeda //! report the failure in the kernel log but will not fail the actual test. Saving the pointer in
26a66d733dSMiguel Ojeda //! e.g. a `static` per test does not fully solve the issue either, because currently KUnit does
27a66d733dSMiguel Ojeda //! not support assertions (only expectations) from other tasks. Thus leave that feature for
28a66d733dSMiguel Ojeda //! the future, which simplifies the code here too. We could also simply not allow `assert`s in
29a66d733dSMiguel Ojeda //! other tasks, but that seems overly constraining, and we do want to support them, eventually.
30a66d733dSMiguel Ojeda 
31a66d733dSMiguel Ojeda use std::{
32a66d733dSMiguel Ojeda     fs,
33a66d733dSMiguel Ojeda     fs::File,
34a66d733dSMiguel Ojeda     io::{BufWriter, Read, Write},
35a66d733dSMiguel Ojeda     path::{Path, PathBuf},
36a66d733dSMiguel Ojeda };
37a66d733dSMiguel Ojeda 
38a66d733dSMiguel Ojeda /// Find the real path to the original file based on the `file` portion of the test name.
39a66d733dSMiguel Ojeda ///
40a66d733dSMiguel Ojeda /// `rustdoc` generated `file`s look like `sync_locked_by_rs`. Underscores (except the last one)
41a66d733dSMiguel Ojeda /// may represent an actual underscore in a directory/file, or a path separator. Thus the actual
42a66d733dSMiguel Ojeda /// file might be `sync_locked_by.rs`, `sync/locked_by.rs`, `sync_locked/by.rs` or
43a66d733dSMiguel Ojeda /// `sync/locked/by.rs`. This function walks the file system to determine which is the real one.
44a66d733dSMiguel Ojeda ///
45a66d733dSMiguel Ojeda /// This does require that ambiguities do not exist, but that seems fair, especially since this is
46a66d733dSMiguel Ojeda /// all supposed to be temporary until `rustdoc` gives us proper metadata to build this. If such
47a66d733dSMiguel Ojeda /// ambiguities are detected, they are diagnosed and the script panics.
find_real_path<'a>(srctree: &Path, valid_paths: &'a mut Vec<PathBuf>, file: &str) -> &'a str48a66d733dSMiguel Ojeda fn find_real_path<'a>(srctree: &Path, valid_paths: &'a mut Vec<PathBuf>, file: &str) -> &'a str {
49a66d733dSMiguel Ojeda     valid_paths.clear();
50a66d733dSMiguel Ojeda 
51a66d733dSMiguel Ojeda     let potential_components: Vec<&str> = file.strip_suffix("_rs").unwrap().split('_').collect();
52a66d733dSMiguel Ojeda 
53a66d733dSMiguel Ojeda     find_candidates(srctree, valid_paths, Path::new(""), &potential_components);
54a66d733dSMiguel Ojeda     fn find_candidates(
55a66d733dSMiguel Ojeda         srctree: &Path,
56a66d733dSMiguel Ojeda         valid_paths: &mut Vec<PathBuf>,
57a66d733dSMiguel Ojeda         prefix: &Path,
58a66d733dSMiguel Ojeda         potential_components: &[&str],
59a66d733dSMiguel Ojeda     ) {
60a66d733dSMiguel Ojeda         // The base case: check whether all the potential components left, joined by underscores,
61a66d733dSMiguel Ojeda         // is a file.
62a66d733dSMiguel Ojeda         let joined_potential_components = potential_components.join("_") + ".rs";
63a66d733dSMiguel Ojeda         if srctree
64a66d733dSMiguel Ojeda             .join("rust/kernel")
65a66d733dSMiguel Ojeda             .join(prefix)
66a66d733dSMiguel Ojeda             .join(&joined_potential_components)
67a66d733dSMiguel Ojeda             .is_file()
68a66d733dSMiguel Ojeda         {
69a66d733dSMiguel Ojeda             // Avoid `srctree` here in order to keep paths relative to it in the KTAP output.
70a66d733dSMiguel Ojeda             valid_paths.push(
71a66d733dSMiguel Ojeda                 Path::new("rust/kernel")
72a66d733dSMiguel Ojeda                     .join(prefix)
73a66d733dSMiguel Ojeda                     .join(joined_potential_components),
74a66d733dSMiguel Ojeda             );
75a66d733dSMiguel Ojeda         }
76a66d733dSMiguel Ojeda 
77a66d733dSMiguel Ojeda         // In addition, check whether each component prefix, joined by underscores, is a directory.
78a66d733dSMiguel Ojeda         // If not, there is no need to check for combinations with that prefix.
79a66d733dSMiguel Ojeda         for i in 1..potential_components.len() {
80a66d733dSMiguel Ojeda             let (components_prefix, components_rest) = potential_components.split_at(i);
81a66d733dSMiguel Ojeda             let prefix = prefix.join(components_prefix.join("_"));
82a66d733dSMiguel Ojeda             if srctree.join("rust/kernel").join(&prefix).is_dir() {
83a66d733dSMiguel Ojeda                 find_candidates(srctree, valid_paths, &prefix, components_rest);
84a66d733dSMiguel Ojeda             }
85a66d733dSMiguel Ojeda         }
86a66d733dSMiguel Ojeda     }
87a66d733dSMiguel Ojeda 
88a66d733dSMiguel Ojeda     assert!(
89a66d733dSMiguel Ojeda         valid_paths.len() > 0,
90*fbefae55SGuillaume Gomez         "No path candidates found for `{file}`. This is likely a bug in the build system, or some \
91*fbefae55SGuillaume Gomez         files went away while compiling."
92a66d733dSMiguel Ojeda     );
93a66d733dSMiguel Ojeda 
94a66d733dSMiguel Ojeda     if valid_paths.len() > 1 {
95a66d733dSMiguel Ojeda         eprintln!("Several path candidates found:");
96a66d733dSMiguel Ojeda         for path in valid_paths {
97a66d733dSMiguel Ojeda             eprintln!("    {path:?}");
98a66d733dSMiguel Ojeda         }
99a66d733dSMiguel Ojeda         panic!(
100*fbefae55SGuillaume Gomez             "Several path candidates found for `{file}`, please resolve the ambiguity by renaming \
101*fbefae55SGuillaume Gomez             a file or folder."
102a66d733dSMiguel Ojeda         );
103a66d733dSMiguel Ojeda     }
104a66d733dSMiguel Ojeda 
105a66d733dSMiguel Ojeda     valid_paths[0].to_str().unwrap()
106a66d733dSMiguel Ojeda }
107a66d733dSMiguel Ojeda 
main()108a66d733dSMiguel Ojeda fn main() {
109a66d733dSMiguel Ojeda     let srctree = std::env::var("srctree").unwrap();
110a66d733dSMiguel Ojeda     let srctree = Path::new(&srctree);
111a66d733dSMiguel Ojeda 
112a66d733dSMiguel Ojeda     let mut paths = fs::read_dir("rust/test/doctests/kernel")
113a66d733dSMiguel Ojeda         .unwrap()
114a66d733dSMiguel Ojeda         .map(|entry| entry.unwrap().path())
115a66d733dSMiguel Ojeda         .collect::<Vec<_>>();
116a66d733dSMiguel Ojeda 
117a66d733dSMiguel Ojeda     // Sort paths.
118a66d733dSMiguel Ojeda     paths.sort();
119a66d733dSMiguel Ojeda 
120a66d733dSMiguel Ojeda     let mut rust_tests = String::new();
121a66d733dSMiguel Ojeda     let mut c_test_declarations = String::new();
122a66d733dSMiguel Ojeda     let mut c_test_cases = String::new();
123a66d733dSMiguel Ojeda     let mut body = String::new();
124a66d733dSMiguel Ojeda     let mut last_file = String::new();
125a66d733dSMiguel Ojeda     let mut number = 0;
126a66d733dSMiguel Ojeda     let mut valid_paths: Vec<PathBuf> = Vec::new();
127a66d733dSMiguel Ojeda     let mut real_path: &str = "";
128a66d733dSMiguel Ojeda     for path in paths {
129a66d733dSMiguel Ojeda         // The `name` follows the `{file}_{line}_{number}` pattern (see description in
130a66d733dSMiguel Ojeda         // `scripts/rustdoc_test_builder.rs`). Discard the `number`.
131a66d733dSMiguel Ojeda         let name = path.file_name().unwrap().to_str().unwrap().to_string();
132a66d733dSMiguel Ojeda 
133a66d733dSMiguel Ojeda         // Extract the `file` and the `line`, discarding the `number`.
134a66d733dSMiguel Ojeda         let (file, line) = name.rsplit_once('_').unwrap().0.rsplit_once('_').unwrap();
135a66d733dSMiguel Ojeda 
136a66d733dSMiguel Ojeda         // Generate an ID sequence ("test number") for each one in the file.
137a66d733dSMiguel Ojeda         if file == last_file {
138a66d733dSMiguel Ojeda             number += 1;
139a66d733dSMiguel Ojeda         } else {
140a66d733dSMiguel Ojeda             number = 0;
141a66d733dSMiguel Ojeda             last_file = file.to_string();
142a66d733dSMiguel Ojeda 
143a66d733dSMiguel Ojeda             // Figure out the real path, only once per file.
144a66d733dSMiguel Ojeda             real_path = find_real_path(srctree, &mut valid_paths, file);
145a66d733dSMiguel Ojeda         }
146a66d733dSMiguel Ojeda 
147a66d733dSMiguel Ojeda         // Generate a KUnit name (i.e. test name and C symbol) for this test.
148a66d733dSMiguel Ojeda         //
149a66d733dSMiguel Ojeda         // We avoid the line number, like `rustdoc` does, to make things slightly more stable for
150a66d733dSMiguel Ojeda         // bisection purposes. However, to aid developers in mapping back what test failed, we will
151a66d733dSMiguel Ojeda         // print a diagnostics line in the KTAP report.
152a66d733dSMiguel Ojeda         let kunit_name = format!("rust_doctest_kernel_{file}_{number}");
153a66d733dSMiguel Ojeda 
154a66d733dSMiguel Ojeda         // Read the test's text contents to dump it below.
155a66d733dSMiguel Ojeda         body.clear();
156a66d733dSMiguel Ojeda         File::open(path).unwrap().read_to_string(&mut body).unwrap();
157a66d733dSMiguel Ojeda 
158a66d733dSMiguel Ojeda         // Calculate how many lines before `main` function (including the `main` function line).
159a66d733dSMiguel Ojeda         let body_offset = body
160a66d733dSMiguel Ojeda             .lines()
161a66d733dSMiguel Ojeda             .take_while(|line| !line.contains("fn main() {"))
162a66d733dSMiguel Ojeda             .count()
163a66d733dSMiguel Ojeda             + 1;
164a66d733dSMiguel Ojeda 
165a66d733dSMiguel Ojeda         use std::fmt::Write;
166a66d733dSMiguel Ojeda         write!(
167a66d733dSMiguel Ojeda             rust_tests,
168a66d733dSMiguel Ojeda             r#"/// Generated `{name}` KUnit test case from a Rust documentation test.
169a66d733dSMiguel Ojeda #[no_mangle]
170a66d733dSMiguel Ojeda pub extern "C" fn {kunit_name}(__kunit_test: *mut kernel::bindings::kunit) {{
171a66d733dSMiguel Ojeda     /// Overrides the usual [`assert!`] macro with one that calls KUnit instead.
172a66d733dSMiguel Ojeda     #[allow(unused)]
173a66d733dSMiguel Ojeda     macro_rules! assert {{
174a66d733dSMiguel Ojeda         ($cond:expr $(,)?) => {{{{
175a66d733dSMiguel Ojeda             kernel::kunit_assert!("{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond);
176a66d733dSMiguel Ojeda         }}}}
177a66d733dSMiguel Ojeda     }}
178a66d733dSMiguel Ojeda 
179a66d733dSMiguel Ojeda     /// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead.
180a66d733dSMiguel Ojeda     #[allow(unused)]
181a66d733dSMiguel Ojeda     macro_rules! assert_eq {{
182a66d733dSMiguel Ojeda         ($left:expr, $right:expr $(,)?) => {{{{
183a66d733dSMiguel Ojeda             kernel::kunit_assert_eq!("{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right);
184a66d733dSMiguel Ojeda         }}}}
185a66d733dSMiguel Ojeda     }}
186a66d733dSMiguel Ojeda 
187a66d733dSMiguel Ojeda     // Many tests need the prelude, so provide it by default.
188a66d733dSMiguel Ojeda     #[allow(unused)]
189a66d733dSMiguel Ojeda     use kernel::prelude::*;
190a66d733dSMiguel Ojeda 
191a66d733dSMiguel Ojeda     // Unconditionally print the location of the original doctest (i.e. rather than the location in
192a66d733dSMiguel Ojeda     // the generated file) so that developers can easily map the test back to the source code.
193a66d733dSMiguel Ojeda     //
194a66d733dSMiguel Ojeda     // This information is also printed when assertions fail, but this helps in the successful cases
195a66d733dSMiguel Ojeda     // when the user is running KUnit manually, or when passing `--raw_output` to `kunit.py`.
196a66d733dSMiguel Ojeda     //
197a66d733dSMiguel Ojeda     // This follows the syntax for declaring test metadata in the proposed KTAP v2 spec, which may
198a66d733dSMiguel Ojeda     // be used for the proposed KUnit test attributes API. Thus hopefully this will make migration
199a66d733dSMiguel Ojeda     // easier later on.
200a66d733dSMiguel Ojeda     kernel::kunit::info(format_args!("    # {kunit_name}.location: {real_path}:{line}\n"));
201a66d733dSMiguel Ojeda 
202a66d733dSMiguel Ojeda     /// The anchor where the test code body starts.
203a66d733dSMiguel Ojeda     #[allow(unused)]
204a66d733dSMiguel Ojeda     static __DOCTEST_ANCHOR: i32 = core::line!() as i32 + {body_offset} + 1;
205a66d733dSMiguel Ojeda     {{
206a66d733dSMiguel Ojeda         {body}
207a66d733dSMiguel Ojeda         main();
208a66d733dSMiguel Ojeda     }}
209a66d733dSMiguel Ojeda }}
210a66d733dSMiguel Ojeda 
211a66d733dSMiguel Ojeda "#
212a66d733dSMiguel Ojeda         )
213a66d733dSMiguel Ojeda         .unwrap();
214a66d733dSMiguel Ojeda 
215a66d733dSMiguel Ojeda         write!(c_test_declarations, "void {kunit_name}(struct kunit *);\n").unwrap();
216a66d733dSMiguel Ojeda         write!(c_test_cases, "    KUNIT_CASE({kunit_name}),\n").unwrap();
217a66d733dSMiguel Ojeda     }
218a66d733dSMiguel Ojeda 
219a66d733dSMiguel Ojeda     let rust_tests = rust_tests.trim();
220a66d733dSMiguel Ojeda     let c_test_declarations = c_test_declarations.trim();
221a66d733dSMiguel Ojeda     let c_test_cases = c_test_cases.trim();
222a66d733dSMiguel Ojeda 
223a66d733dSMiguel Ojeda     write!(
224a66d733dSMiguel Ojeda         BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()),
225a66d733dSMiguel Ojeda         r#"//! `kernel` crate documentation tests.
226a66d733dSMiguel Ojeda 
227a66d733dSMiguel Ojeda const __LOG_PREFIX: &[u8] = b"rust_doctests_kernel\0";
228a66d733dSMiguel Ojeda 
229a66d733dSMiguel Ojeda {rust_tests}
230a66d733dSMiguel Ojeda "#
231a66d733dSMiguel Ojeda     )
232a66d733dSMiguel Ojeda     .unwrap();
233a66d733dSMiguel Ojeda 
234a66d733dSMiguel Ojeda     write!(
235a66d733dSMiguel Ojeda         BufWriter::new(File::create("rust/doctests_kernel_generated_kunit.c").unwrap()),
236a66d733dSMiguel Ojeda         r#"/*
237a66d733dSMiguel Ojeda  * `kernel` crate documentation tests.
238a66d733dSMiguel Ojeda  */
239a66d733dSMiguel Ojeda 
240a66d733dSMiguel Ojeda #include <kunit/test.h>
241a66d733dSMiguel Ojeda 
242a66d733dSMiguel Ojeda {c_test_declarations}
243a66d733dSMiguel Ojeda 
244a66d733dSMiguel Ojeda static struct kunit_case test_cases[] = {{
245a66d733dSMiguel Ojeda     {c_test_cases}
246a66d733dSMiguel Ojeda     {{ }}
247a66d733dSMiguel Ojeda }};
248a66d733dSMiguel Ojeda 
249a66d733dSMiguel Ojeda static struct kunit_suite test_suite = {{
250a66d733dSMiguel Ojeda     .name = "rust_doctests_kernel",
251a66d733dSMiguel Ojeda     .test_cases = test_cases,
252a66d733dSMiguel Ojeda }};
253a66d733dSMiguel Ojeda 
254a66d733dSMiguel Ojeda kunit_test_suite(test_suite);
255a66d733dSMiguel Ojeda 
256a66d733dSMiguel Ojeda MODULE_LICENSE("GPL");
257a66d733dSMiguel Ojeda "#
258a66d733dSMiguel Ojeda     )
259a66d733dSMiguel Ojeda     .unwrap();
260a66d733dSMiguel Ojeda }
261