@@ -6,6 +6,7 @@ use std::io::prelude::*;
6
6
use std:: path:: PathBuf ;
7
7
use std:: process:: { Command , Output } ;
8
8
use std:: {
9
+ fmt:: Write as _,
9
10
fs:: { self , File , OpenOptions } ,
10
11
path:: Path ,
11
12
} ;
@@ -67,7 +68,7 @@ impl std::fmt::Debug for ProcessFailed {
67
68
68
69
trait CommandHelper {
69
70
fn capture_outputs (
70
- & self ,
71
+ & mut self ,
71
72
cant_fail : bool ,
72
73
name : & str ,
73
74
stdout : Option < & PathBuf > ,
@@ -76,26 +77,48 @@ trait CommandHelper {
76
77
) -> Result < ( ) , TestError > ;
77
78
}
78
79
79
- impl CommandHelper for Output {
80
+ impl CommandHelper for Command {
81
+ #[ tracing:: instrument( skip_all, fields( stdout = tracing:: field:: Empty , stderr = tracing:: field:: Empty ) ) ]
80
82
fn capture_outputs (
81
- & self ,
83
+ & mut self ,
82
84
cant_fail : bool ,
83
85
name : & str ,
84
86
stdout : Option < & PathBuf > ,
85
87
stderr : Option < & PathBuf > ,
86
88
previous_processes_stderr : & [ PathBuf ] ,
87
89
) -> Result < ( ) , TestError > {
90
+ let output = self . get_output ( true ) ?;
91
+ let out_payload = String :: from_utf8_lossy ( & output. stdout ) ;
88
92
if let Some ( out) = stdout {
89
- let out_payload = String :: from_utf8_lossy ( & self . stdout ) ;
90
93
file_helper ( & out_payload, out) ?;
91
94
} ;
92
95
96
+ let err_payload = String :: from_utf8_lossy ( & output. stderr ) ;
93
97
if let Some ( err) = stderr {
94
- let err_payload = String :: from_utf8_lossy ( & self . stderr ) ;
95
98
file_helper ( & err_payload, err) ?;
96
99
} ;
97
-
98
- if cant_fail && !self . status . success ( ) {
100
+ if cant_fail && !output. status . success ( ) {
101
+ let span = tracing:: Span :: current ( ) ;
102
+ let mut message = format ! ( "Process failed: {}" , self . display( ) ) ;
103
+ if !out_payload. trim ( ) . is_empty ( ) {
104
+ span. record (
105
+ "stdout" ,
106
+ tracing:: field:: display (
107
+ stdout. map ( |p| p. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ,
108
+ ) ,
109
+ ) ;
110
+ write ! ( message, "\n stdout: \n {}" , out_payload) . unwrap ( ) ;
111
+ }
112
+ if !err_payload. trim ( ) . is_empty ( ) {
113
+ span. record (
114
+ "stderr" ,
115
+ tracing:: field:: display (
116
+ stderr. map ( |p| p. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ,
117
+ ) ,
118
+ ) ;
119
+ write ! ( message, "\n stderr: \n {}" , err_payload) . unwrap ( ) ;
120
+ }
121
+ tracing:: error!( message=%message) ;
99
122
return Err ( ProcessFailed {
100
123
command : name. into ( ) ,
101
124
stdout : stdout. cloned ( ) ,
@@ -125,18 +148,17 @@ impl TestCase {
125
148
. with_context ( || anyhow ! ( "when setting up case for {}" , self . name( ) ) ) ?;
126
149
// Run `cargo check`, capturing stderr to a log file
127
150
let cargo_check_err_file = path_helper_base ( & chip_dir, & [ "cargo-check.err.log" ] ) ;
128
- let output = Command :: new ( "cargo" )
151
+ Command :: new ( "cargo" )
129
152
. arg ( "check" )
130
153
. current_dir ( & chip_dir)
131
- . output ( )
154
+ . capture_outputs (
155
+ true ,
156
+ "cargo check" ,
157
+ None ,
158
+ Some ( & cargo_check_err_file) ,
159
+ & process_stderr_paths,
160
+ )
132
161
. with_context ( || "failed to check" ) ?;
133
- output. capture_outputs (
134
- true ,
135
- "cargo check" ,
136
- None ,
137
- Some ( & cargo_check_err_file) ,
138
- & process_stderr_paths,
139
- ) ?;
140
162
process_stderr_paths. push ( cargo_check_err_file) ;
141
163
Ok ( if opts. verbose > 1 {
142
164
Some ( process_stderr_paths)
@@ -180,9 +202,8 @@ impl TestCase {
180
202
. arg ( "--vcs" )
181
203
. arg ( "none" )
182
204
. arg ( & chip_dir)
183
- . output ( )
184
- . with_context ( || "Failed to cargo init" ) ?
185
- . capture_outputs ( true , "cargo init" , None , None , & [ ] ) ?;
205
+ . capture_outputs ( true , "cargo init" , None , None , & [ ] )
206
+ . with_context ( || "Failed to cargo init" ) ?;
186
207
let svd_toml = path_helper_base ( & chip_dir, & [ "Cargo.toml" ] ) ;
187
208
let mut file = OpenOptions :: new ( )
188
209
. write ( true )
@@ -242,23 +263,22 @@ impl TestCase {
242
263
if let Some ( command) = command {
243
264
svd2rust_bin. arg ( command) ;
244
265
}
245
- let output = svd2rust_bin
266
+ svd2rust_bin
246
267
. args ( [ "-i" , & chip_svd] )
247
268
. args ( [ "--target" , target] )
248
269
. current_dir ( & chip_dir)
249
- . get_output ( ) ?;
250
- output. capture_outputs (
251
- true ,
252
- "svd2rust" ,
253
- Some ( & lib_rs_file) . filter ( |_| {
254
- !matches ! (
255
- self . arch,
256
- Target :: CortexM | Target :: Msp430 | Target :: XtensaLX
257
- )
258
- } ) ,
259
- Some ( & svd2rust_err_file) ,
260
- & [ ] ,
261
- ) ?;
270
+ . capture_outputs (
271
+ true ,
272
+ "svd2rust" ,
273
+ Some ( & lib_rs_file) . filter ( |_| {
274
+ !matches ! (
275
+ self . arch,
276
+ Target :: CortexM | Target :: Msp430 | Target :: XtensaLX
277
+ )
278
+ } ) ,
279
+ Some ( & svd2rust_err_file) ,
280
+ & [ ] ,
281
+ ) ?;
262
282
process_stderr_paths. push ( svd2rust_err_file) ;
263
283
match self . arch {
264
284
Target :: CortexM | Target :: Mips | Target :: Msp430 | Target :: XtensaLX => {
@@ -287,20 +307,19 @@ impl TestCase {
287
307
let new_lib_rs_file = path_helper_base ( & chip_dir, & [ "lib.rs" ] ) ;
288
308
std:: fs:: rename ( lib_rs_file, & new_lib_rs_file)
289
309
. with_context ( || "While moving lib.rs file" ) ?;
290
- let output = Command :: new ( form_bin_path)
310
+ Command :: new ( form_bin_path)
291
311
. arg ( "--input" )
292
312
. arg ( & new_lib_rs_file)
293
313
. arg ( "--outdir" )
294
314
. arg ( & src_dir)
295
- . output ( )
315
+ . capture_outputs (
316
+ true ,
317
+ "form" ,
318
+ None ,
319
+ Some ( & form_err_file) ,
320
+ & process_stderr_paths,
321
+ )
296
322
. with_context ( || "failed to form" ) ?;
297
- output. capture_outputs (
298
- true ,
299
- "form" ,
300
- None ,
301
- Some ( & form_err_file) ,
302
- & process_stderr_paths,
303
- ) ?;
304
323
std:: fs:: remove_file ( & new_lib_rs_file)
305
324
. with_context ( || "While removing lib.rs file after form" ) ?;
306
325
}
@@ -322,15 +341,14 @@ impl TestCase {
322
341
let output = Command :: new ( rustfmt_bin_path)
323
342
. arg ( entry)
324
343
. args ( [ "--edition" , "2021" ] )
325
- . output ( )
344
+ . capture_outputs (
345
+ false ,
346
+ "rustfmt" ,
347
+ None ,
348
+ Some ( & rustfmt_err_file) ,
349
+ & process_stderr_paths,
350
+ )
326
351
. with_context ( || "failed to format" ) ?;
327
- output. capture_outputs (
328
- false ,
329
- "rustfmt" ,
330
- None ,
331
- Some ( & rustfmt_err_file) ,
332
- & process_stderr_paths,
333
- ) ?;
334
352
}
335
353
336
354
process_stderr_paths. push ( rustfmt_err_file) ;
0 commit comments