1#[doc(hidden)]
7pub mod test_macro_support {
8 #![expect(unsafe_code)]
10
11 use super::TestCase;
12 pub use linkme;
13
14 #[linkme::distributed_slice]
15 pub static TESTS: [Option<fn() -> (&'static str, Vec<TestCase>)>];
16
17 #[linkme::distributed_slice(TESTS)]
21 static WORKAROUND: Option<fn() -> (&'static str, Vec<TestCase>)> = None;
22}
23
24use crate::PetriLogSource;
25use crate::TestArtifactRequirements;
26use crate::TestArtifacts;
27use crate::requirements::HostContext;
28use crate::requirements::TestCaseRequirements;
29use crate::requirements::can_run_test_with_context;
30use crate::tracing::try_init_tracing;
31use anyhow::Context as _;
32use petri_artifacts_core::ArtifactResolver;
33use std::panic::AssertUnwindSafe;
34use std::panic::catch_unwind;
35use test_macro_support::TESTS;
36
37#[macro_export]
39macro_rules! test {
40 ($f:ident, $req:expr) => {
41 $crate::multitest!(vec![
42 $crate::SimpleTest::new(stringify!($f), $req, $f, None,).into()
43 ]);
44 };
45}
46
47#[macro_export]
49macro_rules! multitest {
50 ($tests:expr) => {
51 const _: () = {
52 use $crate::test_macro_support::linkme;
53 #[linkme::distributed_slice($crate::test_macro_support::TESTS)]
54 #[linkme(crate = linkme)]
55 static TEST: Option<fn() -> (&'static str, Vec<$crate::TestCase>)> =
56 Some(|| (module_path!(), $tests));
57 };
58 };
59}
60
61pub struct TestCase(Box<dyn DynRunTest>);
63
64impl TestCase {
65 pub fn new(test: impl 'static + RunTest) -> Self {
67 Self(Box::new(test))
68 }
69}
70
71impl<T: 'static + RunTest> From<T> for TestCase {
72 fn from(test: T) -> Self {
73 Self::new(test)
74 }
75}
76
77struct Test {
79 module: &'static str,
80 test: TestCase,
81 artifact_requirements: TestArtifactRequirements,
82}
83
84impl Test {
85 fn all() -> impl Iterator<Item = Self> {
87 TESTS.iter().flatten().flat_map(|f| {
88 let (module, tests) = f();
89 tests.into_iter().filter_map(move |test| {
90 let mut artifact_requirements = test.0.artifact_requirements()?;
91 artifact_requirements
93 .require(petri_artifacts_common::artifacts::TEST_LOG_DIRECTORY);
94 Some(Self {
95 module,
96 artifact_requirements,
97 test,
98 })
99 })
100 })
101 }
102
103 fn name(&self) -> String {
105 match self.module.split_once("::") {
107 Some((_crate_name, rest)) => format!("{}::{}", rest, self.test.0.leaf_name()),
108 None => self.test.0.leaf_name().to_owned(),
109 }
110 }
111
112 fn run(
113 &self,
114 resolve: fn(&str, TestArtifactRequirements) -> anyhow::Result<TestArtifacts>,
115 ) -> anyhow::Result<()> {
116 let name = self.name();
117 let artifacts = resolve(&name, self.artifact_requirements.clone())
118 .context("failed to resolve artifacts")?;
119 let output_dir = artifacts.get(petri_artifacts_common::artifacts::TEST_LOG_DIRECTORY);
120 let logger = try_init_tracing(output_dir).context("failed to initialize tracing")?;
121 let mut post_test_hooks = Vec::new();
122
123 let r = catch_unwind(AssertUnwindSafe(|| {
127 self.test.0.run(
128 PetriTestParams {
129 test_name: &name,
130 logger: &logger,
131 post_test_hooks: &mut post_test_hooks,
132 },
133 &artifacts,
134 )
135 }));
136 let r = r.unwrap_or_else(|err| {
137 let msg = err
140 .downcast_ref::<&str>()
141 .copied()
142 .or_else(|| err.downcast_ref::<String>().map(|x| x.as_str()));
143
144 let err = if let Some(msg) = msg {
145 anyhow::anyhow!("test panicked: {msg}")
146 } else {
147 anyhow::anyhow!("test panicked (unknown payload type)")
148 };
149 Err(err)
150 });
151 logger.log_test_result(&name, &r);
152
153 for hook in post_test_hooks {
154 tracing::info!(name = hook.name(), "Running post-test hook");
155 if let Err(e) = hook.run(r.is_ok()) {
156 tracing::error!(
157 error = e.as_ref() as &dyn std::error::Error,
158 "Post-test hook failed"
159 );
160 } else {
161 tracing::info!("Post-test hook completed successfully");
162 }
163 }
164
165 r
166 }
167
168 fn trial(
170 self,
171 resolve: fn(&str, TestArtifactRequirements) -> anyhow::Result<TestArtifacts>,
172 ) -> libtest_mimic::Trial {
173 libtest_mimic::Trial::test(self.name(), move || {
174 self.run(resolve).map_err(|err| format!("{err:#}").into())
175 })
176 }
177}
178
179pub trait RunTest: Send {
183 type Artifacts;
185
186 fn leaf_name(&self) -> &str;
191 fn resolve(&self, resolver: &ArtifactResolver<'_>) -> Option<Self::Artifacts>;
197 fn run(&self, params: PetriTestParams<'_>, artifacts: Self::Artifacts) -> anyhow::Result<()>;
200 fn host_requirements(&self) -> Option<&TestCaseRequirements>;
202}
203
204trait DynRunTest: Send {
205 fn leaf_name(&self) -> &str;
206 fn artifact_requirements(&self) -> Option<TestArtifactRequirements>;
207 fn run(&self, params: PetriTestParams<'_>, artifacts: &TestArtifacts) -> anyhow::Result<()>;
208 fn host_requirements(&self) -> Option<&TestCaseRequirements>;
209}
210
211impl<T: RunTest> DynRunTest for T {
212 fn leaf_name(&self) -> &str {
213 self.leaf_name()
214 }
215
216 fn artifact_requirements(&self) -> Option<TestArtifactRequirements> {
217 let mut requirements = TestArtifactRequirements::new();
218 self.resolve(&ArtifactResolver::collector(&mut requirements))?;
219 Some(requirements)
220 }
221
222 fn run(&self, params: PetriTestParams<'_>, artifacts: &TestArtifacts) -> anyhow::Result<()> {
223 let artifacts = self
224 .resolve(&ArtifactResolver::resolver(artifacts))
225 .context("test should have been skipped")?;
226 self.run(params, artifacts)
227 }
228
229 fn host_requirements(&self) -> Option<&TestCaseRequirements> {
230 self.host_requirements()
231 }
232}
233
234pub struct PetriTestParams<'a> {
236 pub test_name: &'a str,
238 pub logger: &'a PetriLogSource,
240 pub post_test_hooks: &'a mut Vec<PetriPostTestHook>,
242}
243
244pub struct PetriPostTestHook {
247 name: String,
249 hook: Box<dyn FnOnce(bool) -> anyhow::Result<()>>,
251}
252
253impl PetriPostTestHook {
254 pub fn new(name: String, hook: impl FnOnce(bool) -> anyhow::Result<()> + 'static) -> Self {
255 Self {
256 name,
257 hook: Box::new(hook),
258 }
259 }
260
261 pub fn name(&self) -> &str {
262 &self.name
263 }
264
265 pub fn run(self, test_passed: bool) -> anyhow::Result<()> {
266 (self.hook)(test_passed)
267 }
268}
269
270pub struct SimpleTest<A, F> {
272 leaf_name: &'static str,
273 resolve: A,
274 run: F,
275 pub host_requirements: Option<TestCaseRequirements>,
277}
278
279impl<A, AR, F, E> SimpleTest<A, F>
280where
281 A: 'static + Send + Fn(&ArtifactResolver<'_>) -> Option<AR>,
282 F: 'static + Send + Fn(PetriTestParams<'_>, AR) -> Result<(), E>,
283 E: Into<anyhow::Error>,
284{
285 pub fn new(
288 leaf_name: &'static str,
289 resolve: A,
290 run: F,
291 host_requirements: Option<TestCaseRequirements>,
292 ) -> Self {
293 SimpleTest {
294 leaf_name,
295 resolve,
296 run,
297 host_requirements,
298 }
299 }
300}
301
302impl<A, AR, F, E> RunTest for SimpleTest<A, F>
303where
304 A: 'static + Send + Fn(&ArtifactResolver<'_>) -> Option<AR>,
305 F: 'static + Send + Fn(PetriTestParams<'_>, AR) -> Result<(), E>,
306 E: Into<anyhow::Error>,
307{
308 type Artifacts = AR;
309
310 fn leaf_name(&self) -> &str {
311 self.leaf_name
312 }
313
314 fn resolve(&self, resolver: &ArtifactResolver<'_>) -> Option<Self::Artifacts> {
315 (self.resolve)(resolver)
316 }
317
318 fn run(&self, params: PetriTestParams<'_>, artifacts: Self::Artifacts) -> anyhow::Result<()> {
319 (self.run)(params, artifacts).map_err(Into::into)
320 }
321
322 fn host_requirements(&self) -> Option<&TestCaseRequirements> {
323 self.host_requirements.as_ref()
324 }
325}
326
327#[derive(clap::Parser)]
328struct Options {
329 #[clap(long)]
331 list_required_artifacts: bool,
332 #[clap(flatten)]
333 inner: libtest_mimic::Arguments,
334}
335
336pub fn test_main(
338 resolve: fn(&str, TestArtifactRequirements) -> anyhow::Result<TestArtifacts>,
339) -> ! {
340 let mut args = <Options as clap::Parser>::parse();
341 if args.list_required_artifacts {
342 for test in Test::all() {
344 println!("{}:", test.name());
345 for artifact in test.artifact_requirements.required_artifacts() {
346 println!("required: {artifact:?}");
347 }
348 for artifact in test.artifact_requirements.optional_artifacts() {
349 println!("optional: {artifact:?}");
350 }
351 println!();
352 }
353 std::process::exit(0);
354 }
355
356 if !matches!(args.inner.test_threads, None | Some(1)) {
361 eprintln!("warning: ignoring value passed to --test-threads, using 1");
362 }
363 args.inner.test_threads = Some(1);
364
365 let host_context = futures::executor::block_on(HostContext::new());
367
368 let trials = Test::all()
369 .map(|test| {
370 let can_run = can_run_test_with_context(test.test.0.host_requirements(), &host_context);
371 test.trial(resolve).with_ignored_flag(!can_run)
372 })
373 .collect();
374
375 libtest_mimic::run(&args.inner, trials).exit();
376}