use crate::compress::caps::{cap_classified_blocks, ClassifiedBlock, DropClass};
use crate::compress::generic::GenericCompressor;
use crate::compress::line_cut::ensure_final_lines;
use crate::compress::{CompressionResult, Compressor};
pub struct CargoCompressor;
impl Compressor for CargoCompressor {
fn matches(&self, command: &str) -> bool {
command
.split_whitespace()
.next()
.is_some_and(|head| head == "cargo")
}
fn compress_with_exit_code(
&self,
command: &str,
output: &str,
exit_code: Option<i64>,
) -> CompressionResult {
if let Some(raw) = short_output_passthrough(output) {
return raw;
}
match cargo_subcommand(command).as_deref() {
Some("build" | "check" | "clippy") => compress_build_like(output),
Some("test") => compress_test(output, exit_code),
_ => GenericCompressor::compress_output(output).into(),
}
}
fn matches_output(&self, output: &str) -> bool {
output.lines().any(is_cargo_test_signature_line)
}
fn compress_output_match_with_exit_code(
&self,
output: &str,
exit_code: Option<i64>,
) -> CompressionResult {
if let Some(raw) = short_output_passthrough(output) {
return raw;
}
compress_test(output, exit_code)
}
}
const SHORT_OUTPUT_BYTES: usize = 1024;
fn short_output_passthrough(output: &str) -> Option<CompressionResult> {
(output.len() <= SHORT_OUTPUT_BYTES).then(|| CompressionResult::new(output.to_string()))
}
fn is_cargo_test_signature_line(line: &str) -> bool {
line.starts_with("test result:")
|| line.starts_with("failures:")
|| (line.starts_with("---- ") && line.ends_with(" stdout ----"))
}
fn cargo_subcommand(command: &str) -> Option<String> {
let mut tokens = command.split_whitespace();
while let Some(token) = tokens.next() {
if token == "cargo" {
break;
}
}
while let Some(token) = tokens.next() {
if crate::compress::is_shell_boundary(token) {
return None;
}
if matches!(
token,
"-Z" | "--config"
| "--color"
| "--manifest-path"
| "--target-dir"
| "--lockfile-path"
| "-C"
) {
if tokens.next().is_none_or(crate::compress::is_shell_boundary) {
return None;
}
} else if !token.starts_with('-') && !token.starts_with('+') {
return Some(token.to_string());
}
}
None
}
fn compress_build_like(output: &str) -> CompressionResult {
let lines: Vec<&str> = output.lines().collect();
let has_diagnostic = lines
.iter()
.any(|line| is_warning_or_error(line) || line.trim_start().starts_with("error["));
if !has_diagnostic {
return CompressionResult::new(output.trim_end().to_string());
}
let mut blocks = Vec::new();
let mut index = 0usize;
while index < lines.len() {
let line = lines[index];
let noise = line.trim().is_empty()
|| (is_cargo_status_line(line) && !is_final_cargo_summary(line))
|| is_test_progress_line(line.trim());
if noise {
index += 1;
continue;
}
if is_warning_or_error(line) || line.trim_start().starts_with("error[") {
let class = if line.trim_start().starts_with("warning:") {
DropClass::Warning
} else {
DropClass::Error
};
let start = index;
index += 1;
let mut body = DiagnosticBody::default();
while index < lines.len()
&& !starts_next_build_message(lines[index])
&& body.continues(lines[index])
{
index += 1;
}
blocks.push(ClassifiedBlock::new(class, lines[start..index].join("\n")));
continue;
}
blocks.push(ClassifiedBlock::unclassified(line.to_string()));
index += 1;
}
let capped = cap_classified_blocks(blocks);
CompressionResult::with_class_drops(trim_trailing_lines(&capped.text), capped.dropped_by_class)
.map_text(|text| ensure_final_lines(output, text))
}
fn starts_next_build_message(line: &str) -> bool {
is_cargo_status_line(line)
|| is_warning_or_error(line)
|| line.trim_start().starts_with("error[")
|| is_final_cargo_summary(line)
}
fn is_warning_or_error(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("warning:") || trimmed.starts_with("error:")
}
fn is_error_diagnostic(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("error:") || trimmed.starts_with("error[")
}
fn is_ignored_progress(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed == "Updating crates.io index" || is_compiling_line(trimmed)
}
fn is_compiling_line(trimmed: &str) -> bool {
let Some(rest) = trimmed.strip_prefix("Compiling ") else {
return false;
};
let mut parts = rest.split_whitespace();
let _crate_name = parts.next();
parts.next().is_some_and(|part| {
part.strip_prefix('v').is_some_and(|version| {
version
.chars()
.all(|char| char.is_ascii_digit() || char == '.')
})
})
}
fn is_final_cargo_summary(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("Finished ")
|| trimmed.starts_with("error: could not compile")
|| trimmed.starts_with("test result:")
}
const CARGO_STATUS_VERBS: &[&str] = &[
"Adding",
"Blocking",
"Building",
"Checking",
"Compiling",
"Doc-tests",
"Documenting",
"Downloaded",
"Downloading",
"Executable",
"Finished",
"Fresh",
"Locking",
"Running",
"Updating",
];
fn is_cargo_status_line(line: &str) -> bool {
if is_ignored_progress(line) {
return true;
}
let trimmed = line.trim_start();
line.starts_with(' ')
&& CARGO_STATUS_VERBS.iter().any(|verb| {
trimmed
.strip_prefix(verb)
.is_some_and(|rest| rest.starts_with(' '))
})
}
fn is_test_progress_line(trimmed: &str) -> bool {
trimmed.strip_prefix("test ").is_some_and(|rest| {
rest.contains(" ... ") || rest.ends_with(" has been running for over 60 seconds")
}) || is_nextest_progress_line(trimmed)
}
fn is_nextest_progress_line(trimmed: &str) -> bool {
let bracketed = |status: &str| {
trimmed
.strip_prefix(status)
.and_then(|rest| rest.trim_start().strip_prefix('['))
.and_then(|rest| rest.split_once(']'))
.map(|(inside, _)| inside.trim().trim_start_matches("> "))
};
let timed = |inside: &str| {
inside
.strip_suffix('s')
.is_some_and(|seconds| seconds.parse::<f64>().is_ok())
};
bracketed("PASS").is_some_and(timed)
|| bracketed("SLOW").is_some_and(timed)
|| bracketed("SKIP").is_some()
|| trimmed
.strip_prefix("Starting ")
.is_some_and(|rest| rest.contains(" tests across "))
}
#[derive(Default)]
struct DiagnosticBody {
label_column: Option<usize>,
}
impl DiagnosticBody {
fn continues(&mut self, line: &str) -> bool {
let trimmed = line.trim_start();
let indent = line.len() - trimmed.len();
if trimmed.is_empty() {
self.label_column = None;
return false;
}
if is_equals_label(trimmed) {
self.label_column = Some(indent);
return true;
}
if self
.label_column
.is_some_and(|column| indent > column + "= ".len())
{
return true;
}
self.label_column = None;
trimmed.starts_with("--> ")
|| trimmed.starts_with("::: ")
|| is_diagnostic_gutter(line)
|| trimmed.starts_with('^')
|| trimmed == "..."
|| trimmed.starts_with("help:")
|| trimmed.starts_with("note:")
}
}
fn is_equals_label(trimmed: &str) -> bool {
trimmed
.strip_prefix("= ")
.and_then(|rest| rest.split_once(':'))
.is_some_and(|(word, _)| {
!word.is_empty() && word.chars().all(|char| char.is_ascii_lowercase())
})
}
fn is_diagnostic_gutter(line: &str) -> bool {
let trimmed = line.trim_start();
let digits = trimmed.len()
- trimmed
.trim_start_matches(|char: char| char.is_ascii_digit())
.len();
let after_number = &trimmed[digits..];
let marker_text = after_number.trim_start();
let mut marker = marker_text.chars();
let Some(symbol) = marker.next() else {
return false;
};
let marker_ends = marker.next().is_none_or(char::is_whitespace);
if digits == 0 {
return line.starts_with(char::is_whitespace) && symbol == '|' && marker_ends;
}
after_number.starts_with(char::is_whitespace)
&& matches!(symbol, '|' | '+' | '-' | '~')
&& marker_ends
}
fn test_noise_mask(lines: &[&str]) -> Vec<bool> {
let mut warning: Option<DiagnosticBody> = None;
lines
.iter()
.map(|line| {
let trimmed = line.trim();
if trimmed.is_empty() {
warning = None;
return true;
}
if trimmed.starts_with("warning:") {
warning = Some(DiagnosticBody::default());
return true;
}
if warning.as_mut().is_some_and(|body| body.continues(line)) {
return true;
}
warning = None;
is_test_progress_line(trimmed) || is_cargo_status_line(line)
})
.collect()
}
fn compress_test(output: &str, exit_code: Option<i64>) -> CompressionResult {
let lines: Vec<&str> = output.lines().collect();
let noise = test_noise_mask(&lines);
let failure_headers = failure_list_headers(&lines);
let has_failures = failure_headers.iter().any(|header| *header);
let total = cargo_test_total_line(&lines);
if !has_failures {
let has_error_diagnostic = lines.iter().any(|line| is_error_diagnostic(line));
let has_warning_or_error = lines
.iter()
.any(|line| is_warning_or_error(line) || line.trim_start().starts_with("error["));
if has_error_diagnostic
|| (matches!(exit_code, Some(code) if code != 0) && has_warning_or_error)
{
return append_cargo_test_total(compress_build_like(output), total.as_deref());
}
let result: Vec<String> = lines
.iter()
.zip(&noise)
.filter(|(line, noise)| {
let trimmed = line.trim_start();
trimmed.starts_with("running ")
|| trimmed.starts_with("test result:")
|| is_final_cargo_summary(trimmed)
|| !**noise
})
.map(|(line, _)| (*line).to_string())
.collect();
return append_cargo_test_total(
CompressionResult::new(ensure_final_lines(
output,
&trim_trailing_lines(&result.join("\n")),
)),
total.as_deref(),
);
}
let mut sections = Vec::new();
let mut summary = Vec::new();
let mut names = Vec::new();
let mut seen_names = std::collections::HashSet::new();
let mut in_failure_list = false;
let mut warnings = 0;
let mut index = 0usize;
while index < lines.len() {
let line = lines[index];
let trimmed = line.trim_start();
if failure_headers[index] {
in_failure_list = true;
index += 1;
continue;
}
if let Some(name) = failure_name(trimmed.trim_end(), in_failure_list) {
if seen_names.insert(name) {
names.push(name);
}
if !line.starts_with("---- ") {
index += 1;
continue;
}
} else if !trimmed.is_empty() {
in_failure_list = false;
}
if line.starts_with("---- ") {
let start = index;
while index < lines.len() {
index += 1;
if index < lines.len()
&& (lines[index].starts_with("---- ")
|| lines[index].trim_start().starts_with("test result:")
|| failure_headers[index])
{
break;
}
}
sections.push(&lines[start..index]);
in_failure_list = false;
continue;
}
if trimmed.starts_with("warning:") {
warnings += 1;
}
if trimmed.starts_with("test result:") {
seen_names.clear();
}
if !noise[index] {
summary.push(line);
}
index += 1;
}
let list = format!("failures:\n {}", names.join("\n "));
let verdict_at = summary
.iter()
.position(|line| line.trim_start().starts_with("test result:"))
.unwrap_or(summary.len());
summary.insert(verdict_at, &list);
let summary =
append_cargo_test_total(CompressionResult::new(summary.join("\n")), total.as_deref());
let summary = summary.map_text(|text| ensure_final_lines(output, text));
let (details, dropped) = bounded_failure_sections(§ions, summary.text.len());
let text = if details.is_empty() {
summary.text
} else {
summary
.text
.replacen(&list, &format!("{details}\n{list}"), 1)
};
let mut drops = std::collections::BTreeMap::new();
if warnings > 0 {
drops.insert(DropClass::Warning, warnings);
}
if dropped > 0 {
drops.insert(DropClass::Failure, dropped);
}
let mut result = CompressionResult::with_class_drops(text, drops);
result.bounded_cargo_test_details = true;
result
}
test_support_visibility! {
const FAILURE_SECTION_LINES: usize = 6;
}
test_support_visibility! {
const FAILURE_DETAIL_LINES: usize = 20;
}
const FAILURE_DETAIL_BYTES: usize = super::caps::COMPLETION_FAILURE_TAIL_BYTES;
fn bounded_failure_sections(sections: &[&[&str]], summary_bytes: usize) -> (String, usize) {
let mut remaining_bytes = FAILURE_DETAIL_BYTES.saturating_sub(summary_bytes + 1);
let mut remaining_lines = FAILURE_DETAIL_LINES;
let mut kept = Vec::new();
let mut dropped = 0;
for section in sections {
let panic = section.iter().position(|line| line.contains("panicked at"));
let start = panic.unwrap_or(1);
let body: Vec<&str> = section[start..]
.iter()
.copied()
.filter(|line| {
!line.trim().is_empty()
&& !line
.trim_start()
.starts_with("note: run with `RUST_BACKTRACE")
})
.collect();
let mut candidate = vec![section[0]];
let mut bytes = section[0].len() + 1;
let minimum = body.len().min(2);
let minimum_bytes = bytes
+ body[..minimum]
.iter()
.map(|line| line.len() + 1)
.sum::<usize>();
if remaining_lines < minimum + 1 || remaining_bytes < minimum_bytes {
dropped += 1;
continue;
}
for line in &body {
if candidate.len() == FAILURE_SECTION_LINES
|| candidate.len() == remaining_lines
|| bytes + line.len() + 1 > remaining_bytes
{
break;
}
candidate.push(line);
bytes += line.len() + 1;
}
if candidate.len() < body.len() + 1 {
dropped += 1;
}
remaining_lines -= candidate.len();
remaining_bytes -= bytes;
kept.extend(candidate);
}
(kept.join("\n"), dropped)
}
test_support_visibility! {
pub(super) fn failure_summary_preserved(raw: &str, compressed: &str) -> bool {
let raw_counts = failure_name_counts(raw);
let compressed_counts = failure_name_counts(compressed);
if raw_counts.is_empty()
|| raw_counts.iter().any(|(name, views)| {
compressed_counts.get(name).map_or(0, |counts| counts[2]) < *views.iter().max().unwrap()
})
{
return false;
}
let mut saw_result = false;
let compressed_lines: std::collections::HashSet<_> =
compressed.lines().map(str::trim).collect();
for line in raw.lines().map(str::trim) {
if line.starts_with("test result:") {
saw_result = true;
if !compressed_lines.contains(line) {
return false;
}
}
}
saw_result && compressed_lines.contains("failures:")
}
}
fn failure_list_headers(lines: &[&str]) -> Vec<bool> {
lines
.iter()
.enumerate()
.map(|(index, line)| {
if line.trim() != "failures:" {
return false;
}
let mut saw_name = false;
for next in &lines[index + 1..] {
let trimmed = next.trim();
if trimmed.is_empty() {
continue;
}
if !saw_name && trimmed.starts_with("---- ") && trimmed.ends_with(" stdout ----") {
return true;
}
if next.starts_with(char::is_whitespace) && failure_name(trimmed, true).is_some() {
saw_name = true;
continue;
}
return saw_name && trimmed.starts_with("test result:");
}
false
})
.collect()
}
fn failure_name_counts(output: &str) -> std::collections::HashMap<&str, [usize; 3]> {
let lines: Vec<_> = output.lines().collect();
let headers = failure_list_headers(&lines);
let mut counts = std::collections::HashMap::<_, [usize; 3]>::new();
let mut in_list = false;
for (index, line) in lines.iter().enumerate() {
let trimmed = line.trim();
if headers[index] {
in_list = true;
continue;
}
if let Some(name) = failure_name(trimmed, in_list) {
let kind = failure_name_kind(trimmed);
if kind == 1 {
in_list = false;
}
counts.entry(name).or_default()[kind] += 1;
} else if !trimmed.is_empty() {
in_list = false;
}
}
counts
}
fn failure_name_kind(line: &str) -> usize {
if line.starts_with("test ") || is_nextest_verdict(line) {
0
} else if line.starts_with("---- ") {
1
} else {
2
}
}
fn append_cargo_test_total(
compressed: CompressionResult,
total: Option<&str>,
) -> CompressionResult {
match total {
Some(total) => compressed.map_text(|text| insert_cargo_test_total(text, total)),
None => compressed,
}
}
fn insert_cargo_test_total(output: &str, total: &str) -> String {
let mut lines = output.lines().map(str::to_string).collect::<Vec<_>>();
let insert_at = lines
.iter()
.rposition(|line| line.trim_start().starts_with("test result:"))
.map_or(lines.len(), |index| index + 1);
lines.insert(insert_at, total.to_string());
lines.join("\n")
}
fn cargo_test_total_line(lines: &[&str]) -> Option<String> {
let mut passed = 0usize;
let mut failed = 0usize;
let mut ignored = 0usize;
let mut binaries = 0usize;
let mut unparsed_binaries = 0usize;
for line in lines {
if !line.trim_start().starts_with("test result:") {
continue;
}
binaries += 1;
let counts = ["passed", "failed", "ignored"].map(|label| {
line.split(';').find_map(|field| {
let mut words = field.split_whitespace().rev();
(words.next()? == label).then(|| words.next()?.parse::<usize>().ok())?
})
});
match counts {
[Some(binary_passed), Some(binary_failed), Some(binary_ignored)] => {
passed = passed.saturating_add(binary_passed);
failed = failed.saturating_add(binary_failed);
ignored = ignored.saturating_add(binary_ignored);
}
_ => unparsed_binaries += 1,
}
}
if binaries < 2 {
return None;
}
let mut total = format!(
"cargo test total: {passed} passed, {failed} failed, {ignored} ignored across {binaries} binaries"
);
let mut incomplete = Vec::new();
if lines
.iter()
.any(|line| line.trim_start().starts_with("error: could not compile"))
{
incomplete.push("one or more test binaries failed to compile".to_string());
}
let started_binaries = lines
.iter()
.filter(|line| {
let Some(rest) = line.trim_start().strip_prefix("running ") else {
return false;
};
let mut words = rest.split_whitespace();
words
.next()
.is_some_and(|count| count.parse::<usize>().is_ok())
&& matches!(words.next(), Some("test" | "tests"))
})
.count();
let cut_off_binaries = started_binaries.saturating_sub(binaries);
if cut_off_binaries == 1 {
incomplete.push("1 test binary was cut off before reporting a result".to_string());
} else if cut_off_binaries > 1 {
incomplete.push(format!(
"{cut_off_binaries} test binaries were cut off before reporting a result"
));
}
if unparsed_binaries == 1 {
incomplete.push("1 test result could not be parsed".to_string());
} else if unparsed_binaries > 1 {
incomplete.push(format!(
"{unparsed_binaries} test results could not be parsed"
));
}
if !incomplete.is_empty() {
total.push_str(" (incomplete: ");
total.push_str(&incomplete.join("; "));
total.push(')');
}
Some(total)
}
fn trim_trailing_lines(input: &str) -> String {
input
.lines()
.map(str::trim_end)
.collect::<Vec<_>>()
.join("\n")
}
pub struct TestVerdict {
pub required: Vec<usize>,
pub empty_results: Vec<usize>,
}
pub fn test_verdict(lines: &[&str]) -> Option<TestVerdict> {
use std::collections::HashSet;
if !lines.iter().any(|line| {
is_cargo_test_signature_line(line.trim_start()) || is_nextest_verdict(line.trim_start())
}) {
return None;
}
let mut required = Vec::new();
let mut empty_results = Vec::new();
let mut names = std::collections::HashMap::<_, [usize; 3]>::new();
let mut locations = HashSet::new();
let failure_headers = failure_list_headers(lines);
let mut in_failure_list = false;
for (index, line) in lines.iter().enumerate() {
let trimmed = line.trim();
if failure_headers[index] {
in_failure_list = true;
continue;
}
let failure_name = failure_name(trimmed, in_failure_list);
if trimmed.starts_with("---- ") || (!trimmed.is_empty() && failure_name.is_none()) {
in_failure_list = false;
}
let is_name = failure_name.is_some_and(|name| {
let counts = names.entry(name).or_default();
let previous = *counts.iter().max().unwrap();
let kind = failure_name_kind(trimmed);
counts[kind] += 1;
counts[kind] > previous
});
let is_panic = (trimmed.starts_with("thread '") && trimmed.contains("panicked at"))
|| (index > 0 && lines[index - 1].trim_end().ends_with("panicked at"));
let is_location = is_panic && locations.len() < 20 && locations.insert(trimmed);
if trimmed.starts_with(
"test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;",
) {
empty_results.push(index);
} else if is_name
|| is_location
|| trimmed.starts_with("test result:")
|| trimmed.starts_with("cargo test total:")
|| trimmed.starts_with("Summary [")
|| trimmed.starts_with("error: test failed, to rerun")
{
required.push(index);
}
}
if let Some(bounded) = bounded_failure_verdict_lines(lines) {
required.extend(bounded);
required.sort_unstable();
required.dedup();
}
Some(TestVerdict {
required,
empty_results,
})
}
fn bounded_failure_verdict_lines(lines: &[&str]) -> Option<Vec<usize>> {
let mut required = Vec::new();
let mut in_section = false;
let mut in_list = false;
let mut section_lines = 0;
let mut detail_lines = 0;
let mut bytes = 0;
let failure_headers = failure_list_headers(lines);
for (index, line) in lines.iter().enumerate() {
let trimmed = line.trim();
if line.starts_with("---- ") && line.ends_with(" stdout ----") {
in_section = true;
in_list = false;
section_lines = 0;
} else if failure_headers[index] {
in_section = false;
in_list = true;
} else if trimmed.starts_with("test result:")
|| trimmed.starts_with("cargo test total:")
|| trimmed.starts_with("error: test failed, to rerun")
{
in_section = false;
in_list = false;
} else if in_list && !trimmed.is_empty() && failure_name(trimmed, true).is_none() {
in_list = false;
}
if trimmed.is_empty() {
continue;
}
if in_section {
section_lines += 1;
detail_lines += 1;
if section_lines > FAILURE_SECTION_LINES || detail_lines > FAILURE_DETAIL_LINES {
return None;
}
}
if in_section
|| in_list
|| trimmed.starts_with("test result:")
|| trimmed.starts_with("cargo test total:")
|| trimmed.starts_with("error: test failed, to rerun")
{
bytes += line.len() + 1;
if bytes > FAILURE_DETAIL_BYTES {
return None;
}
required.push(index);
}
}
(detail_lines > 0).then_some(required)
}
fn is_nextest_verdict(line: &str) -> bool {
["FAIL [", "TIMEOUT [", "Summary ["]
.iter()
.any(|prefix| line.starts_with(prefix))
}
fn failure_name(line: &str, in_failure_list: bool) -> Option<&str> {
if let Some(name) = line
.strip_prefix("test ")
.and_then(|line| line.strip_suffix(" ... FAILED"))
{
return Some(name);
}
if let Some(name) = line
.strip_prefix("---- ")
.and_then(|line| line.strip_suffix(" stdout ----"))
{
return Some(name);
}
if line.starts_with("FAIL [") || line.starts_with("TIMEOUT [") {
return line.split_once(']').map(|(_, name)| name.trim());
}
if in_failure_list && !line.is_empty() && !line.contains(char::is_whitespace) {
return Some(line);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compress::caps::DropClass;
use crate::compress::{
builtin_filters, compress_with_registry_exit_code, toml_filter::build_registry,
};
const HOST_SUPERVISOR_FAILURES: &str = include_str!(
"../../../aft/tests/integration/fixtures/compress_filters/cargo-test/host-supervisor-two-failures.stdout"
);
#[test]
fn cargo_test_printed_failure_header_is_not_a_failure_list() {
let raw = format!("{}running 1 test\ntest tests::foo ... FAILED\n\nfailures:\n\n---- tests::foo stdout ----\nthread 'tests::foo' panicked at src/lib.rs:1:1:\nfailures:\nnot_a_test\n\nfailures:\n tests::foo\n\ntest result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out\nerror: test failed, to rerun pass `--lib`\n", " Compiling fixture v0.1.0\n".repeat(80));
let registry = build_registry(builtin_filters::ALL, None, None);
let result = compress_with_registry_exit_code("cargo test", &raw, Some(101), ®istry);
assert!(result.bounded_cargo_test_details);
assert!(
result.text.contains("failures:\nnot_a_test\n"),
"{}",
result.text
);
assert!(!result.text.lines().any(|line| line == " not_a_test"));
assert_eq!(failure_name_counts(&raw).len(), 1);
assert_eq!(
failure_name_counts(&raw).get("tests::foo"),
Some(&[1, 1, 1])
);
assert_eq!(
failure_name_counts(&result.text).get("tests::foo"),
Some(&[0, 1, 1])
);
assert!(failure_summary_preserved(&raw, &result.text));
}
#[test]
fn cargo_test_reserves_final_line_before_spending_detail_budget() {
let final_line = format!("warning: final status {}", "x".repeat(1100));
let raw = format!("{HOST_SUPERVISOR_FAILURES}{final_line}\n");
let result = compress_test(&raw, Some(101));
assert_eq!(result.text.lines().last(), Some(final_line.as_str()));
assert!(
result.text.len() <= FAILURE_DETAIL_BYTES,
"{} bytes",
result.text.len()
);
assert!(failure_summary_preserved(&raw, &result.text));
}
#[test]
fn cargo_test_caps_failure_details_but_keeps_every_failure_name() {
let mut output = String::from("running 40 tests\n\nfailures:\n\n");
for index in 0..40 {
output.push_str(&format!(
"---- case_{index} stdout ----\nthread 'case_{index}' panicked at src/lib.rs:{index}:1\nstack line {index}\n\n"
));
}
output.push_str("failures:\n");
for index in 0..40 {
output.push_str(&format!(" case_{index}\n"));
}
output.push_str(
"\ntest result: FAILED. 0 passed; 40 failed; 0 ignored; 0 measured; 0 filtered out\n",
);
let result = compress_test(&output, None);
assert!(
result
.dropped_by_class
.get(&DropClass::Failure)
.copied()
.unwrap_or(0)
> 0
);
let (details, names) = result.text.split_once("failures:\n").expect("failure list");
assert!(details.lines().count() <= FAILURE_DETAIL_LINES + 1);
assert!(result.text.len() <= FAILURE_DETAIL_BYTES);
assert!(details.contains("---- case_0 stdout ----"));
assert!(details.contains("stack line 0"));
assert!(!details.contains("---- case_39 stdout ----"));
for index in 0..40 {
assert!(names
.lines()
.any(|line| line == format!(" case_{index}")));
}
assert!(result.had_inner_drop);
assert!(!result.offset_hint_eligible);
}
#[test]
fn cargo_test_compile_error_preserves_diagnostic_with_unknown_exit() {
let output = r#" Compiling demo v0.1.0 (/tmp/demo)
error[E0432]: unresolved import `crate::missing`
--> src/lib.rs:1:5
|
1 | use crate::missing;
| ^^^^^^^^^^^^^^ no `missing` in the root
error: could not compile `demo` (lib test) due to 1 previous error
"#;
let result = compress_test(output, None);
assert!(result.text.contains("error[E0432]"));
assert!(result.text.contains("unresolved import"));
assert!(result.text.contains("error: could not compile"));
}
#[test]
fn cargo_test_sums_results_even_when_output_is_wrapped() {
let output = "runner: cargo test\nrunning 2 tests\ntest result: ok. 2 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out\nrunning 1 test\ntest result: FAILED. 1 passed; 1 failed; 2 ignored; 0 measured; 0 filtered out\n";
assert!(CargoCompressor.matches_output(output));
let result = compress_test(output, Some(101));
assert!(result
.text
.contains("cargo test total: 3 passed, 1 failed, 3 ignored across 2 binaries"));
}
#[test]
fn cargo_test_total_marks_binaries_that_failed_to_compile() {
let output = "running 1 test\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\nrunning 2 tests\ntest result: ok. 2 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out\nerror: could not compile `broken` (bin test) due to 1 previous error\n";
let result = compress_test(output, Some(101));
assert!(result
.text
.contains("cargo test total: 3 passed, 0 failed, 1 ignored across 2 binaries"));
assert!(result
.text
.contains("incomplete: one or more test binaries failed to compile"));
}
#[test]
fn cargo_test_total_marks_binaries_without_a_result_as_incomplete() {
let output = "running 1 test\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\nrunning 2 tests\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\nrunning 1 test\n";
let result = compress_test(output, Some(0));
assert!(result
.text
.contains("cargo test total: 3 passed, 0 failed, 0 ignored across 2 binaries"));
assert!(result
.text
.contains("incomplete: 1 test binary was cut off before reporting a result"));
}
#[test]
fn cargo_verdict_keeps_aggregate_total_for_byte_capped_replies() {
let lines = [
"running 1 test",
"test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out",
"cargo test total: 1 passed, 0 failed, 0 ignored across 2 binaries",
];
let verdict = test_verdict(&lines).expect("cargo test output has a verdict");
assert!(verdict.required.contains(&2));
}
#[test]
fn cargo_verdict_extractor_keeps_nonfinal_rerun_with_unknown_exit() {
let output = format!("running 1 test\n\nfailures:\n\n---- case stdout ----\nthread 'case' panicked at src/lib.rs:1:1:\n\nfailures:\n case\n\ntest result: FAILED. 0 passed; 1 failed\nerror: test failed, to rerun pass `--lib`\n{}wrapper finished\n", "wrapper progress\n".repeat(200));
let result = compress_test(&output, None);
assert!(result
.text
.contains("error: test failed, to rerun pass `--lib`"));
assert!(result.text.ends_with("wrapper finished"));
}
const GATE_SCRIPT_OUTPUT: &str = "fmt-ok\nwin-check rc=0\ntest result: ok. 803 passed; 0 failed; 19 ignored; 0 measured; 8977 filtered out; finished in 23.38s\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2592 filtered out; finished in 0.12s\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2592 filtered out; finished in 0.16s\ntest result: ok. 114 passed; 0 failed; 3 ignored; 0 measured; 2476 filtered out; finished in 11.74s\n";
#[test]
fn cargo_test_summary_keeps_gate_script_verdicts_it_does_not_read() {
let result = compress_test(GATE_SCRIPT_OUTPUT, Some(0));
assert_eq!(
result.text,
format!(
"{}\ncargo test total: 919 passed, 0 failed, 22 ignored across 4 binaries",
GATE_SCRIPT_OUTPUT.trim_end()
)
);
}
#[test]
fn cargo_test_summary_keeps_a_failed_verdict_it_does_not_read() {
let output = GATE_SCRIPT_OUTPUT.replace("win-check rc=0", "win-check rc=101");
let result = compress_test(&output, Some(0));
assert!(
result
.text
.starts_with("fmt-ok\nwin-check rc=101\ntest result: ok. 803 passed"),
"{}",
result.text
);
}
#[test]
fn cargo_test_summary_drops_progress_but_keeps_printed_lines_in_order() {
let output = " Compiling demo v0.1.0 (/tmp/demo)\n Finished `test` profile [unoptimized + debuginfo] target(s) in 0.50s\n Running unittests src/lib.rs (target/debug/deps/demo-0123)\n\nrunning 2 tests\ntest a ... ok\nfmt-ok\ntest b ... ok\nwin-check rc=101\n\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n";
let result = compress_test(output, Some(0));
assert_eq!(
result.text,
" Finished `test` profile [unoptimized + debuginfo] target(s) in 0.50s\nrunning 2 tests\nfmt-ok\nwin-check rc=101\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s"
);
}
#[test]
fn cargo_test_summary_drops_nextest_progress_but_keeps_its_failures() {
let output = "running 1 test\ntest a ... ok\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\n Starting 3 tests across 1 binary\n PASS [ 0.004s] demo tests::a\n SKIP [ ] demo tests::c\n FAIL [ 0.010s] demo tests::b\nPASS [lint] rc=0\n Summary [ 0.020s] 3 tests run: 1 passed, 1 failed, 1 skipped\n";
let result = compress_test(output, Some(0));
assert_eq!(
result.text,
"running 1 test\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\n FAIL [ 0.010s] demo tests::b\nPASS [lint] rc=0\n Summary [ 0.020s] 3 tests run: 1 passed, 1 failed, 1 skipped"
);
}
#[test]
fn cargo_test_failure_summary_keeps_lines_it_does_not_read() {
let output = "fmt-ok\nwin-check rc=101\nrunning 2 tests\ntest a ... ok\ntest case ... FAILED\n\nfailures:\n\n---- case stdout ----\nthread 'case' panicked at src/lib.rs:1:1\n\nfailures:\n case\n\ntest result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out\nlint-check rc=0\n";
let result = compress_test(output, Some(101));
assert!(
result
.text
.starts_with("fmt-ok\nwin-check rc=101\nrunning 2 tests\n"),
"{}",
result.text
);
assert!(
result.text.contains("---- case stdout ----"),
"{}",
result.text
);
assert!(
result.text.ends_with("\nlint-check rc=0"),
"{}",
result.text
);
assert!(!result.text.contains("test a ... ok"), "{}", result.text);
}
fn full_warning(index: usize) -> String {
let line = index + 1;
format!(
"warning: mismatched value `w{index}`\n\
\x20 --> src/lib.rs:{line}:9\n\
\x20 | w{index}\n\
{line} | let w{index}: u32 = \"w{index}\";\n\
\x20 | --- ^^^^^ expected `u32`, found `&str` for w{index}\n\
\x20 | |\n\
\x20 | expected due to this w{index}\n\
...\n\
\x20 = note: expected type `u32` for w{index}\n\
\x20 found reference `&'static str` for w{index}\n\
\x20 = help: for further information visit https://example.invalid/w{index}\n\
help: try a number for w{index}\n\
\x20 |\n\
{line} - let w{index}: u32 = \"w{index}\";\n\
{line} + let w{index}: u32 = 1;\n\
\x20 | w{index}\n\
note: the lint level for w{index} is defined here\n\
\x20 ::: src/main.rs:1:9 w{index}\n"
)
}
fn capped_warnings_then_indented_verdict(separator: &str) -> String {
let mut output = String::from(" Checking demo v0.1.0 (/tmp/demo)\n");
for index in 0..11 {
if index > 0 {
output.push('\n');
}
output.push_str(&full_warning(index));
}
output.push_str(separator);
output.push_str(" win-check rc=101\n Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.50s\n");
output
}
#[test]
fn build_like_diagnostic_block_owns_every_continuation_shape() {
let output = capped_warnings_then_indented_verdict("\n");
let result = compress_build_like(&output);
assert_eq!(result.dropped_by_class.get(&DropClass::Warning), Some(&1));
assert!(!result.text.contains("w10"), "{}", result.text);
for shape in [
" --> src/lib.rs:10:9",
"10 | let w9: u32 = \"w9\";",
"expected due to this w9",
" found reference `&'static str` for w9",
" = help: for further information visit https://example.invalid/w9",
"10 + let w9: u32 = 1;",
" ::: src/main.rs:1:9 w9",
] {
assert!(result.text.contains(shape), "{shape}: {}", result.text);
}
}
#[test]
fn build_like_summaries_keep_an_indented_verdict_after_a_capped_warning() {
for separator in ["", "\n"] {
let output = capped_warnings_then_indented_verdict(separator);
assert!(output.len() > SHORT_OUTPUT_BYTES);
for command in ["cargo build", "cargo check", "cargo clippy"] {
let result = CargoCompressor.compress_with_exit_code(command, &output, Some(0));
assert!(
result.text.contains("\n win-check rc=101\n"),
"{command} {separator:?}: {}",
result.text
);
assert!(
!result.text.contains("w10"),
"{command} {separator:?}: {}",
result.text
);
}
}
}
#[test]
fn cargo_test_summary_keeps_an_indented_verdict_after_a_warning() {
for separator in ["", "\n"] {
let output = format!(
"{}{separator} win-check rc=101\nrunning 1 test\ntest a ... ok\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\n",
full_warning(0)
);
let result = compress_test(&output, Some(0));
assert_eq!(
result.text,
" win-check rc=101\nrunning 1 test\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out",
"{separator:?}"
);
}
}
#[test]
fn cargo_build_like_summaries_keep_lines_they_do_not_read() {
let mut output = String::from("fmt-ok\n Checking demo v0.1.0 (/tmp/demo)\n");
for index in 0..11 {
output.push_str(&format!(
"warning: unused variable: `x{index}`\n --> src/lib.rs:{line}:9\n |\n{line} | let x{index} = 1;\n | ^^ help: prefix it with an underscore\n |\n = note: `#[warn(unused_variables)]` on by default\n\n",
line = index + 1
));
}
output.push_str("win-check rc=101\nwarning: `demo` (lib) generated 11 warnings\n Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.50s\n");
assert!(output.len() > SHORT_OUTPUT_BYTES);
for command in ["cargo build", "cargo check", "cargo clippy"] {
let result = CargoCompressor.compress_with_exit_code(command, &output, Some(0));
assert!(
result.text.starts_with("fmt-ok\n"),
"{command}: {}",
result.text
);
assert!(
result.text.contains("\nwin-check rc=101\n"),
"{command}: {}",
result.text
);
assert!(
!result.text.contains("Checking demo"),
"{command}: {}",
result.text
);
assert!(
result.text.contains("Finished `dev` profile"),
"{command}: {}",
result.text
);
assert_eq!(result.dropped_by_class.get(&DropClass::Warning), Some(&2));
}
}
#[test]
fn cargo_test_compile_error_summary_keeps_lines_it_does_not_read() {
let output = "fmt-ok\n Compiling demo v0.1.0 (/tmp/demo)\nerror[E0432]: unresolved import `crate::missing`\n --> src/lib.rs:1:5\n |\n1 | use crate::missing;\n | ^^^^^^^^^^^^^^ no `missing` in the root\n\nwin-check rc=101\nerror: could not compile `demo` (lib test) due to 1 previous error\n";
let result = compress_test(output, Some(101));
assert!(
result.text.starts_with("fmt-ok\nerror[E0432]"),
"{}",
result.text
);
assert!(
result
.text
.contains("no `missing` in the root\nwin-check rc=101\nerror: could not compile"),
"{}",
result.text
);
}
#[test]
fn cargo_nextest_output_keeps_lines_it_does_not_read() {
let mut output =
String::from("fmt-ok\nwin-check rc=101\n Starting 40 tests across 1 binary\n");
for index in 0..40 {
output.push_str(&format!(
" PASS [ 0.004s] demo tests::case_{index}\n"
));
}
output.push_str(" Summary [ 0.010s] 40 tests run: 40 passed, 0 skipped\n");
assert!(output.len() > SHORT_OUTPUT_BYTES);
let result = CargoCompressor.compress_with_exit_code("cargo nextest run", &output, Some(0));
assert_eq!(result.text, output);
}
#[test]
fn cargo_subcommand_returns_none_for_pipe_before_subcommand() {
assert_eq!(cargo_subcommand("cargo --verbose | grep error"), None);
}
#[test]
fn cargo_subcommand_returns_subcommand_when_before_pipe() {
assert_eq!(
cargo_subcommand("cargo test | grep FAIL").as_deref(),
Some("test")
);
}
#[test]
fn cargo_subcommand_unaffected_without_metacharacters() {
assert_eq!(
cargo_subcommand("cargo test --release").as_deref(),
Some("test")
);
}
}
#[cfg(any(test, feature = "test-support"))]
mod audit_regressions {
#[test]
fn cargo_global_options_before_subcommand() {
for command in [
"cargo +nightly test",
"cargo --locked test",
"cargo -Z unstable-options test",
"cargo --config k=v test",
"cargo --config=k=v test",
"cargo --color always test",
"cargo -Zunstable-options test",
] {
assert_eq!(
super::cargo_subcommand(command).as_deref(),
Some("test"),
"{command}"
);
}
}
}