OXYGENHUB
# ac689cd21adf oxygenhub / crates / hub / src / coverage.rs
38.2 KiB · Rust · last changed JaCoCo coverage is read (closes #308) 10 days ago source blame history raw
1
//! Coverage reports (oxygenhub#113): Coveralls built into the hub. An
2
//! OxygenFlow job that declares an artifact named `coverage` in LCOV
3
//! format (what `cargo llvm-cov --lcov`, Istanbul, coverage.py and
4
//! gcov/lcov write) or Cobertura XML has it parsed when it is uploaded. The totals and each
5
//! file's counts are kept per run; the per-line counts are read again from
6
//! the artifact's blob for the file view. Paths are taken relative to the
7
//! job's workspace (`/src`), and only files of the run's state are kept.
8
9
use crate::db::{now_ms, Pool};
10
use crate::Shared;
11
use std::collections::{BTreeMap, HashSet};
12
use std::sync::Mutex;
13
14
/// The artifact name a pipeline gives its report.
15
pub const ARTIFACT: &str = "coverage";
16
/// The largest report parsed.
17
pub const MAX_REPORT: usize = 64 << 20;
18
19
/// One file's coverage, from a report.
20
#[derive(Clone, Debug, Default, PartialEq, Eq)]
21
pub struct FileCoverage {
22
    /// Relative to the repository's root.
23
    pub path: String,
24
    /// Each instrumented line and how often it ran, by line number.
25
    pub lines: BTreeMap<u32, u64>,
26
    pub functions_found: u64,
27
    pub functions_hit: u64,
28
    pub branches_found: u64,
29
    pub branches_hit: u64,
30
    /// The file's own `LF`/`LH` totals, when it had one record: the tool's
31
    /// own count, which can name lines the `DA` lines leave out (llvm-cov
32
    /// does), so the hub agrees with what the tool printed.
33
    pub declared_lines: Option<(u64, u64)>,
34
}
35
36
impl FileCoverage {
37
    pub fn lines_found(&self) -> u64 {
38
        self.declared_lines.map(|d| d.0).unwrap_or(self.lines.len() as u64)
39
    }
40
41
    pub fn lines_hit(&self) -> u64 {
42
        self.declared_lines.map(|d| d.1).unwrap_or_else(|| self.lines.values().filter(|n| **n > 0).count() as u64)
43
    }
44
45
    /// Every run of every line, added up.
46
    pub fn hits(&self) -> u64 {
47
        self.lines.values().fold(0u64, |a, n| a.saturating_add(*n))
48
    }
49
}
50
51
/// A path as a report names it, relative to the repository: the Flow
52
/// workspace's `/src/` and a leading `./` taken off. `None` for a path
53
/// outside the repository.
54
pub fn relative(path: &str) -> Option<String> {
55
    let p = path.trim();
56
    let p = p.strip_prefix("/src/").unwrap_or(p);
57
    let p = p.strip_prefix("./").unwrap_or(p);
58
    if p.is_empty() || p.starts_with('/') || p.contains('\\') || p.split('/').any(|s| s.is_empty() || s == "." || s == "..") {
59
        return None;
60
    }
61
    Some(p.to_string())
62
}
63
64
#[derive(Default)]
65
struct Acc {
66
    lines: BTreeMap<u32, u64>,
67
    functions: BTreeMap<String, u64>,
68
    branches: BTreeMap<(u32, String, String), u64>,
69
    declared_fn: (u64, u64),
70
    declared_br: (u64, u64),
71
    declared_lines: (Option<u64>, Option<u64>),
72
    records: u32,
73
}
74
75
/// Parse an LCOV report into each file's coverage, records for the same
76
/// file (from several test binaries) added together. Files whose paths
77
/// are outside the repository are counted in the second value.
78
pub fn parse_lcov(text: &str) -> Result<(Vec<FileCoverage>, usize), String> {
79
    let mut files: BTreeMap<String, Acc> = BTreeMap::new();
80
    let mut current: Option<Option<String>> = None;
81
    let mut outside = HashSet::new();
82
    let mut records = 0usize;
83
    // A new SF: record has begun and not yet been counted for its file.
84
    let mut started = false;
85
    let num = |s: &str| s.trim().parse::<u64>().unwrap_or(0);
86
    for line in text.lines() {
87
        let line = line.trim();
88
        if let Some(path) = line.strip_prefix("SF:") {
89
            records += 1;
90
            let rel = relative(path);
91
            if rel.is_none() {
92
                outside.insert(path.to_string());
93
            }
94
            current = Some(rel);
95
            started = true;
96
            continue;
97
        }
98
        if line == "end_of_record" {
99
            current = None;
100
            continue;
101
        }
102
        let Some(Some(path)) = &current else { continue };
103
        let acc = files.entry(path.clone()).or_default();
104
        if line == "TN:" || line.starts_with("TN:") {
105
            continue;
106
        }
107
        if std::mem::take(&mut started) {
108
            acc.records += 1;
109
        }
110
        if let Some(n) = line.strip_prefix("LF:") {
111
            acc.declared_lines.0 = Some(num(n));
112
        } else if let Some(n) = line.strip_prefix("LH:") {
113
            acc.declared_lines.1 = Some(num(n));
114
        } else if let Some(rest) = line.strip_prefix("DA:") {
115
            let mut parts = rest.split(',');
116
            if let (Some(l), Some(n)) = (parts.next(), parts.next()) {
117
                if let Ok(l) = l.trim().parse::<u32>() {
118
                    *acc.lines.entry(l).or_default() += num(n);
119
                }
120
            }
121
        } else if let Some(rest) = line.strip_prefix("FN:") {
122
            if let Some((_, name)) = rest.split_once(',') {
123
                acc.functions.entry(name.to_string()).or_default();
124
            }
125
        } else if let Some(rest) = line.strip_prefix("FNDA:") {
126
            if let Some((n, name)) = rest.split_once(',') {
127
                *acc.functions.entry(name.to_string()).or_default() += num(n);
128
            }
129
        } else if let Some(rest) = line.strip_prefix("BRDA:") {
130
            let parts: Vec<&str> = rest.split(',').collect();
131
            if let [l, block, branch, taken] = parts[..] {
132
                if let Ok(l) = l.trim().parse::<u32>() {
133
                    let taken = if taken.trim() == "-" { 0 } else { num(taken) };
134
                    *acc.branches.entry((l, block.to_string(), branch.to_string())).or_default() += taken;
135
                }
136
            }
137
        } else if let Some(n) = line.strip_prefix("FNF:") {
138
            acc.declared_fn.0 = acc.declared_fn.0.max(num(n));
139
        } else if let Some(n) = line.strip_prefix("FNH:") {
140
            acc.declared_fn.1 = acc.declared_fn.1.max(num(n));
141
        } else if let Some(n) = line.strip_prefix("BRF:") {
142
            acc.declared_br.0 = acc.declared_br.0.max(num(n));
143
        } else if let Some(n) = line.strip_prefix("BRH:") {
144
            acc.declared_br.1 = acc.declared_br.1.max(num(n));
145
        }
146
    }
147
    if records == 0 {
148
        return Err("this is not an LCOV report: it names no source file (no SF: line)".into());
149
    }
150
    let out = files
151
        .into_iter()
152
        .map(|(path, a)| {
153
            let (functions_found, functions_hit) = if a.functions.is_empty() {
154
                a.declared_fn
155
            } else {
156
                (a.functions.len() as u64, a.functions.values().filter(|n| **n > 0).count() as u64)
157
            };
158
            let (branches_found, branches_hit) = if a.branches.is_empty() {
159
                a.declared_br
160
            } else {
161
                (a.branches.len() as u64, a.branches.values().filter(|n| **n > 0).count() as u64)
162
            };
163
            let declared_lines = match (a.records, a.declared_lines) {
164
                (1, (Some(f), Some(h))) if h <= f && f >= a.lines.len() as u64 => Some((f, h)),
165
                _ => None,
166
            };
167
            FileCoverage { path, lines: a.lines, functions_found, functions_hit, branches_found, branches_hit, declared_lines }
168
        })
169
        .collect();
170
    Ok((out, outside.len()))
171
}
172
173
/// Parse a report in any format the hub reads: JaCoCo XML (a `<report>`),
174
/// Cobertura XML (a `<coverage>`) or LCOV. Returns the files, those outside
175
/// the repository, and the format's name.
176
pub fn parse(text: &str) -> Result<(Vec<FileCoverage>, usize, &'static str), String> {
177
    let t = text.trim_start();
178
    if t.starts_with('<') && root_element(t).as_deref() == Some("report") {
179
        parse_jacoco(text).map(|(f, o)| (f, o, "jacoco"))
180
    } else if t.starts_with('<') {
181
        parse_cobertura(text).map(|(f, o)| (f, o, "cobertura"))
182
    } else {
183
        parse_lcov(text).map(|(f, o)| (f, o, "lcov"))
184
    }
185
}
186
187
/// The name of an XML document's first element.
188
fn root_element(text: &str) -> Option<String> {
189
    use quick_xml::events::Event;
190
    let mut r = quick_xml::Reader::from_str(text);
191
    loop {
192
        match r.read_event().ok()? {
193
            Event::Start(e) | Event::Empty(e) => return Some(e.local_name().as_ref().to_string()),
194
            Event::Eof => return None,
195
            _ => {}
196
        }
197
    }
198
}
199
200
/// Parse a JaCoCo XML report (Gradle's `jacocoTestReport`, Kotlin's Kover;
201
/// oxygenhub#308): each `<package name>`'s `<sourcefile name>` with its
202
/// `<line nr mi ci mb cb>` (hit when any of the line's instructions ran;
203
/// its branches missed and covered), and each `<class sourcefilename>`'s
204
/// `<method>`s as functions, hit when their METHOD counter says so. Paths
205
/// are the package's, relative to a source root (`io/oxygenhub/api/X.kt`);
206
/// [`keep`] finds them in the state by their ending.
207
pub fn parse_jacoco(text: &str) -> Result<(Vec<FileCoverage>, usize), String> {
208
    use quick_xml::events::Event;
209
    let mut r = quick_xml::Reader::from_str(text);
210
    let mut files: BTreeMap<String, Acc> = BTreeMap::new();
211
    let mut package = String::new();
212
    let mut sourcefile: Option<String> = None;
213
    let mut class_file: Option<String> = None;
214
    let mut method: Option<(String, bool)> = None;
215
    let mut seen_root = false;
216
    let path = |package: &str, name: &str| if package.is_empty() { name.to_string() } else { format!("{package}/{name}") };
217
    loop {
218
        let ev = r.read_event().map_err(|e| format!("this is not a JaCoCo report: {e}"))?;
219
        match ev {
220
            Event::Start(ref e) | Event::Empty(ref e) => {
221
                let tag = e.local_name().as_ref().to_string();
222
                match tag.as_str() {
223
                    "report" => seen_root = true,
224
                    "package" => package = xml_attr(e, "name").trim_matches('/').to_string(),
225
                    "class" => {
226
                        let f = xml_attr(e, "sourcefilename");
227
                        class_file = (!f.is_empty()).then(|| path(&package, &f));
228
                    }
229
                    "method" if class_file.is_some() => method = Some((xml_attr(e, "name") + &xml_attr(e, "desc"), false)),
230
                    "counter" => {
231
                        if let Some((_, hit)) = &mut method {
232
                            if xml_attr(e, "type") == "METHOD" {
233
                                *hit |= xml_attr(e, "covered").trim().parse::<u64>().unwrap_or(0) > 0;
234
                            }
235
                        }
236
                    }
237
                    "sourcefile" => {
238
                        let f = path(&package, &xml_attr(e, "name"));
239
                        files.entry(f.clone()).or_default().records += 1;
240
                        sourcefile = Some(f);
241
                    }
242
                    "line" => {
243
                        let Some(f) = &sourcefile else { continue };
244
                        let Ok(n) = xml_attr(e, "nr").trim().parse::<u32>() else { continue };
245
                        let num = |a: &str| xml_attr(e, a).trim().parse::<u64>().unwrap_or(0);
246
                        let acc = files.entry(f.clone()).or_default();
247
                        *acc.lines.entry(n).or_default() += num("ci");
248
                        let (missed, covered) = (num("mb"), num("cb"));
249
                        for i in 0..missed + covered {
250
                            let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default();
251
                            *b = (*b).max(u64::from(i < covered));
252
                        }
253
                    }
254
                    _ => {}
255
                }
256
                if matches!(ev, Event::Empty(_)) && tag == "method" {
257
                    if let (Some(f), Some((name, hit))) = (&class_file, method.take()) {
258
                        *files.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit);
259
                    }
260
                }
261
            }
262
            Event::End(e) => match e.local_name().as_ref() {
263
                "package" => package.clear(),
264
                "sourcefile" => sourcefile = None,
265
                "class" => class_file = None,
266
                "method" => {
267
                    if let (Some(f), Some((name, hit))) = (&class_file, method.take()) {
268
                        *files.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit);
269
                    }
270
                }
271
                _ => {}
272
            },
273
            Event::Eof => break,
274
            _ => {}
275
        }
276
    }
277
    if !seen_root {
278
        return Err("this is not a JaCoCo report: no <report> element".into());
279
    }
280
    let out: Vec<FileCoverage> = files
281
        .into_iter()
282
        .filter(|(_, a)| a.records > 0)
283
        .map(|(path, a)| FileCoverage {
284
            path,
285
            functions_found: a.functions.len() as u64,
286
            functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64,
287
            branches_found: a.branches.len() as u64,
288
            branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64,
289
            lines: a.lines,
290
            declared_lines: None,
291
        })
292
        .collect();
293
    if out.is_empty() {
294
        return Err("this JaCoCo report names no source file (no <sourcefile>)".into());
295
    }
296
    Ok((out, 0))
297
}
298
299
/// A report's path that is not in the state, matched by its ending to the
300
/// one file of the state it can be (a JaCoCo or JVM Cobertura path is
301
/// relative to a source root, `app/src/main/java`); left as it is when
302
/// none or several match.
303
pub fn resolve_endings(files: Vec<FileCoverage>, in_state: &HashSet<String>) -> Vec<FileCoverage> {
304
    files
305
        .into_iter()
306
        .map(|mut f| {
307
            if !in_state.contains(&f.path) {
308
                let tail = format!("/{}", f.path);
309
                let mut found = in_state.iter().filter(|p| p.ends_with(&tail));
310
                if let (Some(p), None) = (found.next(), found.next()) {
311
                    f.path = p.clone();
312
                }
313
            }
314
            f
315
        })
316
        .collect()
317
}
318
319
fn xml_attr(e: &quick_xml::events::BytesStart, name: &str) -> String {
320
    e.try_get_attribute(name)
321
        .ok()
322
        .flatten()
323
        .and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.into_owned()))
324
        .unwrap_or_default()
325
}
326
327
/// Parse a Cobertura XML report (coverage.py `coverage xml`, JaCoCo via
328
/// cover2cover, .NET Coverlet, `cargo llvm-cov --cobertura`): each
329
/// `<class>`'s `<line number hits>` by file, with `branch` lines'
330
/// `condition-coverage="50% (1/2)"` as branches and `<method>`s as
331
/// functions (one is hit when any of its lines ran). A file named by
332
/// several classes is added together. A `<source>` is tried as the base of
333
/// a relative filename, the workspace's `/src` among them.
334
pub fn parse_cobertura(text: &str) -> Result<(Vec<FileCoverage>, usize), String> {
335
    use quick_xml::events::Event;
336
    let mut r = quick_xml::Reader::from_str(text);
337
    let mut sources: Vec<String> = Vec::new();
338
    let mut in_source = false;
339
    let mut source_text = String::new();
340
    // Files as the classes name them, resolved once the sources are known.
341
    let mut raw: BTreeMap<String, Acc> = BTreeMap::new();
342
    let mut file: Option<String> = None;
343
    let mut method: Option<(String, bool)> = None;
344
    let mut seen_root = false;
345
    loop {
346
        let ev = r.read_event().map_err(|e| format!("this is not a Cobertura report: {e}"))?;
347
        match ev {
348
            Event::Start(ref e) | Event::Empty(ref e) => {
349
                let tag = e.local_name().as_ref().to_string();
350
                match tag.as_str() {
351
                    "coverage" => seen_root = true,
352
                    "source" if matches!(ev, Event::Start(_)) => {
353
                        in_source = true;
354
                        source_text.clear();
355
                    }
356
                    "class" => {
357
                        let f = xml_attr(e, "filename");
358
                        raw.entry(f.clone()).or_default().records += 1;
359
                        file = Some(f);
360
                    }
361
                    "method" => method = Some((xml_attr(e, "name") + &xml_attr(e, "signature"), false)),
362
                    "line" => {
363
                        let Some(f) = &file else { continue };
364
                        let Ok(n) = xml_attr(e, "number").trim().parse::<u32>() else { continue };
365
                        let hits = xml_attr(e, "hits").trim().parse::<u64>().unwrap_or(0);
366
                        if let Some((_, hit)) = &mut method {
367
                            *hit |= hits > 0;
368
                            // A method's lines repeat the class's; count them once.
369
                            continue;
370
                        }
371
                        let acc = raw.entry(f.clone()).or_default();
372
                        *acc.lines.entry(n).or_default() += hits;
373
                        if xml_attr(e, "branch") == "true" {
374
                            // "50% (1/2)": covered of total conditions.
375
                            let cc = xml_attr(e, "condition-coverage");
376
                            if let Some((hit, total)) = cc
377
                                .split_once('(')
378
                                .and_then(|(_, rest)| rest.trim_end_matches(')').split_once('/'))
379
                                .and_then(|(h, t)| Some((h.trim().parse::<u64>().ok()?, t.trim().parse::<u64>().ok()?)))
380
                            {
381
                                for i in 0..total {
382
                                    let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default();
383
                                    *b = (*b).max(u64::from(i < hit));
384
                                }
385
                            }
386
                        }
387
                    }
388
                    _ => {}
389
                }
390
                if matches!(ev, Event::Empty(_)) && tag == "method" {
391
                    if let (Some(f), Some((name, hit))) = (&file, method.take()) {
392
                        *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit);
393
                    }
394
                }
395
            }
396
            Event::Text(t) if in_source => source_text.push_str(&t.xml10_content()),
397
            Event::End(e) => match e.local_name().as_ref() {
398
                "source" => {
399
                    in_source = false;
400
                    sources.push(source_text.trim().trim_end_matches('/').to_string());
401
                }
402
                "class" => file = None,
403
                "method" => {
404
                    if let (Some(f), Some((name, hit))) = (&file, method.take()) {
405
                        *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit);
406
                    }
407
                }
408
                _ => {}
409
            },
410
            Event::Eof => break,
411
            _ => {}
412
        }
413
    }
414
    if !seen_root {
415
        return Err("this is not a Cobertura report: no <coverage> element".into());
416
    }
417
    if raw.is_empty() {
418
        return Err("this Cobertura report names no source file (no <class filename>)".into());
419
    }
420
    // A filename relative to one of the sources, or to the workspace.
421
    let resolve = |f: &str| -> Option<String> {
422
        if f.starts_with('/') {
423
            return relative(f);
424
        }
425
        sources.iter().filter(|s| !s.is_empty() && *s != ".").find_map(|s| relative(&format!("{s}/{f}"))).or_else(|| relative(f))
426
    };
427
    let mut files: BTreeMap<String, Acc> = BTreeMap::new();
428
    let mut outside = HashSet::new();
429
    for (f, a) in raw {
430
        let Some(path) = resolve(&f) else {
431
            outside.insert(f);
432
            continue;
433
        };
434
        let into = files.entry(path).or_default();
435
        for (l, n) in a.lines {
436
            *into.lines.entry(l).or_default() += n;
437
        }
438
        for (k, n) in a.functions {
439
            *into.functions.entry(k).or_default() += n;
440
        }
441
        for (k, n) in a.branches {
442
            let b = into.branches.entry(k).or_default();
443
            *b = (*b).max(n);
444
        }
445
    }
446
    let out = files
447
        .into_iter()
448
        .map(|(path, a)| FileCoverage {
449
            path,
450
            functions_found: a.functions.len() as u64,
451
            functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64,
452
            branches_found: a.branches.len() as u64,
453
            branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64,
454
            lines: a.lines,
455
            declared_lines: None,
456
        })
457
        .collect();
458
    Ok((out, outside.len()))
459
}
460
461
/// A percentage to one decimal place, or none when nothing was counted.
462
pub fn percent(hit: i64, found: i64) -> Option<f64> {
463
    (found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0)
464
}
465
466
#[derive(Clone, Debug, sqlx::FromRow)]
467
pub struct Report {
468
    pub id: i64,
469
    pub repo_id: i64,
470
    /// The Flow run and job it came from; none for an upload (#136).
471
    pub run_id: Option<i64>,
472
    pub job_id: Option<i64>,
473
    pub job: String,
474
    pub run_number: i64,
475
    pub bookmark: String,
476
    pub state: String,
477
    pub heads: String,
478
    pub blob: String,
479
    pub format: String,
480
    pub lines_found: i64,
481
    pub lines_hit: i64,
482
    pub functions_found: i64,
483
    pub functions_hit: i64,
484
    pub branches_found: i64,
485
    pub branches_hit: i64,
486
    pub hits: i64,
487
    pub files: i64,
488
    pub dropped: i64,
489
    pub created_ms: i64,
490
    /// Who uploaded it from a workstation (#136); empty for a job's report.
491
    pub uploader: String,
492
}
493
494
impl Report {
495
    /// Whether it was uploaded rather than measured by a Flow job.
496
    pub fn uploaded(&self) -> bool {
497
        !self.uploader.is_empty()
498
    }
499
500
    /// Where its figure came from: "run #61", or "uploaded by NAME".
501
    pub fn source(&self) -> String {
502
        if self.uploaded() {
503
            format!("uploaded by {}", self.uploader)
504
        } else {
505
            format!("run #{}", self.run_number)
506
        }
507
    }
508
509
    /// How often an instrumented line ran, on average.
510
    pub fn hits_per_line(&self) -> Option<f64> {
511
        (self.lines_found > 0).then(|| self.hits as f64 / self.lines_found as f64)
512
    }
513
514
    pub fn percent(&self) -> Option<f64> {
515
        percent(self.lines_hit, self.lines_found)
516
    }
517
518
    pub fn functions_percent(&self) -> Option<f64> {
519
        percent(self.functions_hit, self.functions_found)
520
    }
521
522
    pub fn branches_percent(&self) -> Option<f64> {
523
        percent(self.branches_hit, self.branches_found)
524
    }
525
526
    pub fn head_list(&self) -> Vec<&str> {
527
        self.heads.split(',').filter(|h| !h.is_empty()).collect()
528
    }
529
}
530
531
#[derive(Clone, Debug, sqlx::FromRow)]
532
pub struct FileRow {
533
    pub report_id: i64,
534
    pub path: String,
535
    pub lines_found: i64,
536
    pub lines_hit: i64,
537
    pub functions_found: i64,
538
    pub functions_hit: i64,
539
    pub branches_found: i64,
540
    pub branches_hit: i64,
541
}
542
543
impl FileRow {
544
    pub fn percent(&self) -> Option<f64> {
545
        percent(self.lines_hit, self.lines_found)
546
    }
547
548
    pub fn missed(&self) -> i64 {
549
        self.lines_found - self.lines_hit
550
    }
551
}
552
553
/// Repositories with a report, so their pages show the Coverage tab:
554
/// keyed by the hub's repository root and the id, unique even between
555
/// hubs in one process.
556
static HAS: Mutex<Option<HashSet<String>>> = Mutex::new(None);
557
558
fn key(app: &Shared, repo_id: i64) -> String {
559
    format!("{}#{repo_id}", app.repos.root().display())
560
}
561
562
pub fn has_reports(app: &Shared, repo_id: i64) -> bool {
563
    HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id)))
564
}
565
566
fn mark(app: &Shared, repo_id: i64) {
567
    HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(HashSet::new).insert(key(app, repo_id));
568
}
569
570
/// At start: which repositories have reports.
571
pub async fn load(app: &Shared) -> sqlx::Result<()> {
572
    let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM coverage_reports").fetch_all(&app.db).await?;
573
    for (id,) in ids {
574
        mark(app, id);
575
    }
576
    Ok(())
577
}
578
579
/// What a run's report is kept with.
580
pub struct Origin<'a> {
581
    pub repo_id: i64,
582
    /// The Flow run and job; none for an upload, which names its uploader.
583
    pub run_id: Option<i64>,
584
    pub run_number: i64,
585
    pub job_id: Option<i64>,
586
    pub uploader: &'a str,
587
    pub job: &'a str,
588
    pub bookmark: &'a str,
589
    pub state: &'a str,
590
    pub heads: &'a [String],
591
    pub blob: &'a str,
592
    /// `lcov` or `cobertura`.
593
    pub format: &'a str,
594
}
595
596
/// Keep a parsed report: the files outside `in_state` are dropped and
597
/// counted. Replaces an earlier report of the same job.
598
pub async fn keep(app: &Shared, o: &Origin<'_>, files: Vec<FileCoverage>, outside: usize, in_state: &HashSet<String>) -> sqlx::Result<i64> {
599
    let files = resolve_endings(files, in_state);
600
    let (kept, missing): (Vec<FileCoverage>, Vec<FileCoverage>) = files.into_iter().partition(|f| in_state.contains(&f.path));
601
    let sum = |f: &dyn Fn(&FileCoverage) -> u64| kept.iter().map(f).sum::<u64>() as i64;
602
    let mut tx = crate::db::begin_write(&app.db).await?;
603
    if let Some(job) = o.job_id {
604
        sqlx::query("DELETE FROM coverage_reports WHERE job_id = $1").bind(job).execute(&mut *tx).await?;
605
    }
606
    let id: i64 = sqlx::query_scalar(
607
        "INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \
608
         lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, hits, files, dropped, created_ms, uploader) \
609
         VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) RETURNING id",
610
    )
611
    .bind(o.repo_id)
612
    .bind(o.run_id)
613
    .bind(o.job_id)
614
    .bind(o.job)
615
    .bind(o.run_number)
616
    .bind(o.bookmark)
617
    .bind(o.state)
618
    .bind(o.heads.join(","))
619
    .bind(o.blob)
620
    .bind(o.format)
621
    .bind(sum(&|f| f.lines_found()))
622
    .bind(sum(&|f| f.lines_hit()))
623
    .bind(sum(&|f| f.functions_found))
624
    .bind(sum(&|f| f.functions_hit))
625
    .bind(sum(&|f| f.branches_found))
626
    .bind(sum(&|f| f.branches_hit))
627
    .bind(kept.iter().fold(0u64, |a, f| a.saturating_add(f.hits())).min(i64::MAX as u64) as i64)
628
    .bind(kept.len() as i64)
629
    .bind((missing.len() + outside) as i64)
630
    .bind(now_ms())
631
    .bind(o.uploader)
632
    .fetch_one(&mut *tx)
633
    .await?;
634
    for f in &kept {
635
        sqlx::query(
636
            "INSERT INTO coverage_files (report_id, path, lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit) \
637
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
638
        )
639
        .bind(id)
640
        .bind(&f.path)
641
        .bind(f.lines_found() as i64)
642
        .bind(f.lines_hit() as i64)
643
        .bind(f.functions_found as i64)
644
        .bind(f.functions_hit as i64)
645
        .bind(f.branches_found as i64)
646
        .bind(f.branches_hit as i64)
647
        .execute(&mut *tx)
648
        .await?;
649
    }
650
    tx.commit().await?;
651
    mark(app, o.repo_id);
652
    Ok(id)
653
}
654
655
pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Report>> {
656
    sqlx::query_as("SELECT * FROM coverage_reports WHERE id = $1 AND repo_id = $2").bind(id).bind(repo_id).fetch_optional(pool).await
657
}
658
659
/// The newest reports on a bookmark, newest first.
660
pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result<Vec<Report>> {
661
    sqlx::query_as("SELECT * FROM coverage_reports WHERE repo_id = $1 AND bookmark = $2 ORDER BY created_ms DESC, id DESC LIMIT $3")
662
        .bind(repo_id)
663
        .bind(bookmark)
664
        .bind(limit)
665
        .fetch_all(pool)
666
        .await
667
}
668
669
/// When the repository's first report on any bookmark was kept.
670
pub async fn first_ms(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<i64>> {
671
    sqlx::query_scalar("SELECT MIN(created_ms) FROM coverage_reports WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await
672
}
673
674
/// The bookmarks with reports, the most recent first.
675
pub async fn bookmarks(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> {
676
    sqlx::query_scalar("SELECT bookmark FROM coverage_reports WHERE repo_id = $1 GROUP BY bookmark ORDER BY MAX(created_ms) DESC")
677
        .bind(repo_id)
678
        .fetch_all(pool)
679
        .await
680
}
681
682
/// A repository's coverage rules.
683
#[derive(Clone, Copy, Debug, Default, PartialEq)]
684
pub struct Rules {
685
    /// The check fails below this percentage.
686
    pub min: Option<f64>,
687
    /// The check fails when coverage falls more than this many points.
688
    pub max_drop: Option<f64>,
689
}
690
691
pub async fn rules(pool: &Pool, repo_id: i64) -> sqlx::Result<Rules> {
692
    let row: Option<(Option<f64>, Option<f64>)> =
693
        sqlx::query_as("SELECT min, max_drop FROM coverage_settings WHERE repo_id = $1").bind(repo_id).fetch_optional(pool).await?;
694
    Ok(row.map(|(min, max_drop)| Rules { min, max_drop }).unwrap_or_default())
695
}
696
697
pub async fn set_rules(pool: &Pool, repo_id: i64, r: Rules) -> sqlx::Result<()> {
698
    sqlx::query(
699
        "INSERT INTO coverage_settings (repo_id, min, max_drop) VALUES ($1, $2, $3) \
700
         ON CONFLICT (repo_id) DO UPDATE SET min = excluded.min, max_drop = excluded.max_drop",
701
    )
702
    .bind(repo_id)
703
    .bind(r.min)
704
    .bind(r.max_drop)
705
    .execute(pool)
706
    .await?;
707
    Ok(())
708
}
709
710
/// What a report is measured against: on the default bookmark, the report
711
/// before it there; on any other, the default bookmark's latest before it.
712
pub async fn baseline(pool: &Pool, r: &Report, default_bookmark: &str) -> sqlx::Result<Option<Report>> {
713
    sqlx::query_as(
714
        "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)) \
715
         ORDER BY created_ms DESC, id DESC LIMIT 1",
716
    )
717
    .bind(r.repo_id)
718
    .bind(default_bookmark)
719
    .bind(r.id)
720
    .bind(r.created_ms)
721
    .bind(r.created_ms)
722
    .bind(r.id)
723
    .fetch_optional(pool)
724
    .await
725
}
726
727
/// The `coverage` check for a report: passed or failed, and what to say.
728
pub fn verdict(r: &Report, base: Option<&Report>, base_name: &str, rules: Rules) -> (&'static str, String) {
729
    let Some(p) = r.percent() else { return ("passed", "no instrumented lines".into()) };
730
    let delta = base.and_then(|b| Some(p - b.percent()?));
731
    let against = match delta {
732
        Some(d) if d.abs() < 0.05 => format!(", unchanged against {base_name}"),
733
        Some(d) => format!(" ({d:+.1} against {base_name})"),
734
        None => String::new(),
735
    };
736
    if let Some(min) = rules.min.filter(|m| p < *m) {
737
        return ("failed", format!("{p:.1}%{against}: below the minimum of {min}%"));
738
    }
739
    if let (Some(max), Some(d)) = (rules.max_drop, delta) {
740
        if d < -max {
741
            return ("failed", format!("{p:.1}%{against}: falls more than the {max} points allowed"));
742
        }
743
    }
744
    ("passed", format!("{p:.1}%{against}"))
745
}
746
747
pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Report>> {
748
    sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = $1 ORDER BY job").bind(run_id).fetch_all(pool).await
749
}
750
751
pub async fn files(pool: &Pool, report_id: i64) -> sqlx::Result<Vec<FileRow>> {
752
    sqlx::query_as("SELECT * FROM coverage_files WHERE report_id = $1 ORDER BY path").bind(report_id).fetch_all(pool).await
753
}
754
755
#[cfg(test)]
756
mod tests {
757
    use super::*;
758
759
    /// oxygenhub#308: a JaCoCo report (Gradle's jacocoTestReport, Kover):
760
    /// lines hit when an instruction ran, branches, methods; package paths
761
    /// found in the state by their ending.
762
    #[test]
763
    fn a_jacoco_report_is_read_and_its_paths_found() {
764
        let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd"><report name="app"><sessioninfo id="x" start="1" dump="2"/><package name="io/oxygenhub/android/api"><class name="io/oxygenhub/android/api/Links" sourcefilename="Links.kt"><method name="routeFor" desc="(Ljava/lang/String;)" line="11"><counter type="INSTRUCTION" missed="2" covered="30"/><counter type="METHOD" missed="0" covered="1"/></method><method name="unused" desc="()V" line="40"><counter type="METHOD" missed="1" covered="0"/></method></class><sourcefile name="Links.kt"><line nr="11" mi="0" ci="5" mb="1" cb="1"/><line nr="12" mi="3" ci="0" mb="0" cb="0"/><line nr="40" mi="2" ci="0" mb="0" cb="0"/><counter type="LINE" missed="2" covered="1"/></sourcefile></package></report>"#;
765
        let (files, outside, format) = parse(xml).unwrap();
766
        assert_eq!((format, outside, files.len()), ("jacoco", 0, 1));
767
        let f = &files[0];
768
        assert_eq!(f.path, "io/oxygenhub/android/api/Links.kt");
769
        assert_eq!((f.lines_found(), f.lines_hit()), (3, 1));
770
        assert_eq!((f.branches_found, f.branches_hit), (2, 1));
771
        assert_eq!((f.functions_found, f.functions_hit), (2, 1));
772
        let state: HashSet<String> =
773
            ["app/src/main/java/io/oxygenhub/android/api/Links.kt".to_string(), "README.md".to_string()].into_iter().collect();
774
        let found = resolve_endings(files, &state);
775
        assert_eq!(found[0].path, "app/src/main/java/io/oxygenhub/android/api/Links.kt");
776
        // Two files of the state end the same way: left as it is.
777
        let twice: HashSet<String> = ["a/x/Links.kt".to_string(), "b/x/Links.kt".to_string()].into_iter().collect();
778
        let left = resolve_endings(vec![FileCoverage { path: "x/Links.kt".into(), ..Default::default() }], &twice);
779
        assert_eq!(left[0].path, "x/Links.kt");
780
        assert!(parse_jacoco("<report></report>").is_err(), "no source file");
781
    }
782
783
    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\
784
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\
785
SF:/src/crates/a/src/lib.rs\nDA:9,1\nend_of_record\n\
786
SF:./README.md\nDA:1,1\nend_of_record\n\
787
SF:/home/runner/.cargo/registry/src/dep/lib.rs\nDA:1,1\nend_of_record\n";
788
789
    #[test]
790
    fn lcov_reports_are_read_and_merged() {
791
        let (files, outside) = parse_lcov(LCOV).unwrap();
792
        assert_eq!(outside, 1, "the dependency outside the workspace");
793
        assert_eq!(files.len(), 2);
794
        let lib = &files[1];
795
        assert_eq!(lib.path, "crates/a/src/lib.rs");
796
        assert_eq!(lib.lines, [(3, 4), (4, 4), (9, 1), (10, 0)].into_iter().collect());
797
        assert_eq!((lib.lines_found(), lib.lines_hit()), (4, 3), "the second record ran line 9");
798
        assert_eq!((lib.functions_found, lib.functions_hit), (2, 1));
799
        assert_eq!((lib.branches_found, lib.branches_hit), (2, 1));
800
        assert_eq!(files[0].path, "README.md");
801
        assert_eq!(lib.declared_lines, None, "two records: counted from the lines");
802
        // One record whose totals name lines its DA lines leave out, as llvm-cov writes.
803
        let (one, _) = parse_lcov("SF:/src/a.rs\nDA:1,1\nDA:2,0\nLF:5\nLH:3\nend_of_record\n").unwrap();
804
        assert_eq!((one[0].lines_found(), one[0].lines_hit()), (5, 3));
805
        assert!(parse_lcov("not a report\n").unwrap_err().contains("not an LCOV report"));
806
    }
807
808
    #[test]
809
    fn the_check_follows_the_rules() {
810
        let report = |pct_hit: i64| Report {
811
            id: 2,
812
            repo_id: 1,
813
            run_id: Some(1),
814
            job_id: Some(1),
815
            job: "test".into(),
816
            run_number: 1,
817
            bookmark: "feature".into(),
818
            state: String::new(),
819
            heads: String::new(),
820
            blob: String::new(),
821
            format: "lcov".into(),
822
            lines_found: 1000,
823
            lines_hit: pct_hit,
824
            functions_found: 0,
825
            functions_hit: 0,
826
            branches_found: 0,
827
            branches_hit: 0,
828
            hits: 0,
829
            files: 1,
830
            dropped: 0,
831
            created_ms: 0,
832
            uploader: String::new(),
833
        };
834
        let (now, before) = (report(819), report(822));
835
        let none = Rules::default();
836
        assert_eq!(verdict(&now, Some(&before), "main", none), ("passed", "81.9% (-0.3 against main)".to_string()));
837
        assert_eq!(verdict(&now, None, "main", none), ("passed", "81.9%".to_string()));
838
        assert_eq!(verdict(&now, Some(&now), "main", none).1, "81.9%, unchanged against main");
839
        let strict = Rules { min: Some(85.0), max_drop: None };
840
        assert_eq!(
841
            verdict(&now, Some(&before), "main", strict),
842
            ("failed", "81.9% (-0.3 against main): below the minimum of 85%".to_string())
843
        );
844
        let drop = Rules { min: None, max_drop: Some(0.2) };
845
        assert_eq!(verdict(&now, Some(&before), "main", drop).0, "failed");
846
        assert_eq!(verdict(&now, Some(&before), "main", Rules { min: None, max_drop: Some(0.5) }).0, "passed");
847
    }
848
849
    #[test]
850
    fn paths_are_taken_relative_to_the_workspace() {
851
        assert_eq!(relative("/src/a/b.rs").as_deref(), Some("a/b.rs"));
852
        assert_eq!(relative("./a/b.rs").as_deref(), Some("a/b.rs"));
853
        assert_eq!(relative("a/b.rs").as_deref(), Some("a/b.rs"));
854
        for bad in ["/etc/passwd", "a/../b", "", "/src/", "a//b", "C:\\x\\y.rs"] {
855
            assert_eq!(relative(bad), None, "{bad}");
856
        }
857
        assert_eq!(percent(1, 3), Some(33.3));
858
        assert_eq!(percent(0, 0), None);
859
    }
860
861
    #[test]
862
    fn cobertura_from_coverage_py_and_coverlet() {
863
        // coverage.py: a source to resolve against, branches by condition.
864
        let py = r#"<?xml version="1.0" ?>
865
<coverage version="7.4" line-rate="0.6" branch-rate="0.5">
866
  <sources><source>/src/app</source></sources>
867
  <packages><package name="app"><classes>
868
    <class name="main.py" filename="main.py" line-rate="0.66">
869
      <methods/>
870
      <lines>
871
        <line number="1" hits="1"/>
872
        <line number="2" hits="0"/>
873
        <line number="3" hits="4" branch="true" condition-coverage="50% (1/2)"/>
874
      </lines>
875
    </class>
876
    <class name="gone.py" filename="/usr/lib/python3/x.py"><lines><line number="1" hits="1"/></lines></class>
877
  </classes></package></packages>
878
</coverage>"#;
879
        let (files, outside, format) = parse(py).unwrap();
880
        assert_eq!((format, outside), ("cobertura", 1));
881
        let f = &files[0];
882
        assert_eq!(f.path, "app/main.py");
883
        assert_eq!((f.lines_found(), f.lines_hit(), f.hits()), (3, 2, 5));
884
        assert_eq!((f.branches_found, f.branches_hit), (2, 1));
885
886
        // Coverlet: methods with their own lines, two classes in one file.
887
        let net = r#"<coverage><sources><source>/src/</source></sources><packages><package name="Lib"><classes>
888
<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>
889
<lines><line number="5" hits="2"/><line number="9" hits="0"/></lines></class>
890
<class name="Lib.A/Inner" filename="Lib/A.cs"><methods/><lines><line number="12" hits="1"/></lines></class>
891
</classes></package></packages></coverage>"#;
892
        let (files, _, _) = parse(net).unwrap();
893
        let f = &files[0];
894
        assert_eq!(f.path, "Lib/A.cs");
895
        assert_eq!((f.lines_found(), f.lines_hit()), (3, 2));
896
        assert_eq!((f.functions_found, f.functions_hit), (2, 1));
897
898
        assert!(parse("<html/>").unwrap_err().contains("Cobertura"));
899
        assert_eq!(parse("SF:/src/a.rs\nDA:1,1\nend_of_record\n").unwrap().2, "lcov");
900
    }
901
}