-
Notifications
You must be signed in to change notification settings - Fork 34
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
getting the command's exit status after using stderr_to_stdout
#114
Comments
How about this? fn main() -> anyhow::Result<()> {
let bash_script = "echo hi && echo lo 1>&2 && exit 42";
let output = duct::cmd!("bash", "-c", bash_script)
.stderr_to_stdout()
.stdout_capture()
.unchecked()
.run()?;
assert_eq!(output.status.code(), Some(42));
assert_eq!(&output.stdout, b"hi\nlo\n");
Ok(())
} |
I'm using the |
Ah this is a bit tricky. The use duct::cmd;
use std::io::prelude::*;
use std::io::BufReader;
const PYTHON: &str = r#"
import sys
for i in range(1_000_000):
sys.stdout.write(f"stdout {i}\n")
sys.stderr.write(f"stderr {i}\n")
"#;
fn main() -> anyhow::Result<()> {
let reader = cmd!("python3", "-c", PYTHON).stderr_to_stdout().reader()?;
let mut buf_reader = BufReader::new(reader);
let num_lines = (&mut buf_reader).lines().count();
println!("counted {num_lines} lines");
let reader = buf_reader.into_inner();
println!("exit status: {:?}", reader.try_wait()?.unwrap());
Ok(())
} |
That said,
So if what you want is to read the handle all the way to the end and then return an error if the child's status was non-zero, that's what happens automatically! Take a look at this example: use duct::cmd;
use std::io::prelude::*;
use std::io::BufReader;
const PYTHON: &str = r#"
print("one")
print("two")
print("three")
assert 1 == 2 # raise an exception and ultimately exit with a non-zero status
"#;
fn main() -> anyhow::Result<()> {
let reader = cmd!("python3", "-c", PYTHON)
.env("PYTHONUNBUFFERED", "1")
.stderr_to_stdout()
.reader()?;
for line in BufReader::new(reader).lines() {
println!("read a line: {:?}", line?);
}
println!("We never get here!");
Ok(())
}
|
Is it possible for me to call
.stderr_to_stdout()
on an expression and also get an exit status?The text was updated successfully, but these errors were encountered: