isothrum_runner/
checks.rs1use std::collections::BTreeSet;
9
10use isoloom_manifest::{Check, CheckLine, CheckRunner};
11use serde::{Deserialize, Serialize};
12
13use crate::Result;
14use crate::error::Error;
15use crate::events::EventKind;
16use crate::events::WarningCode;
17use crate::executor::{Cx, run_argv, run_steps};
18
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21#[non_exhaustive]
22pub struct CheckResult {
23 pub runner: String,
25 pub from: String,
27 pub name: String,
29 pub ok: bool,
31 pub detail: String,
33}
34
35#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
37#[non_exhaustive]
38pub struct CheckReport {
39 pub ok: bool,
41 pub results: Vec<CheckResult>,
43 pub unfinished: Vec<String>,
45}
46
47pub(crate) async fn run(cx: &mut Cx<'_>) -> Result<CheckReport> {
49 let checks = &cx.run.manifest().checks;
50 let mut report = CheckReport::default();
51 let setup = run_steps(cx, "checks.setup", &checks.setup).await;
52 if setup.is_ok() {
53 for runner in &checks.runners {
54 run_runner(cx, runner, &mut report).await;
55 }
56 }
57 let teardown = run_steps(cx, "checks.teardown", &checks.teardown).await;
58 if let (Err(_), Err(e)) = (&setup, &teardown) {
59 cx.events.warning(WarningCode::CheckCleanupFailed, &format!("checks.teardown: {e}"));
61 }
62 setup?;
63 teardown?;
64 report.ok = !checks.runners.is_empty() && report.unfinished.is_empty() && report.results.iter().all(|r| r.ok);
65 Ok(report)
66}
67
68async fn run_runner(cx: &mut Cx<'_>, runner: &CheckRunner, report: &mut CheckReport) {
70 let id = runner.id.as_str();
71 let before = report.results.len();
72 let mut ended = false;
73 let mut record = |line: &str| match CheckLine::parse(line) {
74 Some(CheckLine::Pass { name }) => push(cx, &mut report.results, result(runner, name, true, String::new())),
75 Some(CheckLine::Fail { name, reason }) => {
76 push(cx, &mut report.results, result(runner, name, false, reason));
77 }
78 Some(CheckLine::End { .. }) => ended = true,
80 None => {}
81 };
82 let ran = run_argv(cx, "checks.runners", &runner.step, &mut record).await;
83 if let Err(e) = &ran {
84 cx.events.warning(WarningCode::CheckRunnerFailed, &format!("{id}: {e}"));
85 if matches!(e, Error::Timeout { .. }) {
86 if let Err(cleanup) = run_steps(cx, "checks.on_timeout", &runner.on_timeout).await {
89 cx.events.warning(WarningCode::CheckCleanupFailed, &format!("{id}: {cleanup}"));
90 }
91 }
92 }
93 let reported: BTreeSet<String> = report.results.iter().skip(before).map(|r| r.name.clone()).collect();
94 for check in runner.checks.iter().filter(|c| !reported.contains(&c.name)) {
95 push(cx, &mut report.results, unreported(runner, check));
96 }
97 if ran.is_err() || !ended {
98 report.unfinished.push(id.to_owned());
99 }
100}
101
102fn push(cx: &Cx<'_>, results: &mut Vec<CheckResult>, r: CheckResult) {
104 cx.events.emit(EventKind::Check {
105 runner: r.runner.clone(),
106 from: r.from.clone(),
107 name: r.name.clone(),
108 ok: r.ok,
109 detail: r.detail.clone(),
110 });
111 results.push(r);
112}
113
114fn result(runner: &CheckRunner, name: String, ok: bool, detail: String) -> CheckResult {
115 CheckResult {
116 runner: runner.id.to_string(),
117 from: runner.from.clone(),
118 name,
119 ok,
120 detail,
121 }
122}
123
124fn unreported(runner: &CheckRunner, check: &Check) -> CheckResult {
126 result(runner, check.name.clone(), false, "the runner stopped before this check".into())
127}