162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci//! Test builder for `rustdoc`-generated tests.
462306a36Sopenharmony_ci//!
562306a36Sopenharmony_ci//! This script is a hack to extract the test from `rustdoc`'s output. Ideally, `rustdoc` would
662306a36Sopenharmony_ci//! have an option to generate this information instead, e.g. as JSON output.
762306a36Sopenharmony_ci//!
862306a36Sopenharmony_ci//! The `rustdoc`-generated test names look like `{file}_{line}_{number}`, e.g.
962306a36Sopenharmony_ci//! `...path_rust_kernel_sync_arc_rs_42_0`. `number` is the "test number", needed in cases like
1062306a36Sopenharmony_ci//! a macro that expands into items with doctests is invoked several times within the same line.
1162306a36Sopenharmony_ci//!
1262306a36Sopenharmony_ci//! However, since these names are used for bisection in CI, the line number makes it not stable
1362306a36Sopenharmony_ci//! at all. In the future, we would like `rustdoc` to give us the Rust item path associated with
1462306a36Sopenharmony_ci//! the test, plus a "test number" (for cases with several examples per item) and generate a name
1562306a36Sopenharmony_ci//! from that. For the moment, we generate ourselves a new name, `{file}_{number}` instead, in
1662306a36Sopenharmony_ci//! the `gen` script (done there since we need to be aware of all the tests in a given file).
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciuse std::io::Read;
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cifn main() {
2162306a36Sopenharmony_ci    let mut stdin = std::io::stdin().lock();
2262306a36Sopenharmony_ci    let mut body = String::new();
2362306a36Sopenharmony_ci    stdin.read_to_string(&mut body).unwrap();
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci    // Find the generated function name looking for the inner function inside `main()`.
2662306a36Sopenharmony_ci    //
2762306a36Sopenharmony_ci    // The line we are looking for looks like one of the following:
2862306a36Sopenharmony_ci    //
2962306a36Sopenharmony_ci    // ```
3062306a36Sopenharmony_ci    // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_28_0() {
3162306a36Sopenharmony_ci    // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl core::fmt::Debug> {
3262306a36Sopenharmony_ci    // ```
3362306a36Sopenharmony_ci    //
3462306a36Sopenharmony_ci    // It should be unlikely that doctest code matches such lines (when code is formatted properly).
3562306a36Sopenharmony_ci    let rustdoc_function_name = body
3662306a36Sopenharmony_ci        .lines()
3762306a36Sopenharmony_ci        .find_map(|line| {
3862306a36Sopenharmony_ci            Some(
3962306a36Sopenharmony_ci                line.split_once("fn main() {")?
4062306a36Sopenharmony_ci                    .1
4162306a36Sopenharmony_ci                    .split_once("fn ")?
4262306a36Sopenharmony_ci                    .1
4362306a36Sopenharmony_ci                    .split_once("()")?
4462306a36Sopenharmony_ci                    .0,
4562306a36Sopenharmony_ci            )
4662306a36Sopenharmony_ci            .filter(|x| x.chars().all(|c| c.is_alphanumeric() || c == '_'))
4762306a36Sopenharmony_ci        })
4862306a36Sopenharmony_ci        .expect("No test function found in `rustdoc`'s output.");
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci    // Qualify `Result` to avoid the collision with our own `Result` coming from the prelude.
5162306a36Sopenharmony_ci    let body = body.replace(
5262306a36Sopenharmony_ci        &format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"),
5362306a36Sopenharmony_ci        &format!("{rustdoc_function_name}() -> core::result::Result<(), impl core::fmt::Debug> {{"),
5462306a36Sopenharmony_ci    );
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci    // For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on
5762306a36Sopenharmony_ci    // the return value to check there were no returned errors. Instead, we use our assert macro
5862306a36Sopenharmony_ci    // since we want to just fail the test, not panic the kernel.
5962306a36Sopenharmony_ci    //
6062306a36Sopenharmony_ci    // We save the result in a variable so that the failed assertion message looks nicer.
6162306a36Sopenharmony_ci    let body = body.replace(
6262306a36Sopenharmony_ci        &format!("}} {rustdoc_function_name}().unwrap() }}"),
6362306a36Sopenharmony_ci        &format!("}} let test_return_value = {rustdoc_function_name}(); assert!(test_return_value.is_ok()); }}"),
6462306a36Sopenharmony_ci    );
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci    // Figure out a smaller test name based on the generated function name.
6762306a36Sopenharmony_ci    let name = rustdoc_function_name.split_once("_rust_kernel_").unwrap().1;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci    let path = format!("rust/test/doctests/kernel/{name}");
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci    std::fs::write(path, body.as_bytes()).unwrap();
7262306a36Sopenharmony_ci}
73