use super::*;
use crate::cli_outbound::Chunk;
use tempfile::tempdir;
fn drained(stream: Stream) -> (String, i32) {
let mut out = Vec::new();
let mut exit = -1;
for chunk in stream {
match chunk {
Chunk::Stdout(bytes) | Chunk::Stderr(bytes) => out.extend(bytes),
Chunk::Exited(info) => exit = info.shell_code(),
}
}
(String::from_utf8_lossy(&out).into_owned(), exit)
}
#[test]
fn the_input_arrives_and_the_pipe_closes() {
let dir = tempdir().expect("tmp");
let tool = dir.path().join("cat-tool");
crate::test_support::write_exec(&tool, "#!/bin/sh\ncat\nexit 3\n");
let stream = Cli::new(&tool)
.run_input(Some(dir.path()), b"{\"command\":\"ls\"}", &[])
.expect("spawned");
assert_eq!(
drained(stream),
("{\"command\":\"ls\"}".to_owned(), 3),
"read to EOF, and the exit is the child's"
);
}
#[test]
fn a_child_that_ignores_its_input_still_answers() {
let dir = tempdir().expect("tmp");
let tool = dir.path().join("deaf-tool");
crate::test_support::write_exec(&tool, "#!/bin/sh\necho 'never read it'\n");
let stream = Cli::new(&tool)
.run_input(None, &vec![b'x'; 4096], &[])
.expect("spawned");
assert_eq!(drained(stream), ("never read it\n".to_owned(), 0));
}
#[test]
fn an_unspawnable_command_is_the_one_error() {
let dir = tempdir().expect("tmp");
assert!(
Cli::new(dir.path().join("no-such-binary"))
.run_input(None, b"", &[])
.is_err()
);
}