feat: add output pipeline
This commit is contained in:
+12
-17
@@ -3,19 +3,10 @@ use std::process::Command;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
|
||||
pub fn run_commands(commands: &[String], workdir: &Path) -> Result<()> {
|
||||
for cmd in commands {
|
||||
run_command(cmd, workdir)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_command(cmd: &str, workdir: &Path) -> Result<()> {
|
||||
pub fn execute(cmd: &str, workdir: &Path) -> Result<()> {
|
||||
let mut command = shell_command(cmd);
|
||||
command.current_dir(workdir);
|
||||
|
||||
println!("[Local] running: {cmd} ...");
|
||||
|
||||
let status = command
|
||||
.status()
|
||||
.with_context(|| format!("failed to spawn '{cmd}' in {}", workdir.display()))?;
|
||||
@@ -25,7 +16,7 @@ fn run_command(cmd: &str, workdir: &Path) -> Result<()> {
|
||||
Some(code) => format!("exit code {code}"),
|
||||
None => "terminated by signal".to_string(),
|
||||
};
|
||||
bail!("[Local] running: {cmd} ... FAILED ({code})");
|
||||
bail!("{cmd} FAILED ({code})");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -85,20 +76,24 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn successful_commands_pass_silently() {
|
||||
let cmds = vec![ok_cmd(), ok_cmd()];
|
||||
run_commands(&cmds, Path::new(".")).unwrap();
|
||||
execute(&ok_cmd(), Path::new(".")).unwrap();
|
||||
execute(&ok_cmd(), Path::new(".")).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fail_fast_stops_on_first_error_with_exit_code() {
|
||||
let workdir = tmpdir("failfast");
|
||||
let marker = workdir.join("first-ran.txt");
|
||||
let cmds = vec![
|
||||
let cmds = [
|
||||
marker_cmd("first-ran.txt"),
|
||||
fail_cmd(7),
|
||||
marker_cmd("never.txt"),
|
||||
];
|
||||
let err = run_commands(&cmds, &workdir).unwrap_err();
|
||||
let err = cmds
|
||||
.iter()
|
||||
.map(|cmd| execute(cmd, &workdir))
|
||||
.collect::<Result<Vec<_>>>()
|
||||
.unwrap_err();
|
||||
assert!(err.to_string().contains("exit code 7"), "{err:#}");
|
||||
assert!(marker.exists(), "first command did not run");
|
||||
assert!(
|
||||
@@ -111,7 +106,7 @@ mod tests {
|
||||
#[test]
|
||||
fn commands_run_in_workdir() {
|
||||
let workdir = tmpdir("workdir");
|
||||
run_commands(&[marker_cmd("in-workdir.txt")], &workdir).unwrap();
|
||||
execute(&marker_cmd("in-workdir.txt"), &workdir).unwrap();
|
||||
assert!(workdir.join("in-workdir.txt").exists());
|
||||
let _ = fs::remove_dir_all(workdir);
|
||||
}
|
||||
@@ -120,7 +115,7 @@ mod tests {
|
||||
fn missing_workdir_is_contextual_error() {
|
||||
let missing = tmpdir("gone");
|
||||
fs::remove_dir(&missing).unwrap();
|
||||
let err = run_commands(&[ok_cmd()], &missing).unwrap_err();
|
||||
let err = execute(&ok_cmd(), &missing).unwrap_err();
|
||||
assert!(err.to_string().contains("failed to spawn"), "{err:#}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user