use std::path::Path;
use trusty_code::bakeoff::{
Baseline, ComparisonInputs, GateReport, Pins, compare_against_baseline, load_bundle, preflight,
};
pub const EXIT_GATE_FAILED: i32 = 1;
pub const EXIT_UNUSABLE: i32 = 2;
pub fn run(
bundle: &Path,
baseline: Option<&Path>,
pins: Pins,
tolerance_pct: f64,
json: bool,
) -> i32 {
if !bundle.is_dir() {
eprintln!(
"tcode bakeoff-gate: no bundle directory at {}",
bundle.display()
);
return EXIT_UNUSABLE;
}
let candidate = load_bundle(bundle);
let inputs = match ComparisonInputs::load(&candidate, tolerance_pct) {
Ok(inputs) => inputs,
Err(e) => {
eprintln!("tcode bakeoff-gate: {e}");
return EXIT_UNUSABLE;
}
};
let mut report: GateReport = preflight(&candidate, &pins);
if let Some(baseline_root) = baseline {
if !baseline_root.is_dir() {
eprintln!(
"tcode bakeoff-gate: no baseline directory at {}",
baseline_root.display()
);
return EXIT_UNUSABLE;
}
let baseline = Baseline(load_bundle(baseline_root));
report.absorb(compare_against_baseline(&candidate, &baseline, &inputs));
} else {
report
.notes
.push("no baseline given — regression comparison was not performed".to_string());
}
if json {
println!("{}", report.render_json());
} else {
print!("{}", report.render_human());
}
if report.passed() { 0 } else { EXIT_GATE_FAILED }
}