refactor: one helper each for the repeated blocks

This commit is contained in:
2026-09-09 12:42:23 +00:00
parent 5dbe99a304
commit c55273d80c
6 changed files with 104 additions and 58 deletions

View File

@@ -1,3 +1,5 @@
use anyhow::Result;
use super::{Argv, Cmd};
// sh -c, the only way to reach a shell feature inside a container
@@ -30,9 +32,9 @@ impl Pipeline {
}
}
pub fn pipe(mut self, next: &dyn Cmd) -> Self {
self.stages.push(next.shell());
self
pub fn pipe(mut self, next: &dyn Cmd) -> Result<Self> {
self.stages.push(next.shell()?);
Ok(self)
}
// for a pipeline whose last stage closes the pipe on purpose, where the
@@ -119,21 +121,26 @@ mod tests {
#[test]
fn a_pipeline_reports_a_stage_that_dies_mid_stream() {
let script = Gunzip.pipe(&Gzip).shell();
let script = Gunzip.pipe(&Gzip).unwrap().shell().unwrap();
assert_eq!(script, "sh -c 'set -o pipefail; gunzip -c | gzip'");
}
#[test]
fn a_pipeline_that_closes_the_pipe_on_purpose_keeps_the_default() {
let script = Gunzip.pipe(&Head::bytes(512)).allow_early_close().shell();
let script = Gunzip
.pipe(&Head::bytes(512))
.unwrap()
.allow_early_close()
.shell()
.unwrap();
assert_eq!(script, "sh -c 'gunzip -c | head -c 512'");
}
#[test]
fn a_pipeline_reaches_a_container_as_one_argument() {
let argv = Gunzip.pipe(&Gzip).in_container("abc123").argv();
let argv = Gunzip.pipe(&Gzip).unwrap().in_container("abc123").argv();
assert_eq!(
argv.words(),