1- use environment:: Environment ;
2- use error_chain:: ChainedError ;
3- use errors:: * ;
4- use output:: { Content , Output , OutputKind , OutputPredicate } ;
51use std:: default;
62use std:: ffi:: { OsStr , OsString } ;
73use std:: io:: Write ;
84use std:: path:: PathBuf ;
95use std:: process:: { Command , Stdio } ;
106use std:: vec:: Vec ;
117
8+ use environment:: Environment ;
9+ use failure;
10+ use failure:: Fail ;
11+
12+ use errors:: * ;
13+ use output:: { Content , Output , OutputKind , OutputPredicate } ;
14+
1215/// Assertions for a specific command.
1316#[ derive( Debug ) ]
1417#[ must_use]
@@ -324,7 +327,7 @@ impl Assert {
324327 /// .execute();
325328 /// assert!(test.is_ok());
326329 /// ```
327- pub fn execute ( self ) -> Result < ( ) > {
330+ pub fn execute ( self ) -> Result < ( ) , AssertionError > {
328331 let bin = & self . cmd [ 0 ] ;
329332
330333 let args: Vec < _ > = self . cmd . iter ( ) . skip ( 1 ) . collect ( ) ;
@@ -344,42 +347,51 @@ impl Assert {
344347
345348 let mut spawned = command
346349 . spawn ( )
347- . chain_err ( || ErrorKind :: SpawnFailed ( self . cmd . clone ( ) ) ) ?;
350+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) ) ?;
348351
349352 if let Some ( ref contents) = self . stdin_contents {
350353 spawned
351354 . stdin
352355 . as_mut ( )
353356 . 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 ( ) ) ) ?;
355359 }
356- let output = spawned. wait_with_output ( ) ?;
360+ let output = spawned
361+ . wait_with_output ( )
362+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) ) ?;
357363
358364 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+ ) ?;
364374 }
365375 }
366376
367377 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+ ) ;
374386 }
375387
376388 self . expect_output
377389 . iter ( )
378390 . map ( |a| {
379391 a. verify ( & output)
380- . chain_err ( || ErrorKind :: AssertionFailed ( self . cmd . clone ( ) ) )
392+ . chain_with ( || AssertionError :: new ( self . cmd . clone ( ) ) )
381393 } )
382- . collect :: < Result < Vec < ( ) > > > ( ) ?;
394+ . collect :: < Result < Vec < ( ) > , AssertionError > > ( ) ?;
383395
384396 Ok ( ( ) )
385397 }
@@ -397,8 +409,16 @@ impl Assert {
397409 /// ```
398410 pub fn unwrap ( self ) {
399411 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) ) ;
401420 }
421+ result
402422 }
403423}
404424
0 commit comments