Skip to main content

TestHarness

Struct TestHarness 

Source
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

Source

pub fn new() -> Self

Creates an empty harness with no overrides applied.

Source

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.

Source

pub fn env_remove(self, key: impl Into<String>) -> Self

Removes key from the real environment for the duration of the run.

Source

pub fn terminal_width(self, cols: usize) -> Self

Forces the reported terminal width to cols.

Source

pub fn no_terminal_width(self) -> Self

Forces terminal-width detection to report “unknown” (as if stdout is not a TTY).

Source

pub fn is_tty(self) -> Self

Claims stdout is attached to a TTY.

Source

pub fn no_tty(self) -> Self

Claims stdout is not a TTY (piped, redirected, …).

Source

pub fn with_color(self) -> Self

Declares that the output target supports ANSI color.

Source

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.

Source

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(...).

Source

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.

Source

pub fn text_output(self) -> Self

Source

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.

Source

pub fn interactive_stdin(self) -> Self

Simulates an interactive terminal for stdin (no piped content).

Source

pub fn clipboard(self, content: impl Into<String>) -> Self

Installs content as the mock clipboard. Handlers using ClipboardSource::new() will read it.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn run<I, T>(self, app: &App, cmd: Command, args: I) -> TestResult
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

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§

Source§

impl Default for TestHarness

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more