Skip to content

Commit 712c738

Browse files
authored
Merge pull request assert-rs#91 from epage/fail
Migrate to failure
2 parents 7052cb0 + 67db224 commit 712c738

File tree

7 files changed

+531
-176
lines changed

7 files changed

+531
-176
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ name = "assert_fixture"
1818
[dependencies]
1919
colored = "1.5"
2020
difference = "2.0"
21-
error-chain = "0.11"
21+
failure = "0.1"
22+
failure_derive = "0.1"
2223
serde_json = "1.0"
2324
environment = "0.1"
2425

src/assert.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
use environment::Environment;
2-
use error_chain::ChainedError;
3-
use errors::*;
4-
use output::{Content, Output, OutputKind, OutputPredicate};
51
use std::default;
62
use std::ffi::{OsStr, OsString};
73
use std::io::Write;
84
use std::path::PathBuf;
95
use std::process::{Command, Stdio};
106
use std::vec::Vec;
117

8+
use environment::Environment;
9+
use failure;
10+
use failure::Fail;
11+
12+
use errors::*;
13+
use output::{Content, Output, OutputKind, OutputPredicate};
14+
1215
/// Assertions for a specific command.
1316
#[derive(Debug)]
1417
#[must_use]
@@ -324,7 +327,7 @@ impl Assert {
324327
/// .execute();
325328
/// assert!(test.is_ok());
326329
/// ```
327-
pub fn execute(self) -> Result<()> {
330+
pub fn execute(self) -> Result<(), AssertionError> {
328331
let bin = &self.cmd[0];
329332

330333
let args: Vec<_> = self.cmd.iter().skip(1).collect();
@@ -344,42 +347,51 @@ impl Assert {
344347

345348
let mut spawned = command
346349
.spawn()
347-
.chain_err(|| ErrorKind::SpawnFailed(self.cmd.clone()))?;
350+
.chain_with(|| AssertionError::new(self.cmd.clone()))?;
348351

349352
if let Some(ref contents) = self.stdin_contents {
350353
spawned
351354
.stdin
352355
.as_mut()
353356
.expect("Couldn't get mut ref to command stdin")
354-
.write_all(contents)?;
357+
.write_all(contents)
358+
.chain_with(|| AssertionError::new(self.cmd.clone()))?;
355359
}
356-
let output = spawned.wait_with_output()?;
360+
let output = spawned
361+
.wait_with_output()
362+
.chain_with(|| AssertionError::new(self.cmd.clone()))?;
357363

358364
if let Some(expect_success) = self.expect_success {
359-
if expect_success != output.status.success() {
360-
let out = String::from_utf8_lossy(&output.stdout).to_string();
361-
let err = String::from_utf8_lossy(&output.stderr).to_string();
362-
let err: Error = ErrorKind::StatusMismatch(expect_success, out, err).into();
363-
bail!(err.chain_err(|| ErrorKind::AssertionFailed(self.cmd.clone())));
365+
let actual_success = output.status.success();
366+
if expect_success != actual_success {
367+
return Err(
368+
AssertionError::new(self.cmd.clone()).chain(StatusError::new(
369+
actual_success,
370+
output.stdout.clone(),
371+
output.stderr.clone(),
372+
)),
373+
)?;
364374
}
365375
}
366376

367377
if self.expect_exit_code.is_some() && self.expect_exit_code != output.status.code() {
368-
let out = String::from_utf8_lossy(&output.stdout).to_string();
369-
let err = String::from_utf8_lossy(&output.stderr).to_string();
370-
let err: Error =
371-
ErrorKind::ExitCodeMismatch(self.expect_exit_code, output.status.code(), out, err)
372-
.into();
373-
bail!(err.chain_err(|| ErrorKind::AssertionFailed(self.cmd.clone())));
378+
return Err(
379+
AssertionError::new(self.cmd.clone()).chain(ExitCodeError::new(
380+
self.expect_exit_code,
381+
output.status.code(),
382+
output.stdout.clone(),
383+
output.stderr.clone(),
384+
)),
385+
);
374386
}
375387

376388
self.expect_output
377389
.iter()
378390
.map(|a| {
379391
a.verify(&output)
380-
.chain_err(|| ErrorKind::AssertionFailed(self.cmd.clone()))
392+
.chain_with(|| AssertionError::new(self.cmd.clone()))
381393
})
382-
.collect::<Result<Vec<()>>>()?;
394+
.collect::<Result<Vec<()>, AssertionError>>()?;
383395

384396
Ok(())
385397
}
@@ -397,8 +409,16 @@ impl Assert {
397409
/// ```
398410
pub fn unwrap(self) {
399411
if let Err(err) = self.execute() {
400-
panic!("{}", err.display_chain());
412+
panic!(Self::format_causes(err.causes()));
413+
}
414+
}
415+
416+
fn format_causes(mut causes: failure::Causes) -> String {
417+
let mut result = causes.next().expect("an error should exist").to_string();
418+
for cause in causes {
419+
result.push_str(&format!("\nwith: {}", cause));
401420
}
421+
result
402422
}
403423
}
404424

src/bin/assert_fixture.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
#[macro_use]
2-
extern crate error_chain;
1+
extern crate failure;
32

3+
use std::io;
4+
use std::io::Write;
45
use std::env;
56
use std::process;
67

7-
error_chain! {
8-
foreign_links {
9-
Env(env::VarError);
10-
ParseInt(std::num::ParseIntError);
11-
}
12-
}
8+
use failure::ResultExt;
139

14-
fn run() -> Result<()> {
10+
fn run() -> Result<(), failure::Error> {
1511
if let Ok(text) = env::var("stdout") {
1612
println!("{}", text);
1713
}
@@ -23,9 +19,18 @@ fn run() -> Result<()> {
2319
.ok()
2420
.map(|v| v.parse::<i32>())
2521
.map_or(Ok(None), |r| r.map(Some))
26-
.chain_err(|| "Invalid exit code")?
22+
.context("Invalid exit code")?
2723
.unwrap_or(0);
2824
process::exit(code);
2925
}
3026

31-
quick_main!(run);
27+
fn main() {
28+
let code = match run() {
29+
Ok(_) => 0,
30+
Err(ref e) => {
31+
write!(&mut io::stderr(), "{}", e).expect("writing to stderr won't fail");
32+
1
33+
}
34+
};
35+
process::exit(code);
36+
}

src/diff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
extern crate colored;
2-
use self::colored::Colorize;
3-
use difference::{Changeset, Difference};
41
use std::fmt::{Error as fmtError, Write};
52

3+
use colored::Colorize;
4+
use difference::{Changeset, Difference};
5+
66
pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String, fmtError> {
77
let mut t = String::new();
88

0 commit comments

Comments
 (0)