Respect exit codes with --exec-batch

This commit is contained in:
amesgen 2022-10-13 23:33:54 +02:00
parent 0984ed91ea
commit cb95f1dcd5
No known key found for this signature in database
GPG key ID: 1A89EC203635A13D
2 changed files with 27 additions and 3 deletions

View file

@ -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.

View file

@ -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]