use anyhow::Result;
use htl::Fix;
use htl::testing::FileReport;
use serde::Serialize;
pub use htl::{Diagnostic, Severity};
pub use htl::cache::DependencyJson;
pub struct Out {
pub json: bool,
diagnostics: Vec<Diagnostic>,
}
impl Out {
pub fn new(json: bool) -> Self {
Self {
json,
diagnostics: Vec::new(),
}
}
pub fn take(&mut self) -> Vec<Diagnostic> {
std::mem::take(&mut self.diagnostics)
}
}
impl htl::project::Output for Out {
fn diagnostic(
&mut self,
severity: Severity,
text: &str,
fix: Option<&Fix>,
dependency: Option<&DependencyJson>,
) {
if self.json {
self.diagnostics
.push(htl::project::diagnostic_of(severity, text, fix, dependency));
} else if let Some(dep) = dependency {
eprintln!("{severity}: {text}\n (required by {})", dep.required_by);
} else {
match fix.map(|f| f.applicability.as_str()) {
Some("safe") | Some("suggest") => {
eprintln!("{severity}: {text} (fixable: htl fix)")
}
Some("unsafe") => eprintln!("{severity}: {text} (fixable: htl fix --unsafe)"),
_ => eprintln!("{severity}: {text}"),
}
}
}
}
#[derive(Serialize, Debug)]
pub struct CheckReport {
pub files: usize,
pub patched: usize,
pub diagnostics: Vec<Diagnostic>,
pub summary: CheckSummary,
}
#[derive(Serialize, Debug)]
pub struct CheckSummary {
pub errors: usize,
pub warnings: usize,
pub lints: usize,
pub denied: usize,
pub strict: bool,
pub ok: bool,
pub cached: bool,
pub replayed: usize,
}
#[derive(Serialize, Debug)]
pub struct TestCase {
pub name: String,
pub ok: bool,
pub ms: f64,
}
#[derive(Serialize, Debug)]
pub struct TestFile {
pub path: String,
pub ok: bool,
pub diagnostics: Vec<Diagnostic>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
pub file_level: bool,
pub passed: usize,
pub failed: usize,
pub failures: Vec<String>,
pub tests: Vec<TestCase>,
pub duration_ms: f64,
pub snapshots_written: Vec<String>,
pub snapshots_updated: Vec<String>,
}
impl TestFile {
pub fn from_report(rep: &FileReport, diagnostics: Vec<Diagnostic>) -> Self {
Self {
path: rep.path.display().to_string(),
ok: rep.ok(),
diagnostics,
error: rep.error.clone(),
file_level: rep.file_level,
passed: rep.passed,
failed: rep.failed,
failures: rep.failures.clone(),
tests: rep
.tests
.iter()
.map(|t| TestCase {
name: t.name.clone(),
ok: t.ok,
ms: t.ms,
})
.collect(),
duration_ms: rep.duration_ms,
snapshots_written: rep.snapshots_written.clone(),
snapshots_updated: rep.snapshots_updated.clone(),
}
}
}
#[derive(Serialize, Debug)]
pub struct TestSummary {
pub files: usize,
pub files_run: usize,
pub passed: usize,
pub failed: usize,
pub files_with_errors: usize,
pub replayed: usize,
pub duration_ms: f64,
pub ok: bool,
pub seed: u64,
}
pub use htl::project::CoverageReport;
#[derive(Serialize, Debug)]
pub struct TestReport {
pub files: Vec<TestFile>,
pub summary: TestSummary,
#[serde(skip_serializing_if = "Option::is_none")]
pub coverage: Option<CoverageReport>,
}
#[derive(Serialize, Debug)]
pub struct FixApplied {
pub file: String,
pub line: usize,
pub rule: String,
pub applicability: &'static str,
pub pass: usize,
}
#[derive(Serialize, Debug)]
pub struct FixSkipped {
pub file: String,
pub line: usize,
pub rule: String,
pub reason: String,
}
#[derive(Serialize, Debug)]
pub struct FixFile {
pub path: String,
pub changed: bool,
pub deferred: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub reverted: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub oscillation: Option<String>,
pub diagnostics: Vec<Diagnostic>,
}
#[derive(Serialize, Debug)]
pub struct FixReport {
pub dry_run: bool,
pub applied: Vec<FixApplied>,
pub skipped: Vec<FixSkipped>,
pub files: Vec<FixFile>,
pub summary: FixSummary,
}
#[derive(Serialize, Debug)]
pub struct FixSummary {
pub files: usize,
pub files_changed: usize,
pub applied: usize,
pub skipped: usize,
pub deferred: usize,
pub reverted: usize,
pub errors_remaining: usize,
pub ok: bool,
}
pub fn emit<T: Serialize>(v: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(v)?);
Ok(())
}