nextest_runner/
input.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
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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Input handling for nextest.
//!
//! Similar to signal handling, input handling is read by the runner and used to control
//! non-signal-related aspects of the test run. For example, "i" to print information about which
//! tests are currently running.

use crate::errors::DisplayErrorChain;
use crossterm::event::{Event, EventStream, KeyCode};
use futures::StreamExt;
use std::{
    io::IsTerminal,
    sync::{Arc, Mutex},
};
use thiserror::Error;
use tracing::{debug, warn};

/// The kind of input handling to set up for a test run.
///
/// An `InputHandlerKind` can be passed into
/// [`TestRunnerBuilder::build`](crate::runner::TestRunnerBuilder::build).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InputHandlerKind {
    /// The standard input handler, which reads from standard input.
    Standard,

    /// A no-op input handler. Useful for tests.
    Noop,
}

impl InputHandlerKind {
    pub(crate) fn build(self) -> InputHandler {
        match self {
            Self::Standard => InputHandler::new(),
            Self::Noop => InputHandler::noop(),
        }
    }
}

/// The input handler implementation.
#[derive(Debug)]
pub(crate) struct InputHandler {
    // A scope guard that ensures non-canonical mode is disabled when this is
    // dropped, along with a stream to read events from.
    imp: Option<(InputHandlerImpl, EventStream)>,
}

impl InputHandler {
    const INFO_CHAR: char = 't';

    /// Creates a new `InputHandler` that reads from standard input.
    pub(crate) fn new() -> Self {
        if std::io::stdin().is_terminal() {
            // Try enabling non-canonical mode.
            match InputHandlerImpl::new() {
                Ok(handler) => {
                    let stream = EventStream::new();
                    debug!("enabled terminal non-canonical mode, reading input events");
                    Self {
                        imp: Some((handler, stream)),
                    }
                }
                Err(error) => {
                    warn!(
                        "failed to enable terminal non-canonical mode, \
                         cannot read input events: {}",
                        error,
                    );
                    Self::noop()
                }
            }
        } else {
            debug!("not reading input because stdin is not a tty");
            Self::noop()
        }
    }

    /// Creates a new `InputHandler` that does nothing.
    pub(crate) fn noop() -> Self {
        Self { imp: None }
    }

    pub(crate) fn status(&self) -> InputHandlerStatus {
        if self.imp.is_some() {
            InputHandlerStatus::Enabled {
                info_char: Self::INFO_CHAR,
            }
        } else {
            InputHandlerStatus::Disabled
        }
    }

    /// Receives an event from the input, or None if the input is closed and there are no more
    /// events.
    ///
    /// This is a cancel-safe operation.
    pub(crate) async fn recv(&mut self) -> Option<InputEvent> {
        let (_, stream) = self.imp.as_mut()?;
        loop {
            let next = stream.next().await?;
            match next {
                Ok(Event::Key(key)) => {
                    if key.code == KeyCode::Char(Self::INFO_CHAR) && key.modifiers.is_empty() {
                        return Some(InputEvent::Info);
                    }
                }
                Ok(event) => {
                    debug!("unhandled event: {:?}", event);
                }
                Err(error) => {
                    warn!("failed to read input event: {}", error);
                }
            }
        }
    }
}

/// The status of the input handler, returned by
/// [`TestRunner::input_handler_status`](crate::runner::TestRunner::input_handler_status).
pub enum InputHandlerStatus {
    /// The input handler is enabled.
    Enabled {
        /// The character that triggers the "info" event.
        info_char: char,
    },

    /// The input handler is disabled.
    Disabled,
}

#[derive(Clone, Debug)]
struct InputHandlerImpl {
    // `Arc<Mutex<_>>` for coordination between the drop handler and the panic
    // hook.
    guard: Arc<Mutex<imp::InputGuard>>,
}

impl InputHandlerImpl {
    fn new() -> Result<Self, InputHandlerCreateError> {
        let guard = imp::InputGuard::new().map_err(InputHandlerCreateError::EnableNonCanonical)?;

        // At this point, the new terminal state is committed. Install a
        // panic hook to restore the original state.
        let ret = Self {
            guard: Arc::new(Mutex::new(guard)),
        };

        let ret2 = ret.clone();
        let panic_hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(move |info| {
            // Ignore errors to avoid double-panicking.
            if let Err(error) = ret2.finish() {
                eprintln!(
                    "failed to restore terminal state: {}",
                    DisplayErrorChain::new(error)
                );
            }
            panic_hook(info);
        }));

        Ok(ret)
    }

    fn finish(&self) -> Result<(), InputHandlerFinishError> {
        // Do not panic here, in case a panic happened while the thread was
        // locked. Instead, ignore the error.
        let mut locked = self
            .guard
            .lock()
            .map_err(|_| InputHandlerFinishError::Poisoned)?;
        locked.finish().map_err(InputHandlerFinishError::Restore)
    }
}

// Defense in depth -- use both the Drop impl (for regular drops and
// panic=unwind) and a panic hook (for panic=abort).
impl Drop for InputHandlerImpl {
    fn drop(&mut self) {
        if let Err(error) = self.finish() {
            eprintln!(
                "failed to restore terminal state: {}",
                DisplayErrorChain::new(error)
            );
        }
    }
}

#[derive(Debug, Error)]
enum InputHandlerCreateError {
    #[error("failed to enable terminal non-canonical mode")]
    EnableNonCanonical(#[source] imp::Error),
}

#[derive(Debug, Error)]
enum InputHandlerFinishError {
    #[error("mutex was poisoned while restoring terminal state")]
    Poisoned,

    #[error("failed to restore terminal state")]
    Restore(#[source] imp::Error),
}

#[cfg(unix)]
mod imp {
    use libc::{tcgetattr, tcsetattr, ECHO, ICANON, TCSAFLUSH, TCSANOW, VMIN, VTIME};
    use std::{ffi::c_int, io, mem, os::fd::AsRawFd};

    pub(super) type Error = io::Error;

    /// A scope guard to enable non-canonical input mode on Unix platforms.
    ///
    /// Importantly, this does not enable the full raw mode that crossterm
    /// provides -- that disables things like signal processing via the terminal
    /// driver, which is unnecessary for our purposes. Here we only disable
    /// options relevant to the input: echoing and canonical mode.
    #[derive(Clone, Debug)]
    pub(super) struct InputGuard {
        // None indicates that the original state has been restored -- only one
        // entity should do this.
        //
        // Note: originally, this used nix's termios support, but that was found
        // to be buggy on illumos (lock up the terminal) -- apparently, not all
        // bitflags were modeled. Using libc directly is more reliable.
        original: Option<libc::termios>,
    }

    impl InputGuard {
        pub(super) fn new() -> io::Result<Self> {
            let mut termios = mem::MaybeUninit::uninit();
            let res = unsafe { tcgetattr(std::io::stdin().as_raw_fd(), termios.as_mut_ptr()) };
            if res == -1 {
                return Err(io::Error::last_os_error());
            }

            // SAFETY: if res is 0, then termios has been initialized.
            let original = unsafe { termios.assume_init() };

            let mut updated = original;

            // Disable echoing inputs and canonical mode. We don't disable things like ISIG -- we
            // handle that via the signal handler.
            updated.c_lflag &= !(ECHO | ICANON);
            // VMIN is 1 and VTIME is 0: this enables blocking reads of 1 byte
            // at a time with no timeout. See
            // https://linux.die.net/man/3/tcgetattr's "Canonical and
            // noncanonical mode" section.
            updated.c_cc[VMIN] = 1;
            updated.c_cc[VTIME] = 0;

            stdin_tcsetattr(TCSAFLUSH, &updated)?;

            Ok(Self {
                original: Some(original),
            })
        }

        pub(super) fn finish(&mut self) -> io::Result<()> {
            if let Some(original) = self.original.take() {
                stdin_tcsetattr(TCSANOW, &original)
            } else {
                Ok(())
            }
        }
    }

    fn stdin_tcsetattr(optional_actions: c_int, updated: &libc::termios) -> io::Result<()> {
        let res = unsafe { tcsetattr(std::io::stdin().as_raw_fd(), optional_actions, updated) };
        if res == -1 {
            Err(io::Error::last_os_error())
        } else {
            Ok(())
        }
    }
}

#[cfg(windows)]
mod imp {
    use std::{io, os::windows::io::AsRawHandle};
    use windows_sys::Win32::System::Console::{
        GetConsoleMode, SetConsoleMode, CONSOLE_MODE, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT,
    };

    pub(super) type Error = io::Error;

    /// A scope guard to enable raw input mode on Windows.
    ///
    /// Importantly, this does not mask out `ENABLE_PROCESSED_INPUT` like
    /// crossterm does -- that disables things like signal processing via the
    /// terminal driver, which is unnecessary for our purposes. Here we only
    /// disable options relevant to the input: `ENABLE_LINE_INPUT` and
    /// `ENABLE_ECHO_INPUT`.
    #[derive(Clone, Debug)]
    pub(super) struct InputGuard {
        original: Option<CONSOLE_MODE>,
    }

    impl InputGuard {
        pub(super) fn new() -> io::Result<Self> {
            let handle = std::io::stdin().as_raw_handle();

            // Read the original console mode.
            let mut original: CONSOLE_MODE = 0;
            let res = unsafe { GetConsoleMode(handle, &mut original) };
            if res == 0 {
                return Err(io::Error::last_os_error());
            }

            // Mask out ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT.
            let updated = original & !(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);

            // Set the new console mode.
            let res = unsafe { SetConsoleMode(handle, updated) };
            if res == 0 {
                return Err(io::Error::last_os_error());
            }

            Ok(Self {
                original: Some(original),
            })
        }

        pub(super) fn finish(&mut self) -> io::Result<()> {
            if let Some(original) = self.original.take() {
                let handle = std::io::stdin().as_raw_handle();
                let res = unsafe { SetConsoleMode(handle, original) };
                if res == 0 {
                    Err(io::Error::last_os_error())
                } else {
                    Ok(())
                }
            } else {
                Ok(())
            }
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum InputEvent {
    Info,
}