diff --git a/src/assert.rs b/src/assert.rs index 0732c35..da769c9 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -241,19 +241,27 @@ impl Assert { if let Some(expect_success) = self.expect_success { if expect_success != output.status.success() { + let out = String::from_utf8_lossy(&output.stdout).to_string(); + let err = String::from_utf8_lossy(&output.stderr).to_string(); bail!(ErrorKind::StatusMismatch( self.cmd.clone(), expect_success, + out, + err, )); } } if self.expect_exit_code.is_some() && self.expect_exit_code != output.status.code() { + let out = String::from_utf8_lossy(&output.stdout).to_string(); + let err = String::from_utf8_lossy(&output.stderr).to_string(); bail!(ErrorKind::ExitCodeMismatch( self.cmd.clone(), self.expect_exit_code, output.status.code(), + out, + err, )); } diff --git a/src/errors.rs b/src/errors.rs index 3499e18..9f0c7ea 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,38 +6,35 @@ error_chain! { Fmt(::std::fmt::Error); } errors { - StatusMismatch(cmd: Vec, expected: bool) { + StatusMismatch(cmd: Vec, expected: bool, out: String, err: String) { description("Wrong status") display( - "{}: (command `{}` expected to {}) (command {})", + "{}: (command `{}` expected to {})\nstatus={}\nstdout=```{}```\nstderr=```{}```", ERROR_PREFIX, cmd.join(" "), expected = if *expected { "succeed" } else { "fail" }, got = if *expected { "failed" } else { "succeeded" }, + out = out, + err = err, ) } - ExitCodeMismatch(cmd: Vec, expected: Option, got: Option) { + ExitCodeMismatch(cmd: Vec, expected: Option, got: Option, out: String, err: String) { description("Wrong exit code") display( - "{}: (exit code of `{}` expected to be `{:?}`) (exit code was: `{:?}`)", + "{}: (exit code of `{}` expected to be `{:?}`)\nexit code=`{:?}`\nstdout=```{}```\nstderr=```{}```", ERROR_PREFIX, cmd.join(" "), expected, got, + out, + err, ) } - StdoutMismatch(cmd: Vec, output_err: ::output::Error) { + OutputMismatch(cmd: Vec, output_err: ::output::Error, kind: ::output::OutputKind) { description("Output was not as expected") display( - "{}: `{}` stdout mismatch: `{}`)", - ERROR_PREFIX, cmd.join(" "), output_err, - ) - } - StderrMismatch(cmd: Vec, output_err: ::output::Error) { - description("Error output was not as expected") - display( - "{}: `{}` stderr mismatch: `{}`)", - ERROR_PREFIX, cmd.join(" "), output_err, + "{}: `{}` {:?} mismatch: {}", + ERROR_PREFIX, cmd.join(" "), kind, output_err, ) } diff --git a/src/output.rs b/src/output.rs index 3376226..920ade6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -35,7 +35,7 @@ impl OutputAssertion { if result != self.expected_result { if self.expected_result { let nice_diff = diff::render(&differences)?; - bail!(ErrorKind::OutputDoesntMatch(nice_diff)); + bail!(ErrorKind::OutputDoesntMatch(self.expect.clone(), got.to_owned(), nice_diff)); } else { bail!(ErrorKind::OutputMatches(got.to_owned())); } @@ -52,7 +52,9 @@ impl OutputAssertion { } else { self.matches_exact(&observed) }; - result.map_err(|e| self.kind.map_err(e, cmd)) + result.map_err(|e| super::errors::ErrorKind::OutputMismatch(cmd.to_vec(), e, self.kind))?; + + Ok(()) } } @@ -69,13 +71,6 @@ impl OutputKind { OutputKind::StdErr => &o.stderr, } } - - pub fn map_err(self, e: Error, cmd: &[String]) -> super::errors::Error { - match self { - OutputKind::StdOut => super::errors::ErrorKind::StdoutMismatch(cmd.to_vec(), e).into(), - OutputKind::StdErr => super::errors::ErrorKind::StderrMismatch(cmd.to_vec(), e).into(), - } - } } mod errors { @@ -86,19 +81,19 @@ mod errors { errors { OutputDoesntContain(expected: String, got: String) { description("Output was not as expected") - display("expected to contain {:?}, got {:?}", expected, got) + display("expected to contain {:?}\noutput=```{}```", expected, got) } OutputContains(expected: String, got: String) { description("Output was not as expected") - display("expected to not contain {:?}, got {:?}", expected, got) + display("expected to not contain {:?}\noutput=```{}```", expected, got) } - OutputDoesntMatch(diff: String) { + OutputDoesntMatch(expected: String, got: String, diff: String) { description("Output was not as expected") - display("{}", diff) + display("diff:\n{}", diff) } OutputMatches(got: String) { description("Output was not as expected") - display("{}", got) + display("expected to not match\noutput=```{}```", got) } } }