Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to read exit code and specific streams #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,16 @@ impl Drop for PushEnv<'_> {
}
}

/// The output of a finished process for specific stream. Another stream will be printed to the corresponds system stream
#[derive(Debug)]
pub struct StreamOutput {
/// The status (exit code) of the process.
pub exit_status: ExitStatus,
/// The data that the process wrote to stdout or stderr.
/// Any trailing newline or carriage return will be trimmed.
pub stream_output: String,
}

/// A builder object for constructing a subprocess.
///
/// A [`Cmd`] is usually created with the [`cmd!`] macro. The command exists
Expand Down Expand Up @@ -968,21 +978,33 @@ impl<'a> Cmd<'a> {

/// Run the command and return its stdout as a string. Any trailing newline or carriage return will be trimmed.
pub fn read(&self) -> Result<String> {
self.read_stream(false)
Ok(self.read_stream(false)?.stream_output)
}

/// Run the command and return its stderr as a string. Any trailing newline or carriage return will be trimmed.
/// Run the command and return its stderr as a string.
pub fn read_stderr(&self) -> Result<String> {
Ok(self.read_stream(true)?.stream_output)
}

/// Run the command and return its stderr as a string and exit status.
/// Any trailing newline or carriage return will be trimmed.
pub fn read_stderr_output(&self) -> Result<StreamOutput> {
self.read_stream(true)
}

/// Run the command and return its stdout as a string and exit status.
/// Any trailing newline or carriage return will be trimmed.
pub fn read_stdout_output(&self) -> Result<StreamOutput> {
self.read_stream(false)
}

/// Run the command and return its output.
pub fn output(&self) -> Result<Output> {
self.output_impl(true, true)
}
// endregion:running

fn read_stream(&self, read_stderr: bool) -> Result<String> {
fn read_stream(&self, read_stderr: bool) -> Result<StreamOutput> {
let read_stdout = !read_stderr;
let output = self.output_impl(read_stdout, read_stderr)?;
self.check_status(output.status)?;
Expand All @@ -997,7 +1019,7 @@ impl<'a> Cmd<'a> {
stream.pop();
}

Ok(stream)
Ok(StreamOutput { exit_status: output.status, stream_output: stream })
}

fn output_impl(&self, read_stdout: bool, read_stderr: bool) -> Result<Output> {
Expand Down
24 changes: 24 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ fn multiline() {
assert_eq!(output, "hello");
}

#[test]
fn read_with_exit_status() {
let sh = setup();
let output = cmd!(
sh,
"
xecho hello
"
)
.read_stdout_output()
.unwrap();
assert_eq!(output.stream_output, "hello");
assert!(output.exit_status.success());
}

#[test]
fn interpolation() {
let sh = setup();
Expand Down Expand Up @@ -185,6 +200,15 @@ fn read_stderr() {
assert!(output.contains("snafu"));
}

#[test]
fn read_stderr_with_exit_code() {
let sh = setup();

let output = cmd!(sh, "xecho -f -e snafu").ignore_status().read_stderr_output().unwrap();
assert!(output.stream_output.contains("snafu"));
assert!(!output.exit_status.success())
}

#[test]
fn unknown_command() {
let sh = setup();
Expand Down
Loading