From cd82806ee10d0c21b9e95518351606dc8c22ed7a Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Fri, 16 Dec 2022 20:49:14 +0100 Subject: [PATCH] Fix and improve call_cmd_with_input, add tests. --- src/utils.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 7871ab4..2ecc690 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,13 +4,31 @@ use std::process::{Command, Stdio}; pub fn call_cmd_with_input(cmd: &str, args: Vec<&str>, input: &str) -> io::Result { let mut cmd = Command::new(cmd).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; - let stdin = cmd.stdin.as_mut().unwrap(); - stdin.write_all(input.as_bytes())?; + let mut stdin = cmd.stdin.take().unwrap(); let stdout = cmd.stdout.as_mut().unwrap(); + let in_data = input.to_string(); + let write_handle = std::thread::spawn(move || stdin.write_all(in_data.as_bytes())); let mut output = Vec::new(); stdout.read_to_end(&mut output)?; + match write_handle.join() { + Ok(_) => (), + Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Failed to run command")), + } match String::from_utf8(output) { Ok(x) => Ok(x), Err(err) => Err(io::Error::new(io::ErrorKind::InvalidData, err.utf8_error())), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cmd_exec_test() { + assert_eq!(call_cmd_with_input("true", vec![], "").unwrap(), "".to_string()); + assert_eq!(call_cmd_with_input("cat", vec![], "ok").unwrap(), "ok".to_string()); + assert_ne!(call_cmd_with_input("cat", vec![], "notok").unwrap(), "ok".to_string()); + assert_eq!(call_cmd_with_input("echo", vec!["-n", "test is ok"], "").unwrap(), "test is ok".to_string()); + } +}