@@ -109,6 +109,7 @@ extern crate difference;
109109extern crate rustc_serialize;
110110
111111use std:: process:: Command ;
112+ use std:: path:: PathBuf ;
112113
113114mod errors;
114115use errors:: * ;
@@ -125,6 +126,7 @@ mod diff;
125126#[ derive( Debug ) ]
126127pub struct Assert {
127128 cmd : Vec < String > ,
129+ current_dir : Option < PathBuf > ,
128130 expect_success : Option < bool > ,
129131 expect_exit_code : Option < i32 > ,
130132 expect_stdout : Option < OutputAssertion < StdOut > > ,
@@ -139,6 +141,7 @@ impl std::default::Default for Assert {
139141 Assert {
140142 cmd : vec ! [ "cargo" , "run" , "--" ]
141143 . into_iter ( ) . map ( String :: from) . collect ( ) ,
144+ current_dir : None ,
142145 expect_success : Some ( true ) ,
143146 expect_exit_code : None ,
144147 expect_stdout : None ,
@@ -202,6 +205,24 @@ impl Assert {
202205 self
203206 }
204207
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+
205226 /// Small helper to make chains more readable.
206227 ///
207228 /// # Examples
@@ -374,6 +395,10 @@ impl Assert {
374395 let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
375396 let mut command = Command :: new ( cmd) ;
376397 let command = command. args ( & args) ;
398+ let command = match self . current_dir {
399+ Some ( ref dir) => command. current_dir ( dir) ,
400+ None => command
401+ } ;
377402 let output = command. output ( ) ?;
378403
379404
0 commit comments