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