rustdoc_test_builder.rs 3.3 KB

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