use std::io::Write;
use std::path::{Path, PathBuf};
use astrs_graph::{DataflowGraph, Severity};
use astrs_manifest::Manifest;
use astrs_manifest::expand::FsModuleLoader;
use astrs_verify::{
Profile, ProveOptions, RenderOptions, VerificationReport, prove, render_human as render_proof,
};
use serde::Serialize;
use crate::diagnostic::{CliDiagnostic, exit_code_for, render_human};
use crate::error::CliError;
#[derive(Debug, Clone)]
pub struct ValidateArgs {
pub manifest_path: PathBuf,
pub prove: bool,
pub profile: Option<PathBuf>,
pub json: bool,
pub color: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct ValidateReport {
pub manifest_path: String,
pub diagnostics: Vec<CliDiagnostic>,
#[serde(skip_serializing_if = "Option::is_none")]
pub proof: Option<VerificationReport>,
}
impl ValidateReport {
#[must_use]
pub fn exit_code(&self) -> i32 {
exit_code_for(&self.diagnostics)
}
}
pub fn run(out: &mut dyn Write, args: &ValidateArgs) -> Result<ValidateReport, CliError> {
let (mut diagnostics, graph) = collect_diagnostics(args);
let proof = if args.prove {
run_proof(args, graph.as_ref(), &mut diagnostics)
} else {
None
};
let report = ValidateReport {
manifest_path: args.manifest_path.display().to_string(),
diagnostics,
proof,
};
render(out, args, &report)?;
if report.proof.is_some() && !astrs_verify::solver_available() {
return Err(CliError::ProofUnavailable);
}
Ok(report)
}
fn run_proof(
args: &ValidateArgs,
graph: Option<&DataflowGraph>,
diagnostics: &mut Vec<CliDiagnostic>,
) -> Option<VerificationReport> {
let Some(graph) = graph else {
diagnostics.push(CliDiagnostic::prove_error(
"--prove needs a graph, and this manifest did not produce one",
));
return None;
};
let profile = match &args.profile {
Some(path) => match Profile::from_file(path) {
Ok(profile) => profile,
Err(err) => {
diagnostics.push(CliDiagnostic::prove_error(err.to_string()));
return None;
}
},
None => Profile::empty(),
};
let profile_errors = profile.validate_against(graph);
if !profile_errors.is_empty() {
for error in &profile_errors {
diagnostics.push(CliDiagnostic::prove_error(error.to_string()));
}
return None;
}
let report = match prove(graph, &ProveOptions::with_profile(profile)) {
Ok(report) => report,
Err(err) => {
diagnostics.push(CliDiagnostic::prove_error(err.to_string()));
return None;
}
};
for outcome in report.violations() {
let headline = outcome
.discharge
.counterexample()
.map_or("obligation refuted", |c| c.headline.as_str());
diagnostics.push(CliDiagnostic::prove_violation(
&outcome.obligation.heading(),
headline,
));
}
if !astrs_verify::solver_available() {
diagnostics.push(CliDiagnostic::prove_unavailable());
} else if !report.everything_discharged() {
let summary = report.summary();
diagnostics.push(CliDiagnostic::prove_incomplete(
report.gap_count().saturating_sub(summary.inconclusive),
summary.inconclusive,
));
}
Some(report)
}
fn collect_diagnostics(args: &ValidateArgs) -> (Vec<CliDiagnostic>, Option<DataflowGraph>) {
let mut diagnostics = Vec::new();
let content = match std::fs::read_to_string(&args.manifest_path) {
Ok(content) => content,
Err(err) => {
diagnostics.push(CliDiagnostic::io(format!(
"failed to read `{}`: {err}",
args.manifest_path.display()
)));
return (diagnostics, None);
}
};
let manifest = match Manifest::from_yaml_str(&content) {
Ok(manifest) => manifest,
Err(err) => {
diagnostics.push(CliDiagnostic::parse(&err));
return (diagnostics, None);
}
};
if let Err(errors) = manifest.validate() {
diagnostics.extend(errors.errors().iter().map(CliDiagnostic::structural));
return (diagnostics, None);
}
let base_dir = base_dir_of(&args.manifest_path);
let loader = FsModuleLoader;
let expanded = match manifest.expand(&base_dir, &loader) {
Ok(expanded) => expanded,
Err(err) => {
diagnostics.push(CliDiagnostic::expand(&err));
return (diagnostics, None);
}
};
if let Err(errors) = expanded.validate() {
diagnostics.extend(errors.errors().iter().map(CliDiagnostic::structural));
return (diagnostics, None);
}
match DataflowGraph::from_manifest(&expanded) {
Ok((graph, construction_diagnostics)) => {
diagnostics.extend(construction_diagnostics.iter().map(CliDiagnostic::graph));
diagnostics.extend(graph.diagnostics().iter().map(CliDiagnostic::graph));
(diagnostics, Some(graph))
}
Err(err) => {
diagnostics.push(CliDiagnostic::graph_build_error(&err));
(diagnostics, None)
}
}
}
fn base_dir_of(manifest_path: &Path) -> PathBuf {
match manifest_path.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent.to_path_buf(),
_ => PathBuf::from("."),
}
}
fn render(
out: &mut dyn Write,
args: &ValidateArgs,
report: &ValidateReport,
) -> Result<(), CliError> {
if args.json {
let json = serde_json::to_string_pretty(report).unwrap_or_else(|err| {
format!("{{\"error\": \"failed to serialize validate report: {err}\"}}")
});
writeln!(out, "{json}").map_err(|e| CliError::io("<output>", e))?;
return Ok(());
}
if report.diagnostics.is_empty() {
writeln!(out, "{}: clean", report.manifest_path)
.map_err(|e| CliError::io("<output>", e))?;
} else {
writeln!(out, "{}", render_human(&report.diagnostics, args.color))
.map_err(|e| CliError::io("<output>", e))?;
let error_count = report
.diagnostics
.iter()
.filter(|d| d.severity == Severity::Error)
.count();
let warning_count = report
.diagnostics
.iter()
.filter(|d| d.severity == Severity::Warning)
.count();
writeln!(out, "{error_count} error(s), {warning_count} warning(s)")
.map_err(|e| CliError::io("<output>", e))?;
}
if let Some(proof) = &report.proof {
writeln!(out).map_err(|e| CliError::io("<output>", e))?;
write!(
out,
"{}",
render_proof(
proof,
&RenderOptions {
color: args.color,
show_systems: false,
}
)
)
.map_err(|e| CliError::io("<output>", e))?;
}
Ok(())
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn write_temp(name: &str, content: &str) -> PathBuf {
static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"astrs-cli-validate-test-{}-{name}-{n}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("manifest.yaml");
std::fs::write(&path, content).unwrap();
path
}
fn args(path: PathBuf) -> ValidateArgs {
ValidateArgs {
manifest_path: path,
prove: false,
profile: None,
json: false,
color: false,
}
}
const LIVE: &str = "\
nodes:
- id: camera
path: ./camera
inputs:
tick:
source: astrs/timer/hz/10
queue_size: 1
outputs: [frames]
- id: sink
path: ./sink
inputs:
frames:
source: camera/frames
queue_size: 1
";
#[cfg(feature = "verify")]
const DEADLOCKED: &str = "\
nodes:
- id: a
path: ./a
inputs: { i: b/out }
outputs: [out]
- id: b
path: ./b
inputs: { i: a/out }
outputs: [out]
";
#[test]
fn missing_file_is_an_io_diagnostic_with_exit_code_two() {
let mut out = Vec::new();
let a = args(std::env::temp_dir().join("astrs-cli-does-not-exist.yaml"));
let report = run(&mut out, &a).unwrap();
assert_eq!(report.exit_code(), 2);
assert_eq!(report.diagnostics.len(), 1);
assert_eq!(report.diagnostics[0].source, crate::diagnostic::Source::Io);
}
#[test]
fn malformed_yaml_is_a_parse_diagnostic() {
let path = write_temp("malformed", "nodes: [");
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(report.exit_code(), 2);
assert_eq!(
report.diagnostics[0].source,
crate::diagnostic::Source::Parse
);
}
#[test]
fn structural_violation_is_reported() {
let path = write_temp(
"dup-id",
"nodes:\n - id: x\n path: ./x\n - id: x\n path: ./y\n",
);
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(report.exit_code(), 2);
assert!(
report
.diagnostics
.iter()
.any(|d| d.source == crate::diagnostic::Source::Structural)
);
}
#[test]
fn clean_manifest_has_no_diagnostics_and_exit_code_zero() {
let path = write_temp(
"clean",
"nodes:\n - id: camera\n path: ./camera\n outputs: [frames]\n - id: sink\n path: ./sink\n inputs:\n frames: camera/frames\n",
);
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(
report.diagnostics.len(),
0,
"diagnostics: {:?}",
report.diagnostics
);
assert_eq!(report.exit_code(), 0);
let text = String::from_utf8(out).unwrap();
assert!(text.contains("clean"));
}
#[test]
fn unconsumed_output_is_an_info_diagnostic_with_exit_code_zero() {
let path = write_temp(
"unconsumed",
"nodes:\n - id: camera\n path: ./camera\n outputs: [frames]\n",
);
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(report.diagnostics.len(), 1);
assert_eq!(report.diagnostics[0].severity, astrs_graph::Severity::Info);
assert_eq!(report.exit_code(), 0);
}
#[test]
fn a_plain_cycle_is_a_warning_with_exit_code_one() {
let path = write_temp(
"cycle",
"nodes:\n - id: a\n path: ./a\n outputs: [out]\n inputs:\n i: b/out\n - id: b\n path: ./b\n outputs: [out]\n inputs:\n i: a/out\n",
);
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(report.exit_code(), 1);
assert!(
report
.diagnostics
.iter()
.any(|d| d.severity == astrs_graph::Severity::Warning)
);
}
#[test]
fn collect_diagnostics_hands_the_graph_back_for_proving() {
let a = args(write_temp("graph-out", LIVE));
let (diagnostics, graph) = collect_diagnostics(&a);
assert!(diagnostics.is_empty(), "{diagnostics:?}");
assert_eq!(graph.map(|g| g.node_count()), Some(2));
}
#[test]
fn a_manifest_that_never_became_a_graph_cannot_be_proved() {
let mut a = args(write_temp("no-graph", "nodes: ["));
a.prove = true;
let mut out = Vec::new();
let report = run(&mut out, &a).expect("a parse failure is reported, not raised");
assert!(report.proof.is_none());
assert!(
report
.diagnostics
.iter()
.any(|d| d.source == crate::diagnostic::Source::Prove)
);
}
#[test]
fn a_profile_that_does_not_match_the_graph_reports_every_mismatch() {
let dir = write_temp("bad-profile", LIVE);
let profile = dir.with_file_name("verify.yaml");
std::fs::write(
&profile,
"nodes:\n ghost:\n wcet: 0.001\n phantom:\n wcet: 0.002\n",
)
.unwrap();
let mut a = args(dir);
a.prove = true;
a.profile = Some(profile);
let mut out = Vec::new();
let report = run(&mut out, &a).expect("a profile failure is reported, not raised");
assert!(report.proof.is_none());
let prove_errors = report
.diagnostics
.iter()
.filter(|d| d.source == crate::diagnostic::Source::Prove)
.count();
assert_eq!(prove_errors, 2, "both mismatches must be reported at once");
}
#[test]
fn a_missing_profile_file_is_reported_not_ignored() {
let mut a = args(write_temp("missing-profile", LIVE));
a.prove = true;
a.profile = Some(std::env::temp_dir().join("astrs-cli-no-such-profile.yaml"));
let mut out = Vec::new();
let report = run(&mut out, &a).expect("a missing profile is reported, not raised");
assert!(report.proof.is_none());
assert!(
report
.diagnostics
.iter()
.any(|d| d.message.contains("verification profile"))
);
}
#[test]
#[cfg(not(feature = "verify"))]
fn without_the_feature_prove_encodes_and_then_reports_no_solver() {
let mut a = args(write_temp("prove-nofeature", LIVE));
a.prove = true;
let mut out = Vec::new();
let err = run(&mut out, &a).unwrap_err();
assert!(matches!(err, CliError::ProofUnavailable), "{err:?}");
assert_eq!(err.exit_code(), crate::error::EXIT_UNAVAILABLE);
let printed = String::from_utf8(out).unwrap();
assert!(printed.contains("--features verify"), "{printed}");
assert!(printed.contains("graph proofs"), "{printed}");
}
#[test]
#[cfg(feature = "verify")]
fn a_live_graph_without_a_profile_proves_clean_but_exits_one() {
let mut a = args(write_temp("prove-clean", LIVE));
a.prove = true;
let mut out = Vec::new();
let report = run(&mut out, &a).unwrap();
let proof = report.proof.as_ref().expect("a proof was run");
assert!(!proof.has_violations());
assert_eq!(report.exit_code(), 1);
let printed = String::from_utf8(out).unwrap();
assert!(printed.contains("no obligation was violated"), "{printed}");
}
#[test]
#[cfg(feature = "verify")]
fn a_deadlocking_graph_is_an_error_with_exit_code_two() {
let mut a = args(write_temp("prove-deadlock", DEADLOCKED));
a.prove = true;
let mut out = Vec::new();
let report = run(&mut out, &a).unwrap();
assert_eq!(report.exit_code(), 2);
let proof = report.proof.as_ref().expect("a proof was run");
assert!(proof.has_violations());
assert!(
report.diagnostics.iter().any(|d| {
d.source == crate::diagnostic::Source::Prove
&& d.severity == astrs_graph::Severity::Error
&& d.message.contains("deadlock freedom")
}),
"{:?}",
report.diagnostics
);
let printed = String::from_utf8(out).unwrap();
assert!(printed.contains("can never fire"), "{printed}");
assert!(printed.contains("fix:"), "{printed}");
}
#[test]
#[cfg(feature = "verify")]
fn a_full_profile_lets_the_run_reach_a_verdict_everywhere() {
let manifest = write_temp("prove-profiled", LIVE);
let profile = manifest.with_file_name("verify.yaml");
std::fs::write(
&profile,
"nodes:\n camera:\n wcet: 0.001\n sink:\n wcet: 0.001\n",
)
.unwrap();
let mut a = args(manifest);
a.prove = true;
a.profile = Some(profile);
let mut out = Vec::new();
let report = run(&mut out, &a).unwrap();
let proof = report.proof.as_ref().expect("a proof was run");
assert!(proof.everything_discharged(), "{:?}", proof.summary());
assert_eq!(report.exit_code(), 0);
}
#[test]
fn prove_output_is_appended_after_the_diagnostics() {
let mut a = args(write_temp("prove-order", LIVE));
a.prove = true;
let mut out = Vec::new();
let _ = run(&mut out, &a);
let printed = String::from_utf8(out).unwrap();
assert!(printed.contains("graph proofs"), "{printed}");
}
#[test]
fn prove_json_nests_the_whole_report() {
let mut a = args(write_temp("prove-json", LIVE));
a.prove = true;
a.json = true;
let mut out = Vec::new();
let _ = run(&mut out, &a);
let text = String::from_utf8(out).unwrap();
let value: serde_json::Value = serde_json::from_str(&text).unwrap();
assert!(value["proof"]["obligations"].is_array(), "{text}");
assert!(value["proof"]["window_seconds"].is_number(), "{text}");
}
#[test]
fn json_output_is_valid_json_and_contains_the_manifest_path() {
let path = write_temp("json", "nodes:\n - id: solo\n path: ./solo\n");
let mut out = Vec::new();
let mut a = args(path.clone());
a.json = true;
let report = run(&mut out, &a).unwrap();
assert_eq!(report.exit_code(), 0);
let text = String::from_utf8(out).unwrap();
let value: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(
value["manifest_path"].as_str(),
Some(path.display().to_string().as_str())
);
}
#[test]
fn module_that_fails_to_expand_is_an_expand_diagnostic() {
let path = write_temp(
"bad-module",
"nodes:\n - id: m\n module: ./missing.yaml\n",
);
let mut out = Vec::new();
let report = run(&mut out, &args(path)).unwrap();
assert_eq!(report.exit_code(), 2);
assert!(
report
.diagnostics
.iter()
.any(|d| d.source == crate::diagnostic::Source::Expand)
);
}
#[test]
fn base_dir_of_bare_filename_is_dot() {
assert_eq!(base_dir_of(Path::new("manifest.yaml")), PathBuf::from("."));
}
#[test]
fn base_dir_of_nested_path_is_its_parent() {
assert_eq!(
base_dir_of(Path::new("/graphs/sub/manifest.yaml")),
PathBuf::from("/graphs/sub")
);
}
}