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

Commit cdf0d1e

Browse files
committed
error formatting
- try to mimic `assert_eq!` - close #14: try to avoid multiline panics
1 parent cc2d7be commit cdf0d1e

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/errors.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
static ERROR_PREFIX: &'static str = "CLI assertion failed";
2+
13
error_chain! {
24
foreign_links {
35
Io(::std::io::Error);
@@ -7,42 +9,60 @@ error_chain! {
79
StatusMismatch(cmd: Vec<String>, expected: bool) {
810
description("Wrong status")
911
display(
10-
"Command {:?} {got} but expected it to {expected}",
12+
"{}: `(command `{}` expected to {})` (command {})",
13+
ERROR_PREFIX,
1114
cmd.join(" "),
12-
got = if *expected { "failed" } else { "succeed" },
13-
expected = if *expected { "succeed" } else { "failed" },
15+
expected = if *expected { "succeed" } else { "fail" },
16+
got = if *expected { "failed" } else { "succeeded" },
1417
)
1518
}
1619
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
1720
description("Wrong exit code")
1821
display(
19-
"Command {:?} exited with code {:?} but expected it to be {:?}",
20-
cmd.join(" "), got, expected,
22+
"{}: `(exit code of `{}` expected to be `{:?}`)` (exit code was: `{:?}`)",
23+
ERROR_PREFIX,
24+
cmd.join(" "),
25+
expected,
26+
got,
2127
)
2228
}
23-
OutputMismatch(expected: String, got: String) {
29+
OutputMismatch(cmd: Vec<String>, expected: String, got: String) {
2430
description("Output was not as expected")
2531
display(
26-
"Expected output to contain\n{}\nbut could not find it in\n{}",
32+
"{}: `(output of `{}` expected to contain `{:?}`)` (output was: `{:?}`)",
33+
ERROR_PREFIX,
34+
cmd.join(" "),
2735
expected,
2836
got,
2937
)
3038
}
31-
ExactOutputMismatch(diff: String) {
39+
ExactOutputMismatch(cmd: Vec<String>, diff: String) {
3240
description("Output was not as expected")
33-
display("{}", diff)
41+
display(
42+
"{}: `(output of `{}` was not as expected)`\n{}\n",
43+
ERROR_PREFIX,
44+
cmd.join(" "),
45+
diff.trim()
46+
)
3447
}
35-
ErrorOutputMismatch(expected: String, got: String) {
48+
ErrorOutputMismatch(cmd: Vec<String>, expected: String, got: String) {
3649
description("Stderr output was not as expected")
3750
display(
38-
"Expected stderr output to contain\n{}\nbut could not find it in\n{}",
51+
"{}: `(stderr output of `{}` expected to contain `{:?}`)` (stderr was: `{:?}`)",
52+
ERROR_PREFIX,
53+
cmd.join(" "),
3954
expected,
4055
got,
4156
)
4257
}
43-
ExactErrorOutputMismatch(diff: String) {
58+
ExactErrorOutputMismatch(cmd: Vec<String>, diff: String) {
4459
description("Stderr output was not as expected")
45-
display("{}", diff)
60+
display(
61+
"{}: `(stderr output of `{}` was not as expected)`\n{}\n",
62+
ERROR_PREFIX,
63+
cmd.join(" "),
64+
diff.trim()
65+
)
4666
}
4767
}
4868
}

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ impl Assert {
351351
match (self.expect_output, self.fuzzy_output) {
352352
(Some(ref expected_output), true) if !stdout.contains(expected_output) => {
353353
bail!(ErrorKind::OutputMismatch(
354+
self.cmd.clone(),
354355
expected_output.clone(),
355356
stdout.into(),
356357
));
@@ -359,7 +360,7 @@ impl Assert {
359360
let differences = Changeset::new(expected_output.trim(), stdout.trim(), "\n");
360361
if differences.distance > 0 {
361362
let nice_diff = diff::render(&differences)?;
362-
bail!(ErrorKind::ExactOutputMismatch(nice_diff));
363+
bail!(ErrorKind::ExactOutputMismatch(self.cmd.clone(), nice_diff));
363364
}
364365
},
365366
_ => {},
@@ -369,6 +370,7 @@ impl Assert {
369370
match (self.expect_error_output, self.fuzzy_error_output) {
370371
(Some(ref expected_output), true) if !stderr.contains(expected_output) => {
371372
bail!(ErrorKind::ErrorOutputMismatch(
373+
self.cmd.clone(),
372374
expected_output.clone(),
373375
stderr.into(),
374376
));
@@ -377,7 +379,7 @@ impl Assert {
377379
let differences = Changeset::new(expected_output.trim(), stderr.trim(), "\n");
378380
if differences.distance > 0 {
379381
let nice_diff = diff::render(&differences)?;
380-
bail!(ErrorKind::ExactErrorOutputMismatch(nice_diff));
382+
bail!(ErrorKind::ExactErrorOutputMismatch(self.cmd.clone(),nice_diff));
381383
}
382384
},
383385
_ => {},
@@ -399,7 +401,7 @@ impl Assert {
399401
/// ```
400402
pub fn unwrap(self) {
401403
if let Err(err) = self.execute() {
402-
panic!("Assert CLI failure:\n{}", err);
404+
panic!("{}", err);
403405
}
404406
}
405407
}

0 commit comments

Comments
 (0)