@@ -109,6 +109,7 @@ extern crate difference;
109
109
extern crate rustc_serialize;
110
110
111
111
use std:: process:: Command ;
112
+ use std:: path:: PathBuf ;
112
113
113
114
mod errors;
114
115
use errors:: * ;
@@ -125,6 +126,7 @@ mod diff;
125
126
#[ derive( Debug ) ]
126
127
pub struct Assert {
127
128
cmd : Vec < String > ,
129
+ current_dir : Option < PathBuf > ,
128
130
expect_success : Option < bool > ,
129
131
expect_exit_code : Option < i32 > ,
130
132
expect_stdout : Option < OutputAssertion < StdOut > > ,
@@ -139,6 +141,7 @@ impl std::default::Default for Assert {
139
141
Assert {
140
142
cmd : vec ! [ "cargo" , "run" , "--" ]
141
143
. into_iter ( ) . map ( String :: from) . collect ( ) ,
144
+ current_dir : None ,
142
145
expect_success : Some ( true ) ,
143
146
expect_exit_code : None ,
144
147
expect_stdout : None ,
@@ -202,6 +205,24 @@ impl Assert {
202
205
self
203
206
}
204
207
208
+ /// Sets the working directory for the command.
209
+ ///
210
+ /// # Examples
211
+ ///
212
+ /// ```rust
213
+ /// extern crate assert_cli;
214
+ ///
215
+ /// assert_cli::Assert::command(&["wc", "lib.rs"])
216
+ /// .current_dir(std::path::Path::new("src"))
217
+ /// .prints("lib.rs")
218
+ /// .execute()
219
+ /// .unwrap();
220
+ /// ```
221
+ pub fn current_dir < P : Into < PathBuf > > ( mut self , dir : P ) -> Self {
222
+ self . current_dir = Some ( dir. into ( ) ) ;
223
+ self
224
+ }
225
+
205
226
/// Small helper to make chains more readable.
206
227
///
207
228
/// # Examples
@@ -374,6 +395,10 @@ impl Assert {
374
395
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
375
396
let mut command = Command :: new ( cmd) ;
376
397
let command = command. args ( & args) ;
398
+ let command = match self . current_dir {
399
+ Some ( ref dir) => command. current_dir ( dir) ,
400
+ None => command
401
+ } ;
377
402
let output = command. output ( ) ?;
378
403
379
404
0 commit comments