Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

Commit

Permalink
Merge #43
Browse files Browse the repository at this point in the history
43: Increase context on failures r=epage a=epage

- Show stdout/stderr on exit code failures
- Show expected/got strings for output assertions

This also includes a minor refactor to simplify `output.rs`.
  • Loading branch information
bors[bot] committed Sep 27, 2017
2 parents f93218f + 44a40f9 commit b75d7f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
8 changes: 8 additions & 0 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}

Expand Down
25 changes: 11 additions & 14 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,35 @@ error_chain! {
Fmt(::std::fmt::Error);
}
errors {
StatusMismatch(cmd: Vec<String>, expected: bool) {
StatusMismatch(cmd: Vec<String>, 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<String>, expected: Option<i32>, got: Option<i32>) {
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>, 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<String>, output_err: ::output::Error) {
OutputMismatch(cmd: Vec<String>, 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<String>, 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,
)
}

Expand Down
23 changes: 9 additions & 14 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand All @@ -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(())
}
}

Expand All @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit b75d7f0

Please sign in to comment.