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
180
181
182
183
184
185
186
187
188
189
190
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::process::{Command, Stdio, Child};
use std::str::from_utf8;
use std::sync::Arc;
use std::path::PathBuf;
use super::cmdresult::CmdResult;
use super::fixtures::read_scenario_fixture;
use super::common::log_info;
use super::settings::SceneSettings;
static ALREADY_RUN: &'static str = "you have already run this UCommand, if you want to run \
another command in the same test, use scene.ucmd()";
static MULTIPLE_STDIN_MEANINGLESS: &'static str = "Ucommand is designed around a typical use case of: provide args and input stream -> spawn process -> block until completion -> return output streams. For verifying that a particular section of the input stream is what causes a particular behavior, use the Command type directly.";
pub struct UCommand {
pub raw: Command,
comm_string: String,
settings: Arc<SceneSettings>,
has_run: bool,
stdin: Option<Vec<u8>>
}
impl UCommand {
pub fn new<T: AsRef<OsStr>, U: AsRef<OsStr>>(
invoked: T,
settings: Arc<SceneSettings>,
env_clear: bool,
curdir: Option<U>
) -> UCommand {
let curdir_used = if let Some(curdir_val) = curdir {
PathBuf::from(&curdir_val.as_ref())
} else {
PathBuf::from(settings.as_ref().tmpd.path())
};
UCommand {
settings: settings,
has_run: false,
raw: {
let mut cmd = Command::new(invoked.as_ref());
cmd.current_dir(&curdir_used);
if env_clear {
if cfg!(windows) {
for (key, _) in env::vars_os() {
if key.as_os_str() != "SYSTEMROOT" {
cmd.env_remove(key);
}
}
} else {
cmd.env_clear();
}
}
cmd
},
comm_string: String::from(invoked.as_ref().to_str().unwrap()),
stdin: None
}
}
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> Box<&mut UCommand> {
if self.has_run {
panic!(ALREADY_RUN);
}
self.comm_string.push_str(" ");
self.comm_string.push_str(arg.as_ref().to_str().unwrap());
self.raw.arg(arg.as_ref());
Box::new(self)
}
pub fn arg_fixture<S: AsRef<OsStr>>(&mut self, file_rel_path: S) -> Box<&mut UCommand> {
let contents = read_scenario_fixture(&self.settings, file_rel_path);
self.arg(contents)
}
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> Box<&mut UCommand> {
if self.has_run {
panic!(MULTIPLE_STDIN_MEANINGLESS);
}
for s in args {
self.comm_string.push_str(" ");
self.comm_string.push_str(s.as_ref().to_str().unwrap());
}
self.raw.args(args.as_ref());
Box::new(self)
}
pub fn pipe_in<T: Into<Vec<u8>>>(&mut self, input: T) -> Box<&mut UCommand> {
if self.stdin.is_some() {
panic!(MULTIPLE_STDIN_MEANINGLESS);
}
self.stdin = Some(input.into());
Box::new(self)
}
pub fn pipe_in_fixture<S: AsRef<OsStr>>(&mut self, file_rel_path: S) -> Box<&mut UCommand> {
let contents = read_scenario_fixture(&self.settings, file_rel_path);
self.pipe_in(contents)
}
pub fn env<K, V>(&mut self, key: K, val: V) -> Box<&mut UCommand> where K: AsRef<OsStr>, V: AsRef<OsStr> {
if self.has_run {
panic!(ALREADY_RUN);
}
self.raw.env(key, val);
Box::new(self)
}
pub fn run_no_wait(&mut self) -> Child {
if self.has_run {
panic!(ALREADY_RUN);
}
self.has_run = true;
log_info("run", &self.comm_string);
let mut result = self.raw
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("There was an error running the provided command. Run cargo test with --verbose to see which command caused the failure");
if let Some(ref input) = self.stdin {
result.stdin
.take()
.unwrap_or_else(
|| panic!(
"Could not take child process stdin"))
.write_all(&input)
.unwrap_or_else(|e| panic!("{}", e));
}
result
}
pub fn run(&mut self) -> CmdResult {
let prog = self.run_no_wait().wait_with_output().unwrap();
CmdResult {
settings: self.settings.clone(),
success: prog.status.success(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(),
stderr: from_utf8(&prog.stderr).unwrap().to_string(),
}
}
pub fn run_piped_stdin<T: Into<Vec<u8>>>(&mut self, input: T) -> CmdResult {
self.pipe_in(input).run()
}
pub fn succeeds(&mut self) -> CmdResult {
let cmd_result = self.run();
cmd_result.success();
cmd_result
}
pub fn fails(&mut self) -> CmdResult {
let cmd_result = self.run();
cmd_result.failure();
cmd_result
}
}