OXYGENHUB
# 554ccacfc123 oxygenhub / crates / hub / src / coverage.rs
14.8 KiB · Rust · last changed Coverage reports, built in 1 month 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) 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
46
/// A path as a report names it, relative to the repository: the Flow
47
/// workspace's `/src/` and a leading `./` taken off. `None` for a path
48
/// outside the repository.
49
pub fn relative(path: &str) -> Option<String> {
50
    let p = path.trim();
51
    let p = p.strip_prefix("/src/").unwrap_or(p);
52
    let p = p.strip_prefix("./").unwrap_or(p);
53
    if p.is_empty() || p.starts_with('/') || p.contains('\\') || p.split('/').any(|s| s.is_empty() || s == "." || s == "..") {
54
        return None;
55
    }
56
    Some(p.to_string())
57
}
58
59
#[derive(Default)]
60
struct Acc {
61
    lines: BTreeMap<u32, u64>,
62
    functions: BTreeMap<String, u64>,
63
    branches: BTreeMap<(u32, String, String), u64>,
64
    declared_fn: (u64, u64),
65
    declared_br: (u64, u64),
66
    declared_lines: (Option<u64>, Option<u64>),
67
    records: u32,
68
}
69
70
/// Parse an LCOV report into each file's coverage, records for the same
71
/// file (from several test binaries) added together. Files whose paths
72
/// are outside the repository are counted in the second value.
73
pub fn parse_lcov(text: &str) -> Result<(Vec<FileCoverage>, usize), String> {
74
    let mut files: BTreeMap<String, Acc> = BTreeMap::new();
75
    let mut current: Option<Option<String>> = None;
76
    let mut outside = HashSet::new();
77
    let mut records = 0usize;
78
    // A new SF: record has begun and not yet been counted for its file.
79
    let mut started = false;
80
    let num = |s: &str| s.trim().parse::<u64>().unwrap_or(0);
81
    for line in text.lines() {
82
        let line = line.trim();
83
        if let Some(path) = line.strip_prefix("SF:") {
84
            records += 1;
85
            let rel = relative(path);
86
            if rel.is_none() {
87
                outside.insert(path.to_string());
88
            }
89
            current = Some(rel);
90
            started = true;
91
            continue;
92
        }
93
        if line == "end_of_record" {
94
            current = None;
95
            continue;
96
        }
97
        let Some(Some(path)) = &current else { continue };
98
        let acc = files.entry(path.clone()).or_default();
99
        if line == "TN:" || line.starts_with("TN:") {
100
            continue;
101
        }
102
        if std::mem::take(&mut started) {
103
            acc.records += 1;
104
        }
105
        if let Some(n) = line.strip_prefix("LF:") {
106
            acc.declared_lines.0 = Some(num(n));
107
        } else if let Some(n) = line.strip_prefix("LH:") {
108
            acc.declared_lines.1 = Some(num(n));
109
        } else if let Some(rest) = line.strip_prefix("DA:") {
110
            let mut parts = rest.split(',');
111
            if let (Some(l), Some(n)) = (parts.next(), parts.next()) {
112
                if let Ok(l) = l.trim().parse::<u32>() {
113
                    *acc.lines.entry(l).or_default() += num(n);
114
                }
115
            }
116
        } else if let Some(rest) = line.strip_prefix("FN:") {
117
            if let Some((_, name)) = rest.split_once(',') {
118
                acc.functions.entry(name.to_string()).or_default();
119
            }
120
        } else if let Some(rest) = line.strip_prefix("FNDA:") {
121
            if let Some((n, name)) = rest.split_once(',') {
122
                *acc.functions.entry(name.to_string()).or_default() += num(n);
123
            }
124
        } else if let Some(rest) = line.strip_prefix("BRDA:") {
125
            let parts: Vec<&str> = rest.split(',').collect();
126
            if let [l, block, branch, taken] = parts[..] {
127
                if let Ok(l) = l.trim().parse::<u32>() {
128
                    let taken = if taken.trim() == "-" { 0 } else { num(taken) };
129
                    *acc.branches.entry((l, block.to_string(), branch.to_string())).or_default() += taken;
130
                }
131
            }
132
        } else if let Some(n) = line.strip_prefix("FNF:") {
133
            acc.declared_fn.0 = acc.declared_fn.0.max(num(n));
134
        } else if let Some(n) = line.strip_prefix("FNH:") {
135
            acc.declared_fn.1 = acc.declared_fn.1.max(num(n));
136
        } else if let Some(n) = line.strip_prefix("BRF:") {
137
            acc.declared_br.0 = acc.declared_br.0.max(num(n));
138
        } else if let Some(n) = line.strip_prefix("BRH:") {
139
            acc.declared_br.1 = acc.declared_br.1.max(num(n));
140
        }
141
    }
142
    if records == 0 {
143
        return Err("this is not an LCOV report: it names no source file (no SF: line)".into());
144
    }
145
    let out = files
146
        .into_iter()
147
        .map(|(path, a)| {
148
            let (functions_found, functions_hit) = if a.functions.is_empty() {
149
                a.declared_fn
150
            } else {
151
                (a.functions.len() as u64, a.functions.values().filter(|n| **n > 0).count() as u64)
152
            };
153
            let (branches_found, branches_hit) = if a.branches.is_empty() {
154
                a.declared_br
155
            } else {
156
                (a.branches.len() as u64, a.branches.values().filter(|n| **n > 0).count() as u64)
157
            };
158
            let declared_lines = match (a.records, a.declared_lines) {
159
                (1, (Some(f), Some(h))) if h <= f && f >= a.lines.len() as u64 => Some((f, h)),
160
                _ => None,
161
            };
162
            FileCoverage { path, lines: a.lines, functions_found, functions_hit, branches_found, branches_hit, declared_lines }
163
        })
164
        .collect();
165
    Ok((out, outside.len()))
166
}
167
168
/// A percentage to one decimal place, or none when nothing was counted.
169
pub fn percent(hit: i64, found: i64) -> Option<f64> {
170
    (found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0)
171
}
172
173
#[derive(Clone, Debug, sqlx::FromRow)]
174
pub struct Report {
175
    pub id: i64,
176
    pub repo_id: i64,
177
    pub run_id: i64,
178
    pub job_id: i64,
179
    pub job: String,
180
    pub run_number: i64,
181
    pub bookmark: String,
182
    pub state: String,
183
    pub heads: String,
184
    pub blob: String,
185
    pub format: String,
186
    pub lines_found: i64,
187
    pub lines_hit: i64,
188
    pub functions_found: i64,
189
    pub functions_hit: i64,
190
    pub branches_found: i64,
191
    pub branches_hit: i64,
192
    pub files: i64,
193
    pub dropped: i64,
194
    pub created_ms: i64,
195
}
196
197
impl Report {
198
    pub fn percent(&self) -> Option<f64> {
199
        percent(self.lines_hit, self.lines_found)
200
    }
201
202
    pub fn functions_percent(&self) -> Option<f64> {
203
        percent(self.functions_hit, self.functions_found)
204
    }
205
206
    pub fn branches_percent(&self) -> Option<f64> {
207
        percent(self.branches_hit, self.branches_found)
208
    }
209
210
    pub fn head_list(&self) -> Vec<&str> {
211
        self.heads.split(',').filter(|h| !h.is_empty()).collect()
212
    }
213
}
214
215
#[derive(Clone, Debug, sqlx::FromRow)]
216
pub struct FileRow {
217
    pub report_id: i64,
218
    pub path: String,
219
    pub lines_found: i64,
220
    pub lines_hit: i64,
221
    pub functions_found: i64,
222
    pub functions_hit: i64,
223
    pub branches_found: i64,
224
    pub branches_hit: i64,
225
}
226
227
impl FileRow {
228
    pub fn percent(&self) -> Option<f64> {
229
        percent(self.lines_hit, self.lines_found)
230
    }
231
232
    pub fn missed(&self) -> i64 {
233
        self.lines_found - self.lines_hit
234
    }
235
}
236
237
/// Repositories with a report, so their pages show the Coverage tab:
238
/// keyed by the hub's repository root and the id, unique even between
239
/// hubs in one process.
240
static HAS: Mutex<Option<HashSet<String>>> = Mutex::new(None);
241
242
fn key(app: &Shared, repo_id: i64) -> String {
243
    format!("{}#{repo_id}", app.repos.root().display())
244
}
245
246
pub fn has_reports(app: &Shared, repo_id: i64) -> bool {
247
    HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id)))
248
}
249
250
fn mark(app: &Shared, repo_id: i64) {
251
    HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(HashSet::new).insert(key(app, repo_id));
252
}
253
254
/// At start: which repositories have reports.
255
pub async fn load(app: &Shared) -> sqlx::Result<()> {
256
    let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM coverage_reports").fetch_all(&app.db).await?;
257
    for (id,) in ids {
258
        mark(app, id);
259
    }
260
    Ok(())
261
}
262
263
/// What a run's report is kept with.
264
pub struct Origin<'a> {
265
    pub repo_id: i64,
266
    pub run_id: i64,
267
    pub run_number: i64,
268
    pub job_id: i64,
269
    pub job: &'a str,
270
    pub bookmark: &'a str,
271
    pub state: &'a str,
272
    pub heads: &'a [String],
273
    pub blob: &'a str,
274
}
275
276
/// Keep a parsed report: the files outside `in_state` are dropped and
277
/// counted. Replaces an earlier report of the same job.
278
pub async fn keep(app: &Shared, o: &Origin<'_>, files: Vec<FileCoverage>, outside: usize, in_state: &HashSet<String>) -> sqlx::Result<i64> {
279
    let (kept, missing): (Vec<FileCoverage>, Vec<FileCoverage>) = files.into_iter().partition(|f| in_state.contains(&f.path));
280
    let sum = |f: &dyn Fn(&FileCoverage) -> u64| kept.iter().map(f).sum::<u64>() as i64;
281
    let mut tx = crate::db::begin_write(&app.db).await?;
282
    sqlx::query("DELETE FROM coverage_reports WHERE job_id = ?").bind(o.job_id).execute(&mut *tx).await?;
283
    let id: i64 = sqlx::query_scalar(
284
        "INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \
285
         lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, files, dropped, created_ms) \
286
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'lcov', ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
287
    )
288
    .bind(o.repo_id)
289
    .bind(o.run_id)
290
    .bind(o.job_id)
291
    .bind(o.job)
292
    .bind(o.run_number)
293
    .bind(o.bookmark)
294
    .bind(o.state)
295
    .bind(o.heads.join(","))
296
    .bind(o.blob)
297
    .bind(sum(&|f| f.lines_found()))
298
    .bind(sum(&|f| f.lines_hit()))
299
    .bind(sum(&|f| f.functions_found))
300
    .bind(sum(&|f| f.functions_hit))
301
    .bind(sum(&|f| f.branches_found))
302
    .bind(sum(&|f| f.branches_hit))
303
    .bind(kept.len() as i64)
304
    .bind((missing.len() + outside) as i64)
305
    .bind(now_ms())
306
    .fetch_one(&mut *tx)
307
    .await?;
308
    for f in &kept {
309
        sqlx::query(
310
            "INSERT INTO coverage_files (report_id, path, lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit) \
311
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
312
        )
313
        .bind(id)
314
        .bind(&f.path)
315
        .bind(f.lines_found() as i64)
316
        .bind(f.lines_hit() as i64)
317
        .bind(f.functions_found as i64)
318
        .bind(f.functions_hit as i64)
319
        .bind(f.branches_found as i64)
320
        .bind(f.branches_hit as i64)
321
        .execute(&mut *tx)
322
        .await?;
323
    }
324
    tx.commit().await?;
325
    mark(app, o.repo_id);
326
    Ok(id)
327
}
328
329
pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Report>> {
330
    sqlx::query_as("SELECT * FROM coverage_reports WHERE id = ? AND repo_id = ?").bind(id).bind(repo_id).fetch_optional(pool).await
331
}
332
333
/// The newest reports on a bookmark, newest first.
334
pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result<Vec<Report>> {
335
    sqlx::query_as("SELECT * FROM coverage_reports WHERE repo_id = ? AND bookmark = ? ORDER BY created_ms DESC, id DESC LIMIT ?")
336
        .bind(repo_id)
337
        .bind(bookmark)
338
        .bind(limit)
339
        .fetch_all(pool)
340
        .await
341
}
342
343
pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Report>> {
344
    sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = ? ORDER BY job").bind(run_id).fetch_all(pool).await
345
}
346
347
pub async fn files(pool: &Pool, report_id: i64) -> sqlx::Result<Vec<FileRow>> {
348
    sqlx::query_as("SELECT * FROM coverage_files WHERE report_id = ? ORDER BY path").bind(report_id).fetch_all(pool).await
349
}
350
351
#[cfg(test)]
352
mod tests {
353
    use super::*;
354
355
    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\
356
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\
357
SF:/src/crates/a/src/lib.rs\nDA:9,1\nend_of_record\n\
358
SF:./README.md\nDA:1,1\nend_of_record\n\
359
SF:/home/runner/.cargo/registry/src/dep/lib.rs\nDA:1,1\nend_of_record\n";
360
361
    #[test]
362
    fn lcov_reports_are_read_and_merged() {
363
        let (files, outside) = parse_lcov(LCOV).unwrap();
364
        assert_eq!(outside, 1, "the dependency outside the workspace");
365
        assert_eq!(files.len(), 2);
366
        let lib = &files[1];
367
        assert_eq!(lib.path, "crates/a/src/lib.rs");
368
        assert_eq!(lib.lines, [(3, 4), (4, 4), (9, 1), (10, 0)].into_iter().collect());
369
        assert_eq!((lib.lines_found(), lib.lines_hit()), (4, 3), "the second record ran line 9");
370
        assert_eq!((lib.functions_found, lib.functions_hit), (2, 1));
371
        assert_eq!((lib.branches_found, lib.branches_hit), (2, 1));
372
        assert_eq!(files[0].path, "README.md");
373
        assert_eq!(lib.declared_lines, None, "two records: counted from the lines");
374
        // One record whose totals name lines its DA lines leave out, as llvm-cov writes.
375
        let (one, _) = parse_lcov("SF:/src/a.rs\nDA:1,1\nDA:2,0\nLF:5\nLH:3\nend_of_record\n").unwrap();
376
        assert_eq!((one[0].lines_found(), one[0].lines_hit()), (5, 3));
377
        assert!(parse_lcov("not a report\n").unwrap_err().contains("not an LCOV report"));
378
    }
379
380
    #[test]
381
    fn paths_are_taken_relative_to_the_workspace() {
382
        assert_eq!(relative("/src/a/b.rs").as_deref(), Some("a/b.rs"));
383
        assert_eq!(relative("./a/b.rs").as_deref(), Some("a/b.rs"));
384
        assert_eq!(relative("a/b.rs").as_deref(), Some("a/b.rs"));
385
        for bad in ["/etc/passwd", "a/../b", "", "/src/", "a//b", "C:\\x\\y.rs"] {
386
            assert_eq!(relative(bad), None, "{bad}");
387
        }
388
        assert_eq!(percent(1, 3), Some(33.3));
389
        assert_eq!(percent(0, 0), None);
390
    }
391
}