rustdoc_test_gen.rs 9.5 KB

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