pub struct ShellOptions<Cmd = Command> { /* private fields */ }
Expand description

Options for executing commands in the shell. Used in Transcript::from_inputs() and in TestConfig.

The type param maps to extensions available for the shell. For example, StdShell extension allows to specify custom aliases for the executed commands.

Implementations§

source§

impl ShellOptions<StdShell>

source

pub fn sh() -> Self

Creates options for an sh shell.

source

pub fn bash() -> Self

Creates options for a Bash shell.

source

pub fn pwsh() -> Self

Creates options for PowerShell 6+ (the one with the pwsh executable).

source

pub fn with_alias(self, name: &str, path_to_bin: &str) -> Self

Creates an alias for the binary at path_to_bin, which should be an absolute path. This allows to call the binary using this alias without complex preparations (such as installing it globally via cargo install), and is more flexible than Self::with_cargo_path().

In integration tests, you may use env!("CARGO_BIN_EXE_<name>") to get a path to binary targets.

Limitations
  • For Bash and PowerShell, name must be a valid name of a function. For sh, name must be a valid name for the alias command. The name validity is not checked.
source§

impl<Cmd: ConfigureCommand> ShellOptions<Cmd>

source

pub fn new(command: Cmd) -> Self

Creates new options with the provided command.

source

pub fn echoing(self, is_echoing: bool) -> ShellOptions<Echoing<Cmd>>

Sets the echoing flag for the shell.

source

pub fn with_current_dir(self, current_dir: impl AsRef<Path>) -> Self

Changes the current directory of the command.

source

pub fn with_io_timeout(self, io_timeout: Duration) -> Self

Sets the I/O timeout for shell commands. This determines how long methods like Transcript::from_inputs() wait for output of a command before proceeding to the next command. Longer values allow to capture output more accurately, but result in longer execution.

By default, the I/O timeout is 0.5 seconds.

source

pub fn with_init_timeout(self, init_timeout: Duration) -> Self

Sets an additional initialization timeout (relative to the one set by Self::with_io_timeout()) before reading the output of the each command input into the shell.

By default, the initialization timeout is 1.5 seconds.

source

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

Adds an initialization command. Such commands are sent to the shell before executing any user input. The corresponding output from the shell is not captured.

source

pub fn with_env(self, name: impl AsRef<str>, value: impl AsRef<OsStr>) -> Self

Sets the value of an environment variable with the specified name.

source

pub fn with_line_decoder<E, F>(self, mapper: F) -> Selfwhere E: Into<Box<dyn Error + Send + Sync>>, F: FnMut(Vec<u8>) -> Result<String, E> + 'static,

Sets the line decoder for the shell. This allows for custom shell text encodings.

The default decoder used is the UTF-8 one. It halts processing with an error if the input is not UTF-8; you may use Self::with_lossy_utf8_decoder() to swallow errors in this case.

source

pub fn with_lossy_utf8_decoder(self) -> Self

Sets the lossy UTF-8 decoder which always succeeds at decoding at the cost of replacing non-UTF-8 chars.

source

pub fn with_status_check<F>( self, command: impl Into<String>, checker: F ) -> Selfwhere F: Fn(&Captured) -> Option<ExitStatus> + 'static,

Sets the ExitStatus checker for the shell. See ExitStatus docs for the semantics of exit statuses.

Arguments
  • command is a command that will be executed to check the exit status of the latest executed command. E.g., in sh-like shells one can use echo $?.
  • checker is a closure that transforms the output of command into an ExitStatus. The output is provided as a Captured string; it usually makes sense to use Captured::to_plaintext() to strip it of possible escape sequences (especially important if captured from PTY). If the exit status is inconclusive or not applicable, the closure should return None.

The command will be executed after each UserInput is input into the shell and the corresponding output is captured. After this, the Captured output will be supplied to the checker closure and its output will be recorded as Interaction::exit_status().

Panics

Panics if command contains newline chars ('\n' or '\r').

source

pub fn with_cargo_path(self) -> Self

Adds paths to cargo binaries (including examples) to the PATH env variable for the shell described by these options. This allows to call them by the corresponding filename, without specifying a path or doing complex preparations (e.g., calling cargo install).

Limitations
  • The caller must be a unit or integration test; the method will work improperly otherwise.
source

pub fn with_additional_path(self, path: impl Into<PathBuf>) -> Self

Adds a specified path to the PATH env variable for the shell described by these options. This method can be called multiple times to add multiple paths and is composable with Self::with_cargo_path().

Trait Implementations§

source§

impl<Cmd: Debug> Debug for ShellOptions<Cmd>

source§

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

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

impl Default for ShellOptions

source§

fn default() -> Self

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

impl<Cmd: ConfigureCommand> From<Cmd> for ShellOptions<Cmd>

source§

fn from(command: Cmd) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Cmd = Command> !RefUnwindSafe for ShellOptions<Cmd>

§

impl<Cmd = Command> !Send for ShellOptions<Cmd>

§

impl<Cmd = Command> !Sync for ShellOptions<Cmd>

§

impl<Cmd> Unpin for ShellOptions<Cmd>where Cmd: Unpin,

§

impl<Cmd = Command> !UnwindSafe for ShellOptions<Cmd>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for Twhere T: Any,

§

fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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