From cb95f1dcd533b33677c97f5005f5721f9b6ae8ec Mon Sep 17 00:00:00 2001 From: amesgen Date: Thu, 13 Oct 2022 23:33:54 +0200 Subject: [PATCH] Respect exit codes with `--exec-batch` --- src/exec/mod.rs | 14 +++++++++++--- tests/tests.rs | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/exec/mod.rs b/src/exec/mod.rs index da83223..33ddaae 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -16,7 +16,7 @@ use argmax::Command; use once_cell::sync::Lazy; use regex::Regex; -use crate::exit_codes::ExitCode; +use crate::exit_codes::{merge_exitcodes, ExitCode}; use self::command::{execute_commands, handle_cmd_error}; use self::input::{basename, dirname, remove_extension}; @@ -120,7 +120,7 @@ impl CommandSet { } } - ExitCode::Success + merge_exitcodes(builders.iter().map(|b| b.exit_code())) } Err(e) => handle_cmd_error(None, e), } @@ -136,6 +136,7 @@ struct CommandBuilder { cmd: Command, count: usize, limit: usize, + exit_code: ExitCode, } impl CommandBuilder { @@ -163,6 +164,7 @@ impl CommandBuilder { cmd, count: 0, limit, + exit_code: ExitCode::Success, }) } @@ -196,7 +198,9 @@ impl CommandBuilder { fn finish(&mut self) -> io::Result<()> { if self.count > 0 { self.cmd.try_args(&self.post_args)?; - self.cmd.status()?; + if !self.cmd.status()?.success() { + self.exit_code = ExitCode::GeneralError; + } self.cmd = Self::new_command(&self.pre_args)?; self.count = 0; @@ -204,6 +208,10 @@ impl CommandBuilder { Ok(()) } + + fn exit_code(&self) -> ExitCode { + self.exit_code + } } /// Represents a template that is utilized to generate command strings. diff --git a/tests/tests.rs b/tests/tests.rs index 50157c4..242d4bc 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1496,6 +1496,8 @@ fn test_exec_batch() { &["foo", "--exec-batch", "echo {}"], "[fd error]: First argument of exec-batch is expected to be a fixed executable", ); + + te.assert_failure_with_error(&["a.foo", "--exec-batch", "bash", "-c", "exit 1"], ""); } } @@ -1551,6 +1553,20 @@ fn test_exec_batch_multi() { ], ] ); + + te.assert_failure_with_error( + &[ + "a.foo", + "--exec-batch", + "echo", + ";", + "--exec-batch", + "bash", + "-c", + "exit 1", + ], + "", + ); } #[test]