@@ -13,7 +13,12 @@ const TIMEOUT_SECS: u64 = 30;
1313
1414/// Run a command with a description for a possible error and append the merged stdout and stderr.
1515/// The boolean in the returned `Result` is true if the command's exit status is success.
16- fn run_cmd ( mut cmd : Command , description : & str , output : Option < & mut Vec < u8 > > ) -> Result < bool > {
16+ fn run_cmd (
17+ mut cmd : Command ,
18+ description : & str ,
19+ cwd : Option < & str > ,
20+ output : Option < & mut Vec < u8 > > ,
21+ ) -> Result < bool > {
1722 let spawn = |mut cmd : Command | {
1823 // The closure drops `cmd` which prevents a pipe deadlock.
1924 cmd. stdin ( Stdio :: null ( ) )
@@ -25,6 +30,9 @@ fn run_cmd(mut cmd: Command, description: &str, output: Option<&mut Vec<u8>>) ->
2530 . wait_timeout ( Duration :: from_secs ( TIMEOUT_SECS ) )
2631 . with_context ( || format ! ( "Failed to wait on `{description}` to exit" ) )
2732 } ;
33+ if let Some ( cwd) = cwd {
34+ cmd. current_dir ( cwd) ;
35+ }
2836
2937 let mut handle = if let Some ( output) = output {
3038 let ( mut reader, writer) =
@@ -133,15 +141,20 @@ impl CmdRunner {
133141 }
134142
135143 /// The boolean in the returned `Result` is true if the command's exit status is success.
136- pub fn run_debug_bin ( & self , bin_name : & str , output : Option < & mut Vec < u8 > > ) -> Result < bool > {
144+ pub fn run_debug_bin (
145+ & self ,
146+ bin_name : & str ,
147+ cwd : & str ,
148+ output : Option < & mut Vec < u8 > > ,
149+ ) -> Result < bool > {
137150 // 7 = "/debug/".len()
138151 let mut bin_path =
139152 PathBuf :: with_capacity ( self . target_dir . as_os_str ( ) . len ( ) + 7 + bin_name. len ( ) ) ;
140153 bin_path. push ( & self . target_dir ) ;
141154 bin_path. push ( "debug" ) ;
142155 bin_path. push ( bin_name) ;
143156
144- run_cmd ( Command :: new ( & bin_path) , bin_name, output)
157+ run_cmd ( Command :: new ( & bin_path) , bin_name, Some ( cwd ) , output)
145158 }
146159}
147160
@@ -161,7 +174,7 @@ impl CargoSubcommand<'_> {
161174
162175 /// The boolean in the returned `Result` is true if the command's exit status is success.
163176 pub fn run ( self , description : & str ) -> Result < bool > {
164- run_cmd ( self . cmd , description, self . output )
177+ run_cmd ( self . cmd , description, None , self . output )
165178 }
166179}
167180
@@ -179,7 +192,7 @@ mod tests {
179192 cmd. arg ( "Hello" ) ;
180193
181194 let mut output = Vec :: with_capacity ( 8 ) ;
182- run_cmd ( cmd, "echo …" , Some ( & mut output) ) . unwrap ( ) ;
195+ run_cmd ( cmd, "echo …" , None , Some ( & mut output) ) . unwrap ( ) ;
183196
184197 assert_eq ! ( output, b"Hello\n \n " ) ;
185198 }
0 commit comments