Merge pull request #1137 from amesgen/batch-exit-code

Respect exit codes with `--exec-batch`
This commit is contained in:
David Peter 2022-11-01 20:42:28 +01:00 committed by GitHub
commit b57ed11f65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View file

@ -16,7 +16,7 @@ use argmax::Command;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; 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::command::{execute_commands, handle_cmd_error};
use self::input::{basename, dirname, remove_extension}; 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), Err(e) => handle_cmd_error(None, e),
} }
@ -136,6 +136,7 @@ struct CommandBuilder {
cmd: Command, cmd: Command,
count: usize, count: usize,
limit: usize, limit: usize,
exit_code: ExitCode,
} }
impl CommandBuilder { impl CommandBuilder {
@ -163,6 +164,7 @@ impl CommandBuilder {
cmd, cmd,
count: 0, count: 0,
limit, limit,
exit_code: ExitCode::Success,
}) })
} }
@ -196,7 +198,9 @@ impl CommandBuilder {
fn finish(&mut self) -> io::Result<()> { fn finish(&mut self) -> io::Result<()> {
if self.count > 0 { if self.count > 0 {
self.cmd.try_args(&self.post_args)?; 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.cmd = Self::new_command(&self.pre_args)?;
self.count = 0; self.count = 0;
@ -204,6 +208,10 @@ impl CommandBuilder {
Ok(()) Ok(())
} }
fn exit_code(&self) -> ExitCode {
self.exit_code
}
} }
/// Represents a template that is utilized to generate command strings. /// Represents a template that is utilized to generate command strings.

View file

@ -1506,6 +1506,8 @@ fn test_exec_batch() {
For more information try '--help'\n\ For more information try '--help'\n\
", ",
); );
te.assert_failure_with_error(&["a.foo", "--exec-batch", "bash", "-c", "exit 1"], "");
} }
} }
@ -1561,6 +1563,20 @@ fn test_exec_batch_multi() {
], ],
] ]
); );
te.assert_failure_with_error(
&[
"a.foo",
"--exec-batch",
"echo",
";",
"--exec-batch",
"bash",
"-c",
"exit 1",
],
"",
);
} }
#[test] #[test]