1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::runner::{AbortStatus, ExecutionResult};
use bstr::ByteSlice;
use once_cell::sync::Lazy;
use regex::bytes::{Regex, RegexBuilder};
use std::fmt;

// This regex works for the default panic handler for Rust -- other panic handlers may not work,
// which is why this is heuristic.
static PANICKED_AT_REGEX_STR: &str = "^thread '([^']+)' panicked at ";
static PANICKED_AT_REGEX: Lazy<Regex> = Lazy::new(|| {
    let mut builder = RegexBuilder::new(PANICKED_AT_REGEX_STR);
    builder.multi_line(true);
    builder.build().unwrap()
});

static ERROR_REGEX_STR: &str = "^Error: ";
static ERROR_REGEX: Lazy<Regex> = Lazy::new(|| {
    let mut builder = RegexBuilder::new(ERROR_REGEX_STR);
    builder.multi_line(true);
    builder.build().unwrap()
});

/// The return result of [`heuristic_extract_description`].
#[derive(Clone, Copy, Debug)]
pub enum DescriptionKind<'a> {
    /// This was some kind of abort.
    Abort {
        /// The reason for the abort.
        status: AbortStatus,
        /// Whether the test leaked handles.
        leaked: bool,
    },

    /// A panic message was found in the output.
    ///
    /// The output is borrowed from standard error.
    PanicMessage {
        /// The subslice of standard error that contains the stack trace.
        stderr_subslice: ByteSubslice<'a>,
    },

    /// An error string was found in the output.
    ///
    /// The output is borrowed from standard error.
    ErrorStr {
        /// The subslice of standard error that contains the stack trace.
        stderr_subslice: ByteSubslice<'a>,
    },

    /// A should-panic test did not panic.
    ///
    /// The output is borrowed from standard output.
    ShouldPanic {
        /// The subslice of standard output that contains the should-panic message.
        stdout_subslice: ByteSubslice<'a>,
    },
}

impl<'a> DescriptionKind<'a> {
    /// Returns the subslice of standard error that contains the description.
    pub fn stderr_subslice(&self) -> Option<ByteSubslice<'a>> {
        match self {
            DescriptionKind::Abort { .. } => None,
            DescriptionKind::PanicMessage { stderr_subslice }
            | DescriptionKind::ErrorStr {
                stderr_subslice, ..
            } => Some(*stderr_subslice),
            DescriptionKind::ShouldPanic { .. } => None,
        }
    }

    /// Returns the subslice of standard output that contains the description.
    pub fn stdout_subslice(&self) -> Option<ByteSubslice<'a>> {
        match self {
            DescriptionKind::Abort { .. } => None,
            DescriptionKind::PanicMessage { .. } => None,
            DescriptionKind::ErrorStr { .. } => None,
            DescriptionKind::ShouldPanic {
                stdout_subslice, ..
            } => Some(*stdout_subslice),
        }
    }

    /// Returns the subslice of combined output (either stdout or stderr) that contains the description.
    pub fn combined_subslice(&self) -> Option<ByteSubslice<'a>> {
        match self {
            DescriptionKind::Abort { .. } => None,
            DescriptionKind::PanicMessage { stderr_subslice }
            | DescriptionKind::ErrorStr {
                stderr_subslice, ..
            } => Some(*stderr_subslice),
            DescriptionKind::ShouldPanic {
                stdout_subslice, ..
            } => Some(*stdout_subslice),
        }
    }

    /// Displays the description as a user-friendly string.
    pub fn display_human(&self) -> DescriptionKindDisplay<'_> {
        DescriptionKindDisplay(*self)
    }
}

/// A subslice of a byte slice.
///
/// This type tracks the start index of the subslice from the parent slice.
#[derive(Clone, Copy, Debug)]
pub struct ByteSubslice<'a> {
    /// The slice.
    pub slice: &'a [u8],

    /// The start index of the subslice from the parent slice.
    pub start: usize,
}

/// A display wrapper for [`DescriptionKind`].
#[derive(Clone, Copy, Debug)]
pub struct DescriptionKindDisplay<'a>(DescriptionKind<'a>);

impl fmt::Display for DescriptionKindDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            DescriptionKind::Abort { status, leaked } => {
                write!(f, "Test aborted")?;
                match status {
                    #[cfg(unix)]
                    AbortStatus::UnixSignal(sig) => {
                        let signal_str = crate::helpers::signal_str(sig)
                            .map(|signal_str| format!("SIG{signal_str}"))
                            .unwrap_or_else(|| sig.to_string());
                        write!(f, " with signal {signal_str}")?;
                    }
                    #[cfg(windows)]
                    AbortStatus::WindowsNtStatus(exception) => {
                        write!(
                            f,
                            " with code {}",
                            crate::helpers::display_nt_status(exception)
                        )?;
                    }
                }
                if leaked {
                    write!(f, ", and also leaked handles")?;
                }
                Ok(())
            }
            DescriptionKind::PanicMessage { stderr_subslice } => {
                // Strip invalid XML characters.
                write!(f, "{}", String::from_utf8_lossy(stderr_subslice.slice))
            }
            DescriptionKind::ErrorStr { stderr_subslice } => {
                write!(f, "{}", String::from_utf8_lossy(stderr_subslice.slice))
            }
            DescriptionKind::ShouldPanic { stdout_subslice } => {
                write!(f, "{}", String::from_utf8_lossy(stdout_subslice.slice))
            }
        }
    }
}

/// Attempts to heuristically extract a description of the test failure from the output of the test.
///
/// `None` indicates that output wasn't captured -- in those cases there isn't much we can do.
pub fn heuristic_extract_description<'a>(
    exec_result: ExecutionResult,
    stdout: Option<&'a [u8]>,
    stderr: Option<&'a [u8]>,
) -> Option<DescriptionKind<'a>> {
    // If the test crashed with a signal, use that.
    if let ExecutionResult::Fail {
        abort_status: Some(status),
        leaked,
    } = exec_result
    {
        return Some(DescriptionKind::Abort { status, leaked });
    }

    // Try the heuristic stack trace extraction first to try and grab more information first.
    if let Some(stderr) = stderr {
        if let Some(stderr_subslice) = heuristic_panic_message(stderr) {
            return Some(DescriptionKind::PanicMessage { stderr_subslice });
        }
        if let Some(stderr_subslice) = heuristic_error_str(stderr) {
            return Some(DescriptionKind::ErrorStr { stderr_subslice });
        }
    }

    if let Some(stdout) = stdout {
        if let Some(stdout_subslice) = heuristic_should_panic(stdout) {
            return Some(DescriptionKind::ShouldPanic { stdout_subslice });
        }
    }

    None
}

fn heuristic_should_panic(stdout: &[u8]) -> Option<ByteSubslice<'_>> {
    let line = stdout
        .lines()
        .find(|line| line.contains_str("note: test did not panic as expected"))?;

    // SAFETY: line is a subslice of stdout.
    let start = unsafe { line.as_ptr().offset_from(stdout.as_ptr()) };

    let start = usize::try_from(start).unwrap_or_else(|error| {
        panic!(
            "negative offset from stdout.as_ptr() ({:x}) to line.as_ptr() ({:x}): {}",
            stdout.as_ptr() as usize,
            line.as_ptr() as usize,
            error
        )
    });
    Some(ByteSubslice { slice: line, start })
}

fn heuristic_panic_message(stderr: &[u8]) -> Option<ByteSubslice<'_>> {
    // 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();
    let prefix = stderr[..start].trim_end_with(|c| c == '\n' || c == '\r');
    if let Some(prev_line_start) = prefix.rfind("\n") {
        if prefix[prev_line_start..].starts_with_str("\nError:") {
            start = prev_line_start + 1;
        }
    }

    // TODO: this grabs too much -- it is possible that destructors print out further messages so we
    // should be more careful. But it's hard to tell what's printed by the panic and what's printed
    // by destructors, so we lean on the side of caution.
    Some(ByteSubslice {
        slice: stderr[start..].trim_end_with(|c| c.is_whitespace()),
        start,
    })
}

fn heuristic_error_str(stderr: &[u8]) -> Option<ByteSubslice<'_>> {
    // Starting Rust 1.66, Result-based errors simply print out "Error: ".
    let error_match = ERROR_REGEX.find(stderr)?;
    let start = error_match.start();

    // TODO: this grabs too much -- it is possible that destructors print out further messages so we
    // should be more careful. But it's hard to tell what's printed by the error and what's printed
    // by destructors, so we lean on the side of caution.
    Some(ByteSubslice {
        slice: stderr[start..].trim_end_with(|c| c.is_whitespace()),
        start,
    })
}

/// Given a slice, find the index of the point at which highlighting should end.
///
/// Returns a value in the range [0, slice.len()].
pub fn highlight_end(slice: &[u8]) -> usize {
    // We want to highlight the first two lines of the output.
    let mut iter = slice.find_iter(b"\n");
    match iter.next() {
        Some(_) => {
            match iter.next() {
                Some(second) => second,
                // No second newline found, so highlight the entire slice.
                None => slice.len(),
            }
        }
        // No newline found, so highlight the entire slice.
        None => slice.len(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_heuristic_should_panic() {
        let tests: &[(&str, &str)] = &[(
            "running 1 test
test test_failure_should_panic - should panic ... FAILED

failures:

---- test_failure_should_panic stdout ----
note: test did not panic as expected

failures:
    test_failure_should_panic

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 13 filtered out; finished in 0.00s",
            "note: test did not panic as expected",
        )];

        for (input, output) in tests {
            let extracted = heuristic_should_panic(input.as_bytes())
                .expect("should-panic message should have been found");
            assert_eq!(
                DisplayWrapper(extracted.slice),
                DisplayWrapper(output.as_bytes())
            );
            assert_eq!(
                extracted.start,
                extracted.slice.as_ptr() as usize - input.as_bytes().as_ptr() as usize
            );
        }
    }

    #[test]
    fn test_heuristic_panic_message() {
        let tests: &[(&str, &str)] = &[
            (
                "thread 'main' panicked at 'foo', src/lib.rs:1\n",
                "thread 'main' panicked at 'foo', src/lib.rs:1",
            ),
            (
                "foobar\n\
            thread 'main' panicked at 'foo', src/lib.rs:1\n\n",
                "thread 'main' panicked at 'foo', src/lib.rs:1",
            ),
            (
                r#"
text: foo
Error: Custom { kind: InvalidData, error: "this is an error" }
thread 'test_result_failure' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/test/src/lib.rs:186:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
more text at the end, followed by some newlines


            "#,
                r#"Error: Custom { kind: InvalidData, error: "this is an error" }
thread 'test_result_failure' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/test/src/lib.rs:186:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
more text at the end, followed by some newlines"#,
            ),
            // 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
line 2
line 3
thread 'reporter::helpers::tests::test_heuristic_stack_trace' panicked at nextest-runner/src/reporter/helpers.rs:237:9:
test
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/panicking.rs:72:14
   2: nextest_runner::reporter::helpers::tests::test_heuristic_stack_trace
             at ./src/reporter/helpers.rs:237:9
   3: nextest_runner::reporter::helpers::tests::test_heuristic_stack_trace::{{closure}}
             at ./src/reporter/helpers.rs:236:36
   4: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
   5: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
more text at the end, followed by some newlines


"#,
                r#"thread 'reporter::helpers::tests::test_heuristic_stack_trace' panicked at nextest-runner/src/reporter/helpers.rs:237:9:
test
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/panicking.rs:72:14
   2: nextest_runner::reporter::helpers::tests::test_heuristic_stack_trace
             at ./src/reporter/helpers.rs:237:9
   3: nextest_runner::reporter::helpers::tests::test_heuristic_stack_trace::{{closure}}
             at ./src/reporter/helpers.rs:236:36
   4: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
   5: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
more text at the end, followed by some newlines"#,
            ),
            // RUST_BACKTRACE=full
            (
                r#"
some initial text
thread 'reporter::helpers::tests::test_heuristic_stack_trace' panicked at nextest-runner/src/reporter/helpers.rs:237:9:
test
stack backtrace:
   0:     0x61e6da135fe5 - std::backtrace_rs::backtrace::libunwind::trace::h23054e327d0d4b55
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x61e6da135fe5 - std::backtrace_rs::backtrace::trace_unsynchronized::h0cc587407d7f7f64
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x61e6da135fe5 - std::sys_common::backtrace::_print_fmt::h4feeb59774730d6b
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x61e6da135fe5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hd736fd5964392270
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x61e6da16433b - core::fmt::rt::Argument::fmt::h105051d8ea1ade1e
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/fmt/rt.rs:165:63
   5:     0x61e6da16433b - core::fmt::write::hc6043626647b98ea
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/fmt/mod.rs:1168:21
some more text at the end, followed by some newlines


"#,
                r#"thread 'reporter::helpers::tests::test_heuristic_stack_trace' panicked at nextest-runner/src/reporter/helpers.rs:237:9:
test
stack backtrace:
   0:     0x61e6da135fe5 - std::backtrace_rs::backtrace::libunwind::trace::h23054e327d0d4b55
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x61e6da135fe5 - std::backtrace_rs::backtrace::trace_unsynchronized::h0cc587407d7f7f64
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x61e6da135fe5 - std::sys_common::backtrace::_print_fmt::h4feeb59774730d6b
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x61e6da135fe5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hd736fd5964392270
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x61e6da16433b - core::fmt::rt::Argument::fmt::h105051d8ea1ade1e
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/fmt/rt.rs:165:63
   5:     0x61e6da16433b - core::fmt::write::hc6043626647b98ea
                               at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/fmt/mod.rs:1168:21
some more text at the end, followed by some newlines"#,
            ),
        ];

        for (input, output) in tests {
            let extracted = heuristic_panic_message(input.as_bytes())
                .expect("stack trace should have been found");
            assert_eq!(
                DisplayWrapper(extracted.slice),
                DisplayWrapper(output.as_bytes())
            );
            assert_eq!(
                extracted.start,
                extracted.slice.as_ptr() as usize - input.as_bytes().as_ptr() as usize
            );
        }
    }

    #[test]
    fn test_heuristic_error_str() {
        let tests: &[(&str, &str)] = &[(
            "foobar\nError: \"this is an error\"\n",
            "Error: \"this is an error\"",
        )];

        for (input, output) in tests {
            let extracted =
                heuristic_error_str(input.as_bytes()).expect("error string should have been found");
            assert_eq!(
                DisplayWrapper(extracted.slice),
                DisplayWrapper(output.as_bytes())
            );
            assert_eq!(
                extracted.start,
                extracted.slice.as_ptr() as usize - input.as_bytes().as_ptr() as usize
            );
        }
    }

    #[test]
    fn test_highlight_end() {
        let tests: &[(&str, usize)] = &[
            ("", 0),
            ("\n", 1),
            ("foo", 3),
            ("foo\n", 4),
            ("foo\nbar", 7),
            ("foo\nbar\n", 7),
            ("foo\nbar\nbaz", 7),
            ("foo\nbar\nbaz\n", 7),
            ("\nfoo\nbar\nbaz", 4),
        ];

        for (input, output) in tests {
            assert_eq!(
                highlight_end(input.as_bytes()),
                *output,
                "for input {input:?}"
            );
        }
    }

    // Wrapper so that panic messages show up nicely in the test output.
    #[derive(Eq, PartialEq)]
    struct DisplayWrapper<'a>(&'a [u8]);

    impl<'a> fmt::Debug for DisplayWrapper<'a> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", String::from_utf8_lossy(self.0))
        }
    }
}