pub struct TestHarness { /* private fields */ }Expand description
Fluent builder for in-process CLI tests.
See the crate-level docs for the usage pattern. The harness
installs every override in TestHarness::run and tears them down on
Drop, so a failed assertion never leaks state into the next test.
Implementations§
Source§impl TestHarness
impl TestHarness
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Sets key=value as a real environment variable for the duration of
the run. Handlers that use EnvSource::new / std::env::var will
see it.
Sourcepub fn env_remove(self, key: impl Into<String>) -> Self
pub fn env_remove(self, key: impl Into<String>) -> Self
Removes key from the real environment for the duration of the run.
Sourcepub fn terminal_width(self, cols: usize) -> Self
pub fn terminal_width(self, cols: usize) -> Self
Forces the reported terminal width to cols.
Sourcepub fn no_terminal_width(self) -> Self
pub fn no_terminal_width(self) -> Self
Forces terminal-width detection to report “unknown” (as if stdout is not a TTY).
Sourcepub fn with_color(self) -> Self
pub fn with_color(self) -> Self
Declares that the output target supports ANSI color.
Sourcepub fn no_color(self) -> Self
pub fn no_color(self) -> Self
Declares that the output target does not support ANSI color. When
--output=auto is used, this forces the Text render path.
Sourcepub fn output_mode(self, mode: OutputMode) -> Self
pub fn output_mode(self, mode: OutputMode) -> Self
Forces a specific OutputMode regardless of the --output flag.
Internally this injects --<flag>=<mode> as the last argument when
TestHarness::run is called. <flag> defaults to output;
override it with output_flag_name for
apps that renamed the flag via AppBuilder::output_flag(...).
Sourcepub fn output_flag_name(self, name: impl Into<String>) -> Self
pub fn output_flag_name(self, name: impl Into<String>) -> Self
Configures the CLI flag name used to force output_mode.
Defaults to "output" (matching AppBuilder’s default). Change it
if the app under test was built with a renamed flag (e.g.
AppBuilder::output_flag(Some("format"))).
No-op when output_mode isn’t set.
Sourcepub fn text_output(self) -> Self
pub fn text_output(self) -> Self
Shortcut for output_mode(OutputMode::Text).
Sourcepub fn piped_stdin(self, content: impl Into<String>) -> Self
pub fn piped_stdin(self, content: impl Into<String>) -> Self
Simulates piped stdin with content. Handlers using
StdinSource::new() will see is_terminal() == false and read
content.
Sourcepub fn interactive_stdin(self) -> Self
pub fn interactive_stdin(self) -> Self
Simulates an interactive terminal for stdin (no piped content).
Sourcepub fn clipboard(self, content: impl Into<String>) -> Self
pub fn clipboard(self, content: impl Into<String>) -> Self
Installs content as the mock clipboard. Handlers using
ClipboardSource::new() will read it.
Sourcepub fn prompts(self, responder: Arc<dyn PromptResponder>) -> Self
pub fn prompts(self, responder: Arc<dyn PromptResponder>) -> Self
Installs a PromptResponder that
every .prompt() call on a standout_input interactive source
will route through during the run.
Use this to test wizard / setup / REPL flows that call
InquireText::new(...).prompt(), InquireSelect::new(...).prompt(),
etc., without launching real prompts. The
ScriptedResponder bundled with
standout-input covers the common case:
use standout_input::{PromptResponse, ScriptedResponder};
use std::sync::Arc;
let result = TestHarness::new()
.prompts(Arc::new(ScriptedResponder::new([
PromptResponse::text("buy milk"), // first text prompt
PromptResponse::Bool(true), // first confirm
PromptResponse::Choice(2), // first select -> options[2]
])))
.run(&app, cmd, ["mycli", "setup"]);The responder is installed via
set_default_prompt_responder
for the duration of the run and reset on drop, matching the
stdin / clipboard pattern.
Sourcepub fn cwd(self, path: impl Into<PathBuf>) -> Self
pub fn cwd(self, path: impl Into<PathBuf>) -> Self
Sets the working directory for the run to path.
If not set and any fixture is declared, the
harness uses the fixture tempdir as the cwd.
Sourcepub fn fixture(self, path: impl AsRef<Path>, content: impl Into<String>) -> Self
pub fn fixture(self, path: impl AsRef<Path>, content: impl Into<String>) -> Self
Declares a file that should exist at path (relative to the
fixture tempdir) with the given text content.
The first call to fixture creates a fresh tempfile::TempDir
which becomes the default cwd. Access it via tempdir.
§Panics
Panics if path is absolute or contains a .. component — both
would let the fixture escape the harness-owned tempdir and
potentially clobber files in the user’s real filesystem.
Sourcepub fn fixture_bytes(
self,
path: impl AsRef<Path>,
content: impl Into<Vec<u8>>,
) -> Self
pub fn fixture_bytes( self, path: impl AsRef<Path>, content: impl Into<Vec<u8>>, ) -> Self
Declares a binary fixture file. Same as fixture
but takes raw bytes. Applies the same path validation.
Sourcepub fn tempdir(&self) -> Option<&Path>
pub fn tempdir(&self) -> Option<&Path>
Returns the fixture tempdir path if one has been allocated.
Useful for constructing absolute paths to pass as handler arguments.
Sourcepub fn run<I, T>(self, app: &App, cmd: Command, args: I) -> TestResult
pub fn run<I, T>(self, app: &App, cmd: Command, args: I) -> TestResult
Installs every override, runs app with the given cmd definition
and argv, and returns a TestResult.
Overrides are torn down when the returned guard held inside the
TestResult is dropped. The TestResult and the harness share the
same lifetime, so a typical test binds the result and lets it fall
out of scope at the end.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TestHarness
impl !RefUnwindSafe for TestHarness
impl Send for TestHarness
impl Sync for TestHarness
impl Unpin for TestHarness
impl UnsafeUnpin for TestHarness
impl !UnwindSafe for TestHarness
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more