capture_stdio/lib.rs
1//! This crate provide some helper functions to capture the stdin/out/err of the current process to help mocking.
2//!
3//! In testing, `cargo test` capture the stdout/err via `std::io::set_output_capture`. We can use the same mechanism to intercept [print!] and [eprint!] output. The crate wraps the call in [OutputCapture] so that you can intercept the output easily.
4//!
5//! The crate also implements a `pipe`-then-`dup` method to intercept stdio. In detail, it creates a pipe and replaces the fd of stdio. You can use [PipedStdin] to intercept stdin, use [PipedStdout] for stdout and [PipedStderr] for stderr.
6#![feature(internal_output_capture)]
7
8use std::io::Error;
9
10pub mod output_capture;
11pub mod pipe;
12
13pub use output_capture::*;
14pub use pipe::*;
15
16/// Common trait to capture stdio and then restore
17///
18/// You should use [Capture::capture] to begin intercept and restore it via [Capture::restore] or [Drop].
19pub trait Capture: Sized {
20 /// Capture stdio
21 fn capture() -> Result<Self, Error>;
22
23 /// Restore stdio
24 fn restore(&mut self);
25}