@@ -12,12 +12,12 @@ use super::*;
12
12
13
13
pub ( crate ) trait OutputFormatter {
14
14
fn write_run_start ( & mut self , len : usize ) -> io:: Result < ( ) > ;
15
- fn write_test_start ( & mut self ,
16
- test : & TestDesc ,
17
- align : NamePadding ,
18
- max_name_len : usize ) -> io:: Result < ( ) > ;
15
+ fn write_test_start ( & mut self , test : & TestDesc ) -> io:: Result < ( ) > ;
19
16
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > ;
20
- fn write_result ( & mut self , desc : & TestDesc , result : & TestResult ) -> io:: Result < ( ) > ;
17
+ fn write_result ( & mut self ,
18
+ desc : & TestDesc ,
19
+ result : & TestResult ,
20
+ stdout : & [ u8 ] ) -> io:: Result < ( ) > ;
21
21
fn write_run_finish ( & mut self , state : & ConsoleTestState ) -> io:: Result < bool > ;
22
22
}
23
23
@@ -26,15 +26,17 @@ pub(crate) struct HumanFormatter<T> {
26
26
terse : bool ,
27
27
use_color : bool ,
28
28
test_count : usize ,
29
+ max_name_len : usize , // number of columns to fill when aligning names
29
30
}
30
31
31
32
impl < T : Write > HumanFormatter < T > {
32
- pub fn new ( out : OutputLocation < T > , use_color : bool , terse : bool ) -> Self {
33
+ pub fn new ( out : OutputLocation < T > , use_color : bool , terse : bool , max_name_len : usize ) -> Self {
33
34
HumanFormatter {
34
35
out,
35
36
terse,
36
37
use_color,
37
38
test_count : 0 ,
39
+ max_name_len,
38
40
}
39
41
}
40
42
@@ -170,20 +172,18 @@ impl<T: Write> OutputFormatter for HumanFormatter<T> {
170
172
self . write_plain ( & format ! ( "\n running {} {}\n " , len, noun) )
171
173
}
172
174
173
- fn write_test_start ( & mut self ,
174
- test : & TestDesc ,
175
- align : NamePadding ,
176
- max_name_len : usize ) -> io:: Result < ( ) > {
177
- if self . terse && align != PadOnRight {
178
- Ok ( ( ) )
179
- }
180
- else {
181
- let name = test. padded_name ( max_name_len, align) ;
182
- self . write_plain ( & format ! ( "test {} ... " , name) )
183
- }
175
+ fn write_test_start ( & mut self , _desc : & TestDesc ) -> io:: Result < ( ) > {
176
+ // Do not print header, as priting it at this point will result in
177
+ // an unreadable output when running tests concurrently.
178
+ Ok ( ( ) )
184
179
}
185
180
186
- fn write_result ( & mut self , _desc : & TestDesc , result : & TestResult ) -> io:: Result < ( ) > {
181
+ fn write_result ( & mut self , desc : & TestDesc , result : & TestResult , _: & [ u8 ] ) -> io:: Result < ( ) > {
182
+ if !( self . terse && desc. name . padding ( ) != PadOnRight ) {
183
+ let name = desc. padded_name ( self . max_name_len , desc. name . padding ( ) ) ;
184
+ self . write_plain ( & format ! ( "test {} ... " , name) ) ?;
185
+ }
186
+
187
187
match * result {
188
188
TrOk => self . write_ok ( ) ,
189
189
TrFailed | TrFailedMsg ( _) => self . write_failed ( ) ,
@@ -259,6 +259,26 @@ impl<T: Write> JsonFormatter<T> {
259
259
self . out . write_all ( s. as_ref ( ) . as_ref ( ) ) ?;
260
260
self . out . write_all ( "\n " . as_ref ( ) )
261
261
}
262
+
263
+ fn write_event ( & mut self ,
264
+ ty : & str ,
265
+ name : & str ,
266
+ evt : & str ,
267
+ extra : Option < String > ) -> io:: Result < ( ) > {
268
+ if let Some ( extras) = extra {
269
+ self . write_str ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}", {} }}"# ,
270
+ ty,
271
+ name,
272
+ evt,
273
+ extras) )
274
+ }
275
+ else {
276
+ self . write_str ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}" }}"# ,
277
+ ty,
278
+ name,
279
+ evt) )
280
+ }
281
+ }
262
282
}
263
283
264
284
impl < T : Write > OutputFormatter for JsonFormatter < T > {
@@ -267,40 +287,45 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
267
287
& * format ! ( r#"{{ "type": "suite", "event": "started", "test_count": "{}" }}"# , len) )
268
288
}
269
289
270
- fn write_test_start ( & mut self ,
271
- desc : & TestDesc ,
272
- _align : NamePadding ,
273
- _max_name_len : usize ) -> io:: Result < ( ) > {
290
+ fn write_test_start ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
274
291
self . write_str ( & * format ! ( r#"{{ "type": "test", "event": "started", "name": "{}" }}"# ,
275
292
desc. name) )
276
293
}
277
294
278
- fn write_result ( & mut self , desc : & TestDesc , result : & TestResult ) -> io:: Result < ( ) > {
279
- let output = match * result {
295
+ fn write_result ( & mut self ,
296
+ desc : & TestDesc ,
297
+ result : & TestResult ,
298
+ stdout : & [ u8 ] ) -> io:: Result < ( ) > {
299
+ match * result {
280
300
TrOk => {
281
- format ! ( r#"{{ "type": "test", "event": "ok", "name": "{}" }}"# ,
282
- desc. name)
301
+ self . write_event ( "test" , desc. name . as_slice ( ) , "ok" , None )
283
302
} ,
284
303
285
304
TrFailed => {
286
- format ! ( r#"{{ "type": "test", "event": "failed", "name": "{}" }}"# ,
287
- desc. name)
305
+ let extra_data = if stdout. len ( ) > 0 {
306
+ Some ( format ! ( r#""stdout": "{}""# ,
307
+ EscapedString ( String :: from_utf8_lossy( stdout) ) ) )
308
+ }
309
+ else {
310
+ None
311
+ } ;
312
+
313
+ self . write_event ( "test" , desc. name . as_slice ( ) , "failed" , extra_data)
288
314
} ,
289
315
290
316
TrFailedMsg ( ref m) => {
291
- format ! ( r#"{{ "type": "test", "event": "failed", "name": "{}", "message": "{}" }}"# ,
292
- desc. name,
293
- EscapedString ( m) )
317
+ self . write_event ( "test" ,
318
+ desc. name . as_slice ( ) ,
319
+ "failed" ,
320
+ Some ( format ! ( r#""message": "{}""# , EscapedString ( m) ) ) )
294
321
} ,
295
322
296
323
TrIgnored => {
297
- format ! ( r#"{{ "type": "test", "event": "ignored", "name": "{}" }}"# ,
298
- desc. name)
324
+ self . write_event ( "test" , desc. name . as_slice ( ) , "ignored" , None )
299
325
} ,
300
326
301
327
TrAllowedFail => {
302
- format ! ( r#"{{ "type": "test", "event": "allowed_failure", "name": "{}" }}"# ,
303
- desc. name)
328
+ self . write_event ( "test" , desc. name . as_slice ( ) , "allowed_failure" , None )
304
329
} ,
305
330
306
331
TrBench ( ref bs) => {
@@ -314,15 +339,18 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
314
339
format ! ( r#", "mib_per_second": {}"# , bs. mb_s)
315
340
} ;
316
341
317
- format ! ( r#"{{ "type": "bench", "name": "{}", "median": {}, "deviation": {}{} }}"# ,
342
+ let line = format ! ( "{{ \" type\" : \" bench\" , \
343
+ \" name\" : \" {}\" , \
344
+ \" median\" : {}, \
345
+ \" deviation\" : {}{} }}",
318
346
desc. name,
319
347
median,
320
348
deviation,
321
- mbps)
322
- } ,
323
- } ;
349
+ mbps) ;
324
350
325
- self . write_str ( & * output)
351
+ self . write_str ( & * line)
352
+ } ,
353
+ }
326
354
}
327
355
328
356
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
@@ -348,28 +376,19 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
348
376
state. measured,
349
377
state. filtered_out) ) ?;
350
378
351
- for & ( ref f, ref stdout) in & state. failures {
352
- if !stdout. is_empty ( ) {
353
- self . write_str (
354
- & * format ! ( r#"{{ "type": "test_output", "name": "{}", "output": "{}" }}"# ,
355
- f. name,
356
- EscapedString ( & * String :: from_utf8_lossy( stdout) ) ) ) ?;
357
- }
358
- }
359
-
360
379
Ok ( state. failed == 0 )
361
380
}
362
381
}
363
382
364
383
/// A formatting utility used to print strings with characters in need of escaping.
365
384
/// Base code taken form `libserialize::json::escape_str`
366
- struct EscapedString < ' a > ( & ' a str ) ;
385
+ struct EscapedString < S : AsRef < str > > ( S ) ;
367
386
368
- impl < ' a > :: std:: fmt:: Display for EscapedString < ' a > {
387
+ impl < S : AsRef < str > > :: std:: fmt:: Display for EscapedString < S > {
369
388
fn fmt ( & self , f : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
370
389
let mut start = 0 ;
371
390
372
- for ( i, byte) in self . 0 . bytes ( ) . enumerate ( ) {
391
+ for ( i, byte) in self . 0 . as_ref ( ) . bytes ( ) . enumerate ( ) {
373
392
let escaped = match byte {
374
393
b'"' => "\\ \" " ,
375
394
b'\\' => "\\ \\ " ,
@@ -410,18 +429,18 @@ impl<'a> ::std::fmt::Display for EscapedString<'a> {
410
429
} ;
411
430
412
431
if start < i {
413
- f. write_str ( & self . 0 [ start..i] ) ?;
432
+ f. write_str ( & self . 0 . as_ref ( ) [ start..i] ) ?;
414
433
}
415
434
416
435
f. write_str ( escaped) ?;
417
436
418
437
start = i + 1 ;
419
438
}
420
439
421
- if start != self . 0 . len ( ) {
422
- f. write_str ( & self . 0 [ start..] ) ?;
440
+ if start != self . 0 . as_ref ( ) . len ( ) {
441
+ f. write_str ( & self . 0 . as_ref ( ) [ start..] ) ?;
423
442
}
424
443
425
444
Ok ( ( ) )
426
445
}
427
- }
446
+ }
0 commit comments