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