Run external programs



examples/external/run-external-command/src/main.rs
use std::process::Command;

fn main() {
    check(3);
    check(0);
}

fn check(exit_code: i32) {
    let result = Command::new("../all/target/release/all")
        .arg(format!("{exit_code}"))
        .output()
        .expect("failed to execute process");
    print!("{}", std::str::from_utf8(&result.stdout).unwrap());
    print!("{}", std::str::from_utf8(&result.stderr).unwrap());
    println!("{}", result.status);

    assert_eq!(std::str::from_utf8(&result.stdout).unwrap(), "to stdout\n");
    assert_eq!(
        std::str::from_utf8(&result.stderr).unwrap(),
        format!("to stderr. Exit code {}\n", exit_code)
    );
    if exit_code == 0 {
        assert!(result.status.success());
    }
    assert_eq!(result.status.code().unwrap(), exit_code);
    println!();
}

to stdout
to stderr. Exit code 3
exit status: 3

to stdout
to stderr. Exit code 0
exit status: 0


see