1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
use std::ffi::OsStr; pub use std::io; pub use std::process::{ Child, ChildStderr, ChildStdin, ChildStdout, Command, ExitStatus, Output, Stdio, }; pub struct Runned { process: Child, } impl Runned { pub fn wait(&mut self) -> io::Result<ExitStatus> { match self.process.try_wait() { Ok(Some(status)) => Ok(status), Ok(None) => self.process.wait(), Err(e) => Err(e), } } pub fn get_stdin(&mut self) -> Option<ChildStdin> { self.process.stdin.take() } pub fn get_stdout(&mut self) -> Option<ChildStdout> { self.process.stdout.take() } pub fn get_stderr(&mut self) -> Option<ChildStderr> { self.process.stderr.take() } } pub struct Runner { cmd: Command, } impl Runner { pub fn new<S: AsRef<OsStr>>(cmd: S, args: Vec<S>) -> Self { let mut cmd = Command::new(cmd); cmd.stdin(Stdio::piped()); cmd.stdout(Stdio::piped()); cmd.stderr(Stdio::piped()); cmd.args(args); Runner { cmd } } pub fn set_stdin<T: Into<Stdio>>(&mut self, stdin: T) -> &mut Self { self.cmd.stdin(stdin); self } pub fn set_stdout<T: Into<Stdio>>(&mut self, stdout: T) -> &mut Self { self.cmd.stdout(stdout); self } pub fn set_stderr<T: Into<Stdio>>(&mut self, stderr: T) -> &mut Self { self.cmd.stderr(stderr); self } pub fn run(&mut self) -> io::Result<Output> { self.cmd.output() } pub fn run_async(&mut self) -> Runned { Runned { process: self.cmd.spawn().expect("failed to execute process"), } } } #[cfg(test)] mod tests { use std::io::{Read, Write}; use super::*; #[test] fn create() { let result = Runner::new("true", vec![]) .run() .expect("failed to execute process"); assert!(result.status.success()); let result = Runner::new("true", vec!["1", "2", "3"]) .run() .expect("failed to execute process"); assert!(result.status.success()); let result = Runner::new("false", vec![]) .run() .expect("failed to execute process"); assert!(!result.status.success()); let result = Runner::new("false", vec!["1", "2", "3"]) .run() .expect("failed to execute process"); assert!(!result.status.success()); } #[test] fn create_async() { let result = Runner::new("true", vec![]) .run_async() .wait() .expect("failed to wait"); assert!(result.success()); let result = Runner::new("true", vec!["1", "2", "3"]) .run_async() .wait() .expect("failed to wait"); assert!(result.success()); let result = Runner::new("false", vec![]) .run_async() .wait() .expect("failed to wait"); assert!(!result.success()); let result = Runner::new("false", vec!["1", "2", "3"]) .run_async() .wait() .expect("failed to wait"); assert!(!result.success()); } #[test] fn std_in_out() { let mut r_async = Runner::new("cat", vec![]).run_async(); { let mut input = r_async.get_stdin().unwrap(); write!(input, "123456").expect("failed in write to pipe"); } let mut output = r_async.get_stdout().unwrap(); let mut line = String::new(); output .read_to_string(&mut line) .expect("failed in read to string"); assert_eq!(line, "123456"); let mut error = r_async.get_stderr().unwrap(); let mut line = String::new(); error .read_to_string(&mut line) .expect("failed in read to string"); assert_eq!(line, ""); } #[test] fn std_pipeline() { let mut r_async = Runner::new("cat", vec![]).run_async(); { let mut input = r_async.get_stdin().unwrap(); write!(input, "123456").expect("failed in write to pipe"); } let mut r_async_two = Runner::new("cat", vec![]) .set_stdin(r_async.get_stdout().unwrap()) .run_async(); let mut output = r_async_two.get_stdout().unwrap(); let mut line = String::new(); output .read_to_string(&mut line) .expect("failed in read to string"); assert_eq!(line, "123456"); let mut error = r_async.get_stderr().unwrap(); let mut line = String::new(); error .read_to_string(&mut line) .expect("failed in read to string"); assert_eq!(line, ""); } }