@@ -108,7 +108,7 @@ impl ExecutionContext {
108
108
cmd. stdout ( stdout. stdio ( ) ) ;
109
109
cmd. stderr ( stderr. stdio ( ) ) ;
110
110
111
- let child = cmd. spawn ( ) . unwrap ( ) ;
111
+ let child = cmd. spawn ( ) ;
112
112
113
113
DeferredCommand { process : Some ( child) , stdout, stderr, command, executed_at }
114
114
}
@@ -157,7 +157,7 @@ impl AsRef<ExecutionContext> for ExecutionContext {
157
157
}
158
158
159
159
pub struct DeferredCommand < ' a > {
160
- process : Option < Child > ,
160
+ process : Option < Result < Child , std :: io :: Error > > ,
161
161
command : & ' a mut BootstrapCommand ,
162
162
stdout : OutputMode ,
163
163
stderr : OutputMode ,
@@ -166,61 +166,77 @@ pub struct DeferredCommand<'a> {
166
166
167
167
impl < ' a > DeferredCommand < ' a > {
168
168
pub fn wait_for_output ( mut self , exec_ctx : impl AsRef < ExecutionContext > ) -> CommandOutput {
169
- if self . process . is_none ( ) {
170
- return CommandOutput :: default ( ) ;
171
- }
172
-
173
169
let exec_ctx = exec_ctx. as_ref ( ) ;
174
170
175
- let output = self . process . take ( ) . unwrap ( ) . wait_with_output ( ) ;
171
+ let process = match self . process . take ( ) {
172
+ Some ( p) => p,
173
+ None => return CommandOutput :: default ( ) ,
174
+ } ;
176
175
177
176
let created_at = self . command . get_created_location ( ) ;
178
177
let executed_at = self . executed_at ;
179
178
180
- use std:: fmt:: Write ;
181
-
182
179
let mut message = String :: new ( ) ;
183
- let output: CommandOutput = match output {
184
- // Command has succeeded
185
- Ok ( output) if output. status . success ( ) => {
186
- CommandOutput :: from_output ( output, self . stdout , self . stderr )
187
- }
188
- // Command has started, but then it failed
189
- Ok ( output) => {
190
- writeln ! (
191
- message,
192
- r#"
180
+
181
+ let output = match process {
182
+ Ok ( child) => match child. wait_with_output ( ) {
183
+ Ok ( result) if result. status . success ( ) => {
184
+ // Successful execution
185
+ CommandOutput :: from_output ( result, self . stdout , self . stderr )
186
+ }
187
+ Ok ( result) => {
188
+ // Command ran but failed
189
+ use std:: fmt:: Write ;
190
+
191
+ writeln ! (
192
+ message,
193
+ r#"
193
194
Command {:?} did not execute successfully.
194
195
Expected success, got {}
195
196
Created at: {created_at}
196
197
Executed at: {executed_at}"# ,
197
- self . command, output. status,
198
- )
199
- . unwrap ( ) ;
198
+ self . command, result. status,
199
+ )
200
+ . unwrap ( ) ;
201
+
202
+ let output = CommandOutput :: from_output ( result, self . stdout , self . stderr ) ;
200
203
201
- let output: CommandOutput =
202
- CommandOutput :: from_output ( output, self . stdout , self . stderr ) ;
204
+ if self . stdout . captures ( ) {
205
+ writeln ! ( message, "\n STDOUT ----\n {}" , output. stdout( ) . trim( ) ) . unwrap ( ) ;
206
+ }
207
+ if self . stderr . captures ( ) {
208
+ writeln ! ( message, "\n STDERR ----\n {}" , output. stderr( ) . trim( ) ) . unwrap ( ) ;
209
+ }
203
210
204
- // If the output mode is OutputMode::Capture, we can now print the output.
205
- // If it is OutputMode::Print, then the output has already been printed to
206
- // stdout/stderr, and we thus don't have anything captured to print anyway.
207
- if self . stdout . captures ( ) {
208
- writeln ! ( message, "\n STDOUT ----\n {}" , output. stdout( ) . trim( ) ) . unwrap ( ) ;
211
+ output
209
212
}
210
- if self . stderr . captures ( ) {
211
- writeln ! ( message, "\n STDERR ----\n {}" , output. stderr( ) . trim( ) ) . unwrap ( ) ;
213
+ Err ( e) => {
214
+ // Failed to wait for output
215
+ use std:: fmt:: Write ;
216
+
217
+ writeln ! (
218
+ message,
219
+ "\n \n Command {:?} did not execute successfully.\
220
+ \n It was not possible to execute the command: {e:?}",
221
+ self . command
222
+ )
223
+ . unwrap ( ) ;
224
+
225
+ CommandOutput :: did_not_start ( self . stdout , self . stderr )
212
226
}
213
- output
214
- }
215
- // The command did not even start
227
+ } ,
216
228
Err ( e) => {
229
+ // Failed to spawn the command
230
+ use std:: fmt:: Write ;
231
+
217
232
writeln ! (
218
233
message,
219
234
"\n \n Command {:?} did not execute successfully.\
220
- \n It was not possible to execute the command: {e:?}",
235
+ \n It was not possible to execute the command: {e:?}",
221
236
self . command
222
237
)
223
238
. unwrap ( ) ;
239
+
224
240
CommandOutput :: did_not_start ( self . stdout , self . stderr )
225
241
}
226
242
} ;
@@ -231,7 +247,6 @@ Executed at: {executed_at}"#,
231
247
if exec_ctx. fail_fast {
232
248
exec_ctx. fail ( & message, output) ;
233
249
}
234
-
235
250
exec_ctx. add_to_delay_failure ( message) ;
236
251
}
237
252
BehaviorOnFailure :: Exit => {
@@ -244,6 +259,7 @@ Executed at: {executed_at}"#,
244
259
}
245
260
}
246
261
}
262
+
247
263
output
248
264
}
249
265
}
0 commit comments