1
1
use std:: default;
2
- use std:: process:: Command ;
2
+ use std:: process:: { Command , Stdio } ;
3
3
use std:: path:: PathBuf ;
4
4
use std:: vec:: Vec ;
5
5
use std:: convert:: Into ;
6
+ use std:: io:: Write ;
7
+
6
8
use errors:: * ;
7
9
use output:: { OutputAssertion , OutputKind } ;
8
10
use environment:: Environment ;
@@ -16,6 +18,7 @@ pub struct Assert {
16
18
expect_success : Option < bool > ,
17
19
expect_exit_code : Option < i32 > ,
18
20
expect_output : Vec < OutputAssertion > ,
21
+ stdin_contents : Option < String > ,
19
22
}
20
23
21
24
impl default:: Default for Assert {
@@ -31,6 +34,7 @@ impl default::Default for Assert {
31
34
expect_success : Some ( true ) ,
32
35
expect_exit_code : None ,
33
36
expect_output : vec ! [ ] ,
37
+ stdin_contents : None ,
34
38
}
35
39
}
36
40
}
@@ -91,6 +95,23 @@ impl Assert {
91
95
self
92
96
}
93
97
98
+ /// Add stdin to the command.
99
+ ///
100
+ /// # Examples
101
+ ///
102
+ /// ```rust
103
+ /// extern crate assert_cli;
104
+ ///
105
+ /// assert_cli::Assert::command(&["cat"])
106
+ /// .stdin("42")
107
+ /// .stdout().contains("42")
108
+ /// .unwrap();
109
+ /// ```
110
+ pub fn stdin ( mut self , contents : & str ) -> Self {
111
+ self . stdin_contents = Some ( String :: from ( contents) ) ;
112
+ self
113
+ }
114
+
94
115
/// Sets the working directory for the command.
95
116
///
96
117
/// # Examples
@@ -257,15 +278,24 @@ impl Assert {
257
278
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
258
279
let mut command = Command :: new ( cmd) ;
259
280
let command = command
281
+ . stdin ( Stdio :: piped ( ) )
282
+ . stdout ( Stdio :: piped ( ) )
283
+ . stderr ( Stdio :: piped ( ) )
260
284
. env_clear ( )
261
- . envs ( :: std :: env :: vars ( ) . chain ( self . env . clone ( ) . vars ) )
285
+ . envs ( self . env . clone ( ) . compile ( ) ) )
262
286
. args ( & args) ;
263
287
264
288
let command = match self . current_dir {
265
289
Some ( ref dir) => command. current_dir ( dir) ,
266
290
None => command,
267
291
} ;
268
- let output = command. output ( ) ?;
292
+
293
+ let mut spawned = command. spawn ( ) ?;
294
+
295
+ if let Some ( ref contents) = self . stdin_contents {
296
+ spawned. stdin . as_mut ( ) . expect ( "Couldn't get mut ref to command stdin" ) . write_all ( contents. as_bytes ( ) ) ?;
297
+ }
298
+ let output = spawned. wait_with_output ( ) ?;
269
299
270
300
if let Some ( expect_success) = self . expect_success {
271
301
if expect_success != output. status . success ( ) {
0 commit comments