huskies: merge 474_story_per_file_test_coverage_report_with_improvement_targets
This commit is contained in:
@@ -1,16 +1,35 @@
|
||||
//! Handler for the `coverage` command — show or refresh test coverage.
|
||||
//!
|
||||
//! Default (no args): reads the cached `.coverage_baseline` file and reports
|
||||
//! the stored value instantly without running the test suite.
|
||||
//! Default (no args): reads the cached `.coverage_report.json` file (written by
|
||||
//! `script/test_coverage`) and reports the overall percentage plus the top 5
|
||||
//! lowest-covered files as improvement targets.
|
||||
//!
|
||||
//! `coverage run`: executes `script/test_coverage` to collect fresh coverage
|
||||
//! data, parses the summary, and reports the result.
|
||||
//! data (which writes `.coverage_report.json`), then reports the result.
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::CommandContext;
|
||||
|
||||
const COVERAGE_REPORT_FILE: &str = ".coverage_report.json";
|
||||
const BASELINE_FILE: &str = ".coverage_baseline";
|
||||
const COVERAGE_SCRIPT: &str = "script/test_coverage";
|
||||
|
||||
/// Top-level structure of `.coverage_report.json`.
|
||||
#[derive(Deserialize)]
|
||||
struct CoverageReport {
|
||||
overall: f64,
|
||||
threshold: f64,
|
||||
files: Vec<FileCoverage>,
|
||||
}
|
||||
|
||||
/// Per-file coverage entry.
|
||||
#[derive(Deserialize)]
|
||||
struct FileCoverage {
|
||||
path: String,
|
||||
coverage: f64,
|
||||
}
|
||||
|
||||
pub(super) fn handle_coverage(ctx: &CommandContext) -> Option<String> {
|
||||
let args = ctx.args.trim();
|
||||
|
||||
@@ -23,8 +42,61 @@ pub(super) fn handle_coverage(ctx: &CommandContext) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Read coverage from `.coverage_baseline` and return a formatted report.
|
||||
/// Read coverage from `.coverage_report.json` (preferred) or `.coverage_baseline`
|
||||
/// (legacy fallback) and return a formatted report.
|
||||
fn read_cached_coverage(project_root: &std::path::Path) -> String {
|
||||
let report_path = project_root.join(COVERAGE_REPORT_FILE);
|
||||
|
||||
if report_path.exists() {
|
||||
return read_coverage_report(&report_path);
|
||||
}
|
||||
|
||||
// Legacy fallback: read the plain-text baseline file.
|
||||
read_legacy_baseline(project_root)
|
||||
}
|
||||
|
||||
/// Parse and format `.coverage_report.json`.
|
||||
fn read_coverage_report(path: &std::path::Path) -> String {
|
||||
let content = match std::fs::read_to_string(path) {
|
||||
Ok(c) => c,
|
||||
Err(e) => return format!("**Coverage (cached)**\n\nError reading `.coverage_report.json`: {e}"),
|
||||
};
|
||||
|
||||
let report: CoverageReport = match serde_json::from_str(&content) {
|
||||
Ok(r) => r,
|
||||
Err(e) => return format!("**Coverage (cached)**\n\nFailed to parse `.coverage_report.json`: {e}"),
|
||||
};
|
||||
|
||||
format_coverage_report(&report)
|
||||
}
|
||||
|
||||
/// Format a `CoverageReport` as a human-readable message.
|
||||
fn format_coverage_report(report: &CoverageReport) -> String {
|
||||
let mut out = "**Coverage (cached)**\n\n".to_string();
|
||||
out.push_str(&format!(
|
||||
"Overall: {:.1}% (threshold: {:.1}%)\n",
|
||||
report.overall, report.threshold
|
||||
));
|
||||
|
||||
// Top 5 lowest-covered files (already sorted ascending in the JSON, but sort
|
||||
// defensively here so the display is correct even if the file was hand-edited).
|
||||
let mut sorted: Vec<&FileCoverage> = report.files.iter().collect();
|
||||
sorted.sort_by(|a, b| a.coverage.partial_cmp(&b.coverage).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
let targets: Vec<&FileCoverage> = sorted.into_iter().take(5).collect();
|
||||
if !targets.is_empty() {
|
||||
out.push_str("\n**Top 5 files needing coverage:**\n");
|
||||
for (i, file) in targets.iter().enumerate() {
|
||||
out.push_str(&format!("{}. {} — {:.1}%\n", i + 1, file.path, file.coverage));
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("\n*Run `coverage run` for fresh results.*");
|
||||
out
|
||||
}
|
||||
|
||||
/// Legacy fallback: read `.coverage_baseline` (plain text, one float per line).
|
||||
fn read_legacy_baseline(project_root: &std::path::Path) -> String {
|
||||
let baseline_path = project_root.join(BASELINE_FILE);
|
||||
|
||||
match std::fs::read_to_string(&baseline_path) {
|
||||
@@ -33,21 +105,17 @@ fn read_cached_coverage(project_root: &std::path::Path) -> String {
|
||||
if lines.is_empty() {
|
||||
return "**Coverage (cached)**\n\nNo baseline data found.\n\n*Run `coverage run` to collect coverage.*".to_string();
|
||||
}
|
||||
format_cached_coverage(&lines)
|
||||
format_legacy_baseline(&lines)
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
"**Coverage (cached)**\n\nNo `.coverage_baseline` file found.\n\n*Run `coverage run` to collect coverage.*".to_string()
|
||||
"**Coverage (cached)**\n\nNo `.coverage_report.json` or `.coverage_baseline` found.\n\n*Run `coverage run` to collect coverage.*".to_string()
|
||||
}
|
||||
Err(e) => format!("**Coverage (cached)**\n\nError reading `.coverage_baseline`: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Format cached coverage lines for display.
|
||||
///
|
||||
/// The baseline file stores the last overall coverage percentage written by
|
||||
/// `script/test_coverage`. It always contains one line (the overall percentage)
|
||||
/// but may have been seeded manually with two lines (Rust + Frontend baselines).
|
||||
fn format_cached_coverage(lines: &[&str]) -> String {
|
||||
/// Format legacy baseline lines for display.
|
||||
fn format_legacy_baseline(lines: &[&str]) -> String {
|
||||
let mut out = "**Coverage (cached)**\n\n".to_string();
|
||||
if lines.len() == 2 {
|
||||
out.push_str(&format!("Rust: {}%\n", lines[0].trim()));
|
||||
@@ -83,17 +151,30 @@ fn run_coverage(project_root: &std::path::Path) -> String {
|
||||
|
||||
// Check if llvm-cov is missing
|
||||
if combined.contains("cargo-llvm-cov not available") && !combined.contains("TOTAL") {
|
||||
return format!(
|
||||
"**Coverage**\n\n`cargo-llvm-cov` is not installed.\n\nInstall it with:\n```\ncargo install cargo-llvm-cov\n```"
|
||||
);
|
||||
return "**Coverage**\n\n`cargo-llvm-cov` is not installed.\n\nInstall it with:\n```\ncargo install cargo-llvm-cov\n```".to_string();
|
||||
}
|
||||
|
||||
// After the script runs, the .coverage_report.json is updated.
|
||||
// Read it to show per-file targets in the response.
|
||||
let report_path = project_root.join(COVERAGE_REPORT_FILE);
|
||||
if report_path.exists() {
|
||||
let mut result = read_coverage_report(&report_path);
|
||||
// Replace the "cached" label with "fresh".
|
||||
result = result.replacen("Coverage (cached)", "Coverage (fresh)", 1);
|
||||
// Replace the cached hint with a pass/fail indicator.
|
||||
let pass_indicator = if out.status.success() { "PASS" } else { "FAIL: coverage below threshold" };
|
||||
result = result.replacen("*Run `coverage run` for fresh results.*", pass_indicator, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Fallback: parse the text output as before.
|
||||
parse_coverage_output(&combined, out.status.success())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the summary block from `script/test_coverage` output and format it.
|
||||
/// Used as a fallback when `.coverage_report.json` is not available after a run.
|
||||
fn parse_coverage_output(output: &str, passed: bool) -> String {
|
||||
let rust_cov = extract_line_value(output, "Rust line coverage:");
|
||||
let frontend_cov = extract_line_value(output, "Frontend line coverage:");
|
||||
@@ -117,7 +198,15 @@ fn parse_coverage_output(output: &str, passed: bool) -> String {
|
||||
|
||||
if rust_cov.is_none() && frontend_cov.is_none() && overall.is_none() {
|
||||
// Could not parse — show raw output excerpt
|
||||
let excerpt: String = output.lines().rev().take(10).collect::<Vec<_>>().into_iter().rev().collect::<Vec<_>>().join("\n");
|
||||
let excerpt: String = output
|
||||
.lines()
|
||||
.rev()
|
||||
.take(10)
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.rev()
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
return format!("**Coverage (fresh)**\n\n```\n{excerpt}\n```");
|
||||
}
|
||||
|
||||
@@ -181,6 +270,17 @@ mod tests {
|
||||
Arc::new(Mutex::new(HashSet::new()))
|
||||
}
|
||||
|
||||
fn sample_coverage_report(overall: f64, threshold: f64, files: Vec<(&str, f64)>) -> String {
|
||||
let files_json: Vec<String> = files
|
||||
.iter()
|
||||
.map(|(path, cov)| format!(r#"{{"path":"{path}","coverage":{cov}}}"#))
|
||||
.collect();
|
||||
format!(
|
||||
r#"{{"overall":{overall},"threshold":{threshold},"files":[{}]}}"#,
|
||||
files_json.join(",")
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_command_is_registered() {
|
||||
use super::super::commands;
|
||||
@@ -203,7 +303,58 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_no_args_reads_baseline() {
|
||||
fn coverage_reads_coverage_report_json() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let report = sample_coverage_report(
|
||||
72.5,
|
||||
60.0,
|
||||
vec![
|
||||
("server/src/low.rs", 15.0),
|
||||
("server/src/medium.rs", 55.0),
|
||||
("server/src/high.rs", 90.0),
|
||||
],
|
||||
);
|
||||
std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();
|
||||
|
||||
let agents = test_agents();
|
||||
let ambient = test_ambient();
|
||||
let ctx = make_ctx(&agents, &ambient, dir.path(), "");
|
||||
let output = handle_coverage(&ctx).unwrap();
|
||||
|
||||
assert!(output.contains("72.5"), "should include overall: {output}");
|
||||
assert!(output.contains("60.0"), "should include threshold: {output}");
|
||||
assert!(output.contains("15.0"), "should include lowest-covered file pct: {output}");
|
||||
assert!(output.contains("server/src/low.rs"), "should include lowest-covered file path: {output}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_shows_top_5_lowest_files() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let files: Vec<(&str, f64)> = vec![
|
||||
("a.rs", 10.0),
|
||||
("b.rs", 20.0),
|
||||
("c.rs", 30.0),
|
||||
("d.rs", 40.0),
|
||||
("e.rs", 50.0),
|
||||
("f.rs", 60.0),
|
||||
("g.rs", 70.0),
|
||||
];
|
||||
let report = sample_coverage_report(40.0, 30.0, files);
|
||||
std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();
|
||||
|
||||
let agents = test_agents();
|
||||
let ambient = test_ambient();
|
||||
let ctx = make_ctx(&agents, &ambient, dir.path(), "");
|
||||
let output = handle_coverage(&ctx).unwrap();
|
||||
|
||||
assert!(output.contains("a.rs"), "should show lowest file: {output}");
|
||||
assert!(output.contains("e.rs"), "should show 5th lowest file: {output}");
|
||||
assert!(!output.contains("f.rs"), "should not show 6th file: {output}");
|
||||
assert!(!output.contains("g.rs"), "should not show 7th file: {output}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_falls_back_to_baseline_when_no_report_json() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
std::fs::write(dir.path().join(".coverage_baseline"), "72.5\n").unwrap();
|
||||
|
||||
@@ -243,7 +394,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coverage_missing_baseline_reports_clearly() {
|
||||
fn coverage_missing_both_files_reports_clearly() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
|
||||
let agents = test_agents();
|
||||
@@ -252,8 +403,8 @@ mod tests {
|
||||
let output = handle_coverage(&ctx).unwrap();
|
||||
|
||||
assert!(
|
||||
output.contains("No `.coverage_baseline`") || output.contains("coverage_baseline"),
|
||||
"missing baseline should mention the file name: {output}"
|
||||
output.contains("coverage_report.json") || output.contains("coverage_baseline"),
|
||||
"missing files should mention a file name: {output}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -288,7 +439,8 @@ mod tests {
|
||||
#[test]
|
||||
fn coverage_command_works_via_dispatch() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
std::fs::write(dir.path().join(".coverage_baseline"), "55.0\n").unwrap();
|
||||
let report = sample_coverage_report(55.0, 50.0, vec![]);
|
||||
std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();
|
||||
|
||||
let agents = test_agents();
|
||||
let ambient = test_ambient();
|
||||
@@ -308,6 +460,23 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_coverage_report_shows_overall_and_threshold() {
|
||||
let report = CoverageReport {
|
||||
overall: 66.25,
|
||||
threshold: 60.0,
|
||||
files: vec![
|
||||
FileCoverage { path: "a.rs".to_string(), coverage: 10.0 },
|
||||
FileCoverage { path: "b.rs".to_string(), coverage: 80.0 },
|
||||
],
|
||||
};
|
||||
let result = format_coverage_report(&report);
|
||||
assert!(result.contains("66.2"), "should show overall: {result}");
|
||||
assert!(result.contains("60.0"), "should show threshold: {result}");
|
||||
assert!(result.contains("a.rs"), "should show lowest file: {result}");
|
||||
assert!(result.contains("10.0"), "should show lowest file pct: {result}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_coverage_output_extracts_fields() {
|
||||
let sample = "\
|
||||
|
||||
Reference in New Issue
Block a user