//! 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) or Cobertura XML 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) } /// Every run of every line, added up. pub fn hits(&self) -> u64 { self.lines.values().fold(0u64, |a, n| a.saturating_add(*n)) } } /// 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())) } /// Parse a report in either format the hub reads: Cobertura XML (it /// starts with `<`) or LCOV. Returns the files, those outside the /// repository, and the format's name. pub fn parse(text: &str) -> Result<(Vec, usize, &'static str), String> { if text.trim_start().starts_with('<') { parse_cobertura(text).map(|(f, o)| (f, o, "cobertura")) } else { parse_lcov(text).map(|(f, o)| (f, o, "lcov")) } } fn xml_attr(e: &quick_xml::events::BytesStart, name: &str) -> String { e.try_get_attribute(name) .ok() .flatten() .and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) .unwrap_or_default() } /// Parse a Cobertura XML report (coverage.py `coverage xml`, JaCoCo via /// cover2cover, .NET Coverlet, `cargo llvm-cov --cobertura`): each /// ``'s `` by file, with `branch` lines' /// `condition-coverage="50% (1/2)"` as branches and ``s as /// functions (one is hit when any of its lines ran). A file named by /// several classes is added together. A `` is tried as the base of /// a relative filename, the workspace's `/src` among them. pub fn parse_cobertura(text: &str) -> Result<(Vec, usize), String> { use quick_xml::events::Event; let mut r = quick_xml::Reader::from_str(text); let mut sources: Vec = Vec::new(); let mut in_source = false; let mut source_text = String::new(); // Files as the classes name them, resolved once the sources are known. let mut raw: BTreeMap = BTreeMap::new(); let mut file: Option = None; let mut method: Option<(String, bool)> = None; let mut seen_root = false; loop { let ev = r.read_event().map_err(|e| format!("this is not a Cobertura report: {e}"))?; match ev { Event::Start(ref e) | Event::Empty(ref e) => { let tag = e.local_name().as_ref().to_string(); match tag.as_str() { "coverage" => seen_root = true, "source" if matches!(ev, Event::Start(_)) => { in_source = true; source_text.clear(); } "class" => { let f = xml_attr(e, "filename"); raw.entry(f.clone()).or_default().records += 1; file = Some(f); } "method" => method = Some((xml_attr(e, "name") + &xml_attr(e, "signature"), false)), "line" => { let Some(f) = &file else { continue }; let Ok(n) = xml_attr(e, "number").trim().parse::() else { continue }; let hits = xml_attr(e, "hits").trim().parse::().unwrap_or(0); if let Some((_, hit)) = &mut method { *hit |= hits > 0; // A method's lines repeat the class's; count them once. continue; } let acc = raw.entry(f.clone()).or_default(); *acc.lines.entry(n).or_default() += hits; if xml_attr(e, "branch") == "true" { // "50% (1/2)": covered of total conditions. let cc = xml_attr(e, "condition-coverage"); if let Some((hit, total)) = cc .split_once('(') .and_then(|(_, rest)| rest.trim_end_matches(')').split_once('/')) .and_then(|(h, t)| Some((h.trim().parse::().ok()?, t.trim().parse::().ok()?))) { for i in 0..total { let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default(); *b = (*b).max(u64::from(i < hit)); } } } } _ => {} } if matches!(ev, Event::Empty(_)) && tag == "method" { if let (Some(f), Some((name, hit))) = (&file, method.take()) { *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); } } } Event::Text(t) if in_source => source_text.push_str(&t.xml10_content()), Event::End(e) => match e.local_name().as_ref() { "source" => { in_source = false; sources.push(source_text.trim().trim_end_matches('/').to_string()); } "class" => file = None, "method" => { if let (Some(f), Some((name, hit))) = (&file, method.take()) { *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); } } _ => {} }, Event::Eof => break, _ => {} } } if !seen_root { return Err("this is not a Cobertura report: no element".into()); } if raw.is_empty() { return Err("this Cobertura report names no source file (no )".into()); } // A filename relative to one of the sources, or to the workspace. let resolve = |f: &str| -> Option { if f.starts_with('/') { return relative(f); } sources.iter().filter(|s| !s.is_empty() && *s != ".").find_map(|s| relative(&format!("{s}/{f}"))).or_else(|| relative(f)) }; let mut files: BTreeMap = BTreeMap::new(); let mut outside = HashSet::new(); for (f, a) in raw { let Some(path) = resolve(&f) else { outside.insert(f); continue; }; let into = files.entry(path).or_default(); for (l, n) in a.lines { *into.lines.entry(l).or_default() += n; } for (k, n) in a.functions { *into.functions.entry(k).or_default() += n; } for (k, n) in a.branches { let b = into.branches.entry(k).or_default(); *b = (*b).max(n); } } let out = files .into_iter() .map(|(path, a)| FileCoverage { path, functions_found: a.functions.len() as u64, functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64, branches_found: a.branches.len() as u64, branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64, lines: a.lines, declared_lines: None, }) .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, /// The Flow run and job it came from; none for an upload (#136). pub run_id: Option, pub job_id: Option, 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 hits: i64, pub files: i64, pub dropped: i64, pub created_ms: i64, /// Who uploaded it from a workstation (#136); empty for a job's report. pub uploader: String, } impl Report { /// Whether it was uploaded rather than measured by a Flow job. pub fn uploaded(&self) -> bool { !self.uploader.is_empty() } /// Where its figure came from: "run #61", or "uploaded by NAME". pub fn source(&self) -> String { if self.uploaded() { format!("uploaded by {}", self.uploader) } else { format!("run #{}", self.run_number) } } /// How often an instrumented line ran, on average. pub fn hits_per_line(&self) -> Option { (self.lines_found > 0).then(|| self.hits as f64 / self.lines_found as f64) } 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, /// The Flow run and job; none for an upload, which names its uploader. pub run_id: Option, pub run_number: i64, pub job_id: Option, pub uploader: &'a str, pub job: &'a str, pub bookmark: &'a str, pub state: &'a str, pub heads: &'a [String], pub blob: &'a str, /// `lcov` or `cobertura`. pub format: &'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?; if let Some(job) = o.job_id { sqlx::query("DELETE FROM coverage_reports WHERE job_id = $1").bind(job).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, hits, files, dropped, created_ms, uploader) \ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) 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(o.format) .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.iter().fold(0u64, |a, f| a.saturating_add(f.hits())).min(i64::MAX as u64) as i64) .bind(kept.len() as i64) .bind((missing.len() + outside) as i64) .bind(now_ms()) .bind(o.uploader) .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 ($1, $2, $3, $4, $5, $6, $7, $8)", ) .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 = $1 AND repo_id = $2").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 = $1 AND bookmark = $2 ORDER BY created_ms DESC, id DESC LIMIT $3") .bind(repo_id) .bind(bookmark) .bind(limit) .fetch_all(pool) .await } /// When the repository's first report on any bookmark was kept. pub async fn first_ms(pool: &Pool, repo_id: i64) -> sqlx::Result> { sqlx::query_scalar("SELECT MIN(created_ms) FROM coverage_reports WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await } /// The bookmarks with reports, the most recent first. pub async fn bookmarks(pool: &Pool, repo_id: i64) -> sqlx::Result> { sqlx::query_scalar("SELECT bookmark FROM coverage_reports WHERE repo_id = $1 GROUP BY bookmark ORDER BY MAX(created_ms) DESC") .bind(repo_id) .fetch_all(pool) .await } /// A repository's coverage rules. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Rules { /// The check fails below this percentage. pub min: Option, /// The check fails when coverage falls more than this many points. pub max_drop: Option, } pub async fn rules(pool: &Pool, repo_id: i64) -> sqlx::Result { let row: Option<(Option, Option)> = sqlx::query_as("SELECT min, max_drop FROM coverage_settings WHERE repo_id = $1").bind(repo_id).fetch_optional(pool).await?; Ok(row.map(|(min, max_drop)| Rules { min, max_drop }).unwrap_or_default()) } pub async fn set_rules(pool: &Pool, repo_id: i64, r: Rules) -> sqlx::Result<()> { sqlx::query( "INSERT INTO coverage_settings (repo_id, min, max_drop) VALUES ($1, $2, $3) \ ON CONFLICT (repo_id) DO UPDATE SET min = excluded.min, max_drop = excluded.max_drop", ) .bind(repo_id) .bind(r.min) .bind(r.max_drop) .execute(pool) .await?; Ok(()) } /// What a report is measured against: on the default bookmark, the report /// before it there; on any other, the default bookmark's latest before it. pub async fn baseline(pool: &Pool, r: &Report, default_bookmark: &str) -> sqlx::Result> { sqlx::query_as( "SELECT * FROM coverage_reports WHERE repo_id = $1 AND bookmark = $2 AND id <> $3 AND (created_ms < $4 OR (created_ms = $5 AND id < $6)) \ ORDER BY created_ms DESC, id DESC LIMIT 1", ) .bind(r.repo_id) .bind(default_bookmark) .bind(r.id) .bind(r.created_ms) .bind(r.created_ms) .bind(r.id) .fetch_optional(pool) .await } /// The `coverage` check for a report: passed or failed, and what to say. pub fn verdict(r: &Report, base: Option<&Report>, base_name: &str, rules: Rules) -> (&'static str, String) { let Some(p) = r.percent() else { return ("passed", "no instrumented lines".into()) }; let delta = base.and_then(|b| Some(p - b.percent()?)); let against = match delta { Some(d) if d.abs() < 0.05 => format!(", unchanged against {base_name}"), Some(d) => format!(" ({d:+.1} against {base_name})"), None => String::new(), }; if let Some(min) = rules.min.filter(|m| p < *m) { return ("failed", format!("{p:.1}%{against}: below the minimum of {min}%")); } if let (Some(max), Some(d)) = (rules.max_drop, delta) { if d < -max { return ("failed", format!("{p:.1}%{against}: falls more than the {max} points allowed")); } } ("passed", format!("{p:.1}%{against}")) } pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = $1 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 = $1 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 the_check_follows_the_rules() { let report = |pct_hit: i64| Report { id: 2, repo_id: 1, run_id: Some(1), job_id: Some(1), job: "test".into(), run_number: 1, bookmark: "feature".into(), state: String::new(), heads: String::new(), blob: String::new(), format: "lcov".into(), lines_found: 1000, lines_hit: pct_hit, functions_found: 0, functions_hit: 0, branches_found: 0, branches_hit: 0, hits: 0, files: 1, dropped: 0, created_ms: 0, uploader: String::new(), }; let (now, before) = (report(819), report(822)); let none = Rules::default(); assert_eq!(verdict(&now, Some(&before), "main", none), ("passed", "81.9% (-0.3 against main)".to_string())); assert_eq!(verdict(&now, None, "main", none), ("passed", "81.9%".to_string())); assert_eq!(verdict(&now, Some(&now), "main", none).1, "81.9%, unchanged against main"); let strict = Rules { min: Some(85.0), max_drop: None }; assert_eq!( verdict(&now, Some(&before), "main", strict), ("failed", "81.9% (-0.3 against main): below the minimum of 85%".to_string()) ); let drop = Rules { min: None, max_drop: Some(0.2) }; assert_eq!(verdict(&now, Some(&before), "main", drop).0, "failed"); assert_eq!(verdict(&now, Some(&before), "main", Rules { min: None, max_drop: Some(0.5) }).0, "passed"); } #[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); } #[test] fn cobertura_from_coverage_py_and_coverlet() { // coverage.py: a source to resolve against, branches by condition. let py = r#" /src/app "#; let (files, outside, format) = parse(py).unwrap(); assert_eq!((format, outside), ("cobertura", 1)); let f = &files[0]; assert_eq!(f.path, "app/main.py"); assert_eq!((f.lines_found(), f.lines_hit(), f.hits()), (3, 2, 5)); assert_eq!((f.branches_found, f.branches_hit), (2, 1)); // Coverlet: methods with their own lines, two classes in one file. let net = r#"/src/ "#; let (files, _, _) = parse(net).unwrap(); let f = &files[0]; assert_eq!(f.path, "Lib/A.cs"); assert_eq!((f.lines_found(), f.lines_hit()), (3, 2)); assert_eq!((f.functions_found, f.functions_hit), (2, 1)); assert!(parse("").unwrap_err().contains("Cobertura")); assert_eq!(parse("SF:/src/a.rs\nDA:1,1\nend_of_record\n").unwrap().2, "lcov"); } }