Skip to content

Commit

Permalink
fix pipefail issue for run_fun!() macro
Browse files Browse the repository at this point in the history
fix #66
  • Loading branch information
tao-guo committed Sep 4, 2024
1 parent 2995ac0 commit 8090c39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,20 @@ impl FunChildren {

fn inner_wait_with_all(&mut self, capture_stderr: bool) -> (CmdResult, String, String) {
// wait for the last child result
let handle = self.children.pop().unwrap();
let last_handle = self.children.pop().unwrap();
let mut stdout_buf = Vec::new();
let mut stderr = String::new();
let res = handle.wait_with_all(capture_stderr, &mut stdout_buf, &mut stderr);
let _ = CmdChildren::wait_children(&mut self.children);
let last_res = last_handle.wait_with_all(capture_stderr, &mut stdout_buf, &mut stderr);
let res = CmdChildren::wait_children(&mut self.children);
let mut stdout: String = String::from_utf8_lossy(&stdout_buf).into();
if stdout.ends_with('\n') {
stdout.pop();
}
(res, stdout, stderr)
if res.is_err() && !self.ignore_error && process::pipefail_enabled() {
(res, stdout, stderr)
} else {
(last_res, stdout, stderr)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,13 @@ fn test_empty_arg() {
let opt = "";
assert!(run_cmd!(ls $opt).is_ok());
}

#[test]
fn test_pipefail_in_fun() {
assert!(run_cmd!(false | true).is_err());
assert!(run_fun!(false | true).is_err());
assert!(run_fun!(ignore false | true).is_ok());
set_pipefail(false);
assert!(run_fun!(ignore false | true).is_ok());
set_pipefail(true);
}

0 comments on commit 8090c39

Please sign in to comment.