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
+ use std:: io:: Write ;
5
6
6
7
use errors:: * ;
7
8
use output:: { OutputAssertion , OutputKind } ;
@@ -14,6 +15,7 @@ pub struct Assert {
14
15
expect_success : Option < bool > ,
15
16
expect_exit_code : Option < i32 > ,
16
17
expect_output : Vec < OutputAssertion > ,
18
+ stdin_contents : Option < String > ,
17
19
}
18
20
19
21
impl default:: Default for Assert {
@@ -28,6 +30,7 @@ impl default::Default for Assert {
28
30
expect_success : Some ( true ) ,
29
31
expect_exit_code : None ,
30
32
expect_output : vec ! [ ] ,
33
+ stdin_contents : None ,
31
34
}
32
35
}
33
36
}
@@ -87,6 +90,23 @@ impl Assert {
87
90
self
88
91
}
89
92
93
+ /// Add stdin to the command.
94
+ ///
95
+ /// # Examples
96
+ ///
97
+ /// ```rust
98
+ /// extern crate assert_cli;
99
+ ///
100
+ /// assert_cli::Assert::command(&["cat"])
101
+ /// .stdin("42")
102
+ /// .stdout().contains("42")
103
+ /// .unwrap();
104
+ /// ```
105
+ pub fn stdin ( mut self , contents : & str ) -> Self {
106
+ self . stdin_contents = Some ( String :: from ( contents) ) ;
107
+ self
108
+ }
109
+
90
110
/// Sets the working directory for the command.
91
111
///
92
112
/// # Examples
@@ -232,12 +252,22 @@ impl Assert {
232
252
let cmd = & self . cmd [ 0 ] ;
233
253
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
234
254
let mut command = Command :: new ( cmd) ;
255
+ let command = command
256
+ . stdin ( Stdio :: piped ( ) )
257
+ . stdout ( Stdio :: piped ( ) )
258
+ . stderr ( Stdio :: piped ( ) ) ;
235
259
let command = command. args ( & args) ;
236
260
let command = match self . current_dir {
237
261
Some ( ref dir) => command. current_dir ( dir) ,
238
262
None => command,
239
263
} ;
240
- let output = command. output ( ) ?;
264
+
265
+ let mut spawned = command. spawn ( ) ?;
266
+
267
+ if let Some ( ref contents) = self . stdin_contents {
268
+ spawned. stdin . as_mut ( ) . expect ( "Couldn't get mut ref to command stdin" ) . write_all ( contents. as_bytes ( ) ) ?;
269
+ }
270
+ let output = spawned. wait_with_output ( ) ?;
241
271
242
272
if let Some ( expect_success) = self . expect_success {
243
273
if expect_success != output. status . success ( ) {
0 commit comments