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

Commit 44a40f9

Browse files
committed
Tweak error output to include context
1 parent 0072b5c commit 44a40f9

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

src/errors.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,11 @@ error_chain! {
3030
err,
3131
)
3232
}
33-
StdoutMismatch(cmd: Vec<String>, output_err: ::output::Error) {
33+
OutputMismatch(cmd: Vec<String>, output_err: ::output::Error, kind: ::output::OutputKind) {
3434
description("Output was not as expected")
3535
display(
36-
"{}: `{}` stdout mismatch: `{}`)",
37-
ERROR_PREFIX, cmd.join(" "), output_err,
38-
)
39-
}
40-
StderrMismatch(cmd: Vec<String>, output_err: ::output::Error) {
41-
description("Error output was not as expected")
42-
display(
43-
"{}: `{}` stderr mismatch: `{}`)",
44-
ERROR_PREFIX, cmd.join(" "), output_err,
36+
"{}: `{}` {:?} mismatch: {}",
37+
ERROR_PREFIX, cmd.join(" "), kind, output_err,
4538
)
4639
}
4740

src/output.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl OutputAssertion {
3535
if result != self.expected_result {
3636
if self.expected_result {
3737
let nice_diff = diff::render(&differences)?;
38-
bail!(ErrorKind::OutputDoesntMatch(nice_diff));
38+
bail!(ErrorKind::OutputDoesntMatch(self.expect.clone(), got.to_owned(), nice_diff));
3939
} else {
4040
bail!(ErrorKind::OutputMatches(got.to_owned()));
4141
}
@@ -52,7 +52,9 @@ impl OutputAssertion {
5252
} else {
5353
self.matches_exact(&observed)
5454
};
55-
result.map_err(|e| self.kind.map_err(e, cmd))
55+
result.map_err(|e| super::errors::ErrorKind::OutputMismatch(cmd.to_vec(), e, self.kind))?;
56+
57+
Ok(())
5658
}
5759
}
5860

@@ -69,13 +71,6 @@ impl OutputKind {
6971
OutputKind::StdErr => &o.stderr,
7072
}
7173
}
72-
73-
pub fn map_err(self, e: Error, cmd: &[String]) -> super::errors::Error {
74-
match self {
75-
OutputKind::StdOut => super::errors::ErrorKind::StdoutMismatch(cmd.to_vec(), e).into(),
76-
OutputKind::StdErr => super::errors::ErrorKind::StderrMismatch(cmd.to_vec(), e).into(),
77-
}
78-
}
7974
}
8075

8176
mod errors {
@@ -86,19 +81,19 @@ mod errors {
8681
errors {
8782
OutputDoesntContain(expected: String, got: String) {
8883
description("Output was not as expected")
89-
display("expected to contain {:?}, got {:?}", expected, got)
84+
display("expected to contain {:?}\noutput=```{}```", expected, got)
9085
}
9186
OutputContains(expected: String, got: String) {
9287
description("Output was not as expected")
93-
display("expected to not contain {:?}, got {:?}", expected, got)
88+
display("expected to not contain {:?}\noutput=```{}```", expected, got)
9489
}
95-
OutputDoesntMatch(diff: String) {
90+
OutputDoesntMatch(expected: String, got: String, diff: String) {
9691
description("Output was not as expected")
97-
display("{}", diff)
92+
display("diff:\n{}", diff)
9893
}
9994
OutputMatches(got: String) {
10095
description("Output was not as expected")
101-
display("{}", got)
96+
display("expected to not match\noutput=```{}```", got)
10297
}
10398
}
10499
}

0 commit comments

Comments
 (0)