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
//! Utilities for capture output from tests run in a child process

use crate::{
    errors::CollectTestOutputError,
    reporter::{heuristic_extract_description, DescriptionKind},
    runner::ExecutionResult,
};
use bstr::{ByteSlice, Lines};
use bytes::{Bytes, BytesMut};
use std::{borrow::Cow, sync::OnceLock};
use tokio::io::BufReader;

/// 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.
///
/// This is a wrapper around a [`Bytes`] that provides some convenience methods.
#[derive(Clone, Debug)]
pub struct TestSingleOutput {
    /// 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 TestSingleOutput {
    #[inline]
    fn from(buf: Bytes) -> Self {
        Self {
            buf,
            as_str: OnceLock::new(),
        }
    }
}

impl TestSingleOutput {
    /// 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 complete captured output of a child process.
#[derive(Clone, Debug)]
pub enum TestExecutionOutput {
    /// The process was run and the output was captured.
    Output(TestOutput),

    /// There was an execution failure.
    ExecFail {
        /// A single-line message.
        message: String,

        /// The full description, including other errors, to print out.
        description: String,
    },
}

/// The output of a test.
///
/// Part of [`TestExecutionOutput`].
#[derive(Clone, Debug)]
pub enum TestOutput {
    /// The output was split into stdout and stderr.
    Split {
        /// The captured stdout.
        stdout: TestSingleOutput,

        /// The captured stderr.
        stderr: TestSingleOutput,
    },

    /// The output was combined into stdout and stderr.
    Combined {
        /// The captured output.
        output: TestSingleOutput,
    },
}

impl TestOutput {
    /// Attempts to extract a description of a test failure from the output of the test.
    pub fn heuristic_extract_description(
        &self,
        exec_result: ExecutionResult,
    ) -> Option<DescriptionKind<'_>> {
        match self {
            Self::Split { stdout, stderr } => {
                if let Some(kind) =
                    heuristic_extract_description(exec_result, &stdout.buf, &stderr.buf)
                {
                    return Some(kind);
                }
            }
            Self::Combined { output } => {
                if let Some(kind) =
                    heuristic_extract_description(exec_result, &output.buf, &output.buf)
                {
                    return Some(kind);
                }
            }
        }

        None
    }
}

/// The size of each buffered reader's buffer, and the size at which we grow
/// the interleaved buffer.
///
/// This size is not totally arbitrary, but rather the (normal) page size on
/// most linux, windows, and macos systems.
const CHUNK_SIZE: usize = 4 * 1024;

/// Collects the stdout and/or stderr streams into a single buffer
pub async fn collect_test_output(
    streams: Option<crate::test_command::Output>,
) -> Result<Option<TestOutput>, CollectTestOutputError> {
    use tokio::io::AsyncBufReadExt as _;

    let Some(output) = streams else {
        return Ok(None);
    };

    match output {
        crate::test_command::Output::Split { stdout, stderr } => {
            let mut stdout = BufReader::with_capacity(CHUNK_SIZE, stdout);
            let mut stderr = BufReader::with_capacity(CHUNK_SIZE, stderr);

            let mut stdout_acc = BytesMut::with_capacity(CHUNK_SIZE);
            let mut stderr_acc = BytesMut::with_capacity(CHUNK_SIZE);

            let mut out_done = false;
            let mut err_done = false;

            loop {
                tokio::select! {
                    res = stdout.fill_buf(), if !out_done => {
                        let read = {
                            let buf = res.map_err(CollectTestOutputError::ReadStdout)?;
                            stdout_acc.extend_from_slice(buf);
                            buf.len()
                        };

                        stdout.consume(read);
                        out_done = read == 0;
                    }
                    res = stderr.fill_buf(), if !err_done => {
                        let read = {
                            let buf = res.map_err(CollectTestOutputError::ReadStderr)?;
                            stderr_acc.extend_from_slice(buf);
                            buf.len()
                        };

                        stderr.consume(read);
                        err_done = read == 0;
                    }
                    else => break,
                };
            }

            Ok(Some(TestOutput::Split {
                stdout: stdout_acc.freeze().into(),
                stderr: stderr_acc.freeze().into(),
            }))
        }
        crate::test_command::Output::Combined(output) => {
            let mut output = BufReader::with_capacity(CHUNK_SIZE, output);
            let mut acc = BytesMut::with_capacity(CHUNK_SIZE);

            loop {
                let read = {
                    let buf = output
                        .fill_buf()
                        .await
                        .map_err(CollectTestOutputError::ReadStdout)?;
                    acc.extend_from_slice(buf);
                    buf.len()
                };

                output.consume(read);
                if read == 0 {
                    break;
                }
            }

            Ok(Some(TestOutput::Combined {
                output: acc.freeze().into(),
            }))
        }
    }
}