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

Commit 0072b5c

Browse files
committed
Show more context on exit code failures
stdout and stderr will now be shown when the exit code is an unexpected value. This will serve to provide additional context for the developer without them having to fire up a debugger or manually reproduce the failure condition.
1 parent f93218f commit 0072b5c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@ 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
}
2933
StdoutMismatch(cmd: Vec<String>, output_err: ::output::Error) {

0 commit comments

Comments
 (0)