| 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 | 11× | pub fn lines_found(&self) -> u64 { |
| 38 | 11× | self.declared_lines.map(|d| d.0).unwrap_or(self.lines.len() as u64) |
| 39 | 11× | } |
| 40 | |
|
| 41 | 11× | pub fn lines_hit(&self) -> u64 { |
| 42 | 37× | self.declared_lines.map(|d| d.1).unwrap_or_else(|| self.lines.values().filter(|n| **n > 0).count() as u64) |
| 43 | 11× | } |
| 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 | 10× | pub fn parse(text: &str) -> Result<(Vec<FileCoverage>, usize, &'static str), String> { |
| 177 | 10× | let t = text.trim_start(); |
| 178 | 10× | if t.starts_with('<') && root_element(t).as_deref() == Some("report") { |
| 179 | 1× | parse_jacoco(text).map(|(f, o)| (f, o, "jacoco")) |
| 180 | 9× | } else if t.starts_with('<') { |
| 181 | 3× | parse_cobertura(text).map(|(f, o)| (f, o, "cobertura")) |
| 182 | | } else { |
| 183 | 6× | parse_lcov(text).map(|(f, o)| (f, o, "lcov")) |
| 184 | | } |
| 185 | 10× | } |
| 186 | |
|
| 187 | |
|
| 188 | 4× | fn root_element(text: &str) -> Option<String> { |
| 189 | | use quick_xml::events::Event; |
| 190 | 4× | let mut r = quick_xml::Reader::from_str(text); |
| 191 | | loop { |
| 192 | 8× | match r.read_event().ok()? { |
| 193 | 4× | Event::Start(e) | Event::Empty(e) => return Some(e.local_name().as_ref().to_string()), |
| 194 | 0× | Event::Eof => return None, |
| 195 | 4× | _ => {} |
| 196 | | } |
| 197 | | } |
| 198 | 4× | } |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | 2× | pub fn parse_jacoco(text: &str) -> Result<(Vec<FileCoverage>, usize), String> { |
| 208 | | use quick_xml::events::Event; |
| 209 | 2× | let mut r = quick_xml::Reader::from_str(text); |
| 210 | 2× | let mut files: BTreeMap<String, Acc> = BTreeMap::new(); |
| 211 | 2× | let mut package = String::new(); |
| 212 | 2× | let mut sourcefile: Option<String> = None; |
| 213 | 2× | let mut class_file: Option<String> = None; |
| 214 | 2× | let mut method: Option<(String, bool)> = None; |
| 215 | 2× | let mut seen_root = false; |
| 216 | 2× | let path = |package: &str, name: &str| if package.is_empty() { name.to_string() } else { format!("{package}/{name}") }; |
| 217 | | loop { |
| 218 | 26× | let ev = r.read_event().map_err(|e| format!("this is not a JaCoCo report: {e}"))?; |
| 219 | 26× | match ev { |
| 220 | 8× | Event::Start(ref e) | Event::Empty(ref e) => { |
| 221 | 15× | let tag = e.local_name().as_ref().to_string(); |
| 222 | 15× | match tag.as_str() { |
| 223 | 15× | "report" => seen_root = true, |
| 224 | 13× | "package" => package = xml_attr(e, "name").trim_matches('/').to_string(), |
| 225 | 12× | "class" => { |
| 226 | 1× | let f = xml_attr(e, "sourcefilename"); |
| 227 | 1× | class_file = (!f.is_empty()).then(|| path(&package, &f)); |
| 228 | | } |
| 229 | 11× | "method" if class_file.is_some() => method = Some((xml_attr(e, "name") + &xml_attr(e, "desc"), false)), |
| 230 | 9× | "counter" => { |
| 231 | 4× | if let Some((_, hit)) = &mut method { |
| 232 | 3× | if xml_attr(e, "type") == "METHOD" { |
| 233 | 2× | *hit |= xml_attr(e, "covered").trim().parse::<u64>().unwrap_or(0) > 0; |
| 234 | 2× | } |
| 235 | 1× | } |
| 236 | | } |
| 237 | 5× | "sourcefile" => { |
| 238 | 1× | let f = path(&package, &xml_attr(e, "name")); |
| 239 | 1× | files.entry(f.clone()).or_default().records += 1; |
| 240 | 1× | sourcefile = Some(f); |
| 241 | 1× | } |
| 242 | 4× | "line" => { |
| 243 | 3× | let Some(f) = &sourcefile else { continue }; |
| 244 | 3× | let Ok(n) = xml_attr(e, "nr").trim().parse::<u32>() else { continue }; |
| 245 | 9× | let num = |a: &str| xml_attr(e, a).trim().parse::<u64>().unwrap_or(0); |
| 246 | 3× | let acc = files.entry(f.clone()).or_default(); |
| 247 | 3× | *acc.lines.entry(n).or_default() += num("ci"); |
| 248 | 3× | let (missed, covered) = (num("mb"), num("cb")); |
| 249 | 3× | for i in 0..missed + covered { |
| 250 | 2× | let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default(); |
| 251 | 2× | *b = (*b).max(u64::from(i < covered)); |
| 252 | 2× | } |
| 253 | | } |
| 254 | 1× | _ => {} |
| 255 | | } |
| 256 | 15× | if matches!(ev, Event::Empty(_)) && tag == "method" { |
| 257 | 0× | if let (Some(f), Some((name, hit))) = (&class_file, method.take()) { |
| 258 | 0× | *files.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 259 | 0× | } |
| 260 | 15× | } |
| 261 | | } |
| 262 | 7× | Event::End(e) => match e.local_name().as_ref() { |
| 263 | 7× | "package" => package.clear(), |
| 264 | 6× | "sourcefile" => sourcefile = None, |
| 265 | 5× | "class" => class_file = None, |
| 266 | 4× | "method" => { |
| 267 | 2× | if let (Some(f), Some((name, hit))) = (&class_file, method.take()) { |
| 268 | 2× | *files.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 269 | 2× | } |
| 270 | | } |
| 271 | 2× | _ => {} |
| 272 | | }, |
| 273 | 2× | Event::Eof => break, |
| 274 | 2× | _ => {} |
| 275 | | } |
| 276 | | } |
| 277 | 2× | if !seen_root { |
| 278 | 0× | return Err("this is not a JaCoCo report: no <report> element".into()); |
| 279 | 2× | } |
| 280 | 2× | let out: Vec<FileCoverage> = files |
| 281 | 2× | .into_iter() |
| 282 | 2× | .filter(|(_, a)| a.records > 0) |
| 283 | 2× | .map(|(path, a)| FileCoverage { |
| 284 | 1× | path, |
| 285 | 1× | functions_found: a.functions.len() as u64, |
| 286 | 2× | functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64, |
| 287 | 1× | branches_found: a.branches.len() as u64, |
| 288 | 2× | branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64, |
| 289 | 1× | lines: a.lines, |
| 290 | 1× | declared_lines: None, |
| 291 | 1× | }) |
| 292 | 2× | .collect(); |
| 293 | 2× | if out.is_empty() { |
| 294 | 1× | return Err("this JaCoCo report names no source file (no <sourcefile>)".into()); |
| 295 | 1× | } |
| 296 | 1× | Ok((out, 0)) |
| 297 | 2× | } |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | 5× | pub fn resolve_endings(files: Vec<FileCoverage>, in_state: &HashSet<String>) -> Vec<FileCoverage> { |
| 304 | 5× | files |
| 305 | 5× | .into_iter() |
| 306 | 8× | .map(|mut f| { |
| 307 | 8× | if !in_state.contains(&f.path) { |
| 308 | 5× | let tail = format!("/{}", f.path); |
| 309 | 14× | let mut found = in_state.iter().filter(|p| p.ends_with(&tail)); |
| 310 | 5× | if let (Some(p), None) = (found.next(), found.next()) { |
| 311 | 1× | f.path = p.clone(); |
| 312 | 4× | } |
| 313 | 3× | } |
| 314 | 8× | f |
| 315 | 8× | }) |
| 316 | 5× | .collect() |
| 317 | 5× | } |
| 318 | |
|
| 319 | 58× | fn xml_attr(e: &quick_xml::events::BytesStart, name: &str) -> String { |
| 320 | 58× | e.try_get_attribute(name) |
| 321 | 58× | .ok() |
| 322 | 58× | .flatten() |
| 323 | 58× | .and_then(|a| a.normalized_value(quick_xml::XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) |
| 324 | 58× | .unwrap_or_default() |
| 325 | 58× | } |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | 3× | pub fn parse_cobertura(text: &str) -> Result<(Vec<FileCoverage>, usize), String> { |
| 335 | | use quick_xml::events::Event; |
| 336 | 3× | let mut r = quick_xml::Reader::from_str(text); |
| 337 | 3× | let mut sources: Vec<String> = Vec::new(); |
| 338 | 3× | let mut in_source = false; |
| 339 | 3× | let mut source_text = String::new(); |
| 340 | | |
| 341 | 3× | let mut raw: BTreeMap<String, Acc> = BTreeMap::new(); |
| 342 | 3× | let mut file: Option<String> = None; |
| 343 | 3× | let mut method: Option<(String, bool)> = None; |
| 344 | 3× | let mut seen_root = false; |
| 345 | | loop { |
| 346 | 86× | let ev = r.read_event().map_err(|e| format!("this is not a Cobertura report: {e}"))?; |
| 347 | 20× | match ev { |
| 348 | 25× | Event::Start(ref e) | Event::Empty(ref e) => { |
| 349 | 37× | let tag = e.local_name().as_ref().to_string(); |
| 350 | 37× | match tag.as_str() { |
| 351 | 37× | "coverage" => seen_root = true, |
| 352 | 35× | "source" if matches!(ev, Event::Start(_)) => { |
| 353 | 2× | in_source = true; |
| 354 | 2× | source_text.clear(); |
| 355 | 2× | } |
| 356 | 33× | "class" => { |
| 357 | 4× | let f = xml_attr(e, "filename"); |
| 358 | 4× | raw.entry(f.clone()).or_default().records += 1; |
| 359 | 4× | file = Some(f); |
| 360 | 4× | } |
| 361 | 29× | "method" => method = Some((xml_attr(e, "name") + &xml_attr(e, "signature"), false)), |
| 362 | 27× | "line" => { |
| 363 | 9× | let Some(f) = &file else { continue }; |
| 364 | 9× | let Ok(n) = xml_attr(e, "number").trim().parse::<u32>() else { continue }; |
| 365 | 9× | let hits = xml_attr(e, "hits").trim().parse::<u64>().unwrap_or(0); |
| 366 | 9× | if let Some((_, hit)) = &mut method { |
| 367 | 2× | *hit |= hits > 0; |
| 368 | | |
| 369 | 2× | continue; |
| 370 | 7× | } |
| 371 | 7× | let acc = raw.entry(f.clone()).or_default(); |
| 372 | 7× | *acc.lines.entry(n).or_default() += hits; |
| 373 | 7× | if xml_attr(e, "branch") == "true" { |
| 374 | | |
| 375 | 1× | let cc = xml_attr(e, "condition-coverage"); |
| 376 | 1× | if let Some((hit, total)) = cc |
| 377 | 1× | .split_once('(') |
| 378 | 1× | .and_then(|(_, rest)| rest.trim_end_matches(')').split_once('/')) |
| 379 | 1× | .and_then(|(h, t)| Some((h.trim().parse::<u64>().ok()?, t.trim().parse::<u64>().ok()?))) |
| 380 | | { |
| 381 | 2× | for i in 0..total { |
| 382 | 2× | let b = acc.branches.entry((n, "0".into(), i.to_string())).or_default(); |
| 383 | 2× | *b = (*b).max(u64::from(i < hit)); |
| 384 | 2× | } |
| 385 | 0× | } |
| 386 | 6× | } |
| 387 | | } |
| 388 | 18× | _ => {} |
| 389 | | } |
| 390 | 35× | if matches!(ev, Event::Empty(_)) && tag == "method" { |
| 391 | 0× | if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
| 392 | 0× | *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 393 | 0× | } |
| 394 | 35× | } |
| 395 | | } |
| 396 | 2× | Event::Text(t) if in_source => source_text.push_str(&t.xml10_content()), |
| 397 | 25× | Event::End(e) => match e.local_name().as_ref() { |
| 398 | 25× | "source" => { |
| 399 | 2× | in_source = false; |
| 400 | 2× | sources.push(source_text.trim().trim_end_matches('/').to_string()); |
| 401 | 2× | } |
| 402 | 23× | "class" => file = None, |
| 403 | 19× | "method" => { |
| 404 | 2× | if let (Some(f), Some((name, hit))) = (&file, method.take()) { |
| 405 | 2× | *raw.entry(f.clone()).or_default().functions.entry(name).or_default() += u64::from(hit); |
| 406 | 2× | } |
| 407 | | } |
| 408 | 17× | _ => {} |
| 409 | | }, |
| 410 | 3× | Event::Eof => break, |
| 411 | 19× | _ => {} |
| 412 | | } |
| 413 | | } |
| 414 | 3× | if !seen_root { |
| 415 | 1× | return Err("this is not a Cobertura report: no <coverage> element".into()); |
| 416 | 2× | } |
| 417 | 2× | if raw.is_empty() { |
| 418 | 0× | return Err("this Cobertura report names no source file (no <class filename>)".into()); |
| 419 | 2× | } |
| 420 | | |
| 421 | 3× | let resolve = |f: &str| -> Option<String> { |
| 422 | 3× | if f.starts_with('/') { |
| 423 | 1× | return relative(f); |
| 424 | 2× | } |
| 425 | 2× | sources.iter().filter(|s| !s.is_empty() && *s != ".").find_map(|s| relative(&format!("{s}/{f}"))).or_else(|| relative(f)) |
| 426 | 3× | }; |
| 427 | 2× | let mut files: BTreeMap<String, Acc> = BTreeMap::new(); |
| 428 | 2× | let mut outside = HashSet::new(); |
| 429 | 3× | for (f, a) in raw { |
| 430 | 3× | let Some(path) = resolve(&f) else { |
| 431 | 1× | outside.insert(f); |
| 432 | 1× | continue; |
| 433 | | }; |
| 434 | 2× | let into = files.entry(path).or_default(); |
| 435 | 6× | for (l, n) in a.lines { |
| 436 | 6× | *into.lines.entry(l).or_default() += n; |
| 437 | 6× | } |
| 438 | 2× | for (k, n) in a.functions { |
| 439 | 2× | *into.functions.entry(k).or_default() += n; |
| 440 | 2× | } |
| 441 | 2× | for (k, n) in a.branches { |
| 442 | 2× | let b = into.branches.entry(k).or_default(); |
| 443 | 2× | *b = (*b).max(n); |
| 444 | 2× | } |
| 445 | | } |
| 446 | 2× | let out = files |
| 447 | 2× | .into_iter() |
| 448 | 2× | .map(|(path, a)| FileCoverage { |
| 449 | 2× | path, |
| 450 | 2× | functions_found: a.functions.len() as u64, |
| 451 | 2× | functions_hit: a.functions.values().filter(|n| **n > 0).count() as u64, |
| 452 | 2× | branches_found: a.branches.len() as u64, |
| 453 | 2× | branches_hit: a.branches.values().filter(|n| **n > 0).count() as u64, |
| 454 | 2× | lines: a.lines, |
| 455 | 2× | declared_lines: None, |
| 456 | 2× | }) |
| 457 | 2× | .collect(); |
| 458 | 2× | Ok((out, outside.len())) |
| 459 | 3× | } |
| 460 | |
|
| 461 | |
|
| 462 | 60× | pub fn percent(hit: i64, found: i64) -> Option<f64> { |
| 463 | 60× | (found > 0).then(|| (hit as f64 * 1000.0 / found as f64).round() / 10.0) |
| 464 | 60× | } |
| 465 | |
|
| 466 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 467 | | pub struct Report { |
| 468 | | pub id: i64, |
| 469 | | pub repo_id: i64, |
| 470 | | |
| 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 | | |
| 491 | | pub uploader: String, |
| 492 | | } |
| 493 | |
|
| 494 | | impl Report { |
| 495 | | |
| 496 | 12× | pub fn uploaded(&self) -> bool { |
| 497 | 12× | !self.uploader.is_empty() |
| 498 | 12× | } |
| 499 | |
|
| 500 | | |
| 501 | 4× | pub fn source(&self) -> String { |
| 502 | 4× | if self.uploaded() { |
| 503 | 3× | format!("uploaded by {}", self.uploader) |
| 504 | | } else { |
| 505 | 1× | format!("run #{}", self.run_number) |
| 506 | | } |
| 507 | 4× | } |
| 508 | |
|
| 509 | | |
| 510 | 4× | pub fn hits_per_line(&self) -> Option<f64> { |
| 511 | 4× | (self.lines_found > 0).then(|| self.hits as f64 / self.lines_found as f64) |
| 512 | 4× | } |
| 513 | |
|
| 514 | 36× | pub fn percent(&self) -> Option<f64> { |
| 515 | 36× | percent(self.lines_hit, self.lines_found) |
| 516 | 36× | } |
| 517 | |
|
| 518 | 1× | pub fn functions_percent(&self) -> Option<f64> { |
| 519 | 1× | percent(self.functions_hit, self.functions_found) |
| 520 | 1× | } |
| 521 | |
|
| 522 | 0× | pub fn branches_percent(&self) -> Option<f64> { |
| 523 | 0× | percent(self.branches_hit, self.branches_found) |
| 524 | 0× | } |
| 525 | |
|
| 526 | 1× | pub fn head_list(&self) -> Vec<&str> { |
| 527 | 1× | self.heads.split(',').filter(|h| !h.is_empty()).collect() |
| 528 | 1× | } |
| 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 | 21× | pub fn percent(&self) -> Option<f64> { |
| 545 | 21× | percent(self.lines_hit, self.lines_found) |
| 546 | 21× | } |
| 547 | |
|
| 548 | 2× | pub fn missed(&self) -> i64 { |
| 549 | 2× | self.lines_found - self.lines_hit |
| 550 | 2× | } |
| 551 | | } |
| 552 | |
|
| 553 | |
|
| 554 | |
|
| 555 | |
|
| 556 | | static HAS: Mutex<Option<HashSet<String>>> = Mutex::new(None); |
| 557 | |
|
| 558 | 8× | fn key(app: &Shared, repo_id: i64) -> String { |
| 559 | 8× | format!("{}#{repo_id}", app.repos.root().display()) |
| 560 | 8× | } |
| 561 | |
|
| 562 | 540× | pub fn has_reports(app: &Shared, repo_id: i64) -> bool { |
| 563 | 540× | HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id))) |
| 564 | 540× | } |
| 565 | |
|
| 566 | 3× | fn mark(app: &Shared, repo_id: i64) { |
| 567 | 3× | HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(HashSet::new).insert(key(app, repo_id)); |
| 568 | 3× | } |
| 569 | |
|
| 570 | |
|
| 571 | 208× | pub async fn load(app: &Shared) -> sqlx::Result<()> { |
| 572 | 208× | let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM coverage_reports").fetch_all(&app.db).await?; |
| 573 | 208× | for (id,) in ids { |
| 574 | 0× | mark(app, id); |
| 575 | 0× | } |
| 576 | 208× | Ok(()) |
| 577 | 208× | } |
| 578 | |
|
| 579 | |
|
| 580 | | pub struct Origin<'a> { |
| 581 | | pub repo_id: i64, |
| 582 | | |
| 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 | | |
| 593 | | pub format: &'a str, |
| 594 | | } |
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | 3× | pub async fn keep(app: &Shared, o: &Origin<'_>, files: Vec<FileCoverage>, outside: usize, in_state: &HashSet<String>) -> sqlx::Result<i64> { |
| 599 | 3× | let files = resolve_endings(files, in_state); |
| 600 | 6× | let (kept, missing): (Vec<FileCoverage>, Vec<FileCoverage>) = files.into_iter().partition(|f| in_state.contains(&f.path)); |
| 601 | 18× | let sum = |f: &dyn Fn(&FileCoverage) -> u64| kept.iter().map(f).sum::<u64>() as i64; |
| 602 | 3× | let mut tx = crate::db::begin_write(&app.db).await?; |
| 603 | 3× | if let Some(job) = o.job_id { |
| 604 | 2× | sqlx::query("DELETE FROM coverage_reports WHERE job_id = $1").bind(job).execute(&mut *tx).await?; |
| 605 | 1× | } |
| 606 | 3× | let id: i64 = sqlx::query_scalar( |
| 607 | 3× | "INSERT INTO coverage_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, heads, blob, format, \ |
| 608 | 3× | lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit, hits, files, dropped, created_ms, uploader) \ |
| 609 | 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", |
| 610 | | ) |
| 611 | 3× | .bind(o.repo_id) |
| 612 | 3× | .bind(o.run_id) |
| 613 | 3× | .bind(o.job_id) |
| 614 | 3× | .bind(o.job) |
| 615 | 3× | .bind(o.run_number) |
| 616 | 3× | .bind(o.bookmark) |
| 617 | 3× | .bind(o.state) |
| 618 | 3× | .bind(o.heads.join(",")) |
| 619 | 3× | .bind(o.blob) |
| 620 | 3× | .bind(o.format) |
| 621 | 3× | .bind(sum(&|f| f.lines_found())) |
| 622 | 3× | .bind(sum(&|f| f.lines_hit())) |
| 623 | 3× | .bind(sum(&|f| f.functions_found)) |
| 624 | 3× | .bind(sum(&|f| f.functions_hit)) |
| 625 | 3× | .bind(sum(&|f| f.branches_found)) |
| 626 | 3× | .bind(sum(&|f| f.branches_hit)) |
| 627 | 3× | .bind(kept.iter().fold(0u64, |a, f| a.saturating_add(f.hits())).min(i64::MAX as u64) as i64) |
| 628 | 3× | .bind(kept.len() as i64) |
| 629 | 3× | .bind((missing.len() + outside) as i64) |
| 630 | 3× | .bind(now_ms()) |
| 631 | 3× | .bind(o.uploader) |
| 632 | 3× | .fetch_one(&mut *tx) |
| 633 | 3× | .await?; |
| 634 | 3× | for f in &kept { |
| 635 | 3× | sqlx::query( |
| 636 | 3× | "INSERT INTO coverage_files (report_id, path, lines_found, lines_hit, functions_found, functions_hit, branches_found, branches_hit) \ |
| 637 | 3× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", |
| 638 | 3× | ) |
| 639 | 3× | .bind(id) |
| 640 | 3× | .bind(&f.path) |
| 641 | 3× | .bind(f.lines_found() as i64) |
| 642 | 3× | .bind(f.lines_hit() as i64) |
| 643 | 3× | .bind(f.functions_found as i64) |
| 644 | 3× | .bind(f.functions_hit as i64) |
| 645 | 3× | .bind(f.branches_found as i64) |
| 646 | 3× | .bind(f.branches_hit as i64) |
| 647 | 3× | .execute(&mut *tx) |
| 648 | 3× | .await?; |
| 649 | | } |
| 650 | 3× | tx.commit().await?; |
| 651 | 3× | mark(app, o.repo_id); |
| 652 | 3× | Ok(id) |
| 653 | 3× | } |
| 654 | |
|
| 655 | 6× | pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Report>> { |
| 656 | 6× | sqlx::query_as("SELECT * FROM coverage_reports WHERE id = $1 AND repo_id = $2").bind(id).bind(repo_id).fetch_optional(pool).await |
| 657 | 6× | } |
| 658 | |
|
| 659 | |
|
| 660 | 5× | pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result<Vec<Report>> { |
| 661 | 5× | sqlx::query_as("SELECT * FROM coverage_reports WHERE repo_id = $1 AND bookmark = $2 ORDER BY created_ms DESC, id DESC LIMIT $3") |
| 662 | 5× | .bind(repo_id) |
| 663 | 5× | .bind(bookmark) |
| 664 | 5× | .bind(limit) |
| 665 | 5× | .fetch_all(pool) |
| 666 | 5× | .await |
| 667 | 5× | } |
| 668 | |
|
| 669 | |
|
| 670 | 2× | pub async fn first_ms(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<i64>> { |
| 671 | 2× | sqlx::query_scalar("SELECT MIN(created_ms) FROM coverage_reports WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await |
| 672 | 2× | } |
| 673 | |
|
| 674 | |
|
| 675 | 4× | pub async fn bookmarks(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 676 | 4× | sqlx::query_scalar("SELECT bookmark FROM coverage_reports WHERE repo_id = $1 GROUP BY bookmark ORDER BY MAX(created_ms) DESC") |
| 677 | 4× | .bind(repo_id) |
| 678 | 4× | .fetch_all(pool) |
| 679 | 4× | .await |
| 680 | 4× | } |
| 681 | |
|
| 682 | |
|
| 683 | | #[derive(Clone, Copy, Debug, Default, PartialEq)] |
| 684 | | pub struct Rules { |
| 685 | | |
| 686 | | pub min: Option<f64>, |
| 687 | | |
| 688 | | pub max_drop: Option<f64>, |
| 689 | | } |
| 690 | |
|
| 691 | 135× | pub async fn rules(pool: &Pool, repo_id: i64) -> sqlx::Result<Rules> { |
| 692 | 135× | let row: Option<(Option<f64>, Option<f64>)> = |
| 693 | 135× | sqlx::query_as("SELECT min, max_drop FROM coverage_settings WHERE repo_id = $1").bind(repo_id).fetch_optional(pool).await?; |
| 694 | 135× | Ok(row.map(|(min, max_drop)| Rules { min, max_drop }).unwrap_or_default()) |
| 695 | 135× | } |
| 696 | |
|
| 697 | 2× | pub async fn set_rules(pool: &Pool, repo_id: i64, r: Rules) -> sqlx::Result<()> { |
| 698 | 2× | sqlx::query( |
| 699 | 2× | "INSERT INTO coverage_settings (repo_id, min, max_drop) VALUES ($1, $2, $3) \ |
| 700 | 2× | ON CONFLICT (repo_id) DO UPDATE SET min = excluded.min, max_drop = excluded.max_drop", |
| 701 | 2× | ) |
| 702 | 2× | .bind(repo_id) |
| 703 | 2× | .bind(r.min) |
| 704 | 2× | .bind(r.max_drop) |
| 705 | 2× | .execute(pool) |
| 706 | 2× | .await?; |
| 707 | 2× | Ok(()) |
| 708 | 2× | } |
| 709 | |
|
| 710 | |
|
| 711 | |
|
| 712 | 2× | pub async fn baseline(pool: &Pool, r: &Report, default_bookmark: &str) -> sqlx::Result<Option<Report>> { |
| 713 | 2× | sqlx::query_as( |
| 714 | 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)) \ |
| 715 | 2× | ORDER BY created_ms DESC, id DESC LIMIT 1", |
| 716 | 2× | ) |
| 717 | 2× | .bind(r.repo_id) |
| 718 | 2× | .bind(default_bookmark) |
| 719 | 2× | .bind(r.id) |
| 720 | 2× | .bind(r.created_ms) |
| 721 | 2× | .bind(r.created_ms) |
| 722 | 2× | .bind(r.id) |
| 723 | 2× | .fetch_optional(pool) |
| 724 | 2× | .await |
| 725 | 2× | } |
| 726 | |
|
| 727 | |
|
| 728 | 8× | pub fn verdict(r: &Report, base: Option<&Report>, base_name: &str, rules: Rules) -> (&'static str, String) { |
| 729 | 8× | let Some(p) = r.percent() else { return ("passed", "no instrumented lines".into()) }; |
| 730 | 8× | let delta = base.and_then(|b| Some(p - b.percent()?)); |
| 731 | 8× | let against = match delta { |
| 732 | 5× | Some(d) if d.abs() < 0.05 => format!(", unchanged against {base_name}"), |
| 733 | 4× | Some(d) => format!(" ({d:+.1} against {base_name})"), |
| 734 | 3× | None => String::new(), |
| 735 | | }; |
| 736 | 8× | if let Some(min) = rules.min.filter(|m| p < *m) { |
| 737 | 2× | return ("failed", format!("{p:.1}%{against}: below the minimum of {min}%")); |
| 738 | 6× | } |
| 739 | 6× | if let (Some(max), Some(d)) = (rules.max_drop, delta) { |
| 740 | 2× | if d < -max { |
| 741 | 1× | return ("failed", format!("{p:.1}%{against}: falls more than the {max} points allowed")); |
| 742 | 1× | } |
| 743 | 4× | } |
| 744 | 5× | ("passed", format!("{p:.1}%{against}")) |
| 745 | 8× | } |
| 746 | |
|
| 747 | 24× | pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Report>> { |
| 748 | 24× | sqlx::query_as("SELECT * FROM coverage_reports WHERE run_id = $1 ORDER BY job").bind(run_id).fetch_all(pool).await |
| 749 | 24× | } |
| 750 | |
|
| 751 | 5× | pub async fn files(pool: &Pool, report_id: i64) -> sqlx::Result<Vec<FileRow>> { |
| 752 | 5× | sqlx::query_as("SELECT * FROM coverage_files WHERE report_id = $1 ORDER BY path").bind(report_id).fetch_all(pool).await |
| 753 | 5× | } |
| 754 | |
|
| 755 | | #[cfg(test)] |
| 756 | | mod tests { |
| 757 | | use super::*; |
| 758 | |
|
| 759 | | |
| 760 | | |
| 761 | | |
| 762 | | #[test] |
| 763 | 1× | fn a_jacoco_report_is_read_and_its_paths_found() { |
| 764 | 1× | 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 | 1× | let (files, outside, format) = parse(xml).unwrap(); |
| 766 | 1× | assert_eq!((format, outside, files.len()), ("jacoco", 0, 1)); |
| 767 | 1× | let f = &files[0]; |
| 768 | 1× | assert_eq!(f.path, "io/oxygenhub/android/api/Links.kt"); |
| 769 | 1× | assert_eq!((f.lines_found(), f.lines_hit()), (3, 1)); |
| 770 | 1× | assert_eq!((f.branches_found, f.branches_hit), (2, 1)); |
| 771 | 1× | assert_eq!((f.functions_found, f.functions_hit), (2, 1)); |
| 772 | 1× | let state: HashSet<String> = |
| 773 | 1× | ["app/src/main/java/io/oxygenhub/android/api/Links.kt".to_string(), "README.md".to_string()].into_iter().collect(); |
| 774 | 1× | let found = resolve_endings(files, &state); |
| 775 | 1× | assert_eq!(found[0].path, "app/src/main/java/io/oxygenhub/android/api/Links.kt"); |
| 776 | | |
| 777 | 1× | let twice: HashSet<String> = ["a/x/Links.kt".to_string(), "b/x/Links.kt".to_string()].into_iter().collect(); |
| 778 | 1× | let left = resolve_endings(vec![FileCoverage { path: "x/Links.kt".into(), ..Default::default() }], &twice); |
| 779 | 1× | assert_eq!(left[0].path, "x/Links.kt"); |
| 780 | 1× | assert!(parse_jacoco("<report></report>").is_err(), "no source file"); |
| 781 | 1× | } |
| 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 | 1× | fn lcov_reports_are_read_and_merged() { |
| 791 | 1× | let (files, outside) = parse_lcov(LCOV).unwrap(); |
| 792 | 1× | assert_eq!(outside, 1, "the dependency outside the workspace"); |
| 793 | 1× | assert_eq!(files.len(), 2); |
| 794 | 1× | let lib = &files[1]; |
| 795 | 1× | assert_eq!(lib.path, "crates/a/src/lib.rs"); |
| 796 | 1× | assert_eq!(lib.lines, [(3, 4), (4, 4), (9, 1), (10, 0)].into_iter().collect()); |
| 797 | 1× | assert_eq!((lib.lines_found(), lib.lines_hit()), (4, 3), "the second record ran line 9"); |
| 798 | 1× | assert_eq!((lib.functions_found, lib.functions_hit), (2, 1)); |
| 799 | 1× | assert_eq!((lib.branches_found, lib.branches_hit), (2, 1)); |
| 800 | 1× | assert_eq!(files[0].path, "README.md"); |
| 801 | 1× | assert_eq!(lib.declared_lines, None, "two records: counted from the lines"); |
| 802 | | |
| 803 | 1× | let (one, _) = parse_lcov("SF:/src/a.rs\nDA:1,1\nDA:2,0\nLF:5\nLH:3\nend_of_record\n").unwrap(); |
| 804 | 1× | assert_eq!((one[0].lines_found(), one[0].lines_hit()), (5, 3)); |
| 805 | 1× | assert!(parse_lcov("not a report\n").unwrap_err().contains("not an LCOV report")); |
| 806 | 1× | } |
| 807 | |
|
| 808 | | #[test] |
| 809 | 1× | fn the_check_follows_the_rules() { |
| 810 | 1× | let report = |pct_hit: i64| Report { |
| 811 | | id: 2, |
| 812 | | repo_id: 1, |
| 813 | 2× | run_id: Some(1), |
| 814 | 2× | job_id: Some(1), |
| 815 | 2× | job: "test".into(), |
| 816 | | run_number: 1, |
| 817 | 2× | bookmark: "feature".into(), |
| 818 | 2× | state: String::new(), |
| 819 | 2× | heads: String::new(), |
| 820 | 2× | blob: String::new(), |
| 821 | 2× | format: "lcov".into(), |
| 822 | | lines_found: 1000, |
| 823 | 2× | 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 | 2× | uploader: String::new(), |
| 833 | 2× | }; |
| 834 | 1× | let (now, before) = (report(819), report(822)); |
| 835 | 1× | let none = Rules::default(); |
| 836 | 1× | assert_eq!(verdict(&now, Some(&before), "main", none), ("passed", "81.9% (-0.3 against main)".to_string())); |
| 837 | 1× | assert_eq!(verdict(&now, None, "main", none), ("passed", "81.9%".to_string())); |
| 838 | 1× | assert_eq!(verdict(&now, Some(&now), "main", none).1, "81.9%, unchanged against main"); |
| 839 | 1× | let strict = Rules { min: Some(85.0), max_drop: None }; |
| 840 | 1× | assert_eq!( |
| 841 | 1× | verdict(&now, Some(&before), "main", strict), |
| 842 | 1× | ("failed", "81.9% (-0.3 against main): below the minimum of 85%".to_string()) |
| 843 | | ); |
| 844 | 1× | let drop = Rules { min: None, max_drop: Some(0.2) }; |
| 845 | 1× | assert_eq!(verdict(&now, Some(&before), "main", drop).0, "failed"); |
| 846 | 1× | assert_eq!(verdict(&now, Some(&before), "main", Rules { min: None, max_drop: Some(0.5) }).0, "passed"); |
| 847 | 1× | } |
| 848 | |
|
| 849 | | #[test] |
| 850 | 1× | fn paths_are_taken_relative_to_the_workspace() { |
| 851 | 1× | assert_eq!(relative("/src/a/b.rs").as_deref(), Some("a/b.rs")); |
| 852 | 1× | assert_eq!(relative("./a/b.rs").as_deref(), Some("a/b.rs")); |
| 853 | 1× | assert_eq!(relative("a/b.rs").as_deref(), Some("a/b.rs")); |
| 854 | 6× | for bad in ["/etc/passwd", "a/../b", "", "/src/", "a//b", "C:\\x\\y.rs"] { |
| 855 | 6× | assert_eq!(relative(bad), None, "{bad}"); |
| 856 | | } |
| 857 | 1× | assert_eq!(percent(1, 3), Some(33.3)); |
| 858 | 1× | assert_eq!(percent(0, 0), None); |
| 859 | 1× | } |
| 860 | |
|
| 861 | | #[test] |
| 862 | 1× | fn cobertura_from_coverage_py_and_coverlet() { |
| 863 | | |
| 864 | 1× | let py = r#"<?xml version="1.0" ?> |
| 865 | 1× | <coverage version="7.4" line-rate="0.6" branch-rate="0.5"> |
| 866 | 1× | <sources><source>/src/app</source></sources> |
| 867 | 1× | <packages><package name="app"><classes> |
| 868 | 1× | <class name="main.py" filename="main.py" line-rate="0.66"> |
| 869 | 1× | <methods/> |
| 870 | 1× | <lines> |
| 871 | 1× | <line number="1" hits="1"/> |
| 872 | 1× | <line number="2" hits="0"/> |
| 873 | 1× | <line number="3" hits="4" branch="true" condition-coverage="50% (1/2)"/> |
| 874 | 1× | </lines> |
| 875 | 1× | </class> |
| 876 | 1× | <class name="gone.py" filename="/usr/lib/python3/x.py"><lines><line number="1" hits="1"/></lines></class> |
| 877 | 1× | </classes></package></packages> |
| 878 | 1× | </coverage>"#; |
| 879 | 1× | let (files, outside, format) = parse(py).unwrap(); |
| 880 | 1× | assert_eq!((format, outside), ("cobertura", 1)); |
| 881 | 1× | let f = &files[0]; |
| 882 | 1× | assert_eq!(f.path, "app/main.py"); |
| 883 | 1× | assert_eq!((f.lines_found(), f.lines_hit(), f.hits()), (3, 2, 5)); |
| 884 | 1× | assert_eq!((f.branches_found, f.branches_hit), (2, 1)); |
| 885 | |
|
| 886 | | |
| 887 | 1× | let net = r#"<coverage><sources><source>/src/</source></sources><packages><package name="Lib"><classes> |
| 888 | 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> |
| 889 | 1× | <lines><line number="5" hits="2"/><line number="9" hits="0"/></lines></class> |
| 890 | 1× | <class name="Lib.A/Inner" filename="Lib/A.cs"><methods/><lines><line number="12" hits="1"/></lines></class> |
| 891 | 1× | </classes></package></packages></coverage>"#; |
| 892 | 1× | let (files, _, _) = parse(net).unwrap(); |
| 893 | 1× | let f = &files[0]; |
| 894 | 1× | assert_eq!(f.path, "Lib/A.cs"); |
| 895 | 1× | assert_eq!((f.lines_found(), f.lines_hit()), (3, 2)); |
| 896 | 1× | assert_eq!((f.functions_found, f.functions_hit), (2, 1)); |
| 897 | |
|
| 898 | 1× | assert!(parse("<html/>").unwrap_err().contains("Cobertura")); |
| 899 | 1× | assert_eq!(parse("SF:/src/a.rs\nDA:1,1\nend_of_record\n").unwrap().2, "lcov"); |
| 900 | 1× | } |
| 901 | | } |