cargo_test_support

Struct ProcessBuilder

source
pub struct ProcessBuilder {
    program: OsString,
    args: Vec<OsString>,
    env: BTreeMap<String, Option<OsString>>,
    cwd: Option<OsString>,
    wrappers: Vec<OsString>,
    jobserver: Option<Client>,
    display_env_vars: bool,
    retry_with_argfile: bool,
    stdin: Option<Vec<u8>>,
}
Expand description

A builder object for an external process, similar to std::process::Command.

Fields§

§program: OsString§args: Vec<OsString>§env: BTreeMap<String, Option<OsString>>§cwd: Option<OsString>§wrappers: Vec<OsString>§jobserver: Option<Client>§display_env_vars: bool§retry_with_argfile: bool§stdin: Option<Vec<u8>>

Implementations§

source§

impl ProcessBuilder

source

pub fn new<T>(cmd: T) -> ProcessBuilder
where T: AsRef<OsStr>,

Creates a new ProcessBuilder with the given executable path.

source

pub fn program<T>(&mut self, program: T) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Sets the executable for the process.

source

pub fn arg<T>(&mut self, arg: T) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Adds arg to the args list.

source

pub fn args<T>(&mut self, args: &[T]) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Adds multiple args to the args list.

source

pub fn args_replace<T>(&mut self, args: &[T]) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Replaces the args list with the given args.

source

pub fn cwd<T>(&mut self, path: T) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Sets the current working directory of the process.

source

pub fn env<T>(&mut self, key: &str, val: T) -> &mut ProcessBuilder
where T: AsRef<OsStr>,

(chainable) Sets an environment variable for the process.

source

pub fn env_remove(&mut self, key: &str) -> &mut ProcessBuilder

(chainable) Unsets an environment variable for the process.

source

pub fn get_program(&self) -> &OsString

Gets the executable name.

source

pub fn get_args(&self) -> impl Iterator<Item = &OsString>

Gets the program arguments.

source

pub fn get_cwd(&self) -> Option<&Path>

Gets the current working directory for the process.

source

pub fn get_env(&self, var: &str) -> Option<OsString>

Gets an environment variable as the process will see it (will inherit from environment unless explicitally unset).

source

pub fn get_envs(&self) -> &BTreeMap<String, Option<OsString>>

Gets all environment variables explicitly set or unset for the process (not inherited vars).

source

pub fn inherit_jobserver(&mut self, jobserver: &Client) -> &mut ProcessBuilder

Sets the make jobserver. See the jobserver crate for more information.

source

pub fn display_env_vars(&mut self) -> &mut ProcessBuilder

Enables environment variable display.

source

pub fn retry_with_argfile(&mut self, enabled: bool) -> &mut ProcessBuilder

Enables retrying with an argfile if hitting “command line too big” error

This is primarily for the @path arg of rustc and rustdoc, which treat each line as an command-line argument, so LF and CRLF bytes are not valid as an argument for argfile at this moment. For example, RUSTDOCFLAGS="--crate-version foo\nbar" cargo doc is valid when invoking from command-line but not from argfile.

To sum up, the limitations of the argfile are:

  • Must be valid UTF-8 encoded.
  • Must not contain any newlines in each argument.

Ref:

source

pub fn stdin<T>(&mut self, stdin: T) -> &mut ProcessBuilder
where T: Into<Vec<u8>>,

Sets a value that will be written to stdin of the process on launch.

source

pub fn status(&self) -> Result<ExitStatus, Error>

Like Command::status but with a better error message.

source

pub fn exec(&self) -> Result<(), Error>

Runs the process, waiting for completion, and mapping non-success exit codes to an error.

source

pub fn exec_replace(&self) -> Result<(), Error>

Replaces the current process with the target process.

On Unix, this executes the process using the Unix syscall execvp, which will block this process, and will only return if there is an error.

On Windows this isn’t technically possible. Instead we emulate it to the best of our ability. One aspect we fix here is that we specify a handler for the Ctrl-C handler. In doing so (and by effectively ignoring it) we should emulate proxying Ctrl-C handling to the application at hand, which will either terminate or handle it itself. According to Microsoft’s documentation at https://docs.microsoft.com/en-us/windows/console/ctrl-c-and-ctrl-break-signals. the Ctrl-C signal is sent to all processes attached to a terminal, which should include our child process. If the child terminates then we’ll reap them in Cargo pretty quickly, and if the child handles the signal then we won’t terminate (and we shouldn’t!) until the process itself later exits.

source

pub fn output(&self) -> Result<Output, Error>

Like Command::output but with a better error message.

source

pub fn exec_with_output(&self) -> Result<Output, Error>

Executes the process, returning the stdio output, or an error if non-zero exit status.

source

pub fn exec_with_streaming( &self, on_stdout_line: &mut dyn FnMut(&str) -> Result<(), Error>, on_stderr_line: &mut dyn FnMut(&str) -> Result<(), Error>, capture_output: bool, ) -> Result<Output, Error>

Executes a command, passing each line of stdout and stderr to the supplied callbacks, which can mutate the string data.

If any invocations of these function return an error, it will be propagated.

If capture_output is true, then all the output will also be buffered and stored in the returned Output object. If it is false, no caching is done, and the callbacks are solely responsible for handling the output.

source

pub fn build_command(&self) -> Command

Converts ProcessBuilder into a std::process::Command, and handles the jobserver, if present.

Note that this method doesn’t take argfile fallback into account. The caller should handle it by themselves.

source

pub fn wrapped(self, wrapper: Option<impl AsRef<OsStr>>) -> ProcessBuilder

Wraps an existing command with the provided wrapper, if it is present and valid.

§Examples
use cargo_util::ProcessBuilder;
// Running this would execute `rustc`
let cmd = ProcessBuilder::new("rustc");

// Running this will execute `sccache rustc`
let cmd = cmd.wrapped(Some("sccache"));

Trait Implementations§

source§

impl ArgLineCommandExt for &mut ProcessBuilder

source§

fn arg<S: AsRef<OsStr>>(self, s: S) -> Self

source§

fn arg_line(self, s: &str) -> Self

source§

impl ChannelChangerCommandExt for &mut ProcessBuilder

source§

fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self

The list of reasons should be why nightly cargo is needed. If it is because of an unstable feature put the name of the feature as the reason, e.g. &["print-im-a-teapot"].
source§

impl Clone for ProcessBuilder

source§

fn clone(&self) -> ProcessBuilder

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for ProcessBuilder

source§

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

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

impl Display for ProcessBuilder

source§

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

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

impl TestEnvCommandExt for &mut ProcessBuilder

source§

fn current_dir<S: AsRef<Path>>(self, path: S) -> Self

source§

fn env<S: AsRef<OsStr>>(self, key: &str, value: S) -> Self

source§

fn env_remove(self, key: &str) -> Self

source§

fn test_env(self) -> Self

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
§

impl<D> ToDebug for D
where D: Debug,

§

fn to_debug(&self) -> Data

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
§

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

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 160 bytes