use std::borrow::Cow;
use std::sync::LazyLock;
use regex::RegexSet;
use super::{TRUNCATED_MIDDLE, omitted_lines, push_line};
const FAILURE_EXCERPT_BYTES: usize = 12_000;
const BODY_BUDGET: usize = FAILURE_EXCERPT_BYTES - 600;
const LINE_BUDGET: usize = BODY_BUDGET - 800;
const MAX_LINE_BYTES: usize = 2_000;
const LEAD_LINES: usize = 2;
const TRAIL_LINES: usize = 14;
const TAIL_LINES: usize = 60;
const VERDICT_LINES: usize = 5;
static FAILURE_LINES: LazyLock<RegexSet> = LazyLock::new(|| {
RegexSet::new([
r"^test .* \.\.\. FAILED",
r"^test result: FAILED",
r"^---- .* stdout ----",
r"^error(\[E\d+\])?:",
r"^thread '.*' panicked",
r"^E\s{2,}\S",
r"^_{3,} .* _{3,}$",
r"^FAILED \S",
r"^Traceback \(most recent call last\)",
r"^[\w.]*Error:",
r"^\s*AssertionError",
r"^panic: ",
r"^--- FAIL: ",
r"^\s*● ",
r"^error TS\d+:",
r"^\S+\(\d+,\d+\): error TS\d+:",
r"^\s*\d+:\d+\s+error\s",
r"^\S+\.\w+:\d+(:\d+)?: error\b",
r"^Diff in \S+",
r"^would reformat ",
r"^(FAILED|FAIL)\b",
r"^not ok \d+",
r"\*\*\* .*Error \d+",
r"^fatal: ",
])
.expect("the failure-line patterns are valid")
});
static WEAK_LINES: LazyLock<RegexSet> = LazyLock::new(|| {
RegexSet::new([
r"^failures:",
r"^=+ FAILURES",
r"^\S+\.\w+:\d+(:\d+)?: ",
r"^\[warn\] \S",
])
.expect("the weak-marker patterns are valid")
});
static LOOSE_LINES: LazyLock<RegexSet> = LazyLock::new(|| {
RegexSet::new([r"(?i)\b(error|failed|failure|panic|assert|assertion|exception|fatal)\b"])
.expect("the loose failure-line pattern is valid")
});
#[must_use]
pub(crate) fn failure_excerpt(output: &str) -> String {
if output.len() <= FAILURE_EXCERPT_BYTES {
return output.to_string();
}
let lines: Vec<&str> = output.lines().collect();
let accepted = select(&lines);
let mut body = String::new();
let mut shown = 0usize;
let mut omitted = 0usize;
for kept in accepted {
let Some(text) = kept else {
omitted += 1;
continue;
};
let marker = (omitted > 0).then(|| omitted_lines(omitted, "omitted"));
let cost = text.len() + 1 + marker.as_ref().map_or(0, |marker| marker.len() + 1);
if body.len() + cost > BODY_BUDGET {
omitted += 1;
continue;
}
if let Some(marker) = marker {
push_line(&mut body, &marker);
}
omitted = 0;
push_line(&mut body, &text);
shown += 1;
}
if omitted > 0 {
push_line(&mut body, &omitted_lines(omitted, "omitted"));
}
format!("{}\n{body}", header(shown, lines.len()))
}
fn header(shown: usize, total: usize) -> String {
format!(
"[failure excerpt: {shown} of {total} lines — the lines that name a failure and the run's \
end, where they fit; the failure may not be among them]"
)
}
fn select<'a>(lines: &[&'a str]) -> Vec<Option<Cow<'a, str>>> {
let strong = windowed(lines, &FAILURE_LINES);
let weak = windowed(lines, &WEAK_LINES);
let names = strong.iter().any(|kept| *kept);
let points = weak.iter().any(|kept| *kept);
let mut accepted = vec![None; lines.len()];
let mut used = 0usize;
for (index, line) in lines.iter().enumerate() {
if line.trim() == TRUNCATED_MIDDLE {
accepted[index] = Some(Cow::Borrowed(*line));
used += line.len() + 1;
}
}
let verdict = if names || points {
VERDICT_LINES
} else {
TAIL_LINES
};
accept(&tail_mask(lines, verdict), &mut accepted, &mut used, lines);
if names {
accept(&strong, &mut accepted, &mut used, lines);
accept(&weak, &mut accepted, &mut used, lines);
} else if points {
accept(&weak, &mut accepted, &mut used, lines);
} else {
accept(
&windowed(lines, &LOOSE_LINES),
&mut accepted,
&mut used,
lines,
);
}
accepted
}
fn windowed(lines: &[&str], patterns: &RegexSet) -> Vec<bool> {
let mut keep = vec![false; lines.len()];
for (index, line) in lines.iter().enumerate() {
if patterns.is_match(line) {
let start = index.saturating_sub(LEAD_LINES);
let end = (index + TRAIL_LINES + 1).min(lines.len());
keep[start..end].fill(true);
}
}
keep
}
fn accept<'a>(
mask: &[bool],
accepted: &mut [Option<Cow<'a, str>>],
used: &mut usize,
lines: &[&'a str],
) {
let marked: Vec<usize> = mask
.iter()
.enumerate()
.filter_map(|(index, marked)| marked.then_some(index))
.collect();
let mut front = 0usize;
let mut back = marked.len();
let mut from_front = true;
while front < back {
let index = if from_front {
front += 1;
marked[front - 1]
} else {
back -= 1;
marked[back]
};
from_front = !from_front;
if accepted[index].is_some() {
continue;
}
let line = lines[index];
let text = if line.len() > MAX_LINE_BYTES {
Cow::Owned(cut_line(line, MAX_LINE_BYTES))
} else {
Cow::Borrowed(line)
};
let remaining = LINE_BUDGET.saturating_sub(*used);
if text.len() < remaining {
*used += text.len() + 1;
accepted[index] = Some(text);
continue;
}
let head_bytes = remaining.saturating_sub(1);
if line.floor_char_boundary(head_bytes) == 0 {
break;
}
accepted[index] = Some(Cow::Owned(cut_line(line, head_bytes)));
*used = LINE_BUDGET;
break;
}
}
fn cut_line(line: &str, bytes: usize) -> String {
let head = &line[..line.floor_char_boundary(bytes)];
format!(
"{head}… ({} more chars on this line)",
line[head.len()..].chars().count()
)
}
fn tail_mask(lines: &[&str], count: usize) -> Vec<bool> {
let mut mask = vec![false; lines.len()];
let mut used = 0usize;
for (index, line) in lines.iter().enumerate().rev().take(count) {
used += line.len().min(MAX_LINE_BYTES) + 1;
if used > LINE_BUDGET {
break;
}
mask[index] = true;
}
mask
}
#[cfg(test)]
mod tests {
use super::*;
fn noise(prefix: &str, count: usize) -> String {
(1..=count)
.map(|i| format!("{prefix} {i} of the run"))
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn short_output_is_returned_whole() {
let output = "error: broken\n --> src/lib.rs:1:1";
assert_eq!(failure_excerpt(output), output);
}
#[test]
fn a_compiler_error_keeps_its_span_and_code_frame() {
let output = format!(
"{}\nerror[E0308]: mismatched types\n --> src/main.rs:3:9\n |\n\
3 | let x: u8 = \"s\";\n | ^^^^^^ expected `u8`, found `&str`\n{}",
noise("Compiling", 300),
noise("Finished", 300),
);
let excerpt = failure_excerpt(&output);
for needle in [
"error[E0308]: mismatched types",
"--> src/main.rs:3:9",
"3 | let x: u8",
"expected `u8`, found `&str`",
] {
assert!(excerpt.contains(needle), "{excerpt} lacks {needle:?}");
}
}
#[test]
fn a_format_check_diff_is_named_by_its_header() {
let mut lines = vec!["Diff in /repo/src/main.rs:1:".to_string()];
lines.extend((1..=600).map(|i| format!(" unchanged context line {i}")));
let output = lines.join("\n");
let excerpt = failure_excerpt(&output);
assert!(
excerpt.contains("Diff in /repo/src/main.rs:1:"),
"{excerpt}"
);
assert!(excerpt.starts_with("[failure excerpt:"), "{excerpt}");
}
#[test]
fn nothing_named_keeps_the_end_of_the_output() {
let excerpt = failure_excerpt(&noise("ok", 1000));
assert!(excerpt.starts_with("[failure excerpt:"), "{excerpt}");
assert!(excerpt.contains("ok 1000 of the run"), "{excerpt}");
assert!(!excerpt.contains("\nok 1 of the run"), "{excerpt}");
}
#[test]
fn a_tail_too_long_for_the_budget_keeps_its_very_end() {
let output = (1..=100)
.map(|i| format!("ok {i} {}", "x".repeat(300)))
.collect::<Vec<_>>()
.join("\n");
let excerpt = failure_excerpt(&output);
assert!(excerpt.contains("ok 100 "), "the last line is kept");
assert!(
!excerpt.contains("\nok 1 x"),
"the front of the tail is what goes"
);
}
#[test]
fn the_excerpt_is_bounded_whatever_names_the_failure() {
let wall = (1..=5000)
.map(|i| format!("error: failure {i}"))
.collect::<Vec<_>>()
.join("\n");
let mut scattered = Vec::new();
for i in 1..=200 {
scattered.push(format!("error: failure {i}"));
scattered.extend((1..=40).map(|j| format!("noise {j} around failure {i}")));
}
for (output, last) in [
(wall, "error: failure 5000"),
(scattered.join("\n"), "error: failure 200"),
] {
let excerpt = failure_excerpt(&output);
assert!(
excerpt.len() <= FAILURE_EXCERPT_BYTES,
"{} bytes",
excerpt.len()
);
assert!(excerpt.contains("error: failure 1"), "{excerpt}");
assert!(excerpt.contains(last), "{excerpt} lacks {last:?}");
assert!(excerpt.contains("lines omitted"), "{excerpt}");
}
}
#[test]
fn a_failing_test_in_the_middle_survives_its_surroundings() {
let output = format!(
"{}\n---- test_add stdout ----\n\
thread 'test_add' panicked at src/lib.rs:10:5:\n\
assertion `left == right` failed\n left: 1\n right: 2\n\
test result: FAILED. 800 passed; 1 failed\n\
error: test failed, to rerun pass `--lib`\n{}",
noise("checking", 400),
noise("cleanup", 400),
);
let excerpt = failure_excerpt(&output);
for needle in [
"---- test_add stdout ----",
"assertion `left == right` failed",
"left: 1",
"test result: FAILED. 800 passed; 1 failed",
"error: test failed, to rerun pass `--lib`",
] {
assert!(excerpt.contains(needle), "{excerpt} lacks {needle:?}");
}
assert!(!excerpt.contains("checking 1 of the run"), "{excerpt}");
assert!(!excerpt.contains("cleanup 100 of the run"), "{excerpt}");
assert!(excerpt.contains("lines omitted"), "{excerpt}");
assert!(excerpt.starts_with("[failure excerpt:"), "{excerpt}");
}
#[test]
fn a_verdict_the_patterns_do_not_know_is_kept() {
let mut quiet = vec!["note: failed to load optional plugin config, continuing".to_string()];
quiet.extend((1..=600).map(|i| format!("step {i} of the build")));
let mut incidental = vec![
"thread 'idle_worker' panicked at src/pool.rs:4:5: the worker gave up".to_string(),
];
incidental.extend((1..=600).map(|i| format!("step {i} of the build")));
for mut output in [quiet, incidental] {
output.push("BUILD BROKEN: cannot find module 'left-pad'".to_string());
let excerpt = failure_excerpt(&output.join("\n"));
assert!(
excerpt.contains("BUILD BROKEN: cannot find module 'left-pad'"),
"{excerpt}"
);
}
}
#[test]
fn a_wall_of_locations_does_not_spend_the_verdict() {
let mut lines: Vec<String> = (1..=600)
.map(|i| format!("src/app.js:{}:3: warning: unused variable `v`", i + 10))
.collect();
lines.extend((1..=60).map(|i| format!("step {i} of the build")));
lines.push("BUILD BROKEN: cannot find module 'left-pad'".to_string());
let excerpt = failure_excerpt(&lines.join("\n"));
assert!(
excerpt.contains("BUILD BROKEN: cannot find module 'left-pad'"),
"{excerpt}"
);
}
#[test]
fn a_wall_of_warnings_does_not_spend_the_errors_room() {
let output = format!(
"{}\nerror[E0308]: mismatched types\n --> src/main.rs:3:9\n |\n\
3 | let x: u8 = \"s\";\n | ^^^^^^ expected `u8`, found `&str`\n{}",
noise("[warn] unused variable", 400),
noise("[warn] unused import", 400),
);
let excerpt = failure_excerpt(&output);
for needle in [
"error[E0308]: mismatched types",
"--> src/main.rs:3:9",
"expected `u8`, found `&str`",
] {
assert!(excerpt.contains(needle), "{excerpt} lacks {needle:?}");
}
}
#[test]
fn a_line_too_long_for_the_budget_keeps_its_head() {
let long = format!("error: the module failed to load: {}", "x".repeat(20_000));
let output = format!(
"{}\n{long}\n\nerror: the second thing that broke\n{}",
noise("Compiling", 300),
noise("Finished", 300),
);
let excerpt = failure_excerpt(&output);
assert!(
excerpt.contains("error: the module failed to load"),
"{excerpt}"
);
assert!(
excerpt.contains("error: the second thing that broke"),
"{excerpt}"
);
assert!(
excerpt.contains("more chars on this line"),
"the cut says how much: {excerpt}"
);
assert!(
excerpt.len() <= FAILURE_EXCERPT_BYTES,
"{} bytes",
excerpt.len()
);
}
#[test]
fn many_failures_keep_the_runs_verdict() {
let mut lines = Vec::new();
for i in 1..=100 {
lines.extend([
format!("test case_{i} ... FAILED"),
format!("---- case_{i} stdout ----"),
format!("thread 'case_{i}' panicked at src/lib.rs:{i}:5:"),
"assertion `left == right` failed".to_string(),
" left: 1".to_string(),
" right: 2".to_string(),
]);
}
lines.push("test result: FAILED. 0 passed; 100 failed".to_string());
let excerpt = failure_excerpt(&lines.join("\n"));
for needle in [
"test case_1 ... FAILED",
"test result: FAILED. 0 passed; 100 failed",
] {
assert!(excerpt.contains(needle), "{excerpt} lacks {needle:?}");
}
}
#[test]
fn a_capture_gap_is_kept_wherever_the_failure_sits() {
let output = format!(
"{}\n{}\n{}\n---- test_x stdout ----\n\
thread 'test_x' panicked at src/lib.rs:1:1:\nassertion failed\n",
noise("checking", 500),
crate::util::TRUNCATED_MIDDLE,
noise("still checking", 500),
);
let excerpt = failure_excerpt(&output);
assert!(excerpt.contains("was not captured"), "{excerpt}");
assert!(excerpt.contains("thread 'test_x' panicked"), "{excerpt}");
}
}