rustdoc_test_gen.rs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. match valid_paths.as_slice() {
  82. [] => panic!(
  83. "No path candidates found for `{file}`. This is likely a bug in the build system, or \
  84. some files went away while compiling."
  85. ),
  86. [valid_path] => valid_path.to_str().unwrap(),
  87. valid_paths => {
  88. use std::fmt::Write;
  89. let mut candidates = String::new();
  90. for path in valid_paths {
  91. writeln!(&mut candidates, " {path:?}").unwrap();
  92. }
  93. panic!(
  94. "Several path candidates found for `{file}`, please resolve the ambiguity by \
  95. renaming a file or folder. Candidates:\n{candidates}",
  96. );
  97. }
  98. }
  99. }
  100. fn main() {
  101. let srctree = std::env::var("srctree").unwrap();
  102. let srctree = Path::new(&srctree);
  103. let mut paths = fs::read_dir("rust/test/doctests/kernel")
  104. .unwrap()
  105. .map(|entry| entry.unwrap().path())
  106. .collect::<Vec<_>>();
  107. // Sort paths.
  108. paths.sort();
  109. let mut rust_tests = String::new();
  110. let mut c_test_declarations = String::new();
  111. let mut c_test_cases = String::new();
  112. let mut body = String::new();
  113. let mut last_file = String::new();
  114. let mut number = 0;
  115. let mut valid_paths: Vec<PathBuf> = Vec::new();
  116. let mut real_path: &str = "";
  117. for path in paths {
  118. // The `name` follows the `{file}_{line}_{number}` pattern (see description in
  119. // `scripts/rustdoc_test_builder.rs`). Discard the `number`.
  120. let name = path.file_name().unwrap().to_str().unwrap().to_string();
  121. // Extract the `file` and the `line`, discarding the `number`.
  122. let (file, line) = name.rsplit_once('_').unwrap().0.rsplit_once('_').unwrap();
  123. // Generate an ID sequence ("test number") for each one in the file.
  124. if file == last_file {
  125. number += 1;
  126. } else {
  127. number = 0;
  128. last_file = file.to_string();
  129. // Figure out the real path, only once per file.
  130. real_path = find_real_path(srctree, &mut valid_paths, file);
  131. }
  132. // Generate a KUnit name (i.e. test name and C symbol) for this test.
  133. //
  134. // We avoid the line number, like `rustdoc` does, to make things slightly more stable for
  135. // bisection purposes. However, to aid developers in mapping back what test failed, we will
  136. // print a diagnostics line in the KTAP report.
  137. let kunit_name = format!("rust_doctest_kernel_{file}_{number}");
  138. // Read the test's text contents to dump it below.
  139. body.clear();
  140. File::open(path).unwrap().read_to_string(&mut body).unwrap();
  141. // Calculate how many lines before `main` function (including the `main` function line).
  142. let body_offset = body
  143. .lines()
  144. .take_while(|line| !line.contains("fn main() {"))
  145. .count()
  146. + 1;
  147. use std::fmt::Write;
  148. write!(
  149. rust_tests,
  150. r#"/// Generated `{name}` KUnit test case from a Rust documentation test.
  151. #[no_mangle]
  152. pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
  153. /// Overrides the usual [`assert!`] macro with one that calls KUnit instead.
  154. #[allow(unused)]
  155. macro_rules! assert {{
  156. ($cond:expr $(,)?) => {{{{
  157. ::kernel::kunit_assert!(
  158. "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $cond
  159. );
  160. }}}}
  161. }}
  162. /// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead.
  163. #[allow(unused)]
  164. macro_rules! assert_eq {{
  165. ($left:expr, $right:expr $(,)?) => {{{{
  166. ::kernel::kunit_assert_eq!(
  167. "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
  168. );
  169. }}}}
  170. }}
  171. // Many tests need the prelude, so provide it by default.
  172. #[allow(unused)]
  173. use ::kernel::prelude::*;
  174. // Unconditionally print the location of the original doctest (i.e. rather than the location in
  175. // the generated file) so that developers can easily map the test back to the source code.
  176. //
  177. // This information is also printed when assertions fail, but this helps in the successful cases
  178. // when the user is running KUnit manually, or when passing `--raw_output` to `kunit.py`.
  179. //
  180. // This follows the syntax for declaring test metadata in the proposed KTAP v2 spec, which may
  181. // be used for the proposed KUnit test attributes API. Thus hopefully this will make migration
  182. // easier later on.
  183. ::kernel::kunit::info(fmt!(" # {kunit_name}.location: {real_path}:{line}\n"));
  184. /// The anchor where the test code body starts.
  185. #[allow(unused)]
  186. static __DOCTEST_ANCHOR: i32 = ::core::line!() as i32 + {body_offset} + 2;
  187. {{
  188. #![allow(unreachable_pub, clippy::disallowed_names)]
  189. {body}
  190. main();
  191. }}
  192. }}
  193. "#
  194. )
  195. .unwrap();
  196. write!(c_test_declarations, "void {kunit_name}(struct kunit *);\n").unwrap();
  197. write!(c_test_cases, " KUNIT_CASE({kunit_name}),\n").unwrap();
  198. }
  199. let rust_tests = rust_tests.trim();
  200. let c_test_declarations = c_test_declarations.trim();
  201. let c_test_cases = c_test_cases.trim();
  202. write!(
  203. BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()),
  204. r#"//! `kernel` crate documentation tests.
  205. const __LOG_PREFIX: &[u8] = b"rust_doctests_kernel\0";
  206. {rust_tests}
  207. "#
  208. )
  209. .unwrap();
  210. write!(
  211. BufWriter::new(File::create("rust/doctests_kernel_generated_kunit.c").unwrap()),
  212. r#"/*
  213. * `kernel` crate documentation tests.
  214. */
  215. #include <kunit/test.h>
  216. {c_test_declarations}
  217. static struct kunit_case test_cases[] = {{
  218. {c_test_cases}
  219. {{ }}
  220. }};
  221. static struct kunit_suite test_suite = {{
  222. .name = "rust_doctests_kernel",
  223. .test_cases = test_cases,
  224. }};
  225. kunit_test_suite(test_suite);
  226. MODULE_LICENSE("GPL");
  227. "#
  228. )
  229. .unwrap();
  230. }