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

Commit b75d7f0

Browse files
committed
Merge #43
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`.
2 parents f93218f + 44a40f9 commit b75d7f0

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/assert.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,27 @@ impl Assert {
241241

242242
if let Some(expect_success) = self.expect_success {
243243
if expect_success != output.status.success() {
244+
let out = String::from_utf8_lossy(&output.stdout).to_string();
245+
let err = String::from_utf8_lossy(&output.stderr).to_string();
244246
bail!(ErrorKind::StatusMismatch(
245247
self.cmd.clone(),
246248
expect_success,
249+
out,
250+
err,
247251
));
248252
}
249253
}
250254

251255
if self.expect_exit_code.is_some() &&
252256
self.expect_exit_code != output.status.code() {
257+
let out = String::from_utf8_lossy(&output.stdout).to_string();
258+
let err = String::from_utf8_lossy(&output.stderr).to_string();
253259
bail!(ErrorKind::ExitCodeMismatch(
254260
self.cmd.clone(),
255261
self.expect_exit_code,
256262
output.status.code(),
263+
out,
264+
err,
257265
));
258266
}
259267

src/errors.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,35 @@ error_chain! {
66
Fmt(::std::fmt::Error);
77
}
88
errors {
9-
StatusMismatch(cmd: Vec<String>, expected: bool) {
9+
StatusMismatch(cmd: Vec<String>, expected: bool, out: String, err: String) {
1010
description("Wrong status")
1111
display(
12-
"{}: (command `{}` expected to {}) (command {})",
12+
"{}: (command `{}` expected to {})\nstatus={}\nstdout=```{}```\nstderr=```{}```",
1313
ERROR_PREFIX,
1414
cmd.join(" "),
1515
expected = if *expected { "succeed" } else { "fail" },
1616
got = if *expected { "failed" } else { "succeeded" },
17+
out = out,
18+
err = err,
1719
)
1820
}
19-
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
21+
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>, out: String, err: String) {
2022
description("Wrong exit code")
2123
display(
22-
"{}: (exit code of `{}` expected to be `{:?}`) (exit code was: `{:?}`)",
24+
"{}: (exit code of `{}` expected to be `{:?}`)\nexit code=`{:?}`\nstdout=```{}```\nstderr=```{}```",
2325
ERROR_PREFIX,
2426
cmd.join(" "),
2527
expected,
2628
got,
29+
out,
30+
err,
2731
)
2832
}
29-
StdoutMismatch(cmd: Vec<String>, output_err: ::output::Error) {
33+
OutputMismatch(cmd: Vec<String>, output_err: ::output::Error, kind: ::output::OutputKind) {
3034
description("Output was not as expected")
3135
display(
32-
"{}: `{}` stdout mismatch: `{}`)",
33-
ERROR_PREFIX, cmd.join(" "), output_err,
34-
)
35-
}
36-
StderrMismatch(cmd: Vec<String>, output_err: ::output::Error) {
37-
description("Error output was not as expected")
38-
display(
39-
"{}: `{}` stderr mismatch: `{}`)",
40-
ERROR_PREFIX, cmd.join(" "), output_err,
36+
"{}: `{}` {:?} mismatch: {}",
37+
ERROR_PREFIX, cmd.join(" "), kind, output_err,
4138
)
4239
}
4340

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)