11
11
use super :: * ;
12
12
13
13
pub ( crate ) trait OutputFormatter {
14
- fn write_run_start ( & mut self , len : usize ) -> io:: Result < ( ) > ;
15
- fn write_test_start ( & mut self , test : & TestDesc ) -> io:: Result < ( ) > ;
14
+ fn write_run_start ( & mut self , test_count : usize ) -> io:: Result < ( ) > ;
15
+ fn write_test_start ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > ;
16
16
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > ;
17
17
fn write_result ( & mut self ,
18
18
desc : & TestDesc ,
@@ -26,17 +26,26 @@ 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
+ /// Number of columns to fill when aligning names
31
+ max_name_len : usize ,
32
+
33
+ is_multithreaded : bool ,
30
34
}
31
35
32
36
impl < T : Write > HumanFormatter < T > {
33
- pub fn new ( out : OutputLocation < T > , use_color : bool , terse : bool , max_name_len : usize ) -> Self {
37
+ pub fn new ( out : OutputLocation < T > ,
38
+ use_color : bool ,
39
+ terse : bool ,
40
+ max_name_len : usize ,
41
+ is_multithreaded : bool ) -> Self {
34
42
HumanFormatter {
35
43
out,
36
44
terse,
37
45
use_color,
38
46
test_count : 0 ,
39
47
max_name_len,
48
+ is_multithreaded,
40
49
}
41
50
}
42
51
@@ -160,28 +169,42 @@ impl<T: Write> HumanFormatter<T> {
160
169
}
161
170
Ok ( ( ) )
162
171
}
172
+
173
+ fn write_test_name ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
174
+ if !( self . terse && desc. name . padding ( ) != PadOnRight ) {
175
+ let name = desc. padded_name ( self . max_name_len , desc. name . padding ( ) ) ;
176
+ self . write_plain ( & format ! ( "test {} ... " , name) ) ?;
177
+ }
178
+
179
+ Ok ( ( ) )
180
+ }
163
181
}
164
182
165
183
impl < T : Write > OutputFormatter for HumanFormatter < T > {
166
- fn write_run_start ( & mut self , len : usize ) -> io:: Result < ( ) > {
167
- let noun = if len != 1 {
184
+ fn write_run_start ( & mut self , test_count : usize ) -> io:: Result < ( ) > {
185
+ let noun = if test_count != 1 {
168
186
"tests"
169
187
} else {
170
188
"test"
171
189
} ;
172
- self . write_plain ( & format ! ( "\n running {} {}\n " , len , noun) )
190
+ self . write_plain ( & format ! ( "\n running {} {}\n " , test_count , noun) )
173
191
}
174
192
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.
193
+ fn write_test_start ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
194
+ // When running tests concurrently, we should not print
195
+ // the test's name as the result will be mis-aligned.
196
+ // When running the tests serially, we print the name here so
197
+ // that the user can see which test hangs.
198
+ if !self . is_multithreaded {
199
+ self . write_test_name ( desc) ?;
200
+ }
201
+
178
202
Ok ( ( ) )
179
203
}
180
204
181
205
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) ) ?;
206
+ if self . is_multithreaded {
207
+ self . write_test_name ( desc) ?;
185
208
}
186
209
187
210
match * result {
@@ -197,6 +220,10 @@ impl<T: Write> OutputFormatter for HumanFormatter<T> {
197
220
}
198
221
199
222
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
223
+ if self . is_multithreaded {
224
+ self . write_test_name ( desc) ?;
225
+ }
226
+
200
227
self . write_plain ( & format ! ( "test {} has been running for over {} seconds\n " ,
201
228
desc. name,
202
229
TEST_WARN_TIMEOUT_S ) )
@@ -251,13 +278,14 @@ pub(crate) struct JsonFormatter<T> {
251
278
252
279
impl < T : Write > JsonFormatter < T > {
253
280
pub fn new ( out : OutputLocation < T > ) -> Self {
254
- Self {
255
- out, }
281
+ Self { out }
256
282
}
257
283
258
- fn write_str < S : AsRef < str > > ( & mut self , s : S ) -> io:: Result < ( ) > {
259
- self . out . write_all ( s. as_ref ( ) . as_ref ( ) ) ?;
260
- self . out . write_all ( "\n " . as_ref ( ) )
284
+ fn write_message ( & mut self , s : & str ) -> io:: Result < ( ) > {
285
+ assert ! ( !s. contains( '\n' ) ) ;
286
+
287
+ self . out . write_all ( s. as_ref ( ) ) ?;
288
+ self . out . write_all ( b"\n " )
261
289
}
262
290
263
291
fn write_event ( & mut self ,
@@ -266,14 +294,14 @@ impl<T: Write> JsonFormatter<T> {
266
294
evt : & str ,
267
295
extra : Option < String > ) -> io:: Result < ( ) > {
268
296
if let Some ( extras) = extra {
269
- self . write_str ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}", {} }}"# ,
297
+ self . write_message ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}", {} }}"# ,
270
298
ty,
271
299
name,
272
300
evt,
273
301
extras) )
274
302
}
275
303
else {
276
- self . write_str ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}" }}"# ,
304
+ self . write_message ( & * format ! ( r#"{{ "type": "{}", "name": "{}", "event": "{}" }}"# ,
277
305
ty,
278
306
name,
279
307
evt) )
@@ -282,13 +310,14 @@ impl<T: Write> JsonFormatter<T> {
282
310
}
283
311
284
312
impl < T : Write > OutputFormatter for JsonFormatter < T > {
285
- fn write_run_start ( & mut self , len : usize ) -> io:: Result < ( ) > {
286
- self . write_str (
287
- & * format ! ( r#"{{ "type": "suite", "event": "started", "test_count": "{}" }}"# , len) )
313
+ fn write_run_start ( & mut self , test_count : usize ) -> io:: Result < ( ) > {
314
+ self . write_message (
315
+ & * format ! ( r#"{{ "type": "suite", "event": "started", "test_count": "{}" }}"# ,
316
+ test_count) )
288
317
}
289
318
290
319
fn write_test_start ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
291
- self . write_str ( & * format ! ( r#"{{ "type": "test", "event": "started", "name": "{}" }}"# ,
320
+ self . write_message ( & * format ! ( r#"{{ "type": "test", "event": "started", "name": "{}" }}"# ,
292
321
desc. name) )
293
322
}
294
323
@@ -348,19 +377,19 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
348
377
deviation,
349
378
mbps) ;
350
379
351
- self . write_str ( & * line)
380
+ self . write_message ( & * line)
352
381
} ,
353
382
}
354
383
}
355
384
356
385
fn write_timeout ( & mut self , desc : & TestDesc ) -> io:: Result < ( ) > {
357
- self . write_str ( & * format ! ( r#"{{ "type": "test", "event": "timeout", "name": "{}" }}"# ,
386
+ self . write_message ( & * format ! ( r#"{{ "type": "test", "event": "timeout", "name": "{}" }}"# ,
358
387
desc. name) )
359
388
}
360
389
361
390
fn write_run_finish ( & mut self , state : & ConsoleTestState ) -> io:: Result < bool > {
362
391
363
- self . write_str ( & * format ! ( "{{ \" type\" : \" suite\" , \
392
+ self . write_message ( & * format ! ( "{{ \" type\" : \" suite\" , \
364
393
\" event\" : \" {}\" , \
365
394
\" passed\" : {}, \
366
395
\" failed\" : {}, \
0 commit comments