| 1 |
1 |
|
//! Coverage reports (oxygenhub#113): Coveralls built into the hub. An |
| 2 |
2 |
|
//! OxygenFlow job that declares an artifact named `coverage` in LCOV |
| 3 |
3 |
|
//! format (what `cargo llvm-cov --lcov`, Istanbul, coverage.py and |
| 4 |
|
- |
//! gcov/lcov write) has it parsed when it is uploaded. The totals and each |
|
4 |
+ |
//! gcov/lcov write) or Cobertura XML has it parsed when it is uploaded. The totals and each |
| 5 |
5 |
|
//! file's counts are kept per run; the per-line counts are read again from |
| 6 |
6 |
|
//! the artifact's blob for the file view. Paths are taken relative to the |
| 7 |
7 |
|
//! job's workspace (`/src`), and only files of the run's state are kept. |
|
|
… |
162 unchanged lines |
| 170 |
170 |
|
Ok((out, outside.len())) |
| 171 |
171 |
|
} |
| 172 |
172 |
|
|
|
173 |
+ |
/// Parse a report in either format the hub reads: Cobertura XML (it |
|
174 |
+ |
/// starts with `<`) or LCOV. Returns the files, those outside the |
|
175 |
+ |
/// repository, and the format's name. |
|
176 |
+ |
pub fn parse(text: &str) -> Result<(Vec<FileCoverage>, usize, &'static str), String> { |
|
177 |
+ |
if text.trim_start().starts_with('<') { |
|
178 |
+ |
parse_cobertura(text).map(|(f, o)| (f, o, "cobertura")) |
|
179 |
+ |
} else { |
|
180 |
+ |
parse_lcov(text).map(|(f, o)| (f, o, "lcov")) |
|
181 |
+ |
} |
|
182 |
+ |
} |
|
183 |
+ |
|
|
184 |
+ |
fn xml_attr(e: &quick_xml::events::BytesStart, name: &str) -> String { |
|
185 |
+ |
e.try_get_attribute(name) |
|
186 |
+ |
.ok() |
|
187 |
+ |
.flatten() |
|
188 |
+ |
.and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) |
|
189 |
+ |
.unwrap_or_default() |
|
190 |
+ |
} |
|
191 |
+ |
|
|
192 |
+ |
/// Parse a Cobertura XML report (coverage.py `coverage xml`, JaCoCo via |
|
193 |
+ |
/// cover2cover, .NET Coverlet, `cargo llvm-cov --cobertura`): each |
|
194 |
+ |
/// `<class>`'s `<line number hits>` by file, with `branch` lines' |
|
195 |
+ |
/// `condition-coverage="50% (1/2)"` as branches and `<method>`s as |
|
196 |
+ |
/// functions (one is hit when any of its lines ran). A file named by |
|
197 |
+ |
/// several classes is added together. A `<source>` is tried as the base of |
|
198 |
+ |
/// a relative filename, the workspace's `/src` among them. |
|
199 |
+ |
pub fn parse_cobertura(text: &str) -> Result<(Vec<FileCoverage>, usize), String> { |
|
200 |
+ |
use quick_xml::events::Event; |
|
201 |
+ |
let mut r = quick_xml::Reader::from_str(text); |
|
202 |
+ |
let mut sources: Vec<String> = Vec::new(); |
|
203 |
+ |
let mut in_source = false; |
|
204 |
+ |
let mut source_text = String::new(); |
|
205 |
+ |
// Files as the classes name them, resolved once the sources are known. |
|
206 |
+ |
let mut raw: BTreeMap<String, Acc> = BTreeMap::new(); |
|
207 |
+ |
let mut file: Option<String> = None; |
|
208 |
+ |
let mut method: Option<(String, bool)> = None; |
|
209 |
+ |
let mut seen_root = false; |
|
210 |
+ |
loop { |
|
211 |
+ |
let ev = r.read_event().map_err(|e| format!("this is not a Cobertura report: {e}"))?; |
|
212 |
+ |
match ev { |
|
213 |
+ |
Event::Start(ref e) | Event::Empty(ref e) => { |
|
214 |
+ |
let tag = e.local_name().as_ref().to_string(); |
|
215 |
+ |
match tag.as_str() { |
|
216 |
+ |
"coverage" => seen_root = true, |
|
217 |
+ |
"source" if matches!(ev, Event::Start(_)) => { |
|
218 |
+ |
in_source = true; |
|
219 |
+ |
source_text.clear(); |
|
220 |
+ |
} |
|
221 |
+ |
"class" => { |
|
222 |
+ |
let f = xml_attr(e, "filename"); |
|
223 |
+ |
raw.entry(f.clone()).or_default().records += 1; |
|
224 |
+ |
file = Some(f); |
|
225 |
+ |
} |
|
226 |
+ |
"method" => method = Some((xml_attr(e, "name") + &xml_attr(e, "signature"), false)), |
|
227 |
+ |
"line" => { |
|
228 |
+ |
let Some(f) = &file else { continue }; |
|
229 |
+ |
let Ok(n) = xml_attr(e, "number").trim().parse::<u32>() else { continue }; |
|
230 |
+ |
let hits = xml_attr(e, "hits").trim().parse::<u64>().unwrap_or(0); |
|
231 |
+ |
if let Some((_, hit)) = &mut method { |
|
232 |
+ |
*hit |= hits > 0; |
|
233 |
+ |
// A method's lines repeat the class's; count them once. |
|
234 |
+ |
continue; |
|
235 |
+ |
} |
|
236 |
+ |
let acc = raw.entry(f.clone()).or_default(); |
|
237 |
+ |
*acc.lines.entry(n).or_default() += hits; |
|
238 |
+ |
if xml_attr(e, "branch") == "true" { |
|
239 |
+ |
// "50% (1/2)": covered of total conditions. |
|
240 |
+ |
let cc = xml_attr(e, "condition-coverage"); |
|
241 |
+ |
if let Some((hit, total)) = cc |
|
242 |
+ |
.split_once('(') |
|
243 |
+ |
.and_then(|(_, rest)| rest.trim_end_matches(')').split_once('/')) |
|
244 |
+ |
.and_then(|(h, t)| Some((h.trim().parse::<u64>().ok()?, t.trim().parse::<u64>().ok()?))) |
|
245 |
+ |
{ |
|
246 |
+ |
for i in 0..total { |
|
247 |
+ |
let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default(); |
|
248 |
+ |
*b = (*b).max(u64::from(i < hit)); |
|
249 |
+ |
} |
|
250 |
+ |
} |
|
251 |
+ |
} |
|
252 |
+ |
} |
|
253 |
+ |
_ => {} |
|
254 |
+ |
} |
|
255 |
+ |
if matches!(ev, Event::Empty(_)) && tag == "method" { |
|
256 |
+ |
if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
|
257 |
+ |
*raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
|
258 |
+ |
} |
|
259 |
+ |
} |
|
260 |
+ |
} |
|
261 |
+ |
Event::Text(t) if in_source => source_text.push_str(&t.xml10_content()), |
|
262 |
+ |
Event::End(e) => match e.local_name().as_ref() { |
|
263 |
+ |
"source" => { |
|
264 |
+ |
in_source = false; |
|
265 |
+ |
sources.push(source_text.trim().trim_end_matches('/').to_string()); |
|
266 |
+ |
} |
|
267 |
+ |
"class" => file = None, |
|
268 |
+ |
"method" => { |
|
269 |
+ |
if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
|
270 |
+ |
*raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
|
271 |
+ |
} |
|
272 |
+ |
} |
|
273 |
+ |
_ => {} |
|
274 |
+ |
}, |
|
275 |
+ |
Event::Eof => break, |
|
276 |
+ |
_ => {} |
|
277 |
+ |
} |
|
278 |
+ |
} |
|
279 |
+ |
if !seen_root { |
|
280 |
+ |
return Err("this is not a Cobertura report: no <coverage> element".into()); |
|
281 |
+ |
} |
|
282 |
+ |
if raw.is_empty() { |
|
283 |
+ |
return Err("this Cobertura report names no source file (no <class filename>)".into()); |
|
284 |
+ |
} |
|
285 |
+ |
// A filename relative to one of the sources, or to the workspace. |
|
286 |
+ |
let resolve = |f: &str| -> Option<String> { |
|
287 |
+ |
if f.starts_with('/') { |
|
288 |
+ |
return relative(f); |
|
289 |
+ |
} |
|
290 |
+ |
sources.iter().filter(|s| !s.is_empty() && *s != ".").find_map(|s| relative(&format!("{s}/{f}"))).or_else(|| relative(f)) |
|
291 |
+ |
}; |
|
292 |
+ |
let mut files: BTreeMap<String, Acc> = BTreeMap::new(); |
|
293 |
+ |
let mut outside = HashSet::new(); |
|
294 |
+ |
for (f, a) in raw { |
|
295 |
+ |
let Some(path) = resolve(&f) else { |
|
296 |
+ |
outside.insert(f); |
|
297 |
+ |
continue; |
|
298 |
+ |
}; |
|
299 |
+ |
let into = files.entry(path).or_default(); |
|
300 |
+ |
for (l, n) in a.lines { |
|
301 |
+ |
*into.lines.entry(l).or_default() += n; |
|
302 |
+ |
} |
|
303 |
+ |
for (k, n) in a.functions { |
|
304 |
+ |
*into.functions.entry(k).or_default() += n; |
|
305 |
+ |
} |
|
306 |
+ |
for (k, n) in a.branches { |
|
307 |
+ |
let b = into.branches.entry(k).or_default(); |
|
308 |
+ |
*b = (*b).max(n); |
|
309 |
+ |
} |
|
310 |
+ |
} |
|
311 |
+ |
let out = files |
|
312 |
+ |
.into_iter() |
|
313 |
+ |
.map(|(path, a)| FileCoverage { |
|
314 |
+ |
path, |
|
315 |
+ |
functions_found: a.functions.len() as u64, |
|
316 |
+ |
functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64, |
|
317 |
+ |
branches_found: a.branches.len() as u64, |
|
318 |
+ |
branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64, |
|
319 |
+ |
lines: a.lines, |
|
320 |
+ |
declared_lines: None, |
|
321 |
+ |
}) |
|
322 |
+ |
.collect(); |
|
323 |
+ |
Ok((out, outside.len())) |
|
324 |
+ |
} |
|
325 |
+ |
|
| 173 |
326 |
|
/// A percentage to one decimal place, or none when nothing was counted. |
| 174 |
327 |
|
pub fn percent(hit: i64, found: i64) -> Option<f64> { |
| 175 |
328 |
|
(found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0) |
|
|
… |
106 unchanged lines |
| 282 |
435 |
|
pub state: &'a str, |
| 283 |
436 |
|
pub heads: &'a [String], |
| 284 |
437 |
|
pub blob: &'a str, |
|
438 |
+ |
/// `lcov` or `cobertura`. |
|
439 |
+ |
pub format: &'a str, |
| 285 |
440 |
|
} |
| 286 |
441 |
|
|
| 287 |
442 |
|
/// Keep a parsed report: the files outside `in_state` are dropped and |
|
|
… |
6 unchanged lines |
| 294 |
449 |
|
let id: i64 = sqlx::query_scalar( |
| 295 |
450 |
|
"INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \ |
| 296 |
451 |
|
lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, hits, files, dropped, created_ms) \ |
| 297 |
|
- |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'lcov', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id", |
|
452 |
+ |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id", |
| 298 |
453 |
|
) |
| 299 |
454 |
|
.bind(o.repo_id) |
| 300 |
455 |
|
.bind(o.run_id) |
|
|
… |
4 unchanged lines |
| 305 |
460 |
|
.bind(o.state) |
| 306 |
461 |
|
.bind(o.heads.join(",")) |
| 307 |
462 |
|
.bind(o.blob) |
|
463 |
+ |
.bind(o.format) |
| 308 |
464 |
|
.bind(sum(&|f| f.lines_found())) |
| 309 |
465 |
|
.bind(sum(&|f| f.lines_hit())) |
| 310 |
466 |
|
.bind(sum(&|f| f.functions_found)) |
|
|
… |
206 unchanged lines |
| 517 |
673 |
|
} |
| 518 |
674 |
|
assert_eq!(percent(1, 3), Some(33.3)); |
| 519 |
675 |
|
assert_eq!(percent(0, 0), None); |
|
676 |
+ |
} |
|
677 |
+ |
|
|
678 |
+ |
#[test] |
|
679 |
+ |
fn cobertura_from_coverage_py_and_coverlet() { |
|
680 |
+ |
// coverage.py: a source to resolve against, branches by condition. |
|
681 |
+ |
let py = r#"<?xml version="1.0" ?> |
|
682 |
+ |
<coverage version="7.4" line-rate="0.6" branch-rate="0.5"> |
|
683 |
+ |
<sources><source>/src/app</source></sources> |
|
684 |
+ |
<packages><package name="app"><classes> |
|
685 |
+ |
<class name="main.py" filename="main.py" line-rate="0.66"> |
|
686 |
+ |
<methods/> |
|
687 |
+ |
<lines> |
|
688 |
+ |
<line number="1" hits="1"/> |
|
689 |
+ |
<line number="2" hits="0"/> |
|
690 |
+ |
<line number="3" hits="4" branch="true" condition-coverage="50% (1/2)"/> |
|
691 |
+ |
</lines> |
|
692 |
+ |
</class> |
|
693 |
+ |
<class name="gone.py" filename="/usr/lib/python3/x.py"><lines><line number="1" hits="1"/></lines></class> |
|
694 |
+ |
</classes></package></packages> |
|
695 |
+ |
</coverage>"#; |
|
696 |
+ |
let (files, outside, format) = parse(py).unwrap(); |
|
697 |
+ |
assert_eq!((format, outside), ("cobertura", 1)); |
|
698 |
+ |
let f = &files[0]; |
|
699 |
+ |
assert_eq!(f.path, "app/main.py"); |
|
700 |
+ |
assert_eq!((f.lines_found(), f.lines_hit(), f.hits()), (3, 2, 5)); |
|
701 |
+ |
assert_eq!((f.branches_found, f.branches_hit), (2, 1)); |
|
702 |
+ |
|
|
703 |
+ |
// Coverlet: methods with their own lines, two classes in one file. |
|
704 |
+ |
let net = r#"<coverage><sources><source>/src/</source></sources><packages><package name="Lib"><classes> |
|
705 |
+ |
<class name="Lib.A" filename="Lib/A.cs"><methods><method name="Run" signature="()"><lines><line number="5" hits="2"/></lines></method><method name="Idle" signature="()"><lines><line number="9" hits="0"/></lines></method></methods> |
|
706 |
+ |
<lines><line number="5" hits="2"/><line number="9" hits="0"/></lines></class> |
|
707 |
+ |
<class name="Lib.A/Inner" filename="Lib/A.cs"><methods/><lines><line number="12" hits="1"/></lines></class> |
|
708 |
+ |
</classes></package></packages></coverage>"#; |
|
709 |
+ |
let (files, _, _) = parse(net).unwrap(); |
|
710 |
+ |
let f = &files[0]; |
|
711 |
+ |
assert_eq!(f.path, "Lib/A.cs"); |
|
712 |
+ |
assert_eq!((f.lines_found(), f.lines_hit()), (3, 2)); |
|
713 |
+ |
assert_eq!((f.functions_found, f.functions_hit), (2, 1)); |
|
714 |
+ |
|
|
715 |
+ |
assert!(parse("<html/>").unwrap_err().contains("Cobertura")); |
|
716 |
+ |
assert_eq!(parse("SF:/src/a.rs\nDA:1,1\nend_of_record\n").unwrap().2, "lcov"); |
| 520 |
717 |
|
} |
| 521 |
718 |
|
} |