Skip to content

Commit

Permalink
[nextest-runner] ensure that the last "panicked at" message is picked up
Browse files Browse the repository at this point in the history
Some libraries like proptest produce a lot of "panicked at" messages. Ensure
that only the last one is picked up.

Also switch the color of panics from bold to fail, which highlights them more
clearly in the output.
  • Loading branch information
sunshowers committed Sep 1, 2024
1 parent 0aaa2d6 commit 2516f81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions nextest-runner/src/reporter/displayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,15 +1481,15 @@ impl<'a> TestReporterImpl<'a> {
writer.write_all(&output.buf[..start])?;
writer.write_all(RESET_COLOR)?;

write!(writer, "{}", FmtPrefix(&self.styles.count))?;
write!(writer, "{}", FmtPrefix(&self.styles.fail))?;

// Strip ANSI escapes from this part of the output. It's unlikely there are any, but
// strip it just in case.
let mut no_color = strip_ansi_escapes::Writer::new(writer);
no_color.write_all(&output.buf[start..end])?;
let writer = no_color.into_inner()?;

write!(writer, "{}", FmtSuffix(&self.styles.count))?;
write!(writer, "{}", FmtSuffix(&self.styles.fail))?;

// `end` is guaranteed to be within the bounds of `output.buf`. (It is actually safe
// for it to be equal to `output.buf.len()` -- it gets treated as an empty list in
Expand Down
18 changes: 15 additions & 3 deletions nextest-runner/src/reporter/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ fn heuristic_should_panic(stdout: &[u8]) -> Option<ByteSubslice<'_>> {
}

fn heuristic_panic_message(stderr: &[u8]) -> Option<ByteSubslice<'_>> {
let panicked_at_match = PANICKED_AT_REGEX.find(stderr)?;
// Look for the last instance to handle situations like proptest which repeatedly print out
// `panicked at ...` messages.
let panicked_at_match = PANICKED_AT_REGEX.find_iter(stderr).last()?;
// If the previous line starts with "Error: ", grab it as well -- it contains the error with
// result-based test failures.
let mut start = panicked_at_match.start();
Expand Down Expand Up @@ -300,7 +302,7 @@ test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 13 filtered out;
}

#[test]
fn test_heuristic_stack_trace() {
fn test_heuristic_panic_message() {
let tests: &[(&str, &str)] = &[
(
"thread 'main' panicked at 'foo', src/lib.rs:1\n",
Expand Down Expand Up @@ -330,7 +332,17 @@ thread 'test_result_failure' panicked at 'assertion failed: `(left == right)`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
more text at the end, followed by some newlines"#,
),
// With RUST_BACKTRACE=1
// Multiple panics: only the last one should be extracted.
(
r#"
thread 'main' panicked at src/lib.rs:1:
foo
thread 'main' panicked at src/lib.rs:2:
bar
"#,
r#"thread 'main' panicked at src/lib.rs:2:
bar"#,
), // With RUST_BACKTRACE=1
(
r#"
some initial text
Expand Down

0 comments on commit 2516f81

Please sign in to comment.