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