1
- use environment:: Environment ;
2
- use error_chain:: ChainedError ;
3
- use errors:: * ;
4
- use output:: { Content , Output , OutputKind , OutputPredicate } ;
5
1
use std:: default;
6
2
use std:: ffi:: { OsStr , OsString } ;
7
3
use std:: io:: Write ;
8
4
use std:: path:: PathBuf ;
9
5
use std:: process:: { Command , Stdio } ;
10
6
use std:: vec:: Vec ;
11
7
8
+ use environment:: Environment ;
9
+ use failure;
10
+ use failure:: Fail ;
11
+
12
+ use errors:: * ;
13
+ use output:: { Content , Output , OutputKind , OutputPredicate } ;
14
+
12
15
/// Assertions for a specific command.
13
16
#[ derive( Debug ) ]
14
17
#[ must_use]
@@ -324,7 +327,7 @@ impl Assert {
324
327
/// .execute();
325
328
/// assert!(test.is_ok());
326
329
/// ```
327
- pub fn execute ( self ) -> Result < ( ) > {
330
+ pub fn execute ( self ) -> Result < ( ) , AssertionError > {
328
331
let bin = & self . cmd [ 0 ] ;
329
332
330
333
let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
@@ -344,42 +347,51 @@ impl Assert {
344
347
345
348
let mut spawned = command
346
349
. spawn ( )
347
- . chain_err ( || ErrorKind :: SpawnFailed ( self . cmd . clone ( ) ) ) ?;
350
+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) ) ?;
348
351
349
352
if let Some ( ref contents) = self . stdin_contents {
350
353
spawned
351
354
. stdin
352
355
. as_mut ( )
353
356
. expect ( "Couldn't get mut ref to command stdin" )
354
- . write_all ( contents) ?;
357
+ . write_all ( contents)
358
+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) ) ?;
355
359
}
356
- let output = spawned. wait_with_output ( ) ?;
360
+ let output = spawned
361
+ . wait_with_output ( )
362
+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) ) ?;
357
363
358
364
if let Some ( expect_success) = self . expect_success {
359
- if expect_success != output. status . success ( ) {
360
- let out = String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ;
361
- let err = String :: from_utf8_lossy ( & output. stderr ) . to_string ( ) ;
362
- let err: Error = ErrorKind :: StatusMismatch ( expect_success, out, err) . into ( ) ;
363
- bail ! ( err. chain_err( || ErrorKind :: AssertionFailed ( self . cmd. clone( ) ) ) ) ;
365
+ let actual_success = output. status . success ( ) ;
366
+ if expect_success != actual_success {
367
+ return Err (
368
+ AssertionError :: new ( self . cmd . clone ( ) ) . chain ( StatusError :: new (
369
+ actual_success,
370
+ output. stdout . clone ( ) ,
371
+ output. stderr . clone ( ) ,
372
+ ) ) ,
373
+ ) ?;
364
374
}
365
375
}
366
376
367
377
if self . expect_exit_code . is_some ( ) && self . expect_exit_code != output. status . code ( ) {
368
- let out = String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ;
369
- let err = String :: from_utf8_lossy ( & output. stderr ) . to_string ( ) ;
370
- let err: Error =
371
- ErrorKind :: ExitCodeMismatch ( self . expect_exit_code , output. status . code ( ) , out, err)
372
- . into ( ) ;
373
- bail ! ( err. chain_err( || ErrorKind :: AssertionFailed ( self . cmd. clone( ) ) ) ) ;
378
+ return Err (
379
+ AssertionError :: new ( self . cmd . clone ( ) ) . chain ( ExitCodeError :: new (
380
+ self . expect_exit_code ,
381
+ output. status . code ( ) ,
382
+ output. stdout . clone ( ) ,
383
+ output. stderr . clone ( ) ,
384
+ ) ) ,
385
+ ) ;
374
386
}
375
387
376
388
self . expect_output
377
389
. iter ( )
378
390
. map ( |a| {
379
391
a. verify ( & output)
380
- . chain_err ( || ErrorKind :: AssertionFailed ( self . cmd . clone ( ) ) )
392
+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) )
381
393
} )
382
- . collect :: < Result < Vec < ( ) > > > ( ) ?;
394
+ . collect :: < Result < Vec < ( ) > , AssertionError > > ( ) ?;
383
395
384
396
Ok ( ( ) )
385
397
}
@@ -397,8 +409,16 @@ impl Assert {
397
409
/// ```
398
410
pub fn unwrap ( self ) {
399
411
if let Err ( err) = self . execute ( ) {
400
- panic ! ( "{}" , err. display_chain( ) ) ;
412
+ panic ! ( Self :: format_causes( err. causes( ) ) ) ;
413
+ }
414
+ }
415
+
416
+ fn format_causes ( mut causes : failure:: Causes ) -> String {
417
+ let mut result = causes. next ( ) . expect ( "an error should exist" ) . to_string ( ) ;
418
+ for cause in causes {
419
+ result. push_str ( & format ! ( "\n with: {}" , cause) ) ;
401
420
}
421
+ result
402
422
}
403
423
}
404
424
0 commit comments