Skip to main content

CliRunner

Struct CliRunner 

Source
pub struct CliRunner { /* private fields */ }
Expand description

A test runner for CLI applications.

CliRunner provides a controlled environment for testing CLI commands. It captures stdout, stderr, and tracks exit codes.

§Example

use click::testing::CliRunner;
use click::command::Command;

let cmd = Command::new("greet")
    .callback(|ctx| {
        let default_name = "World".to_string();
        let name = ctx.get_param::<String>("name").unwrap_or(&default_name);
        // Use name for greeting
        Ok(())
    })
    .build();

let runner = CliRunner::new();
let result = runner.invoke(&cmd, &[]);

assert_eq!(result.exit_code, 0);

Implementations§

Source§

impl CliRunner

Source

pub fn new() -> Self

Create a new CLI runner with default settings.

§Example
use click::testing::CliRunner;

let runner = CliRunner::new();
Source

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

Set an environment variable for the invocation.

§Example
use click::testing::CliRunner;

let runner = CliRunner::new()
    .env("MY_VAR", "value");
Source

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

Unset an environment variable for the invocation.

Source

pub fn env_clear(self) -> Self

Clear all custom environment settings.

Source

pub fn echo_stdin(self, echo: bool) -> Self

Set whether to echo stdin to output.

Source

pub fn mix_stderr(self, mix: bool) -> Self

Set whether to mix stderr into stdout.

When true (default), InvokeResult.output contains stdout + stderr. The InvokeResult.stderr field is always populated with captured stderr.

Source

pub fn catch_panics(self, catch: bool) -> Self

Set whether panics should be captured as a failure instead of re-panicking.

Source

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

Set the charset used to decode captured output.

Defaults to "utf-8". Unknown labels fall back to UTF-8.

Source

pub fn invoke(&self, cmd: &dyn CommandLike, args: &[&str]) -> InvokeResult

Invoke a command and capture the result.

§Arguments
  • cmd - The command to invoke
  • args - Command-line arguments
§Example
use click::testing::CliRunner;
use click::command::Command;

let cmd = Command::new("hello")
    .callback(|_| {
        println!("Hello!");
        Ok(())
    })
    .build();

let result = CliRunner::new().invoke(&cmd, &[]);
assert_eq!(result.exit_code, 0);
Source

pub fn invoke_with_input( &self, cmd: &dyn CommandLike, args: &[&str], input: Option<&str>, ) -> InvokeResult

Invoke a command with simulated stdin input.

§Arguments
  • cmd - The command to invoke
  • args - Command-line arguments
  • input - Optional stdin input
§Example
use click::testing::CliRunner;
use click::command::Command;

let cmd = Command::new("echo")
    .callback(|_| Ok(()))
    .build();

let result = CliRunner::new().invoke_with_input(&cmd, &[], Some("test input"));
Source

pub fn invoke_isolated( &self, cmd: &dyn CommandLike, args: &[&str], ) -> InvokeResult

Invoke a command within an isolated filesystem.

This creates a temporary directory and runs the command there.

§Arguments
  • cmd - The command to invoke
  • args - Command-line arguments
§Example
use click::testing::CliRunner;
use click::command::Command;

let cmd = Command::new("test").build();
let result = CliRunner::new().invoke_isolated(&cmd, &[]);

Trait Implementations§

Source§

impl Clone for CliRunner

Source§

fn clone(&self) -> CliRunner

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CliRunner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CliRunner

Source§

fn default() -> CliRunner

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.