| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 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 | |
|
| 15 | | pub const ARTIFACT: &str = "coverage"; |
| 16 | |
|
| 17 | | pub const MAX_REPORT: usize = 64 << 20; |
| 18 | |
|
| 19 | |
|
| 20 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 21 | | pub struct FileCoverage { |
| 22 | | |
| 23 | | pub path: String, |
| 24 | | |
| 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 | | |
| 31 | | |
| 32 | | |
| 33 | | pub declared_lines: Option<(u64, u64)>, |
| 34 | | } |
| 35 | |
|
| 36 | | impl FileCoverage { |
| 37 | 10× | pub fn lines_found(&self) -> u64 { |
| 38 | 10× | self.declared_lines.map(|d| d.0).unwrap_or(self.lines.len() as u64) |
| 39 | 10× | } |
| 40 | |
|
| 41 | 10× | pub fn lines_hit(&self) -> u64 { |
| 42 | 34× | self.declared_lines.map(|d| d.1).unwrap_or_else(|| self.lines.values().filter(|n| **n > 0).count() as u64) |
| 43 | 10× | } |
| 44 | |
|
| 45 | | |
| 46 | 4× | pub fn hits(&self) -> u64 { |
| 47 | 15× | self.lines.values().fold(0u64, |a, n| a.saturating_add(*n)) |
| 48 | 4× | } |
| 49 | | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 29× | pub fn relative(path: &str) -> Option<String> { |
| 55 | 29× | let p = path.trim(); |
| 56 | 29× | let p = p.strip_prefix("/src/").unwrap_or(p); |
| 57 | 29× | let p = p.strip_prefix("./").unwrap_or(p); |
| 58 | 41× | if p.is_empty() || p.starts_with('/') || p.contains('\\') || p.split('/').any(|s| s.is_empty() || s == "." || s == "..") { |
| 59 | 11× | return None; |
| 60 | 18× | } |
| 61 | 18× | Some(p.to_string()) |
| 62 | 29× | } |
| 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 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 9× | pub fn parse_lcov(text: &str) -> Result<(Vec<FileCoverage>, usize), String> { |
| 79 | 9× | let mut files: BTreeMap<String, Acc> = BTreeMap::new(); |
| 80 | 9× | let mut current: Option<Option<String>> = None; |
| 81 | 9× | let mut outside = HashSet::new(); |
| 82 | 9× | let mut records = 0usize; |
| 83 | | |
| 84 | 9× | let mut started = false; |
| 85 | 46× | let num = |s: &str| s.trim().parse::<u64>().unwrap_or(0); |
| 86 | 99× | for line in text.lines() { |
| 87 | 99× | let line = line.trim(); |
| 88 | 99× | if let Some(path) = line.strip_prefix("SF:") { |
| 89 | 17× | records += 1; |
| 90 | 17× | let rel = relative(path); |
| 91 | 17× | if rel.is_none() { |
| 92 | 4× | outside.insert(path.to_string()); |
| 93 | 13× | } |
| 94 | 17× | current = Some(rel); |
| 95 | 17× | started = true; |
| 96 | 17× | continue; |
| 97 | 82× | } |
| 98 | 82× | if line == "end_of_record" { |
| 99 | 17× | current = None; |
| 100 | 17× | continue; |
| 101 | 65× | } |
| 102 | 59× | let Some(Some(path)) = ¤t else { continue }; |
| 103 | 55× | let acc = files.entry(path.clone()).or_default(); |
| 104 | 55× | if line == "TN:" || line.starts_with("TN:") { |
| 105 | 0× | continue; |
| 106 | 55× | } |
| 107 | 55× | if std::mem::take(&mut started) { |
| 108 | 13× | acc.records += 1; |
| 109 | 42× | } |
| 110 | 55× | if let Some(n) = line.strip_prefix("LF:") { |
| 111 | 2× | acc.declared_lines.0 = Some(num(n)); |
| 112 | 53× | } else if let Some(n) = line.strip_prefix("LH:") { |
| 113 | 2× | acc.declared_lines.1 = Some(num(n)); |
| 114 | 51× | } else if let Some(rest) = line.strip_prefix("DA:") { |
| 115 | 29× | let mut parts = rest.split(','); |
| 116 | 29× | if let (Some(l), Some(n)) = (parts.next(), parts.next()) { |
| 117 | 29× | if let Ok(l) = l.trim().parse::<u32>() { |
| 118 | 29× | *acc.lines.entry(l).or_default() += num(n); |
| 119 | 29× | } |
| 120 | 0× | } |
| 121 | 22× | } else if let Some(rest) = line.strip_prefix("FN:") { |
| 122 | 8× | if let Some((_, name)) = rest.split_once(',') { |
| 123 | 8× | acc.functions.entry(name.to_string()).or_default(); |
| 124 | 8× | } |
| 125 | 14× | } else if let Some(rest) = line.strip_prefix("FNDA:") { |
| 126 | 8× | if let Some((n, name)) = rest.split_once(',') { |
| 127 | 8× | *acc.functions.entry(name.to_string()).or_default() += num(n); |
| 128 | 8× | } |
| 129 | 6× | } else if let Some(rest) = line.strip_prefix("BRDA:") { |
| 130 | 2× | let parts: Vec<&str> = rest.split(',').collect(); |
| 131 | 2× | if let [l, block, branch, taken] = parts[..] { |
| 132 | 2× | if let Ok(l) = l.trim().parse::<u32>() { |
| 133 | 2× | let taken = if taken.trim() == "-" { 0 } else { num(taken) }; |
| 134 | 2× | *acc.branches.entry((l, block.to_string(), branch.to_string())).or_default() += taken; |
| 135 | 0× | } |
| 136 | 0× | } |
| 137 | 4× | } else if let Some(n) = line.strip_prefix("FNF:") { |
| 138 | 1× | acc.declared_fn.0 = acc.declared_fn.0.max(num(n)); |
| 139 | 3× | } else if let Some(n) = line.strip_prefix("FNH:") { |
| 140 | 1× | acc.declared_fn.1 = acc.declared_fn.1.max(num(n)); |
| 141 | 2× | } else if let Some(n) = line.strip_prefix("BRF:") { |
| 142 | 1× | acc.declared_br.0 = acc.declared_br.0.max(num(n)); |
| 143 | 1× | } else if let Some(n) = line.strip_prefix("BRH:") { |
| 144 | 1× | acc.declared_br.1 = acc.declared_br.1.max(num(n)); |
| 145 | 1× | } |
| 146 | | } |
| 147 | 9× | if records == 0 { |
| 148 | 2× | return Err("this is not an LCOV report: it names no source file (no SF: line)".into()); |
| 149 | 7× | } |
| 150 | 7× | let out = files |
| 151 | 7× | .into_iter() |
| 152 | 12× | .map(|(path, a)| { |
| 153 | 12× | let (functions_found, functions_hit) = if a.functions.is_empty() { |
| 154 | 8× | a.declared_fn |
| 155 | | } else { |
| 156 | 8× | (a.functions.len() as u64, a.functions.values().filter(|n| **n > 0).count() as u64) |
| 157 | | }; |
| 158 | 12× | let (branches_found, branches_hit) = if a.branches.is_empty() { |
| 159 | 11× | a.declared_br |
| 160 | | } else { |
| 161 | 2× | (a.branches.len() as u64, a.branches.values().filter(|n| **n > 0).count() as u64) |
| 162 | | }; |
| 163 | 12× | let declared_lines = match (a.records, a.declared_lines) { |
| 164 | 1× | (1, (Some(f), Some(h))) if h <= f && f >= a.lines.len() as u64 => Some((f, h)), |
| 165 | 11× | _ => None, |
| 166 | | }; |
| 167 | 12× | FileCoverage { path, lines: a.lines, functions_found, functions_hit, branches_found, branches_hit, declared_lines } |
| 168 | 12× | }) |
| 169 | 7× | .collect(); |
| 170 | 7× | Ok((out, outside.len())) |
| 171 | 9× | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | 9× | pub fn parse(text: &str) -> Result<(Vec<FileCoverage>, usize, &'static str), String> { |
| 177 | 9× | if text.trim_start().starts_with('<') { |
| 178 | 3× | parse_cobertura(text).map(|(f, o)| (f, o, "cobertura")) |
| 179 | | } else { |
| 180 | 6× | parse_lcov(text).map(|(f, o)| (f, o, "lcov")) |
| 181 | | } |
| 182 | 9× | } |
| 183 | |
|
| 184 | 34× | fn xml_attr(e: &quick_xml::events::BytesStart, name: &str) -> String { |
| 185 | 34× | e.try_get_attribute(name) |
| 186 | 34× | .ok() |
| 187 | 34× | .flatten() |
| 188 | 34× | .and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) |
| 189 | 34× | .unwrap_or_default() |
| 190 | 34× | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | 3× | pub fn parse_cobertura(text: &str) -> Result<(Vec<FileCoverage>, usize), String> { |
| 200 | | use quick_xml::events::Event; |
| 201 | 3× | let mut r = quick_xml::Reader::from_str(text); |
| 202 | 3× | let mut sources: Vec<String> = Vec::new(); |
| 203 | 3× | let mut in_source = false; |
| 204 | 3× | let mut source_text = String::new(); |
| 205 | | |
| 206 | 3× | let mut raw: BTreeMap<String, Acc> = BTreeMap::new(); |
| 207 | 3× | let mut file: Option<String> = None; |
| 208 | 3× | let mut method: Option<(String, bool)> = None; |
| 209 | 3× | let mut seen_root = false; |
| 210 | | loop { |
| 211 | 86× | let ev = r.read_event().map_err(|e| format!("this is not a Cobertura report: {e}"))?; |
| 212 | 20× | match ev { |
| 213 | 25× | Event::Start(ref e) | Event::Empty(ref e) => { |
| 214 | 37× | let tag = e.local_name().as_ref().to_string(); |
| 215 | 37× | match tag.as_str() { |
| 216 | 37× | "coverage" => seen_root = true, |
| 217 | 35× | "source" if matches!(ev, Event::Start(_)) => { |
| 218 | 2× | in_source = true; |
| 219 | 2× | source_text.clear(); |
| 220 | 2× | } |
| 221 | 33× | "class" => { |
| 222 | 4× | let f = xml_attr(e, "filename"); |
| 223 | 4× | raw.entry(f.clone()).or_default().records += 1; |
| 224 | 4× | file = Some(f); |
| 225 | 4× | } |
| 226 | 29× | "method" => method = Some((xml_attr(e, "name") + &xml_attr(e, "signature"), false)), |
| 227 | 27× | "line" => { |
| 228 | 9× | let Some(f) = &file else { continue }; |
| 229 | 9× | let Ok(n) = xml_attr(e, "number").trim().parse::<u32>() else { continue }; |
| 230 | 9× | let hits = xml_attr(e, "hits").trim().parse::<u64>().unwrap_or(0); |
| 231 | 9× | if let Some((_, hit)) = &mut method { |
| 232 | 2× | *hit |= hits > 0; |
| 233 | | |
| 234 | 2× | continue; |
| 235 | 7× | } |
| 236 | 7× | let acc = raw.entry(f.clone()).or_default(); |
| 237 | 7× | *acc.lines.entry(n).or_default() += hits; |
| 238 | 7× | if xml_attr(e, "branch") == "true" { |
| 239 | | |
| 240 | 1× | let cc = xml_attr(e, "condition-coverage"); |
| 241 | 1× | if let Some((hit, total)) = cc |
| 242 | 1× | .split_once('(') |
| 243 | 1× | .and_then(|(_, rest)| rest.trim_end_matches(')').split_once('/')) |
| 244 | 1× | .and_then(|(h, t)| Some((h.trim().parse::<u64>().ok()?, t.trim().parse::<u64>().ok()?))) |
| 245 | | { |
| 246 | 2× | for i in 0..total { |
| 247 | 2× | let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default(); |
| 248 | 2× | *b = (*b).max(u64::from(i < hit)); |
| 249 | 2× | } |
| 250 | 0× | } |
| 251 | 6× | } |
| 252 | | } |
| 253 | 18× | _ => {} |
| 254 | | } |
| 255 | 35× | if matches!(ev, Event::Empty(_)) && tag == "method" { |
| 256 | 0× | if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
| 257 | 0× | *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 258 | 0× | } |
| 259 | 35× | } |
| 260 | | } |
| 261 | 2× | Event::Text(t) if in_source => source_text.push_str(&t.xml10_content()), |
| 262 | 25× | Event::End(e) => match e.local_name().as_ref() { |
| 263 | 25× | "source" => { |
| 264 | 2× | in_source = false; |
| 265 | 2× | sources.push(source_text.trim().trim_end_matches('/').to_string()); |
| 266 | 2× | } |
| 267 | 23× | "class" => file = None, |
| 268 | 19× | "method" => { |
| 269 | 2× | if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
| 270 | 2× | *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 271 | 2× | } |
| 272 | | } |
| 273 | 17× | _ => {} |
| 274 | | }, |
| 275 | 3× | Event::Eof => break, |
| 276 | 19× | _ => {} |
| 277 | | } |
| 278 | | } |
| 279 | 3× | if !seen_root { |
| 280 | 1× | return Err("this is not a Cobertura report: no <coverage> element".into()); |
| 281 | 2× | } |
| 282 | 2× | if raw.is_empty() { |
| 283 | 0× | return Err("this Cobertura report names no source file (no <class filename>)".into()); |
| 284 | 2× | } |
| 285 | | |
| 286 | 3× | let resolve = |f: &str| -> Option<String> { |
| 287 | 3× | if f.starts_with('/') { |
| 288 | 1× | return relative(f); |
| 289 | 2× | } |
| 290 | 2× | sources.iter().filter(|s| !s.is_empty() && *s != ".").find_map(|s| relative(&format!("{s}/{f}"))).or_else(|| relative(f)) |
| 291 | 3× | }; |
| 292 | 2× | let mut files: BTreeMap<String, Acc> = BTreeMap::new(); |
| 293 | 2× | let mut outside = HashSet::new(); |
| 294 | 3× | for (f, a) in raw { |
| 295 | 3× | let Some(path) = resolve(&f) else { |
| 296 | 1× | outside.insert(f); |
| 297 | 1× | continue; |
| 298 | | }; |
| 299 | 2× | let into = files.entry(path).or_default(); |
| 300 | 6× | for (l, n) in a.lines { |
| 301 | 6× | *into.lines.entry(l).or_default() += n; |
| 302 | 6× | } |
| 303 | 2× | for (k, n) in a.functions { |
| 304 | 2× | *into.functions.entry(k).or_default() += n; |
| 305 | 2× | } |
| 306 | 2× | for (k, n) in a.branches { |
| 307 | 2× | let b = into.branches.entry(k).or_default(); |
| 308 | 2× | *b = (*b).max(n); |
| 309 | 2× | } |
| 310 | | } |
| 311 | 2× | let out = files |
| 312 | 2× | .into_iter() |
| 313 | 2× | .map(|(path, a)| FileCoverage { |
| 314 | 2× | path, |
| 315 | 2× | functions_found: a.functions.len() as u64, |
| 316 | 2× | functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64, |
| 317 | 2× | branches_found: a.branches.len() as u64, |
| 318 | 2× | branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64, |
| 319 | 2× | lines: a.lines, |
| 320 | 2× | declared_lines: None, |
| 321 | 2× | }) |
| 322 | 2× | .collect(); |
| 323 | 2× | Ok((out, outside.len())) |
| 324 | 3× | } |
| 325 | |
|
| 326 | |
|
| 327 | 60× | pub fn percent(hit: i64, found: i64) -> Option<f64> { |
| 328 | 60× | (found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0) |
| 329 | 60× | } |
| 330 | |
|
| 331 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 332 | | pub struct Report { |
| 333 | | pub id: i64, |
| 334 | | pub repo_id: i64, |
| 335 | | |
| 336 | | pub run_id: Option<i64>, |
| 337 | | pub job_id: Option<i64>, |
| 338 | | pub job: String, |
| 339 | | pub run_number: i64, |
| 340 | | pub bookmark: String, |
| 341 | | pub state: String, |
| 342 | | pub heads: String, |
| 343 | | pub blob: String, |
| 344 | | pub format: String, |
| 345 | | pub lines_found: i64, |
| 346 | | pub lines_hit: i64, |
| 347 | | pub functions_found: i64, |
| 348 | | pub functions_hit: i64, |
| 349 | | pub branches_found: i64, |
| 350 | | pub branches_hit: i64, |
| 351 | | pub hits: i64, |
| 352 | | pub files: i64, |
| 353 | | pub dropped: i64, |
| 354 | | pub created_ms: i64, |
| 355 | | |
| 356 | | pub uploader: String, |
| 357 | | } |
| 358 | |
|
| 359 | | impl Report { |
| 360 | | |
| 361 | 12× | pub fn uploaded(&self) -> bool { |
| 362 | 12× | !self.uploader.is_empty() |
| 363 | 12× | } |
| 364 | |
|
| 365 | | |
| 366 | 4× | pub fn source(&self) -> String { |
| 367 | 4× | if self.uploaded() { |
| 368 | 3× | format!("uploaded by {}", self.uploader) |
| 369 | | } else { |
| 370 | 1× | format!("run #{}", self.run_number) |
| 371 | | } |
| 372 | 4× | } |
| 373 | |
|
| 374 | | |
| 375 | 4× | pub fn hits_per_line(&self) -> Option<f64> { |
| 376 | 4× | (self.lines_found > 0).then(|| self.hits as f64 / self.lines_found as f64) |
| 377 | 4× | } |
| 378 | |
|
| 379 | 36× | pub fn percent(&self) -> Option<f64> { |
| 380 | 36× | percent(self.lines_hit, self.lines_found) |
| 381 | 36× | } |
| 382 | |
|
| 383 | 1× | pub fn functions_percent(&self) -> Option<f64> { |
| 384 | 1× | percent(self.functions_hit, self.functions_found) |
| 385 | 1× | } |
| 386 | |
|
| 387 | 0× | pub fn branches_percent(&self) -> Option<f64> { |
| 388 | 0× | percent(self.branches_hit, self.branches_found) |
| 389 | 0× | } |
| 390 | |
|
| 391 | 1× | pub fn head_list(&self) -> Vec<&str> { |
| 392 | 1× | self.heads.split(',').filter(|h| !h.is_empty()).collect() |
| 393 | 1× | } |
| 394 | | } |
| 395 | |
|
| 396 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 397 | | pub struct FileRow { |
| 398 | | pub report_id: i64, |
| 399 | | pub path: String, |
| 400 | | pub lines_found: i64, |
| 401 | | pub lines_hit: i64, |
| 402 | | pub functions_found: i64, |
| 403 | | pub functions_hit: i64, |
| 404 | | pub branches_found: i64, |
| 405 | | pub branches_hit: i64, |
| 406 | | } |
| 407 | |
|
| 408 | | impl FileRow { |
| 409 | 21× | pub fn percent(&self) -> Option<f64> { |
| 410 | 21× | percent(self.lines_hit, self.lines_found) |
| 411 | 21× | } |
| 412 | |
|
| 413 | 2× | pub fn missed(&self) -> i64 { |
| 414 | 2× | self.lines_found - self.lines_hit |
| 415 | 2× | } |
| 416 | | } |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | | static HAS: Mutex<Option<HashSet<String>>> = Mutex::new(None); |
| 422 | |
|
| 423 | 8× | fn key(app: &Shared, repo_id: i64) -> String { |
| 424 | 8× | format!("{}#{repo_id}", app.repos.root().display()) |
| 425 | 8× | } |
| 426 | |
|
| 427 | 517× | pub fn has_reports(app: &Shared, repo_id: i64) -> bool { |
| 428 | 517× | HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id))) |
| 429 | 517× | } |
| 430 | |
|
| 431 | 3× | fn mark(app: &Shared, repo_id: i64) { |
| 432 | 3× | HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(HashSet::new).insert(key(app, repo_id)); |
| 433 | 3× | } |
| 434 | |
|
| 435 | |
|
| 436 | 189× | pub async fn load(app: &Shared) -> sqlx::Result<()> { |
| 437 | 189× | let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM coverage_reports").fetch_all(&app.db).await?; |
| 438 | 189× | for (id,) in ids { |
| 439 | 0× | mark(app, id); |
| 440 | 0× | } |
| 441 | 189× | Ok(()) |
| 442 | 189× | } |
| 443 | |
|
| 444 | |
|
| 445 | | pub struct Origin<'a> { |
| 446 | | pub repo_id: i64, |
| 447 | | |
| 448 | | pub run_id: Option<i64>, |
| 449 | | pub run_number: i64, |
| 450 | | pub job_id: Option<i64>, |
| 451 | | pub uploader: &'a str, |
| 452 | | pub job: &'a str, |
| 453 | | pub bookmark: &'a str, |
| 454 | | pub state: &'a str, |
| 455 | | pub heads: &'a [String], |
| 456 | | pub blob: &'a str, |
| 457 | | |
| 458 | | pub format: &'a str, |
| 459 | | } |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | 3× | pub async fn keep(app: &Shared, o: &Origin<'_>, files: Vec<FileCoverage>, outside: usize, in_state: &HashSet<String>) -> sqlx::Result<i64> { |
| 464 | 6× | let (kept, missing): (Vec<FileCoverage>, Vec<FileCoverage>) = files.into_iter().partition(|f| in_state.contains(&f.path)); |
| 465 | 18× | let sum = |f: &dyn Fn(&FileCoverage) -> u64| kept.iter().map(f).sum::<u64>() as i64; |
| 466 | 3× | let mut tx = crate::db::begin_write(&app.db).await?; |
| 467 | 3× | if let Some(job) = o.job_id { |
| 468 | 2× | sqlx::query("DELETE FROM coverage_reports WHERE job_id = $1").bind(job).execute(&mut *tx).await?; |
| 469 | 1× | } |
| 470 | 3× | let id: i64 = sqlx::query_scalar( |
| 471 | 3× | "INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \ |
| 472 | 3× | lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, hits, files, dropped, created_ms, uploader) \ |
| 473 | 3× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) RETURNING id", |
| 474 | | ) |
| 475 | 3× | .bind(o.repo_id) |
| 476 | 3× | .bind(o.run_id) |
| 477 | 3× | .bind(o.job_id) |
| 478 | 3× | .bind(o.job) |
| 479 | 3× | .bind(o.run_number) |
| 480 | 3× | .bind(o.bookmark) |
| 481 | 3× | .bind(o.state) |
| 482 | 3× | .bind(o.heads.join(",")) |
| 483 | 3× | .bind(o.blob) |
| 484 | 3× | .bind(o.format) |
| 485 | 3× | .bind(sum(&|f| f.lines_found())) |
| 486 | 3× | .bind(sum(&|f| f.lines_hit())) |
| 487 | 3× | .bind(sum(&|f| f.functions_found)) |
| 488 | 3× | .bind(sum(&|f| f.functions_hit)) |
| 489 | 3× | .bind(sum(&|f| f.branches_found)) |
| 490 | 3× | .bind(sum(&|f| f.branches_hit)) |
| 491 | 3× | .bind(kept.iter().fold(0u64, |a, f| a.saturating_add(f.hits())).min(i64::MAX as u64) as i64) |
| 492 | 3× | .bind(kept.len() as i64) |
| 493 | 3× | .bind((missing.len() + outside) as i64) |
| 494 | 3× | .bind(now_ms()) |
| 495 | 3× | .bind(o.uploader) |
| 496 | 3× | .fetch_one(&mut *tx) |
| 497 | 3× | .await?; |
| 498 | 3× | for f in &kept { |
| 499 | 3× | sqlx::query( |
| 500 | 3× | "INSERT INTO coverage_files (report_id, path, lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit) \ |
| 501 | 3× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", |
| 502 | 3× | ) |
| 503 | 3× | .bind(id) |
| 504 | 3× | .bind(&f.path) |
| 505 | 3× | .bind(f.lines_found() as i64) |
| 506 | 3× | .bind(f.lines_hit() as i64) |
| 507 | 3× | .bind(f.functions_found as i64) |
| 508 | 3× | .bind(f.functions_hit as i64) |
| 509 | 3× | .bind(f.branches_found as i64) |
| 510 | 3× | .bind(f.branches_hit as i64) |
| 511 | 3× | .execute(&mut *tx) |
| 512 | 3× | .await?; |
| 513 | | } |
| 514 | 3× | tx.commit().await?; |
| 515 | 3× | mark(app, o.repo_id); |
| 516 | 3× | Ok(id) |
| 517 | 3× | } |
| 518 | |
|
| 519 | 6× | pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Report>> { |
| 520 | 6× | sqlx::query_as("SELECT * FROM coverage_reports WHERE id = $1 AND repo_id = $2").bind(id).bind(repo_id).fetch_optional(pool).await |
| 521 | 6× | } |
| 522 | |
|
| 523 | |
|
| 524 | 5× | pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result<Vec<Report>> { |
| 525 | 5× | sqlx::query_as("SELECT * FROM coverage_reports WHERE repo_id = $1 AND bookmark = $2 ORDER BY created_ms DESC, id DESC LIMIT $3") |
| 526 | 5× | .bind(repo_id) |
| 527 | 5× | .bind(bookmark) |
| 528 | 5× | .bind(limit) |
| 529 | 5× | .fetch_all(pool) |
| 530 | 5× | .await |
| 531 | 5× | } |
| 532 | |
|
| 533 | |
|
| 534 | 2× | pub async fn first_ms(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<i64>> { |
| 535 | 2× | sqlx::query_scalar("SELECT MIN(created_ms) FROM coverage_reports WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await |
| 536 | 2× | } |
| 537 | |
|
| 538 | |
|
| 539 | 4× | pub async fn bookmarks(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 540 | 4× | sqlx::query_scalar("SELECT bookmark FROM coverage_reports WHERE repo_id = $1 GROUP BY bookmark ORDER BY MAX(created_ms) DESC") |
| 541 | 4× | .bind(repo_id) |
| 542 | 4× | .fetch_all(pool) |
| 543 | 4× | .await |
| 544 | 4× | } |
| 545 | |
|
| 546 | |
|
| 547 | | #[derive(Clone, Copy, Debug, Default, PartialEq)] |
| 548 | | pub struct Rules { |
| 549 | | |
| 550 | | pub min: Option<f64>, |
| 551 | | |
| 552 | | pub max_drop: Option<f64>, |
| 553 | | } |
| 554 | |
|
| 555 | 135× | pub async fn rules(pool: &Pool, repo_id: i64) -> sqlx::Result<Rules> { |
| 556 | 135× | let row: Option<(Option<f64>, Option<f64>)> = |
| 557 | 135× | sqlx::query_as("SELECT min, max_drop FROM coverage_settings WHERE repo_id = $1").bind(repo_id).fetch_optional(pool).await?; |
| 558 | 135× | Ok(row.map(|(min, max_drop)| Rules { min, max_drop }).unwrap_or_default()) |
| 559 | 135× | } |
| 560 | |
|
| 561 | 2× | pub async fn set_rules(pool: &Pool, repo_id: i64, r: Rules) -> sqlx::Result<()> { |
| 562 | 2× | sqlx::query( |
| 563 | 2× | "INSERT INTO coverage_settings (repo_id, min, max_drop) VALUES ($1, $2, $3) \ |
| 564 | 2× | ON CONFLICT (repo_id) DO UPDATE SET min = excluded.min, max_drop = excluded.max_drop", |
| 565 | 2× | ) |
| 566 | 2× | .bind(repo_id) |
| 567 | 2× | .bind(r.min) |
| 568 | 2× | .bind(r.max_drop) |
| 569 | 2× | .execute(pool) |
| 570 | 2× | .await?; |
| 571 | 2× | Ok(()) |
| 572 | 2× | } |
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | 2× | pub async fn baseline(pool: &Pool, r: &Report, default_bookmark: &str) -> sqlx::Result<Option<Report>> { |
| 577 | 2× | sqlx::query_as( |
| 578 | 2× | "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)) \ |
| 579 | 2× | ORDER BY created_ms DESC, id DESC LIMIT 1", |
| 580 | 2× | ) |
| 581 | 2× | .bind(r.repo_id) |
| 582 | 2× | .bind(default_bookmark) |
| 583 | 2× | .bind(r.id) |
| 584 | 2× | .bind(r.created_ms) |
| 585 | 2× | .bind(r.created_ms) |
| 586 | 2× | .bind(r.id) |
| 587 | 2× | .fetch_optional(pool) |
| 588 | 2× | .await |
| 589 | 2× | } |
| 590 | |
|
| 591 | |
|
| 592 | 8× | pub fn verdict(r: &Report, base: Option<&Report>, base_name: &str, rules: Rules) -> (&'static str, String) { |
| 593 | 8× | let Some(p) = r.percent() else { return ("passed", "no instrumented lines".into()) }; |
| 594 | 8× | let delta = base.and_then(|b| Some(p - b.percent()?)); |
| 595 | 8× | let against = match delta { |
| 596 | 5× | Some(d) if d.abs() < 0.05 => format!(", unchanged against {base_name}"), |
| 597 | 4× | Some(d) => format!(" ({d:+.1} against {base_name})"), |
| 598 | 3× | None => String::new(), |
| 599 | | }; |
| 600 | 8× | if let Some(min) = rules.min.filter(|m| p < *m) { |
| 601 | 2× | return ("failed", format!("{p:.1}%{against}: below the minimum of {min}%")); |
| 602 | 6× | } |
| 603 | 6× | if let (Some(max), Some(d)) = (rules.max_drop, delta) { |
| 604 | 2× | if d < -max { |
| 605 | 1× | return ("failed", format!("{p:.1}%{against}: falls more than the {max} points allowed")); |
| 606 | 1× | } |
| 607 | 4× | } |
| 608 | 5× | ("passed", format!("{p:.1}%{against}")) |
| 609 | 8× | } |
| 610 | |
|
| 611 | 23× | pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Report>> { |
| 612 | 23× | sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = $1 ORDER BY job").bind(run_id).fetch_all(pool).await |
| 613 | 23× | } |
| 614 | |
|
| 615 | 5× | pub async fn files(pool: &Pool, report_id: i64) -> sqlx::Result<Vec<FileRow>> { |
| 616 | 5× | sqlx::query_as("SELECT * FROM coverage_files WHERE report_id = $1 ORDER BY path").bind(report_id).fetch_all(pool).await |
| 617 | 5× | } |
| 618 | |
|
| 619 | | #[cfg(test)] |
| 620 | | mod tests { |
| 621 | | use super::*; |
| 622 | |
|
| 623 | | 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\ |
| 624 | | 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\ |
| 625 | | SF:/src/crates/a/src/lib.rs\nDA:9,1\nend_of_record\n\ |
| 626 | | SF:./README.md\nDA:1,1\nend_of_record\n\ |
| 627 | | SF:/home/runner/.cargo/registry/src/dep/lib.rs\nDA:1,1\nend_of_record\n"; |
| 628 | |
|
| 629 | | #[test] |
| 630 | 1× | fn lcov_reports_are_read_and_merged() { |
| 631 | 1× | let (files, outside) = parse_lcov(LCOV).unwrap(); |
| 632 | 1× | assert_eq!(outside, 1, "the dependency outside the workspace"); |
| 633 | 1× | assert_eq!(files.len(), 2); |
| 634 | 1× | let lib = &files[1]; |
| 635 | 1× | assert_eq!(lib.path, "crates/a/src/lib.rs"); |
| 636 | 1× | assert_eq!(lib.lines, [(3, 4), (4, 4), (9, 1), (10, 0)].into_iter().collect()); |
| 637 | 1× | assert_eq!((lib.lines_found(), lib.lines_hit()), (4, 3), "the second record ran line 9"); |
| 638 | 1× | assert_eq!((lib.functions_found, lib.functions_hit), (2, 1)); |
| 639 | 1× | assert_eq!((lib.branches_found, lib.branches_hit), (2, 1)); |
| 640 | 1× | assert_eq!(files[0].path, "README.md"); |
| 641 | 1× | assert_eq!(lib.declared_lines, None, "two records: counted from the lines"); |
| 642 | | |
| 643 | 1× | let (one, _) = parse_lcov("SF:/src/a.rs\nDA:1,1\nDA:2,0\nLF:5\nLH:3\nend_of_record\n").unwrap(); |
| 644 | 1× | assert_eq!((one[0].lines_found(), one[0].lines_hit()), (5, 3)); |
| 645 | 1× | assert!(parse_lcov("not a report\n").unwrap_err().contains("not an LCOV report")); |
| 646 | 1× | } |
| 647 | |
|
| 648 | | #[test] |
| 649 | 1× | fn the_check_follows_the_rules() { |
| 650 | 1× | let report = |pct_hit: i64| Report { |
| 651 | | id: 2, |
| 652 | | repo_id: 1, |
| 653 | 2× | run_id: Some(1), |
| 654 | 2× | job_id: Some(1), |
| 655 | 2× | job: "test".into(), |
| 656 | | run_number: 1, |
| 657 | 2× | bookmark: "feature".into(), |
| 658 | 2× | state: String::new(), |
| 659 | 2× | heads: String::new(), |
| 660 | 2× | blob: String::new(), |
| 661 | 2× | format: "lcov".into(), |
| 662 | | lines_found: 1000, |
| 663 | 2× | lines_hit: pct_hit, |
| 664 | | functions_found: 0, |
| 665 | | functions_hit: 0, |
| 666 | | branches_found: 0, |
| 667 | | branches_hit: 0, |
| 668 | | hits: 0, |
| 669 | | files: 1, |
| 670 | | dropped: 0, |
| 671 | | created_ms: 0, |
| 672 | 2× | uploader: String::new(), |
| 673 | 2× | }; |
| 674 | 1× | let (now, before) = (report(819), report(822)); |
| 675 | 1× | let none = Rules::default(); |
| 676 | 1× | assert_eq!(verdict(&now, Some(&before), "main", none), ("passed", "81.9% (-0.3 against main)".to_string())); |
| 677 | 1× | assert_eq!(verdict(&now, None, "main", none), ("passed", "81.9%".to_string())); |
| 678 | 1× | assert_eq!(verdict(&now, Some(&now), "main", none).1, "81.9%, unchanged against main"); |
| 679 | 1× | let strict = Rules { min: Some(85.0), max_drop: None }; |
| 680 | 1× | assert_eq!( |
| 681 | 1× | verdict(&now, Some(&before), "main", strict), |
| 682 | 1× | ("failed", "81.9% (-0.3 against main): below the minimum of 85%".to_string()) |
| 683 | | ); |
| 684 | 1× | let drop = Rules { min: None, max_drop: Some(0.2) }; |
| 685 | 1× | assert_eq!(verdict(&now, Some(&before), "main", drop).0, "failed"); |
| 686 | 1× | assert_eq!(verdict(&now, Some(&before), "main", Rules { min: None, max_drop: Some(0.5) }).0, "passed"); |
| 687 | 1× | } |
| 688 | |
|
| 689 | | #[test] |
| 690 | 1× | fn paths_are_taken_relative_to_the_workspace() { |
| 691 | 1× | assert_eq!(relative("/src/a/b.rs").as_deref(), Some("a/b.rs")); |
| 692 | 1× | assert_eq!(relative("./a/b.rs").as_deref(), Some("a/b.rs")); |
| 693 | 1× | assert_eq!(relative("a/b.rs").as_deref(), Some("a/b.rs")); |
| 694 | 6× | for bad in ["/etc/passwd", "a/../b", "", "/src/", "a//b", "C:\\x\\y.rs"] { |
| 695 | 6× | assert_eq!(relative(bad), None, "{bad}"); |
| 696 | | } |
| 697 | 1× | assert_eq!(percent(1, 3), Some(33.3)); |
| 698 | 1× | assert_eq!(percent(0, 0), None); |
| 699 | 1× | } |
| 700 | |
|
| 701 | | #[test] |
| 702 | 1× | fn cobertura_from_coverage_py_and_coverlet() { |
| 703 | | |
| 704 | 1× | let py = r#"<?xml version="1.0" ?> |
| 705 | 1× | <coverage version="7.4" line-rate="0.6" branch-rate="0.5"> |
| 706 | 1× | <sources><source>/src/app</source></sources> |
| 707 | 1× | <packages><package name="app"><classes> |
| 708 | 1× | <class name="main.py" filename="main.py" line-rate="0.66"> |
| 709 | 1× | <methods/> |
| 710 | 1× | <lines> |
| 711 | 1× | <line number="1" hits="1"/> |
| 712 | 1× | <line number="2" hits="0"/> |
| 713 | 1× | <line number="3" hits="4" branch="true" condition-coverage="50% (1/2)"/> |
| 714 | 1× | </lines> |
| 715 | 1× | </class> |
| 716 | 1× | <class name="gone.py" filename="/usr/lib/python3/x.py"><lines><line number="1" hits="1"/></lines></class> |
| 717 | 1× | </classes></package></packages> |
| 718 | 1× | </coverage>"#; |
| 719 | 1× | let (files, outside, format) = parse(py).unwrap(); |
| 720 | 1× | assert_eq!((format, outside), ("cobertura", 1)); |
| 721 | 1× | let f = &files[0]; |
| 722 | 1× | assert_eq!(f.path, "app/main.py"); |
| 723 | 1× | assert_eq!((f.lines_found(), f.lines_hit(), f.hits()), (3, 2, 5)); |
| 724 | 1× | assert_eq!((f.branches_found, f.branches_hit), (2, 1)); |
| 725 | |
|
| 726 | | |
| 727 | 1× | let net = r#"<coverage><sources><source>/src/</source></sources><packages><package name="Lib"><classes> |
| 728 | 1× | <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> |
| 729 | 1× | <lines><line number="5" hits="2"/><line number="9" hits="0"/></lines></class> |
| 730 | 1× | <class name="Lib.A/Inner" filename="Lib/A.cs"><methods/><lines><line number="12" hits="1"/></lines></class> |
| 731 | 1× | </classes></package></packages></coverage>"#; |
| 732 | 1× | let (files, _, _) = parse(net).unwrap(); |
| 733 | 1× | let f = &files[0]; |
| 734 | 1× | assert_eq!(f.path, "Lib/A.cs"); |
| 735 | 1× | assert_eq!((f.lines_found(), f.lines_hit()), (3, 2)); |
| 736 | 1× | assert_eq!((f.functions_found, f.functions_hit), (2, 1)); |
| 737 | |
|
| 738 | 1× | assert!(parse("<html/>").unwrap_err().contains("Cobertura")); |
| 739 | 1× | assert_eq!(parse("SF:/src/a.rs\nDA:1,1\nend_of_record\n").unwrap().2, "lcov"); |
| 740 | 1× | } |
| 741 | | } |