1use either::Either;
2
3use crate::{
4 Features, LogPfx,
5 evaluation::{
6 Context, Effects, PAlternativeTest, PGeneralTest, PMainTest, ProcessedTestStatus,
7 SelectableSource, WithEffects, WithNotice,
8 single_evaluation::EvalSignal,
9 testrun2selectable::{ActualTestResultStatus, FatalRunError, run_actual_test},
10 },
11 spec::{AlternativeTest, AlternativeTestHooks, EndThisTestMode, MainTest, MainTestHooks},
12};
13
14impl ProcessedTestStatus {
15 pub fn is_successful(&self) -> bool {
16 match self {
17 Self::JustFailTestRun(_) | Self::Eval(_) => false,
18 Self::Criterion(c) => c.is_fulfilled(),
19 Self::EndedByAction(e) => match e.mode() {
20 EndThisTestMode::Pass => true,
21 EndThisTestMode::Fail => false,
22 },
23 }
24 }
25 pub fn maybe_overwrite_with_signal(
26 &mut self,
27 log_pfx: &LogPfx,
28 signal: Option<EvalSignal>,
29 ) -> &mut Self {
30 #[allow(clippy::collapsible_match)]
31 match signal {
32 Some(EvalSignal::EndTest(end)) => {
33 if self.proceedable() {
34 log::debug!("[{log_pfx}] test status is updated by signal to {end:?}");
35 *self = end.into();
36 }
37 }
38 None => {}
39 }
40
41 self
42 }
43 pub fn proceedable(&self) -> bool {
46 !matches!(self, ProcessedTestStatus::EndedByAction(_))
47 }
48}
49
50pub type MainTestFailure = FatalRunError;
51
52fn process_test_status(
53 status: ActualTestResultStatus,
54 effects: &mut Effects,
55) -> Result<ProcessedTestStatus, FatalRunError> {
56 Ok(match status {
57 Either::Left((run_actions, decisison)) => {
58 effects.extend(run_actions);
59 match decisison {
60 Ok(Either::Left(EvalSignal::EndTest(end_test_action))) => {
61 ProcessedTestStatus::EndedByAction(end_test_action)
62 }
63 Ok(Either::Right(criterion)) => ProcessedTestStatus::Criterion(criterion),
64 Err(eval_error) => ProcessedTestStatus::Eval(eval_error),
65 }
66 }
67 Either::Right(run_failed) => ProcessedTestStatus::JustFailTestRun(run_failed?),
68 })
69}
70
71impl PMainTest {
72 pub async fn new<Fallback: SelectableSource>(
73 ctx: &Context,
74 log_pfx: LogPfx,
75 test: &MainTest,
76 fallback: Fallback,
77 ) -> Result<WithEffects<PMainTest>, WithNotice<MainTestFailure>> {
78 let mut eft = Effects::default();
79 let out = process_main_test(ctx, log_pfx, test, fallback, &mut eft).await;
80 match out {
81 Ok(data) => Ok(WithEffects::new(data, eft)),
82 Err(data) => Err(WithNotice::new(data, eft.take_notice())),
83 }
84 }
85}
86
87async fn process_main_test<Fallback: SelectableSource>(
88 ctx: &Context,
89 log_pfx: LogPfx,
90 test: &MainTest,
91 fallback: Fallback,
92 eft: &mut Effects,
93) -> Result<PMainTest, MainTestFailure> {
94 let hooks = test.general().hooks();
95
96 let (before_main, sig) = hooks
100 .before_main()
101 .run_all(
102 ctx,
103 log_pfx.join("before-main"),
104 eft,
105 Features::PermittedFEAT_PreTestHook,
106 &fallback,
107 )
108 .await;
109
110 let (mut main_status, main_rundata) = if let Some(sig) = sig {
114 match sig {
115 EvalSignal::EndTest(end_this_test) => (end_this_test.into(), None),
116 }
117 } else {
118 let main_result = run_actual_test(
119 ctx,
120 log_pfx.join("main"),
121 test.as_ref(),
122 (&eft, &fallback),
123 Features::PermittedFEAT_PassTestCrit,
124 )
125 .await;
126 let status = process_test_status(main_result.status, eft)?;
127 (status, Some(main_result.testdata))
128 };
129
130 let (after_main, sig) = hooks
131 .after_main()
132 .run_all(
133 ctx,
134 log_pfx.join("after-main"),
135 eft,
136 Features::PermittedFEAT_PostTestHook,
137 (&main_rundata, &fallback),
138 )
139 .await;
140 main_status.maybe_overwrite_with_signal(&log_pfx, sig);
141
142 let mut messages = eft.take_messages();
144 let mut before_alternatives = Default::default();
148
149 if !main_status.proceedable() {
150 log::debug!(
151 "[{log_pfx}] the main test's status is non-proceedable, so all alternatives and further hooks are skipped."
152 )
153 } else if main_status.is_successful() {
154 log::debug!(
155 "[{log_pfx}] the main test's status is successful so before-alternatives-hooks and alternative tests are skipped"
156 );
157 }
158
159 let tried_alternatives = if main_status.proceedable() && !main_status.is_successful() {
160 let sig;
162 (before_alternatives, sig) = hooks
163 .before_alternatives()
164 .run_all(
165 ctx,
166 log_pfx.join("before-alternatives"),
167 eft,
168 Features::PermittedFEAT_PostTestHook,
169 (&main_rundata, &fallback),
170 )
171 .await;
172 main_status.maybe_overwrite_with_signal(&log_pfx, sig);
176 if main_status.proceedable() {
177 try_alternative_tests_of_main(
178 ctx,
179 log_pfx.join("alt"),
180 eft,
181 test.alternative_tests(),
182 &fallback,
183 )
184 .await?
185 } else {
186 log::debug!(
187 "[{log_pfx}] hooks caused the status to be non-proceedable so no alternatives are tried"
188 );
189 vec![]
190 }
191 } else {
192 vec![]
193 };
194
195 let alt_makes_successful = if let Some(last) = tried_alternatives.last() {
196 let successful = last.general().status().is_successful();
197 if successful {
198 log::debug!(
199 "[{log_pfx}] one alternative succeeded so the main test will be counted as successful"
200 );
201 true
202 } else {
203 log::debug!("[{log_pfx}] no alternative succeeded so the main test still fails");
204 false
205 }
206 } else {
207 false
208 };
209
210 let mut after_complete = Default::default();
214 if main_status.proceedable() {
215 let sig;
216 (after_complete, sig) = hooks
217 .after_complete()
218 .run_all(
219 ctx,
220 log_pfx.join("after-complete"),
221 eft,
222 Features::PermittedFEAT_PostTestHook,
223 (&main_rundata, &fallback),
224 )
225 .await;
226 main_status.maybe_overwrite_with_signal(&log_pfx, sig);
227 }
228
229 messages.extend(eft.take_messages());
230 let out = PMainTest {
231 messages,
232 general: PGeneralTest {
233 is_passed: main_status.is_successful() || alt_makes_successful,
234 title: test.general().title().clone().into(),
235 status: main_status,
236 data: main_rundata,
237 hooks: MainTestHooks {
238 before_main,
239 after_main,
240 before_alternatives,
241 after_complete,
242 },
243 },
244 tried_alternatives,
245 };
246
247 Ok(out)
248}
249
250async fn try_alternative_tests_of_main<Source: SelectableSource>(
251 ctx: &Context,
252 log_pfx: LogPfx,
253 eft: &mut Effects,
254 alternatives: impl IntoIterator<Item = &AlternativeTest>,
255 fallback: Source,
256) -> Result<Vec<PAlternativeTest>, MainTestFailure> {
257 let mut tried_alternatives = vec![];
258 log::trace!("[{log_pfx}] enter alternatives-loop");
259
260 let mut count = 0;
261
262 for (index, alternative) in alternatives.into_iter().enumerate() {
263 log::debug!("[{log_pfx}] start considering alternative test with index {index}");
264 count += 1;
265 let log_pfx = log_pfx.join(index);
266 let alt_hooks = alternative.general().hooks();
267
268 let (before_alt, sig) = alt_hooks
272 .before_alt()
273 .run_all(
274 ctx,
275 log_pfx.join("before-alt"),
276 eft,
277 Features::PermittedFEAT_PreTestHook,
278 &fallback,
279 )
280 .await;
281
282 let (mut alt_status, alt_rundata) = if let Some(sig) = sig {
286 match sig {
287 EvalSignal::EndTest(end) => {
288 log::debug!(
289 "[{log_pfx}] a hook send a signal that caused the alternative test to finish early: {end:?}"
290 );
291 (end.into(), None)
292 }
293 }
294 } else {
295 log::debug!("[{log_pfx}] start actual run of alternative test with index {index}");
296 let alternative_result = run_actual_test(
297 ctx,
298 log_pfx.join("alt"),
299 alternative.as_ref(),
300 (&eft, &fallback),
301 Features::PermittedFEAT_PassTestCrit,
302 )
303 .await;
304 let alternative_status = process_test_status(alternative_result.status, eft)?;
305 log::debug!(
306 "[{log_pfx}] actual run of alternative test with index {index} ended with status: {alternative_status:?}"
307 );
308 (alternative_status, Some(alternative_result.testdata))
309 };
310
311 let mut after_alt = Default::default();
315 if alt_status.proceedable() {
316 let sig;
317 (after_alt, sig) = alt_hooks
318 .after_alt()
319 .run_all(
320 ctx,
321 log_pfx.join("after-alt"),
322 eft,
323 Features::PermittedFEAT_PostTestHook,
324 (&alt_rundata, &fallback),
325 )
326 .await;
327 alt_status.maybe_overwrite_with_signal(&log_pfx, sig);
328 }
329
330 let no_other_alternatives_needed = alt_status.is_successful();
331 tried_alternatives.push(PAlternativeTest {
332 messages: eft.take_messages(),
333 general: PGeneralTest {
334 is_passed: alt_status.is_successful(),
335 title: alternative.general().title().clone().into(),
336 status: alt_status,
337 data: alt_rundata,
338 hooks: AlternativeTestHooks {
339 before_alt,
340 after_alt,
341 },
342 },
343 });
344 if no_other_alternatives_needed {
345 log::debug!("[{log_pfx}] no more alternatives needed as current was successful");
346 break;
347 }
348 }
349
350 log::debug!("[{log_pfx}] exit alternatives-loop after {count} tried alternatives");
351 Ok(tried_alternatives)
352}