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

Commit

Permalink
Tweak error output to include context
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 27, 2017
1 parent 0072b5c commit 44a40f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
13 changes: 3 additions & 10 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ error_chain! {
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 44a40f9

Please sign in to comment.