1*a66d733dSMiguel Ojeda // SPDX-License-Identifier: GPL-2.0
2*a66d733dSMiguel Ojeda
3*a66d733dSMiguel Ojeda //! Test builder for `rustdoc`-generated tests.
4*a66d733dSMiguel Ojeda //!
5*a66d733dSMiguel Ojeda //! This script is a hack to extract the test from `rustdoc`'s output. Ideally, `rustdoc` would
6*a66d733dSMiguel Ojeda //! have an option to generate this information instead, e.g. as JSON output.
7*a66d733dSMiguel Ojeda //!
8*a66d733dSMiguel Ojeda //! The `rustdoc`-generated test names look like `{file}_{line}_{number}`, e.g.
9*a66d733dSMiguel Ojeda //! `...path_rust_kernel_sync_arc_rs_42_0`. `number` is the "test number", needed in cases like
10*a66d733dSMiguel Ojeda //! a macro that expands into items with doctests is invoked several times within the same line.
11*a66d733dSMiguel Ojeda //!
12*a66d733dSMiguel Ojeda //! However, since these names are used for bisection in CI, the line number makes it not stable
13*a66d733dSMiguel Ojeda //! at all. In the future, we would like `rustdoc` to give us the Rust item path associated with
14*a66d733dSMiguel Ojeda //! the test, plus a "test number" (for cases with several examples per item) and generate a name
15*a66d733dSMiguel Ojeda //! from that. For the moment, we generate ourselves a new name, `{file}_{number}` instead, in
16*a66d733dSMiguel Ojeda //! the `gen` script (done there since we need to be aware of all the tests in a given file).
17*a66d733dSMiguel Ojeda
18*a66d733dSMiguel Ojeda use std::io::Read;
19*a66d733dSMiguel Ojeda
main()20*a66d733dSMiguel Ojeda fn main() {
21*a66d733dSMiguel Ojeda let mut stdin = std::io::stdin().lock();
22*a66d733dSMiguel Ojeda let mut body = String::new();
23*a66d733dSMiguel Ojeda stdin.read_to_string(&mut body).unwrap();
24*a66d733dSMiguel Ojeda
25*a66d733dSMiguel Ojeda // Find the generated function name looking for the inner function inside `main()`.
26*a66d733dSMiguel Ojeda //
27*a66d733dSMiguel Ojeda // The line we are looking for looks like one of the following:
28*a66d733dSMiguel Ojeda //
29*a66d733dSMiguel Ojeda // ```
30*a66d733dSMiguel Ojeda // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_28_0() {
31*a66d733dSMiguel Ojeda // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl core::fmt::Debug> {
32*a66d733dSMiguel Ojeda // ```
33*a66d733dSMiguel Ojeda //
34*a66d733dSMiguel Ojeda // It should be unlikely that doctest code matches such lines (when code is formatted properly).
35*a66d733dSMiguel Ojeda let rustdoc_function_name = body
36*a66d733dSMiguel Ojeda .lines()
37*a66d733dSMiguel Ojeda .find_map(|line| {
38*a66d733dSMiguel Ojeda Some(
39*a66d733dSMiguel Ojeda line.split_once("fn main() {")?
40*a66d733dSMiguel Ojeda .1
41*a66d733dSMiguel Ojeda .split_once("fn ")?
42*a66d733dSMiguel Ojeda .1
43*a66d733dSMiguel Ojeda .split_once("()")?
44*a66d733dSMiguel Ojeda .0,
45*a66d733dSMiguel Ojeda )
46*a66d733dSMiguel Ojeda .filter(|x| x.chars().all(|c| c.is_alphanumeric() || c == '_'))
47*a66d733dSMiguel Ojeda })
48*a66d733dSMiguel Ojeda .expect("No test function found in `rustdoc`'s output.");
49*a66d733dSMiguel Ojeda
50*a66d733dSMiguel Ojeda // Qualify `Result` to avoid the collision with our own `Result` coming from the prelude.
51*a66d733dSMiguel Ojeda let body = body.replace(
52*a66d733dSMiguel Ojeda &format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"),
53*a66d733dSMiguel Ojeda &format!("{rustdoc_function_name}() -> core::result::Result<(), impl core::fmt::Debug> {{"),
54*a66d733dSMiguel Ojeda );
55*a66d733dSMiguel Ojeda
56*a66d733dSMiguel Ojeda // For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on
57*a66d733dSMiguel Ojeda // the return value to check there were no returned errors. Instead, we use our assert macro
58*a66d733dSMiguel Ojeda // since we want to just fail the test, not panic the kernel.
59*a66d733dSMiguel Ojeda //
60*a66d733dSMiguel Ojeda // We save the result in a variable so that the failed assertion message looks nicer.
61*a66d733dSMiguel Ojeda let body = body.replace(
62*a66d733dSMiguel Ojeda &format!("}} {rustdoc_function_name}().unwrap() }}"),
63*a66d733dSMiguel Ojeda &format!("}} let test_return_value = {rustdoc_function_name}(); assert!(test_return_value.is_ok()); }}"),
64*a66d733dSMiguel Ojeda );
65*a66d733dSMiguel Ojeda
66*a66d733dSMiguel Ojeda // Figure out a smaller test name based on the generated function name.
67*a66d733dSMiguel Ojeda let name = rustdoc_function_name.split_once("_rust_kernel_").unwrap().1;
68*a66d733dSMiguel Ojeda
69*a66d733dSMiguel Ojeda let path = format!("rust/test/doctests/kernel/{name}");
70*a66d733dSMiguel Ojeda
71*a66d733dSMiguel Ojeda std::fs::write(path, body.as_bytes()).unwrap();
72*a66d733dSMiguel Ojeda }
73