|
1 |
| -#!/usr/bin/env python3 |
2 |
| -# SPDX-License-Identifier: GPL-2.0 |
3 |
| -"""rustdoc_test_gen - Generates KUnit tests from saved `rustdoc`-generated tests. |
4 |
| -""" |
5 |
| - |
6 |
| -import os |
7 |
| -import pathlib |
8 |
| - |
9 |
| -RUST_DIR = pathlib.Path("rust") |
10 |
| -TESTS_DIR = RUST_DIR / "test" / "doctests" / "kernel" |
11 |
| - |
12 |
| -RUST_FILE = RUST_DIR / "doctests_kernel_generated.rs" |
13 |
| -C_FILE = RUST_DIR / "doctests_kernel_generated_kunit.c" |
14 |
| - |
15 |
| -RUST_TEMPLATE_TEST = """ |
16 |
| -/// Generated `{test_name}` KUnit test case from a Rust documentation test. |
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Generates KUnit tests from saved `rustdoc`-generated tests. |
| 4 | +
|
| 5 | +use std::io::{BufWriter, Read, Write}; |
| 6 | +use std::{fs, fs::File}; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let mut dirs = fs::read_dir("rust/test/doctests/kernel") |
| 10 | + .unwrap() |
| 11 | + .map(|p| p.unwrap().path()) |
| 12 | + .collect::<Vec<_>>(); |
| 13 | + dirs.sort(); |
| 14 | + |
| 15 | + let mut rust_tests = String::new(); |
| 16 | + let mut c_test_declarations = String::new(); |
| 17 | + let mut c_test_cases = String::new(); |
| 18 | + let mut content = String::new(); |
| 19 | + for path in dirs { |
| 20 | + content.clear(); |
| 21 | + |
| 22 | + File::open(path) |
| 23 | + .unwrap() |
| 24 | + .read_to_string(&mut content) |
| 25 | + .unwrap(); |
| 26 | + |
| 27 | + let (name, body) = content.split_once("\n").unwrap(); |
| 28 | + |
| 29 | + use std::fmt::Write; |
| 30 | + write!( |
| 31 | + rust_tests, |
| 32 | + r#"/// Generated `{name}` KUnit test case from a Rust documentation test. |
17 | 33 | #[no_mangle]
|
18 |
| -pub fn {test_name}(__kunit_test: *mut kernel::bindings::kunit) {{ |
| 34 | +pub fn {name}(__kunit_test: *mut kernel::bindings::kunit) {{ |
19 | 35 | /// Provides mutual exclusion (see `# Implementation` notes).
|
20 | 36 | static __KUNIT_TEST_MUTEX: kernel::sync::smutex::Mutex<()> =
|
21 | 37 | kernel::sync::smutex::Mutex::new(());
|
|
55 | 71 | use kernel::prelude::*;
|
56 | 72 |
|
57 | 73 | {{
|
58 |
| - {test_body} |
| 74 | + {body} |
59 | 75 | main();
|
60 | 76 | }}
|
61 | 77 | }}
|
62 |
| -""" |
63 |
| -RUST_TEMPLATE = """// SPDX-License-Identifier: GPL-2.0 |
| 78 | +
|
| 79 | +"# |
| 80 | + ) |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + write!(c_test_declarations, "void {name}(struct kunit *);\n").unwrap(); |
| 84 | + write!(c_test_cases, " KUNIT_CASE({name}),\n").unwrap(); |
| 85 | + } |
| 86 | + |
| 87 | + let rust_tests = rust_tests.trim(); |
| 88 | + let c_test_declarations = c_test_declarations.trim(); |
| 89 | + let c_test_cases = c_test_cases.trim(); |
| 90 | + |
| 91 | + write!( |
| 92 | + BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()), |
| 93 | + r#"// SPDX-License-Identifier: GPL-2.0 |
64 | 94 |
|
65 | 95 | //! `kernel` crate documentation tests.
|
66 | 96 |
|
|
98 | 128 | // an `AtomicPtr` to hold the context (though each test only writes once before
|
99 | 129 | // threads may be created).
|
100 | 130 |
|
101 |
| -const __LOG_PREFIX: &[u8] = b"rust_kernel_doctests\\0"; |
| 131 | +const __LOG_PREFIX: &[u8] = b"rust_kernel_doctests\0"; |
102 | 132 |
|
103 | 133 | {rust_tests}
|
104 |
| -""" |
| 134 | +"# |
| 135 | + ) |
| 136 | + .unwrap(); |
105 | 137 |
|
106 |
| -C_TEMPLATE_TEST_DECLARATION = "void {test_name}(struct kunit *);\n" |
107 |
| -C_TEMPLATE_TEST_CASE = " KUNIT_CASE({test_name}),\n" |
108 |
| -C_TEMPLATE = """// SPDX-License-Identifier: GPL-2.0 |
| 138 | + write!( |
| 139 | + BufWriter::new(File::create("rust/doctests_kernel_generated_kunit.c").unwrap()), |
| 140 | + r#"// SPDX-License-Identifier: GPL-2.0 |
109 | 141 | /*
|
110 | 142 | * `kernel` crate documentation tests.
|
111 | 143 | */
|
|
127 | 159 | kunit_test_suite(test_suite);
|
128 | 160 |
|
129 | 161 | MODULE_LICENSE("GPL");
|
130 |
| -""" |
131 |
| - |
132 |
| -def main(): |
133 |
| - rust_tests = "" |
134 |
| - c_test_declarations = "" |
135 |
| - c_test_cases = "" |
136 |
| - for filename in sorted(os.listdir(TESTS_DIR)): |
137 |
| - with open(TESTS_DIR / filename, "r") as fd: |
138 |
| - (test_name, test_body) = fd.read().split('\n', 1) |
139 |
| - rust_tests += RUST_TEMPLATE_TEST.format( |
140 |
| - test_name = test_name, |
141 |
| - test_body = test_body |
142 |
| - ) |
143 |
| - c_test_declarations += C_TEMPLATE_TEST_DECLARATION.format( |
144 |
| - test_name = test_name |
145 |
| - ) |
146 |
| - c_test_cases += C_TEMPLATE_TEST_CASE.format( |
147 |
| - test_name = test_name |
148 |
| - ) |
149 |
| - |
150 |
| - with open(RUST_FILE, "w") as fd: |
151 |
| - fd.write(RUST_TEMPLATE.format( |
152 |
| - rust_tests = rust_tests.strip(), |
153 |
| - )) |
154 |
| - |
155 |
| - with open(C_FILE, "w") as fd: |
156 |
| - fd.write(C_TEMPLATE.format( |
157 |
| - c_test_declarations=c_test_declarations.strip(), |
158 |
| - c_test_cases=c_test_cases.strip(), |
159 |
| - )) |
160 |
| - |
161 |
| -if __name__ == "__main__": |
162 |
| - main() |
| 162 | +"# |
| 163 | + ) |
| 164 | + .unwrap(); |
| 165 | +} |
0 commit comments