@@ -109,6 +109,7 @@ extern crate difference;
109
109
extern crate rustc_serialize;
110
110
111
111
use std:: process:: { Command , Output } ;
112
+ use std:: path:: PathBuf ;
112
113
use std:: fmt;
113
114
114
115
use difference:: Changeset ;
@@ -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 > ,
@@ -169,6 +171,7 @@ impl std::default::Default for Assert {
169
171
Assert {
170
172
cmd : vec ! [ "cargo" , "run" , "--" ]
171
173
. into_iter ( ) . map ( String :: from) . collect ( ) ,
174
+ current_dir : None ,
172
175
expect_success : Some ( true ) ,
173
176
expect_exit_code : None ,
174
177
expect_stdout : None ,
@@ -232,6 +235,24 @@ impl Assert {
232
235
self
233
236
}
234
237
238
+ /// Sets the working directory for the command.
239
+ ///
240
+ /// # Examples
241
+ ///
242
+ /// ```rust
243
+ /// extern crate assert_cli;
244
+ ///
245
+ /// assert_cli::Assert::command(&["wc", "lib.rs"])
246
+ /// .current_dir(std::path::Path::new("src"))
247
+ /// .prints("lib.rs")
248
+ /// .execute()
249
+ /// .unwrap();
250
+ /// ```
251
+ pub fn current_dir < P : Into < PathBuf > > ( mut self , dir : P ) -> Self {
252
+ self . current_dir = Some ( dir. into ( ) ) ;
253
+ self
254
+ }
255
+
235
256
/// Small helper to make chains more readable.
236
257
///
237
258
/// # Examples
@@ -400,6 +421,10 @@ impl Assert {
400
421
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
401
422
let mut command = Command :: new ( cmd) ;
402
423
let command = command. args ( & args) ;
424
+ let command = match self . current_dir {
425
+ Some ( ref dir) => command. current_dir ( dir) ,
426
+ None => command
427
+ } ;
403
428
let output = command. output ( ) ?;
404
429
405
430
0 commit comments