//! Coverage reports (oxygenhub#113): Coveralls built into the hub. An //! OxygenFlow job that declares an artifact named `coverage` in LCOV //! format (what `cargo llvm-cov --lcov`, Istanbul, coverage.py and //! gcov/lcov write) has it parsed when it is uploaded. The totals and each //! file's counts are kept per run; the per-line counts are read again from //! the artifact's blob for the file view. Paths are taken relative to the //! job's workspace (`/src`), and only files of the run's state are kept. use crate::db::{now_ms, Pool}; use crate::Shared; use std::collections::{BTreeMap, HashSet}; use std::sync::Mutex; /// The artifact name a pipeline gives its report. pub const ARTIFACT: &str = "coverage"; /// The largest report parsed. pub const MAX_REPORT: usize = 64 << 20; /// One file's coverage, from a report. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct FileCoverage { /// Relative to the repository's root. pub path: String, /// Each instrumented line and how often it ran, by line number. pub lines: BTreeMap, pub functions_found: u64, pub functions_hit: u64, pub branches_found: u64, pub branches_hit: u64, /// The file's own `LF`/`LH` totals, when it had one record: the tool's /// own count, which can name lines the `DA` lines leave out (llvm-cov /// does), so the hub agrees with what the tool printed. pub declared_lines: Option<(u64, u64)>, } impl FileCoverage { pub fn lines_found(&self) -> u64 { self.declared_lines.map(|d| d.0).unwrap_or(self.lines.len() as u64) } pub fn lines_hit(&self) -> u64 { self.declared_lines.map(|d| d.1).unwrap_or_else(|| self.lines.values().filter(|n| **n > 0).count() as u64) } } /// A path as a report names it, relative to the repository: the Flow /// workspace's `/src/` and a leading `./` taken off. `None` for a path /// outside the repository. pub fn relative(path: &str) -> Option { let p = path.trim(); let p = p.strip_prefix("/src/").unwrap_or(p); let p = p.strip_prefix("./").unwrap_or(p); if p.is_empty() || p.starts_with('/') || p.contains('\\') || p.split('/').any(|s| s.is_empty() || s == "." || s == "..") { return None; } Some(p.to_string()) } #[derive(Default)] struct Acc { lines: BTreeMap, functions: BTreeMap, branches: BTreeMap<(u32, String, String), u64>, declared_fn: (u64, u64), declared_br: (u64, u64), declared_lines: (Option, Option), records: u32, } /// Parse an LCOV report into each file's coverage, records for the same /// file (from several test binaries) added together. Files whose paths /// are outside the repository are counted in the second value. pub fn parse_lcov(text: &str) -> Result<(Vec, usize), String> { let mut files: BTreeMap = BTreeMap::new(); let mut current: Option> = None; let mut outside = HashSet::new(); let mut records = 0usize; // A new SF: record has begun and not yet been counted for its file. let mut started = false; let num = |s: &str| s.trim().parse::().unwrap_or(0); for line in text.lines() { let line = line.trim(); if let Some(path) = line.strip_prefix("SF:") { records += 1; let rel = relative(path); if rel.is_none() { outside.insert(path.to_string()); } current = Some(rel); started = true; continue; } if line == "end_of_record" { current = None; continue; } let Some(Some(path)) = ¤t else { continue }; let acc = files.entry(path.clone()).or_default(); if line == "TN:" || line.starts_with("TN:") { continue; } if std::mem::take(&mut started) { acc.records += 1; } if let Some(n) = line.strip_prefix("LF:") { acc.declared_lines.0 = Some(num(n)); } else if let Some(n) = line.strip_prefix("LH:") { acc.declared_lines.1 = Some(num(n)); } else if let Some(rest) = line.strip_prefix("DA:") { let mut parts = rest.split(','); if let (Some(l), Some(n)) = (parts.next(), parts.next()) { if let Ok(l) = l.trim().parse::() { *acc.lines.entry(l).or_default() += num(n); } } } else if let Some(rest) = line.strip_prefix("FN:") { if let Some((_, name)) = rest.split_once(',') { acc.functions.entry(name.to_string()).or_default(); } } else if let Some(rest) = line.strip_prefix("FNDA:") { if let Some((n, name)) = rest.split_once(',') { *acc.functions.entry(name.to_string()).or_default() += num(n); } } else if let Some(rest) = line.strip_prefix("BRDA:") { let parts: Vec<&str> = rest.split(',').collect(); if let [l, block, branch, taken] = parts[..] { if let Ok(l) = l.trim().parse::() { let taken = if taken.trim() == "-" { 0 } else { num(taken) }; *acc.branches.entry((l, block.to_string(), branch.to_string())).or_default() += taken; } } } else if let Some(n) = line.strip_prefix("FNF:") { acc.declared_fn.0 = acc.declared_fn.0.max(num(n)); } else if let Some(n) = line.strip_prefix("FNH:") { acc.declared_fn.1 = acc.declared_fn.1.max(num(n)); } else if let Some(n) = line.strip_prefix("BRF:") { acc.declared_br.0 = acc.declared_br.0.max(num(n)); } else if let Some(n) = line.strip_prefix("BRH:") { acc.declared_br.1 = acc.declared_br.1.max(num(n)); } } if records == 0 { return Err("this is not an LCOV report: it names no source file (no SF: line)".into()); } let out = files .into_iter() .map(|(path, a)| { let (functions_found, functions_hit) = if a.functions.is_empty() { a.declared_fn } else { (a.functions.len() as u64, a.functions.values().filter(|n| **n > 0).count() as u64) }; let (branches_found, branches_hit) = if a.branches.is_empty() { a.declared_br } else { (a.branches.len() as u64, a.branches.values().filter(|n| **n > 0).count() as u64) }; let declared_lines = match (a.records, a.declared_lines) { (1, (Some(f), Some(h))) if h <= f && f >= a.lines.len() as u64 => Some((f, h)), _ => None, }; FileCoverage { path, lines: a.lines, functions_found, functions_hit, branches_found, branches_hit, declared_lines } }) .collect(); Ok((out, outside.len())) } /// A percentage to one decimal place, or none when nothing was counted. pub fn percent(hit: i64, found: i64) -> Option { (found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0) } #[derive(Clone, Debug, sqlx::FromRow)] pub struct Report { pub id: i64, pub repo_id: i64, pub run_id: i64, pub job_id: i64, pub job: String, pub run_number: i64, pub bookmark: String, pub state: String, pub heads: String, pub blob: String, pub format: String, pub lines_found: i64, pub lines_hit: i64, pub functions_found: i64, pub functions_hit: i64, pub branches_found: i64, pub branches_hit: i64, pub files: i64, pub dropped: i64, pub created_ms: i64, } impl Report { pub fn percent(&self) -> Option { percent(self.lines_hit, self.lines_found) } pub fn functions_percent(&self) -> Option { percent(self.functions_hit, self.functions_found) } pub fn branches_percent(&self) -> Option { percent(self.branches_hit, self.branches_found) } pub fn head_list(&self) -> Vec<&str> { self.heads.split(',').filter(|h| !h.is_empty()).collect() } } #[derive(Clone, Debug, sqlx::FromRow)] pub struct FileRow { pub report_id: i64, pub path: String, pub lines_found: i64, pub lines_hit: i64, pub functions_found: i64, pub functions_hit: i64, pub branches_found: i64, pub branches_hit: i64, } impl FileRow { pub fn percent(&self) -> Option { percent(self.lines_hit, self.lines_found) } pub fn missed(&self) -> i64 { self.lines_found - self.lines_hit } } /// Repositories with a report, so their pages show the Coverage tab: /// keyed by the hub's repository root and the id, unique even between /// hubs in one process. static HAS: Mutex>> = Mutex::new(None); fn key(app: &Shared, repo_id: i64) -> String { format!("{}#{repo_id}", app.repos.root().display()) } pub fn has_reports(app: &Shared, repo_id: i64) -> bool { HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id))) } fn mark(app: &Shared, repo_id: i64) { HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(HashSet::new).insert(key(app, repo_id)); } /// At start: which repositories have reports. pub async fn load(app: &Shared) -> sqlx::Result<()> { let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM coverage_reports").fetch_all(&app.db).await?; for (id,) in ids { mark(app, id); } Ok(()) } /// What a run's report is kept with. pub struct Origin<'a> { pub repo_id: i64, pub run_id: i64, pub run_number: i64, pub job_id: i64, pub job: &'a str, pub bookmark: &'a str, pub state: &'a str, pub heads: &'a [String], pub blob: &'a str, } /// Keep a parsed report: the files outside `in_state` are dropped and /// counted. Replaces an earlier report of the same job. pub async fn keep(app: &Shared, o: &Origin<'_>, files: Vec, outside: usize, in_state: &HashSet) -> sqlx::Result { let (kept, missing): (Vec, Vec) = files.into_iter().partition(|f| in_state.contains(&f.path)); let sum = |f: &dyn Fn(&FileCoverage) -> u64| kept.iter().map(f).sum::() as i64; let mut tx = crate::db::begin_write(&app.db).await?; sqlx::query("DELETE FROM coverage_reports WHERE job_id = ?").bind(o.job_id).execute(&mut *tx).await?; let id: i64 = sqlx::query_scalar( "INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \ lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, files, dropped, created_ms) \ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'lcov', ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id", ) .bind(o.repo_id) .bind(o.run_id) .bind(o.job_id) .bind(o.job) .bind(o.run_number) .bind(o.bookmark) .bind(o.state) .bind(o.heads.join(",")) .bind(o.blob) .bind(sum(&|f| f.lines_found())) .bind(sum(&|f| f.lines_hit())) .bind(sum(&|f| f.functions_found)) .bind(sum(&|f| f.functions_hit)) .bind(sum(&|f| f.branches_found)) .bind(sum(&|f| f.branches_hit)) .bind(kept.len() as i64) .bind((missing.len() + outside) as i64) .bind(now_ms()) .fetch_one(&mut *tx) .await?; for f in &kept { sqlx::query( "INSERT INTO coverage_files (report_id, path, lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit) \ VALUES (?, ?, ?, ?, ?, ?, ?, ?)", ) .bind(id) .bind(&f.path) .bind(f.lines_found() as i64) .bind(f.lines_hit() as i64) .bind(f.functions_found as i64) .bind(f.functions_hit as i64) .bind(f.branches_found as i64) .bind(f.branches_hit as i64) .execute(&mut *tx) .await?; } tx.commit().await?; mark(app, o.repo_id); Ok(id) } pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM coverage_reports WHERE id = ? AND repo_id = ?").bind(id).bind(repo_id).fetch_optional(pool).await } /// The newest reports on a bookmark, newest first. pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM coverage_reports WHERE repo_id = ? AND bookmark = ? ORDER BY created_ms DESC, id DESC LIMIT ?") .bind(repo_id) .bind(bookmark) .bind(limit) .fetch_all(pool) .await } pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = ? ORDER BY job").bind(run_id).fetch_all(pool).await } pub async fn files(pool: &Pool, report_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM coverage_files WHERE report_id = ? ORDER BY path").bind(report_id).fetch_all(pool).await } #[cfg(test)] mod tests { use super::*; const LCOV: &str = "TN:\nSF:/src/crates/a/src/lib.rs\nFN:3,alpha\nFN:9,beta\nFNDA:4,alpha\nFNDA:0,beta\nFNF:2\nFNH:1\n\ DA:3,4\nDA:4,4\nDA:9,0\nDA:10,0\nBRDA:4,0,0,3\nBRDA:4,0,1,-\nBRF:2\nBRH:1\nLF:4\nLH:2\nend_of_record\n\ SF:/src/crates/a/src/lib.rs\nDA:9,1\nend_of_record\n\ SF:./README.md\nDA:1,1\nend_of_record\n\ SF:/home/runner/.cargo/registry/src/dep/lib.rs\nDA:1,1\nend_of_record\n"; #[test] fn lcov_reports_are_read_and_merged() { let (files, outside) = parse_lcov(LCOV).unwrap(); assert_eq!(outside, 1, "the dependency outside the workspace"); assert_eq!(files.len(), 2); let lib = &files[1]; assert_eq!(lib.path, "crates/a/src/lib.rs"); assert_eq!(lib.lines, [(3, 4), (4, 4), (9, 1), (10, 0)].into_iter().collect()); assert_eq!((lib.lines_found(), lib.lines_hit()), (4, 3), "the second record ran line 9"); assert_eq!((lib.functions_found, lib.functions_hit), (2, 1)); assert_eq!((lib.branches_found, lib.branches_hit), (2, 1)); assert_eq!(files[0].path, "README.md"); assert_eq!(lib.declared_lines, None, "two records: counted from the lines"); // One record whose totals name lines its DA lines leave out, as llvm-cov writes. let (one, _) = parse_lcov("SF:/src/a.rs\nDA:1,1\nDA:2,0\nLF:5\nLH:3\nend_of_record\n").unwrap(); assert_eq!((one[0].lines_found(), one[0].lines_hit()), (5, 3)); assert!(parse_lcov("not a report\n").unwrap_err().contains("not an LCOV report")); } #[test] fn paths_are_taken_relative_to_the_workspace() { assert_eq!(relative("/src/a/b.rs").as_deref(), Some("a/b.rs")); assert_eq!(relative("./a/b.rs").as_deref(), Some("a/b.rs")); assert_eq!(relative("a/b.rs").as_deref(), Some("a/b.rs")); for bad in ["/etc/passwd", "a/../b", "", "/src/", "a//b", "C:\\x\\y.rs"] { assert_eq!(relative(bad), None, "{bad}"); } assert_eq!(percent(1, 3), Some(33.3)); assert_eq!(percent(0, 0), None); } }