1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

uucore: process: fix exit status processing (#1743)

* uucore: process: fix exit status processing

* tests: timeout: check whether subcommand exit codes are returned
This commit is contained in:
Alex Lyon 2021-02-23 01:21:01 -08:00 committed by GitHub
parent 7e5d9ee32d
commit 5431e947bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 44 deletions

View file

@ -1 +1,18 @@
// ToDO: add tests
use crate::common::util::*;
// FIXME: this depends on the system having true and false in PATH
// the best solution is probably to generate some test binaries that we can call for any
// utility that requires executing another program (kill, for instance)
#[test]
fn test_subcommand_retcode() {
new_ucmd!()
.arg("1")
.arg("true")
.succeeds();
new_ucmd!()
.arg("1")
.arg("false")
.run()
.status_code(1);
}

View file

@ -69,6 +69,8 @@ pub fn repeat_str(s: &str, n: u32) -> String {
pub struct CmdResult {
//tmpd is used for convenience functions for asserts against fixtures
tmpd: Option<Rc<TempDir>>,
/// exit status for command (if there is one)
pub code: Option<i32>,
/// zero-exit from running the Command?
/// see [`success`]
pub success: bool,
@ -91,6 +93,12 @@ impl CmdResult {
Box::new(self)
}
/// asserts that the command's exit code is the same as the given one
pub fn status_code(&self, code: i32) -> Box<&CmdResult> {
assert!(self.code == Some(code));
Box::new(self)
}
/// asserts that the command resulted in empty (zero-length) stderr stream output
/// generally, it's better to use stdout_only() instead,
/// but you might find yourself using this function if
@ -573,6 +581,7 @@ impl UCommand {
CmdResult {
tmpd: self.tmpd.clone(),
code: prog.status.code(),
success: prog.status.success(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(),
stderr: from_utf8(&prog.stderr).unwrap().to_string(),