use std::collections::BTreeSet;
use isoloom_manifest::{Check, CheckLine, CheckRunner};
use serde::{Deserialize, Serialize};
use crate::Result;
use crate::error::Error;
use crate::events::EventKind;
use crate::events::WarningCode;
use crate::executor::{Cx, run_argv, run_steps};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CheckResult {
pub runner: String,
pub from: String,
pub name: String,
pub ok: bool,
pub detail: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CheckReport {
pub ok: bool,
pub results: Vec<CheckResult>,
pub unfinished: Vec<String>,
}
pub(crate) async fn run(cx: &mut Cx<'_>) -> Result<CheckReport> {
let checks = &cx.run.manifest().checks;
let mut report = CheckReport::default();
let setup = run_steps(cx, "checks.setup", &checks.setup).await;
if setup.is_ok() {
for runner in &checks.runners {
run_runner(cx, runner, &mut report).await;
}
}
let teardown = run_steps(cx, "checks.teardown", &checks.teardown).await;
if let (Err(_), Err(e)) = (&setup, &teardown) {
cx.events.warning(WarningCode::CheckCleanupFailed, &format!("checks.teardown: {e}"));
}
setup?;
teardown?;
report.ok = !checks.runners.is_empty() && report.unfinished.is_empty() && report.results.iter().all(|r| r.ok);
Ok(report)
}
async fn run_runner(cx: &mut Cx<'_>, runner: &CheckRunner, report: &mut CheckReport) {
let id = runner.id.as_str();
let before = report.results.len();
let mut ended = false;
let mut record = |line: &str| match CheckLine::parse(line) {
Some(CheckLine::Pass { name }) => push(cx, &mut report.results, result(runner, name, true, String::new())),
Some(CheckLine::Fail { name, reason }) => {
push(cx, &mut report.results, result(runner, name, false, reason));
}
Some(CheckLine::End { .. }) => ended = true,
None => {}
};
let ran = run_argv(cx, "checks.runners", &runner.step, &mut record).await;
if let Err(e) = &ran {
cx.events.warning(WarningCode::CheckRunnerFailed, &format!("{id}: {e}"));
if matches!(e, Error::Timeout { .. }) {
if let Err(cleanup) = run_steps(cx, "checks.on_timeout", &runner.on_timeout).await {
cx.events.warning(WarningCode::CheckCleanupFailed, &format!("{id}: {cleanup}"));
}
}
}
let reported: BTreeSet<String> = report.results.iter().skip(before).map(|r| r.name.clone()).collect();
for check in runner.checks.iter().filter(|c| !reported.contains(&c.name)) {
push(cx, &mut report.results, unreported(runner, check));
}
if ran.is_err() || !ended {
report.unfinished.push(id.to_owned());
}
}
fn push(cx: &Cx<'_>, results: &mut Vec<CheckResult>, r: CheckResult) {
cx.events.emit(EventKind::Check {
runner: r.runner.clone(),
from: r.from.clone(),
name: r.name.clone(),
ok: r.ok,
detail: r.detail.clone(),
});
results.push(r);
}
fn result(runner: &CheckRunner, name: String, ok: bool, detail: String) -> CheckResult {
CheckResult {
runner: runner.id.to_string(),
from: runner.from.clone(),
name,
ok,
detail,
}
}
fn unreported(runner: &CheckRunner, check: &Check) -> CheckResult {
result(runner, check.name.clone(), false, "the runner stopped before this check".into())
}