Skip to content

Commit

Permalink
feat(assert): Context on status failures
Browse files Browse the repository at this point in the history
Provide extra context on status failures (success, failure, interrupted,
code).  At the moment, the choice of whether to print stdout or stderr
is context specific.  I have a feeling this won't work as well as I'm
expecting but we'll start here.

Fixes #16
  • Loading branch information
epage committed Jun 20, 2018
1 parent 2a17c34 commit af52e9c
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str;
use predicates;
use predicates::str::PredicateStrExt;

use cmd::dump_buffer;
use errors::output_fmt;

/// Assert the state of an `Output`.
Expand Down Expand Up @@ -127,7 +128,19 @@ impl Assert {
/// ```
pub fn success(self) -> Self {
if !self.output.status.success() {
panic!("Unexpected failure\n{}", self);
let actual_code = self.output.status.code().unwrap_or_else(|| {
panic!(
"Unexpected failure.\ncode=<interrupted>\nstderr=```{}```\n{}",
dump_buffer(&self.output.stderr),
self
)
});
panic!(
"Unexpected failure.\ncode-{}\nstderr=```{}```\n{}",
actual_code,
dump_buffer(&self.output.stderr),
self
);
}
self
}
Expand All @@ -149,15 +162,23 @@ impl Assert {
/// ```
pub fn failure(self) -> Self {
if self.output.status.success() {
panic!("Unexpected success\n{}", self);
panic!(
"Unexpected success\nstdout=```{}```\n{}",
dump_buffer(&self.output.stdout),
self
);
}
self
}

/// Ensure the command aborted before returning a code.
pub fn interrupted(self) -> Self {
if self.output.status.code().is_some() {
panic!("Unexpected completion\n{}", self);
panic!(
"Unexpected completion\nstdout=```{}```\n{}",
dump_buffer(&self.output.stdout),
self
);
}
self
}
Expand Down Expand Up @@ -186,12 +207,20 @@ impl Assert {
}

fn code_impl(self, pred: &predicates::Predicate<i32>) -> Self {
let actual_code = self.output
.status
.code()
.unwrap_or_else(|| panic!("Command interrupted\n{}", self));
let actual_code = self.output.status.code().unwrap_or_else(|| {
panic!(
"Command interrupted\nstderr=```{}```\n{}",
dump_buffer(&self.output.stderr),
self
)
});
if !pred.eval(&actual_code) {
panic!("Unexpected return code\n{}", self);
panic!(
"Unexpected return code\nstdout=```{}```\nstderr=```{}```\n{}",
dump_buffer(&self.output.stdout),
dump_buffer(&self.output.stderr),
self
);
}
self
}
Expand Down

0 comments on commit af52e9c

Please sign in to comment.