| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | | use crate::db::{now_ms, Pool}; |
| 8 | | use crate::Shared; |
| 9 | | use quick_xml::events::{BytesStart, Event}; |
| 10 | | use quick_xml::{Reader, XmlVersion}; |
| 11 | |
|
| 12 | |
|
| 13 | | pub const ARTIFACT: &str = "junit"; |
| 14 | |
|
| 15 | | pub const MAX_REPORT: usize = 64 << 20; |
| 16 | |
|
| 17 | | pub const MAX_CASES: usize = 50_000; |
| 18 | |
|
| 19 | | const MAX_MESSAGE: usize = 2 << 10; |
| 20 | | const MAX_OUTPUT: usize = 16 << 10; |
| 21 | |
|
| 22 | |
|
| 23 | | #[derive(Clone, Debug, PartialEq, Eq, sqlx::FromRow)] |
| 24 | | pub struct Case { |
| 25 | | |
| 26 | | pub suite: String, |
| 27 | | pub classname: String, |
| 28 | | pub name: String, |
| 29 | | |
| 30 | | pub status: String, |
| 31 | | pub time_ms: i64, |
| 32 | | |
| 33 | | pub message: String, |
| 34 | | |
| 35 | | |
| 36 | | pub output: String, |
| 37 | | } |
| 38 | |
|
| 39 | | impl Case { |
| 40 | | |
| 41 | 15× | pub fn title(&self) -> String { |
| 42 | 15× | if self.classname.is_empty() { |
| 43 | 0× | self.name.clone() |
| 44 | | } else { |
| 45 | 15× | format!("{}::{}", self.classname, self.name) |
| 46 | | } |
| 47 | 15× | } |
| 48 | | } |
| 49 | |
|
| 50 | |
|
| 51 | 13× | fn cut(mut s: String, max: usize) -> String { |
| 52 | 13× | if s.len() > max { |
| 53 | 2× | let mut i = max; |
| 54 | 2× | while !s.is_char_boundary(i) { |
| 55 | 0× | i -= 1; |
| 56 | 0× | } |
| 57 | 2× | s.truncate(i); |
| 58 | 2× | s.push_str("\n… (cut)"); |
| 59 | 11× | } |
| 60 | 13× | s |
| 61 | 13× | } |
| 62 | |
|
| 63 | 78× | fn attr(e: &BytesStart, name: &str) -> String { |
| 64 | 78× | e.try_get_attribute(name) |
| 65 | 78× | .ok() |
| 66 | 78× | .flatten() |
| 67 | 78× | .and_then(|a| a.normalized_value(XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) |
| 68 | 78× | .unwrap_or_default() |
| 69 | 78× | } |
| 70 | |
|
| 71 | |
|
| 72 | 21× | fn secs_ms(s: &str) -> i64 { |
| 73 | 21× | s.replace(',', "").trim().parse::<f64>().map(|v| (v * 1000.0).round().max(0.0) as i64).unwrap_or(0) |
| 74 | 21× | } |
| 75 | |
|
| 76 | |
|
| 77 | | #[derive(PartialEq)] |
| 78 | | enum Collect { |
| 79 | | Nothing, |
| 80 | | Detail, |
| 81 | | Out, |
| 82 | | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 11× | pub fn parse(text: &str) -> Result<Vec<Case>, String> { |
| 87 | 11× | let mut r = Reader::from_str(text); |
| 88 | 11× | r.config_mut().trim_text(false); |
| 89 | 11× | let mut suites: Vec<String> = Vec::new(); |
| 90 | 11× | let mut cases = Vec::new(); |
| 91 | 11× | let mut case: Option<Case> = None; |
| 92 | 11× | let mut detail = String::new(); |
| 93 | 11× | let mut out = String::new(); |
| 94 | 11× | let mut into = Collect::Nothing; |
| 95 | 11× | let mut seen_root = false; |
| 96 | | loop { |
| 97 | 107× | let ev = r.read_event().map_err(|e| format!("not JUnit XML: {e} (at byte {})", r.error_position()))?; |
| 98 | 7× | match ev { |
| 99 | 29× | Event::Start(ref e) | Event::Empty(ref e) |
| 100 | 29× | if e.local_name().as_ref() == "testsuites" || e.local_name().as_ref() == "testsuite" => |
| 101 | | { |
| 102 | 16× | seen_root = true; |
| 103 | 16× | if e.local_name().as_ref() == "testsuite" { |
| 104 | 9× | suites.push(attr(e, "name")); |
| 105 | 9× | } |
| 106 | | |
| 107 | 16× | if matches!(ev, Event::Empty(_)) && e.local_name().as_ref() == "testsuite" { |
| 108 | 0× | suites.pop(); |
| 109 | 16× | } |
| 110 | | } |
| 111 | 16× | Event::Start(ref e) | Event::Empty(ref e) if e.local_name().as_ref() == "testcase" => { |
| 112 | 21× | seen_root = true; |
| 113 | 21× | let c = Case { |
| 114 | 22× | suite: suites.iter().filter(|s| !s.is_empty()).cloned().collect::<Vec<_>>().join("/"), |
| 115 | 21× | classname: attr(e, "classname"), |
| 116 | 21× | name: attr(e, "name"), |
| 117 | 21× | status: "passed".into(), |
| 118 | 21× | time_ms: secs_ms(&attr(e, "time")), |
| 119 | 21× | message: String::new(), |
| 120 | 21× | output: String::new(), |
| 121 | | }; |
| 122 | 21× | if matches!(ev, Event::Empty(_)) { |
| 123 | 14× | cases.push(c); |
| 124 | 14× | } else { |
| 125 | 7× | case = Some(c); |
| 126 | 7× | detail.clear(); |
| 127 | 7× | out.clear(); |
| 128 | 7× | } |
| 129 | | } |
| 130 | 7× | Event::Start(ref e) | Event::Empty(ref e) if case.is_some() => { |
| 131 | 8× | let tag = e.local_name().as_ref().to_string(); |
| 132 | 8× | let c = case.as_mut().unwrap(); |
| 133 | 8× | let status = match tag.as_str() { |
| 134 | 8× | "failure" | "flakyFailure" if c.status == "passed" => Some("failed"), |
| 135 | 3× | "error" | "flakyError" if c.status == "passed" => Some("error"), |
| 136 | 2× | "skipped" if c.status == "passed" => Some("skipped"), |
| 137 | 1× | _ => None, |
| 138 | | }; |
| 139 | | |
| 140 | 8× | let flaky = tag.starts_with("flaky") || tag.starts_with("rerun"); |
| 141 | 8× | if let Some(s) = status.filter(|_| !flaky) { |
| 142 | 6× | c.status = s.into(); |
| 143 | 6× | c.message = attr(e, "message"); |
| 144 | 6× | if matches!(ev, Event::Start(_)) { |
| 145 | 5× | into = Collect::Detail; |
| 146 | 5× | } |
| 147 | 2× | } else if matches!(ev, Event::Start(_)) && (tag == "system-out" || tag == "system-err") { |
| 148 | 1× | into = Collect::Out; |
| 149 | 1× | } |
| 150 | | } |
| 151 | 17× | Event::Text(t) => push_text(&mut into, &mut detail, &mut out, &t.xml10_content()), |
| 152 | 1× | Event::CData(t) => push_text(&mut into, &mut detail, &mut out, &t.xml10_content()), |
| 153 | 2× | Event::GeneralRef(g) => { |
| 154 | 2× | let s = match g.resolve_char_ref() { |
| 155 | 0× | Ok(Some(ch)) => ch.to_string(), |
| 156 | | _ => { |
| 157 | 2× | let name = g.xml10_content(); |
| 158 | 2× | quick_xml::escape::resolve_predefined_entity(&name).map(str::to_string).unwrap_or_else(|| format!("&{name};")) |
| 159 | | } |
| 160 | | }; |
| 161 | 2× | push_text(&mut into, &mut detail, &mut out, &s); |
| 162 | | } |
| 163 | 29× | Event::End(e) => match e.local_name().as_ref() { |
| 164 | 29× | "testsuite" => { |
| 165 | 9× | suites.pop(); |
| 166 | 9× | } |
| 167 | 20× | "testcase" => { |
| 168 | 7× | if let Some(mut c) = case.take() { |
| 169 | 7× | if c.status != "passed" { |
| 170 | 6× | if c.message.is_empty() { |
| 171 | 1× | c.message = detail.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or("").to_string(); |
| 172 | 5× | } |
| 173 | 6× | let mut o = detail.trim().to_string(); |
| 174 | 6× | if !out.trim().is_empty() && c.status != "skipped" { |
| 175 | 1× | if !o.is_empty() { |
| 176 | 1× | o.push_str("\n\n"); |
| 177 | 1× | } |
| 178 | 1× | o.push_str(out.trim()); |
| 179 | 5× | } |
| 180 | 6× | c.output = cut(o, MAX_OUTPUT); |
| 181 | 1× | } |
| 182 | 7× | c.message = cut(c.message, MAX_MESSAGE); |
| 183 | 7× | cases.push(c); |
| 184 | 7× | if cases.len() > MAX_CASES { |
| 185 | 0× | return Err(format!("more than {MAX_CASES} test cases")); |
| 186 | 7× | } |
| 187 | 0× | } |
| 188 | 7× | into = Collect::Nothing; |
| 189 | | } |
| 190 | 13× | "failure" | "error" | "skipped" | "system-out" | "system-err" | "flakyFailure" | "flakyError" | "rerunFailure" |
| 191 | 7× | | "rerunError" => into = Collect::Nothing, |
| 192 | 6× | _ => {} |
| 193 | | }, |
| 194 | 10× | Event::Eof => break, |
| 195 | 2× | _ => {} |
| 196 | | } |
| 197 | | } |
| 198 | 10× | if !seen_root { |
| 199 | 1× | return Err("not JUnit XML: no <testsuites>, <testsuite> or <testcase>".into()); |
| 200 | 9× | } |
| 201 | 9× | Ok(cases) |
| 202 | 11× | } |
| 203 | |
|
| 204 | 20× | fn push_text(into: &mut Collect, detail: &mut String, out: &mut String, s: &str) { |
| 205 | 20× | let (buf, max) = match into { |
| 206 | 7× | Collect::Detail => (detail, MAX_OUTPUT), |
| 207 | 3× | Collect::Out => (out, MAX_OUTPUT), |
| 208 | 10× | Collect::Nothing => return, |
| 209 | | }; |
| 210 | | |
| 211 | 10× | if buf.len() <= max { |
| 212 | 10× | buf.push_str(s); |
| 213 | 10× | } |
| 214 | 20× | } |
| 215 | |
|
| 216 | |
|
| 217 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 218 | | pub struct Report { |
| 219 | | pub id: i64, |
| 220 | | pub repo_id: i64, |
| 221 | | pub run_id: i64, |
| 222 | | pub job_id: i64, |
| 223 | | pub job: String, |
| 224 | | pub run_number: i64, |
| 225 | | pub bookmark: String, |
| 226 | | pub state: String, |
| 227 | | pub blob: String, |
| 228 | | pub tests: i64, |
| 229 | | pub passed: i64, |
| 230 | | pub failed: i64, |
| 231 | | pub errors: i64, |
| 232 | | pub skipped: i64, |
| 233 | | pub time_ms: i64, |
| 234 | | pub created_ms: i64, |
| 235 | | } |
| 236 | |
|
| 237 | | impl Report { |
| 238 | | |
| 239 | 14× | pub fn summary(&self) -> String { |
| 240 | 14× | let mut bad = vec![]; |
| 241 | 14× | if self.failed > 0 { |
| 242 | 6× | bad.push(format!("{} failed", self.failed)); |
| 243 | 8× | } |
| 244 | 14× | if self.errors > 0 { |
| 245 | 0× | bad.push(format!("{} errored", self.errors)); |
| 246 | 14× | } |
| 247 | 14× | if self.skipped > 0 { |
| 248 | 0× | bad.push(format!("{} skipped", self.skipped)); |
| 249 | 14× | } |
| 250 | 14× | let tests = if self.tests == 1 { "1 test".to_string() } else { format!("{} tests", self.tests) }; |
| 251 | 14× | if bad.is_empty() { |
| 252 | 8× | format!("{tests}, all passed") |
| 253 | | } else { |
| 254 | 6× | format!("{tests}: {}", bad.join(", ")) |
| 255 | | } |
| 256 | 14× | } |
| 257 | |
|
| 258 | 47× | pub fn broken(&self) -> i64 { |
| 259 | 47× | self.failed + self.errors |
| 260 | 47× | } |
| 261 | | } |
| 262 | |
|
| 263 | |
|
| 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 blob: &'a str, |
| 273 | | } |
| 274 | |
|
| 275 | |
|
| 276 | 4× | pub async fn keep(app: &Shared, o: &Origin<'_>, cases: &[Case]) -> sqlx::Result<i64> { |
| 277 | 52× | let count = |s: &str| cases.iter().filter(|c| c.status == s).count() as i64; |
| 278 | 4× | let mut tx = crate::db::begin_write(&app.db).await?; |
| 279 | 4× | sqlx::query("DELETE FROM test_reports WHERE job_id = $1").bind(o.job_id).execute(&mut *tx).await?; |
| 280 | 4× | let id: i64 = sqlx::query_scalar( |
| 281 | 4× | "INSERT INTO test_reports (repo_id, run_id, job_id, job, run_number, bookmark, state, blob, \ |
| 282 | 4× | tests, passed, failed, errors, skipped, time_ms, created_ms) \ |
| 283 | 4× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING id", |
| 284 | | ) |
| 285 | 4× | .bind(o.repo_id) |
| 286 | 4× | .bind(o.run_id) |
| 287 | 4× | .bind(o.job_id) |
| 288 | 4× | .bind(o.job) |
| 289 | 4× | .bind(o.run_number) |
| 290 | 4× | .bind(o.bookmark) |
| 291 | 4× | .bind(o.state) |
| 292 | 4× | .bind(o.blob) |
| 293 | 4× | .bind(cases.len() as i64) |
| 294 | 4× | .bind(count("passed")) |
| 295 | 4× | .bind(count("failed")) |
| 296 | 4× | .bind(count("error")) |
| 297 | 4× | .bind(count("skipped")) |
| 298 | 4× | .bind(cases.iter().map(|c| c.time_ms).sum::<i64>()) |
| 299 | 4× | .bind(now_ms()) |
| 300 | 4× | .fetch_one(&mut *tx) |
| 301 | 4× | .await?; |
| 302 | 13× | for (i, c) in cases.iter().enumerate() { |
| 303 | 13× | sqlx::query( |
| 304 | 13× | "INSERT INTO test_cases (report_id, idx, suite, classname, name, status, time_ms, message, output) \ |
| 305 | 13× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", |
| 306 | 13× | ) |
| 307 | 13× | .bind(id) |
| 308 | 13× | .bind(i as i64) |
| 309 | 13× | .bind(&c.suite) |
| 310 | 13× | .bind(&c.classname) |
| 311 | 13× | .bind(&c.name) |
| 312 | 13× | .bind(&c.status) |
| 313 | 13× | .bind(c.time_ms) |
| 314 | 13× | .bind(&c.message) |
| 315 | 13× | .bind(&c.output) |
| 316 | 13× | .execute(&mut *tx) |
| 317 | 13× | .await?; |
| 318 | | } |
| 319 | 4× | tx.commit().await?; |
| 320 | 4× | mark(app, o.repo_id); |
| 321 | 4× | Ok(id) |
| 322 | 4× | } |
| 323 | |
|
| 324 | |
|
| 325 | 26× | pub async fn for_run(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Report>> { |
| 326 | 26× | sqlx::query_as("SELECT * FROM test_reports WHERE run_id = $1 ORDER BY job").bind(run_id).fetch_all(pool).await |
| 327 | 26× | } |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | 2× | pub async fn cases(pool: &Pool, report_id: i64, statuses: &[&str], limit: i64) -> sqlx::Result<Vec<Case>> { |
| 332 | 2× | let filter = if statuses.is_empty() { |
| 333 | 1× | String::new() |
| 334 | | } else { |
| 335 | | |
| 336 | 2× | format!(" AND status IN ({})", (2..2 + statuses.len()).map(|i| format!("${i}")).collect::<Vec<_>>().join(", ")) |
| 337 | | }; |
| 338 | 2× | let limit_at = 2 + statuses.len(); |
| 339 | 2× | let sql = format!( |
| 340 | | "SELECT suite, classname, name, status, time_ms, message, output FROM test_cases WHERE report_id = $1{filter} ORDER BY idx LIMIT ${limit_at}" |
| 341 | | ); |
| 342 | 2× | let mut q = sqlx::query_as(&sql).bind(report_id); |
| 343 | 2× | for s in statuses { |
| 344 | 2× | q = q.bind(*s); |
| 345 | 2× | } |
| 346 | 2× | q.bind(limit).fetch_all(pool).await |
| 347 | 2× | } |
| 348 | |
|
| 349 | |
|
| 350 | 4× | pub async fn failures(pool: &Pool, report_id: i64, limit: i64) -> sqlx::Result<Vec<Case>> { |
| 351 | 4× | sqlx::query_as( |
| 352 | 4× | "SELECT suite, classname, name, status, time_ms, message, output FROM test_cases \ |
| 353 | 4× | WHERE report_id = $1 AND status IN ('failed', 'error') ORDER BY idx LIMIT $2", |
| 354 | 4× | ) |
| 355 | 4× | .bind(report_id) |
| 356 | 4× | .bind(limit) |
| 357 | 4× | .fetch_all(pool) |
| 358 | 4× | .await |
| 359 | 4× | } |
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | | static HAS: std::sync::Mutex<Option<std::collections::HashSet<String>>> = std::sync::Mutex::new(None); |
| 364 | |
|
| 365 | 9× | fn key(app: &Shared, repo_id: i64) -> String { |
| 366 | 9× | format!("{}#{repo_id}", app.repos.root().display()) |
| 367 | 9× | } |
| 368 | |
|
| 369 | 517× | pub fn has_reports(app: &Shared, repo_id: i64) -> bool { |
| 370 | 517× | HAS.lock().unwrap_or_else(|p| p.into_inner()).as_ref().is_some_and(|s| s.contains(&key(app, repo_id))) |
| 371 | 517× | } |
| 372 | |
|
| 373 | 4× | fn mark(app: &Shared, repo_id: i64) { |
| 374 | 4× | HAS.lock().unwrap_or_else(|p| p.into_inner()).get_or_insert_with(Default::default).insert(key(app, repo_id)); |
| 375 | 4× | } |
| 376 | |
|
| 377 | |
|
| 378 | 189× | pub async fn load(app: &Shared) -> sqlx::Result<()> { |
| 379 | 189× | let ids: Vec<(i64,)> = sqlx::query_as("SELECT DISTINCT repo_id FROM test_reports").fetch_all(&app.db).await?; |
| 380 | 189× | for (id,) in ids { |
| 381 | 0× | mark(app, id); |
| 382 | 0× | } |
| 383 | 189× | Ok(()) |
| 384 | 189× | } |
| 385 | |
|
| 386 | 4× | pub async fn report(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Report>> { |
| 387 | 4× | sqlx::query_as("SELECT * FROM test_reports WHERE id = $1 AND repo_id = $2").bind(id).bind(repo_id).fetch_optional(pool).await |
| 388 | 4× | } |
| 389 | |
|
| 390 | |
|
| 391 | 6× | pub async fn on_bookmark(pool: &Pool, repo_id: i64, bookmark: &str, limit: i64) -> sqlx::Result<Vec<Report>> { |
| 392 | 6× | sqlx::query_as("SELECT * FROM test_reports WHERE repo_id = $1 AND bookmark = $2 ORDER BY created_ms DESC, id DESC LIMIT $3") |
| 393 | 6× | .bind(repo_id) |
| 394 | 6× | .bind(bookmark) |
| 395 | 6× | .bind(limit) |
| 396 | 6× | .fetch_all(pool) |
| 397 | 6× | .await |
| 398 | 6× | } |
| 399 | |
|
| 400 | |
|
| 401 | 5× | pub async fn bookmarks(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 402 | 5× | let rows: Vec<(String,)> = |
| 403 | 5× | sqlx::query_as("SELECT bookmark FROM test_reports WHERE repo_id = $1 GROUP BY bookmark ORDER BY MAX(created_ms) DESC") |
| 404 | 5× | .bind(repo_id) |
| 405 | 5× | .fetch_all(pool) |
| 406 | 5× | .await?; |
| 407 | 5× | Ok(rows.into_iter().map(|(b,)| b).collect()) |
| 408 | 5× | } |
| 409 | |
|
| 410 | 3× | pub async fn first_ms(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<i64>> { |
| 411 | 3× | sqlx::query_scalar("SELECT MIN(created_ms) FROM test_reports WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await |
| 412 | 3× | } |
| 413 | |
|
| 414 | |
|
| 415 | 3× | pub async fn slowest(pool: &Pool, report_id: i64, limit: i64) -> sqlx::Result<Vec<Case>> { |
| 416 | 3× | sqlx::query_as( |
| 417 | 3× | "SELECT suite, classname, name, status, time_ms, message, '' AS output FROM test_cases \ |
| 418 | 3× | WHERE report_id = $1 AND status != 'skipped' ORDER BY time_ms DESC, idx LIMIT $2", |
| 419 | 3× | ) |
| 420 | 3× | .bind(report_id) |
| 421 | 3× | .bind(limit) |
| 422 | 3× | .fetch_all(pool) |
| 423 | 3× | .await |
| 424 | 3× | } |
| 425 | |
|
| 426 | |
|
| 427 | 6× | pub async fn statuses(pool: &Pool, report_id: i64) -> sqlx::Result<std::collections::HashMap<(String, String), String>> { |
| 428 | 6× | let rows: Vec<(String, String, String)> = |
| 429 | 6× | sqlx::query_as("SELECT classname, name, status FROM test_cases WHERE report_id = $1").bind(report_id).fetch_all(pool).await?; |
| 430 | 23× | Ok(rows.into_iter().map(|(c, n, s)| ((c, n), s)).collect()) |
| 431 | 6× | } |
| 432 | |
|
| 433 | |
|
| 434 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 435 | | pub struct Run { |
| 436 | | pub report_id: i64, |
| 437 | | pub run_number: i64, |
| 438 | | pub job: String, |
| 439 | | pub created_ms: i64, |
| 440 | | pub status: String, |
| 441 | | pub time_ms: i64, |
| 442 | | pub message: String, |
| 443 | | } |
| 444 | |
|
| 445 | 2× | pub async fn history(pool: &Pool, repo_id: i64, bookmark: &str, classname: &str, name: &str, limit: i64) -> sqlx::Result<Vec<Run>> { |
| 446 | 2× | sqlx::query_as( |
| 447 | 2× | "SELECT r.id AS report_id, r.run_number, r.job, r.created_ms, c.status, c.time_ms, c.message \ |
| 448 | 2× | FROM test_cases c JOIN test_reports r ON r.id = c.report_id \ |
| 449 | 2× | WHERE r.repo_id = $1 AND r.bookmark = $2 AND c.classname = $3 AND c.name = $4 \ |
| 450 | 2× | ORDER BY r.created_ms DESC, r.id DESC LIMIT $5", |
| 451 | 2× | ) |
| 452 | 2× | .bind(repo_id) |
| 453 | 2× | .bind(bookmark) |
| 454 | 2× | .bind(classname) |
| 455 | 2× | .bind(name) |
| 456 | 2× | .bind(limit) |
| 457 | 2× | .fetch_all(pool) |
| 458 | 2× | .await |
| 459 | 2× | } |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | 4× | pub async fn baseline(pool: &Pool, r: &Report, default_bookmark: &str) -> sqlx::Result<Option<Report>> { |
| 464 | 4× | sqlx::query_as( |
| 465 | 4× | "SELECT * FROM test_reports WHERE repo_id = $1 AND bookmark = $2 AND job = $3 AND id != $4 \ |
| 466 | 4× | AND (created_ms < $5 OR (created_ms = $6 AND id < $7)) ORDER BY created_ms DESC, id DESC LIMIT 1", |
| 467 | | ) |
| 468 | 4× | .bind(r.repo_id) |
| 469 | 4× | .bind(default_bookmark) |
| 470 | 4× | .bind(&r.job) |
| 471 | 4× | .bind(r.id) |
| 472 | 4× | .bind(if r.bookmark == default_bookmark { r.created_ms } else { i64::MAX }) |
| 473 | 4× | .bind(r.created_ms) |
| 474 | 4× | .bind(if r.bookmark == default_bookmark { r.id } else { i64::MAX }) |
| 475 | 4× | .fetch_optional(pool) |
| 476 | 4× | .await |
| 477 | 4× | } |
| 478 | |
|
| 479 | |
|
| 480 | 1× | pub async fn newly_failing(pool: &Pool, report: i64, base: i64) -> sqlx::Result<Vec<String>> { |
| 481 | 1× | let rows: Vec<(String, String)> = sqlx::query_as( |
| 482 | 1× | "SELECT c.classname, c.name FROM test_cases c JOIN test_cases b \ |
| 483 | 1× | ON b.report_id = $1 AND b.classname = c.classname AND b.name = c.name AND b.status = 'passed' \ |
| 484 | 1× | WHERE c.report_id = $2 AND c.status IN ('failed', 'error') ORDER BY c.idx", |
| 485 | 1× | ) |
| 486 | 1× | .bind(base) |
| 487 | 1× | .bind(report) |
| 488 | 1× | .fetch_all(pool) |
| 489 | 1× | .await?; |
| 490 | 1× | Ok(rows.into_iter().map(|(c, n)| if c.is_empty() { n } else { format!("{c}::{n}") }).collect()) |
| 491 | 1× | } |
| 492 | |
|
| 493 | |
|
| 494 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 495 | | pub struct Flaky { |
| 496 | | pub classname: String, |
| 497 | | pub name: String, |
| 498 | | pub failed: i64, |
| 499 | | pub passed: i64, |
| 500 | | } |
| 501 | |
|
| 502 | | impl Flaky { |
| 503 | 3× | pub fn title(&self) -> String { |
| 504 | 3× | if self.classname.is_empty() { |
| 505 | 0× | self.name.clone() |
| 506 | | } else { |
| 507 | 3× | format!("{}::{}", self.classname, self.name) |
| 508 | | } |
| 509 | 3× | } |
| 510 | | } |
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | 3× | pub async fn flaky(pool: &Pool, reports: &[i64], limit: i64) -> sqlx::Result<Vec<Flaky>> { |
| 515 | 3× | if reports.is_empty() { |
| 516 | 0× | return Ok(vec![]); |
| 517 | 3× | } |
| 518 | 9× | let ids = reports.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(","); |
| 519 | 3× | sqlx::query_as(&format!( |
| 520 | 3× | "SELECT classname, name, failed, passed FROM (\ |
| 521 | 3× | SELECT classname, name, SUM(CASE WHEN status IN ('failed', 'error') THEN 1 ELSE 0 END) AS failed, \ |
| 522 | 3× | SUM(CASE WHEN status = 'passed' THEN 1 ELSE 0 END) AS passed \ |
| 523 | 3× | FROM test_cases WHERE report_id IN ({ids}) GROUP BY classname, name) t \ |
| 524 | 3× | WHERE failed > 0 AND passed > 0 ORDER BY failed DESC, classname, name LIMIT $1" |
| 525 | 3× | )) |
| 526 | 3× | .bind(limit) |
| 527 | 3× | .fetch_all(pool) |
| 528 | 3× | .await |
| 529 | 3× | } |
| 530 | |
|
| 531 | | #[cfg(test)] |
| 532 | | mod tests { |
| 533 | | use super::*; |
| 534 | |
|
| 535 | | #[test] |
| 536 | 1× | fn pytest_nextest_and_jest_shapes() { |
| 537 | | |
| 538 | 1× | let py = r#"<?xml version="1.0" encoding="utf-8"?> |
| 539 | 1× | <testsuites><testsuite name="pytest" errors="1" failures="1" skipped="1" tests="4" time="3.5"> |
| 540 | 1× | <testcase classname="tests.test_a" name="test_ok" time="0.010"/> |
| 541 | 1× | <testcase classname="tests.test_a" name="test_bad" time="1.25"><failure message="assert 1 == 2">def test_bad(): |
| 542 | 1× | > assert 1 == 2 |
| 543 | 1× | E assert 1 == 2</failure><system-out>printed & kept</system-out></testcase> |
| 544 | 1× | <testcase classname="tests.test_b" name="test_skip" time="0"><skipped type="pytest.skip" message="no emulator"/></testcase> |
| 545 | 1× | <testcase classname="tests.test_b" name="test_err" time="0.5"><error message="fixture failed"><![CDATA[Traceback <here>]]></error></testcase> |
| 546 | 1× | </testsuite></testsuites>"#; |
| 547 | 1× | let c = parse(py).unwrap(); |
| 548 | 4× | let st: Vec<(&str, &str)> = c.iter().map(|c| (c.name.as_str(), c.status.as_str())).collect(); |
| 549 | 1× | assert_eq!(st, [("test_ok", "passed"), ("test_bad", "failed"), ("test_skip", "skipped"), ("test_err", "error")]); |
| 550 | 1× | assert_eq!(c[1].message, "assert 1 == 2"); |
| 551 | 1× | assert!(c[1].output.contains("> assert 1 == 2") && c[1].output.contains("printed & kept"), "{}", c[1].output); |
| 552 | 1× | assert_eq!((c[1].time_ms, c[0].time_ms), (1250, 10)); |
| 553 | 1× | assert_eq!(c[2].message, "no emulator"); |
| 554 | 1× | assert_eq!(c[3].output, "Traceback <here>"); |
| 555 | 1× | assert_eq!(c[1].suite, "pytest"); |
| 556 | 1× | assert_eq!(c[1].title(), "tests.test_a::test_bad"); |
| 557 | 1× | assert!(c[0].output.is_empty()); |
| 558 | |
|
| 559 | | |
| 560 | 1× | let nx = r#"<testsuites name="nextest-run"><testsuite name="oxy-core" tests="2"> |
| 561 | 1× | <testcase name="repo::tests::a" classname="oxy-core::lib" time="0.2"><flakyFailure message="first try" type="test failure">boom</flakyFailure></testcase> |
| 562 | 1× | <testcase name="repo::tests::b" classname="oxy-core::lib" time="0.1"/></testsuite></testsuites>"#; |
| 563 | 1× | let c = parse(nx).unwrap(); |
| 564 | 2× | assert_eq!(c.iter().filter(|c| c.status == "passed").count(), 2, "{c:?}"); |
| 565 | |
|
| 566 | | |
| 567 | 1× | let js = r#"<testsuite name="ui"><testsuite name="Button"><testcase classname="Button renders" name="renders" time="0.003"/></testsuite></testsuite>"#; |
| 568 | 1× | let c = parse(js).unwrap(); |
| 569 | 1× | assert_eq!(c[0].suite, "ui/Button"); |
| 570 | |
|
| 571 | 1× | assert!(parse("<html/>").is_err()); |
| 572 | 1× | assert!(parse("not xml <<<").is_err()); |
| 573 | 1× | assert!(parse("<testsuites/>").unwrap().is_empty()); |
| 574 | 1× | } |
| 575 | |
|
| 576 | | #[test] |
| 577 | 1× | fn long_output_is_cut() { |
| 578 | 1× | let big = "x".repeat(MAX_OUTPUT * 3); |
| 579 | 1× | let xml = format!(r#"<testsuite><testcase name="t"><failure>{big}</failure></testcase></testsuite>"#); |
| 580 | 1× | let c = parse(&xml).unwrap(); |
| 581 | 1× | assert!(c[0].output.len() < MAX_OUTPUT + 32 && c[0].output.ends_with("(cut)")); |
| 582 | 1× | } |
| 583 | | } |