@@ -219,6 +219,7 @@ mod tests {
219
219
220
220
use std:: process:: Child ;
221
221
use std:: process:: Command ;
222
+ use std:: process:: Output ;
222
223
use std:: process:: Stdio ;
223
224
224
225
fn cargo_run_cmd ( ) -> Vec < String > {
@@ -253,6 +254,37 @@ mod tests {
253
254
cmd
254
255
}
255
256
257
+ fn run_debug_binary_no_input ( mut cmd : Command ) -> Output {
258
+ cmd. output ( ) . expect ( "run debug binary" )
259
+ }
260
+
261
+ fn run_debug_binary_with_input ( mut cmd : Command , input : & [ u8 ] ) -> Output {
262
+ use std:: io:: Write ;
263
+ let mut cmd: Child = cmd
264
+ . stdin ( Stdio :: piped ( ) )
265
+ . stdout ( Stdio :: piped ( ) )
266
+ . spawn ( )
267
+ . expect ( "spawn debug binary" ) ;
268
+
269
+ cmd. stdin
270
+ . as_mut ( )
271
+ . expect ( "child stdin" )
272
+ . write_all ( input)
273
+ . expect ( "write to child stdin" ) ;
274
+
275
+ cmd. wait_with_output ( ) . expect ( "run debug binary" )
276
+ }
277
+
278
+ fn assert_cmd_success ( output : & Output ) {
279
+ assert ! ( output. status. success( ) ) ;
280
+ assert_stderr_empty ( & output) ;
281
+ }
282
+
283
+ fn assert_stderr_empty ( output : & Output ) {
284
+ let err = String :: from_utf8_lossy ( & output. stderr ) ;
285
+ assert ! ( err. is_empty( ) ) ;
286
+ }
287
+
256
288
#[ test]
257
289
fn to_utf8_converts_utf_8_bytes_to_utf_8 ( ) {
258
290
let some_utf_8_str = "å" ;
@@ -283,10 +315,8 @@ mod tests {
283
315
}
284
316
285
317
#[ test]
286
- fn width_0_exits_with_code_1 ( ) {
287
- let output = target_binary_with_width ( "0" )
288
- . output ( )
289
- . expect ( "run debug binary" ) ;
318
+ fn arg_width_0_exits_with_code_1 ( ) {
319
+ let output = run_debug_binary_no_input ( target_binary_with_width ( "0" ) ) ;
290
320
291
321
assert_eq ! ( 1 , output. status. code( ) . unwrap( ) ) ;
292
322
@@ -296,10 +326,8 @@ mod tests {
296
326
}
297
327
298
328
#[ test]
299
- fn width_0foo_exits_with_code_1 ( ) {
300
- let output = target_binary_with_width ( "0foo" )
301
- . output ( )
302
- . expect ( "run debug binary" ) ;
329
+ fn arg_width_0foo_exits_with_code_1 ( ) {
330
+ let output = run_debug_binary_no_input ( target_binary_with_width ( "0foo" ) ) ;
303
331
304
332
assert_eq ! ( 1 , output. status. code( ) . unwrap( ) ) ;
305
333
@@ -309,11 +337,11 @@ mod tests {
309
337
}
310
338
311
339
#[ test]
312
- fn width_with_multiple_values_exits_with_code_1 ( ) {
340
+ fn arg_width_with_multiple_values_exits_with_code_1 ( ) {
313
341
let mut cmd = target_binary ( ) ;
314
342
cmd. args ( & [ "-w" , "1" , "2" ] ) ;
315
343
316
- let output = cmd . output ( ) . expect ( "run debug binary" ) ;
344
+ let output = run_debug_binary_no_input ( cmd ) ;
317
345
318
346
assert_eq ! ( 1 , output. status. code( ) . unwrap( ) ) ;
319
347
@@ -322,24 +350,11 @@ mod tests {
322
350
}
323
351
324
352
#[ test]
325
- fn width_1_wraps_body_at_width_1_and_exits_successfully ( ) {
326
- use std:: io:: Write ;
327
-
328
- let mut cmd: Child = target_binary_with_width ( "1" )
329
- . stdin ( Stdio :: piped ( ) )
330
- . stdout ( Stdio :: piped ( ) )
331
- . spawn ( )
332
- . expect ( "spawn debug binary" ) ;
333
-
334
- cmd. stdin
335
- . as_mut ( )
336
- . expect ( "child stdin" )
337
- . write_all ( b"subject\n b o d y" )
338
- . expect ( "write to child stdin" ) ;
339
-
340
- let output = cmd. wait_with_output ( ) . expect ( "run debug binary" ) ;
353
+ fn arg_width_1_wraps_body_at_width_1_and_exits_successfully ( ) {
354
+ let cmd = target_binary_with_width ( "1" ) ;
355
+ let output = run_debug_binary_with_input ( cmd, b"subject\n b o d y" ) ;
341
356
342
- assert ! ( output. status . success ( ) ) ;
357
+ assert_cmd_success ( & output) ;
343
358
344
359
let out = String :: from_utf8_lossy ( & output. stdout ) ;
345
360
assert_eq ! (
352
367
" ,
353
368
out
354
369
) ;
355
-
356
- let err = String :: from_utf8_lossy ( & output. stderr ) ;
357
- assert ! ( err. is_empty( ) ) ;
358
370
}
359
371
360
372
#[ test]
361
- fn width_short_form_is_w ( ) {
362
- use std:: io:: Write ;
363
-
373
+ fn arg_width_short_form_is_w ( ) {
364
374
let mut cmd = target_binary ( ) ;
365
375
cmd. args ( & [ "-w" , "1" ] ) ;
366
- let mut cmd: Child = cmd
367
- . stdin ( Stdio :: piped ( ) )
368
- . stdout ( Stdio :: piped ( ) )
369
- . spawn ( )
370
- . expect ( "spawn debug binary" ) ;
371
-
372
- cmd. stdin
373
- . as_mut ( )
374
- . expect ( "child stdin" )
375
- . write_all ( b"subject\n b o d y" )
376
- . expect ( "write to child stdin" ) ;
376
+ let output = run_debug_binary_with_input ( cmd, b"subject\n b o d y" ) ;
377
377
378
- let output = cmd. wait_with_output ( ) . expect ( "run debug binary" ) ;
379
-
380
- assert ! ( output. status. success( ) ) ;
378
+ assert_cmd_success ( & output) ;
381
379
382
380
let out = String :: from_utf8_lossy ( & output. stdout ) ;
383
381
assert_eq ! (
390
388
" ,
391
389
out
392
390
) ;
393
-
394
- let err = String :: from_utf8_lossy ( & output. stderr ) ;
395
- assert ! ( err. is_empty( ) ) ;
396
391
}
397
392
398
393
#[ test]
399
- fn only_last_specified_width_matters ( ) {
400
- use std:: io:: Write ;
401
-
394
+ fn arg_width_only_last_specified_matters ( ) {
402
395
let mut cmd = target_binary_with_width ( "string" ) ;
403
396
cmd. args ( & [ "-w" , "1" ] ) ;
404
397
cmd. args ( & [ "-w" , "100" ] ) ;
398
+ let output = run_debug_binary_with_input ( cmd, b"subject\n b o d y" ) ;
405
399
406
- let mut cmd: Child = cmd
407
- . stdin ( Stdio :: piped ( ) )
408
- . stdout ( Stdio :: piped ( ) )
409
- . spawn ( )
410
- . expect ( "spawn debug binary" ) ;
411
-
412
- cmd. stdin
413
- . as_mut ( )
414
- . expect ( "child stdin" )
415
- . write_all ( b"subject\n b o d y" )
416
- . expect ( "write to child stdin" ) ;
417
-
418
- let output = cmd. wait_with_output ( ) . expect ( "run debug binary" ) ;
419
-
420
- assert ! ( output. status. success( ) ) ;
400
+ assert_cmd_success ( & output) ;
421
401
422
402
let out = String :: from_utf8_lossy ( & output. stdout ) ;
423
403
assert_eq ! (
@@ -427,9 +407,6 @@ b o d y
427
407
" ,
428
408
out
429
409
) ;
430
-
431
- let err = String :: from_utf8_lossy ( & output. stderr ) ;
432
- assert ! ( err. is_empty( ) ) ;
433
410
}
434
411
435
412
// Sometime after the release of v1.2.0, external changes to Travis CI have
@@ -516,8 +493,5 @@ b o d y
516
493
. expect ( "trigger broken pipe for debug binary" ) ;
517
494
518
495
assert_eq ! ( 141 , output. status. code( ) . unwrap( ) ) ;
519
-
520
- let err = String :: from_utf8_lossy ( & output. stderr ) ;
521
- assert ! ( err. is_empty( ) ) ;
522
496
}
523
497
}
0 commit comments