Skip to content

Commit 6507f9f

Browse files
authored
Merge pull request #17 from epage/context
feat(assert): Context on status failures
2 parents 2a17c34 + a96c4c4 commit 6507f9f

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ matrix:
1818
install:
1919
script:
2020
- cargo check --tests --all-features
21-
- env: CLIPPY_VERSION="0.0.179"
22-
rust: nightly-2018-01-12
21+
- env: CLIPPY_VERSION="0.0.209"
22+
rust: nightly-2018-06-19
2323
install:
2424
- travis_wait cargo install clippy --version $CLIPPY_VERSION || echo "clippy already installed"
2525
script:

src/assert.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::str;
55
use predicates;
66
use predicates::str::PredicateStrExt;
77

8+
use cmd::dump_buffer;
89
use errors::output_fmt;
910

1011
/// Assert the state of an `Output`.
@@ -127,7 +128,19 @@ impl Assert {
127128
/// ```
128129
pub fn success(self) -> Self {
129130
if !self.output.status.success() {
130-
panic!("Unexpected failure\n{}", self);
131+
let actual_code = self.output.status.code().unwrap_or_else(|| {
132+
panic!(
133+
"Unexpected failure.\ncode=<interrupted>\nstderr=```{}```\n{}",
134+
dump_buffer(&self.output.stderr),
135+
self
136+
)
137+
});
138+
panic!(
139+
"Unexpected failure.\ncode-{}\nstderr=```{}```\n{}",
140+
actual_code,
141+
dump_buffer(&self.output.stderr),
142+
self
143+
);
131144
}
132145
self
133146
}
@@ -149,15 +162,23 @@ impl Assert {
149162
/// ```
150163
pub fn failure(self) -> Self {
151164
if self.output.status.success() {
152-
panic!("Unexpected success\n{}", self);
165+
panic!(
166+
"Unexpected success\nstdout=```{}```\n{}",
167+
dump_buffer(&self.output.stdout),
168+
self
169+
);
153170
}
154171
self
155172
}
156173

157174
/// Ensure the command aborted before returning a code.
158175
pub fn interrupted(self) -> Self {
159176
if self.output.status.code().is_some() {
160-
panic!("Unexpected completion\n{}", self);
177+
panic!(
178+
"Unexpected completion\nstdout=```{}```\n{}",
179+
dump_buffer(&self.output.stdout),
180+
self
181+
);
161182
}
162183
self
163184
}
@@ -186,12 +207,20 @@ impl Assert {
186207
}
187208

188209
fn code_impl(self, pred: &predicates::Predicate<i32>) -> Self {
189-
let actual_code = self.output
190-
.status
191-
.code()
192-
.unwrap_or_else(|| panic!("Command interrupted\n{}", self));
210+
let actual_code = self.output.status.code().unwrap_or_else(|| {
211+
panic!(
212+
"Command interrupted\nstderr=```{}```\n{}",
213+
dump_buffer(&self.output.stderr),
214+
self
215+
)
216+
});
193217
if !pred.eval(&actual_code) {
194-
panic!("Unexpected return code\n{}", self);
218+
panic!(
219+
"Unexpected return code\nstdout=```{}```\nstderr=```{}```\n{}",
220+
dump_buffer(&self.output.stdout),
221+
dump_buffer(&self.output.stderr),
222+
self
223+
);
195224
}
196225
self
197226
}

0 commit comments

Comments
 (0)