nextest_runner/test_output.rs
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
//! Utilities for capture output from tests run in a child process
use crate::{
errors::{ChildError, ChildStartError, ErrorList},
reporter::events::ExecutionResult,
};
use bstr::{ByteSlice, Lines};
use bytes::Bytes;
use std::{borrow::Cow, sync::OnceLock};
/// The strategy used to capture test executable output
#[derive(Copy, Clone, PartialEq, Default, Debug)]
pub enum CaptureStrategy {
/// Captures `stdout` and `stderr` separately
///
/// * pro: output from `stdout` and `stderr` can be identified and easily split
/// * con: ordering between the streams cannot be guaranteed
#[default]
Split,
/// Captures `stdout` and `stderr` in a single stream
///
/// * pro: output is guaranteed to be ordered as it would in a terminal emulator
/// * con: distinction between `stdout` and `stderr` is lost, all output is attributed to `stdout`
Combined,
/// Output is not captured
///
/// This mode is used when using --no-capture, causing nextest to execute
/// tests serially without capturing output
None,
}
/// A single output for a test or setup script: standard output, standard error, or a combined
/// buffer.
///
/// This is a wrapper around a [`Bytes`] that provides some convenience methods.
#[derive(Clone, Debug)]
pub struct ChildSingleOutput {
/// The raw output buffer
pub buf: Bytes,
/// A string representation of the output, computed on first access.
///
/// `None` means the output is valid UTF-8.
as_str: OnceLock<Option<Box<str>>>,
}
impl From<Bytes> for ChildSingleOutput {
#[inline]
fn from(buf: Bytes) -> Self {
Self {
buf,
as_str: OnceLock::new(),
}
}
}
impl ChildSingleOutput {
/// Gets this output as a lossy UTF-8 string.
#[inline]
pub fn as_str_lossy(&self) -> &str {
let s = self
.as_str
.get_or_init(|| match String::from_utf8_lossy(&self.buf) {
// A borrowed string from `from_utf8_lossy` is always valid UTF-8. We can't store
// the `Cow` directly because that would be a self-referential struct. (Well, we
// could via a library like ouroboros, but that's really unnecessary.)
Cow::Borrowed(_) => None,
Cow::Owned(s) => Some(s.into_boxed_str()),
});
match s {
Some(s) => s,
// SAFETY: Immediately above, we've established that `None` means `buf` is valid UTF-8.
None => unsafe { std::str::from_utf8_unchecked(&self.buf) },
}
}
/// Iterates over lines in this output.
#[inline]
pub fn lines(&self) -> Lines<'_> {
self.buf.lines()
}
/// Returns true if the output is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
}
/// The result of executing a child process: either that the process was run and
/// at least some output was captured, or that the process could not be started
/// at all.
#[derive(Clone, Debug)]
pub enum ChildExecutionOutput {
/// The process was run and the output was captured.
Output {
/// If the process has finished executing, the final state it is in.
///
/// `None` means execution is currently in progress.
result: Option<ExecutionResult>,
/// The captured output.
output: ChildOutput,
/// Errors that occurred while waiting on the child process or parsing
/// its output.
errors: Option<ErrorList<ChildError>>,
},
/// There was a failure to start the process.
StartError(ChildStartError),
}
impl ChildExecutionOutput {
/// Returns true if there are any errors in this output.
pub(crate) fn has_errors(&self) -> bool {
match self {
ChildExecutionOutput::Output { errors, result, .. } => {
if errors.is_some() {
return true;
}
if let Some(result) = result {
return !result.is_success();
}
false
}
ChildExecutionOutput::StartError(_) => true,
}
}
}
/// The output of a child process: stdout and/or stderr.
///
/// Part of [`ChildExecutionOutput`], and can be used independently as well.
#[derive(Clone, Debug)]
pub enum ChildOutput {
/// The output was split into stdout and stderr.
Split(ChildSplitOutput),
/// The output was combined into stdout and stderr.
Combined {
/// The captured output.
output: ChildSingleOutput,
},
}
/// The output of a child process (test or setup script) with split stdout and stderr.
///
/// One of the variants of [`ChildOutput`].
#[derive(Clone, Debug)]
pub struct ChildSplitOutput {
/// The captured stdout, or `None` if the output was not captured.
pub stdout: Option<ChildSingleOutput>,
/// The captured stderr, or `None` if the output was not captured.
pub stderr: Option<ChildSingleOutput>,
}