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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use bintest::BinTest;
use std::ffi::OsStr;
use std::path::Path;
use std::process::{Child, Command, Output, Stdio};
use testpath::TestPath;
enum ExeLocation<'a> {
BinTest {
executables: &'a BinTest,
name: &'a str,
},
External(&'a Path),
}
pub struct TestCall<'a> {
executable: ExeLocation<'a>,
dir: Option<&'a dyn TestPath>,
}
impl<'a> TestCall<'a> {
pub fn new(executables: &'a BinTest, name: &'a str) -> TestCall<'a> {
TestCall {
executable: ExeLocation::BinTest { executables, name },
dir: None,
}
}
pub fn external_command(path: &'a Path) -> TestCall<'a> {
TestCall {
executable: ExeLocation::External(path),
dir: None,
}
}
pub fn current_dir(&mut self, dir: &'a dyn TestPath) -> &mut Self {
self.dir = Some(dir);
self
}
#[track_caller]
pub fn call_args_envs<IA, S, IE, K, V>(&self, args: IA, envs: IE) -> Output
where
IA: IntoIterator<Item = S>,
S: AsRef<OsStr>,
IE: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
let mut command = match self.executable {
ExeLocation::BinTest { executables, name } => executables.command(name),
ExeLocation::External(path) => Command::new(path),
};
if let Some(dir) = &self.dir {
command.current_dir(dir.path());
}
let mut envs = envs.into_iter().fuse().peekable();
if envs.peek().is_some() {
command.env_clear();
command.envs(envs);
}
let output = command.args(args).output().expect("called command");
output
}
#[inline]
#[track_caller]
pub fn call_args<IA, S>(&self, args: IA) -> Output
where
IA: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.call_args_envs(args, NO_ENVS)
}
#[inline]
#[track_caller]
pub fn call_envs<IE, K, V>(&self, envs: IE) -> Output
where
IE: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.call_args_envs(NO_ARGS, envs)
}
#[inline]
#[track_caller]
pub fn call(&self) -> Output {
self.call_args_envs(NO_ARGS, NO_ENVS)
}
#[track_caller]
pub fn spawn_args_envs<IA, S, IE, K, V>(&self, args: IA, envs: IE) -> TestChild
where
IA: IntoIterator<Item = S>,
S: AsRef<OsStr>,
IE: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
let mut command = match self.executable {
ExeLocation::BinTest { executables, name } => executables.command(name),
ExeLocation::External(path) => Command::new(path),
};
if let Some(dir) = &self.dir {
command.current_dir(dir.path());
}
let mut envs = envs.into_iter().fuse().peekable();
if envs.peek().is_some() {
command.env_clear();
command.envs(envs);
}
command.stdout(Stdio::piped()).stderr(Stdio::piped());
TestChild(command.args(args).spawn().expect("spawned command"))
}
#[inline]
#[track_caller]
pub fn spawn_args<IA, S>(&self, args: IA) -> TestChild
where
IA: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.spawn_args_envs(args, NO_ENVS)
}
#[inline]
#[track_caller]
pub fn spawn_envs<IE, K, V>(&self, envs: IE) -> TestChild
where
IE: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.spawn_args_envs(NO_ARGS, envs)
}
#[inline]
#[track_caller]
pub fn spawn(&self) -> TestChild {
self.spawn_args_envs(NO_ARGS, NO_ENVS)
}
}
pub const NO_ARGS: [&OsStr; 0] = [];
pub const NO_ENVS: [(&OsStr, &OsStr); 0] = [];
pub struct TestChild(Child);
impl TestChild {
pub fn wait(self) -> Output {
self.0.wait_with_output().expect("wait success")
}
pub fn kill(mut self) {
let _ = self.0.kill();
}
}
#[cfg(test)]
#[cfg(unix)]
mod test {
use crate::*;
use std::path::Path;
#[test]
fn echo_no_args() {
let testcall = TestCall::external_command(Path::new("echo"));
testcall.call().assert_success().assert_stdout_utf8("");
}
#[test]
fn echo() {
let testcall = TestCall::external_command(Path::new("echo"));
testcall
.call_args(["Hello World!"])
.assert_success()
.assert_stdout_utf8("Hello World!");
}
#[test]
#[should_panic]
fn echo_fail() {
let testcall = TestCall::external_command(Path::new("echo"));
testcall
.call_args(["No World!"])
.assert_success()
.assert_stdout_utf8("Hello World!");
}
#[test]
fn spawn() {
let testcall = TestCall::external_command(Path::new("echo"));
let child = testcall.spawn_args_envs(["Hello World!"], NO_ENVS);
println!("child spawned");
child
.wait()
.assert_success()
.assert_stdout_utf8("Hello World!");
}
}