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

Commit 2620e07

Browse files
committed
Output assertion error stuff
1 parent b386d23 commit 2620e07

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

src/diff.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ extern crate colored;
22
use self::colored::Colorize;
33

44
use difference::{Difference, Changeset};
5-
use std::fmt::Write;
5+
use std::fmt::{Write, Error as fmtError};
66

7-
use errors::*;
8-
9-
pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String> {
7+
pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String, fmtError> {
108
let mut t = String::new();
119

1210
for (i, diff) in diffs.iter().enumerate() {
@@ -66,11 +64,11 @@ mod tests {
6664
sed do eiusmod tempor incididunt ut labore et dolore magna
6765
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
6866
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
69-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
67+
"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
7068
sed do eiusmod tempor **incididunt** ut labore et dolore magna
7169
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
7270
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
73-
"\n");
71+
"\n");
7472
println!("{}", render(&diff).unwrap());
7573
assert_eq!(render(&diff).unwrap(), " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u{1b}[31m-sed do eiusmod tempor incididunt ut labore et dolore magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor\u{1b}[0m \u{1b}[7;32m**incididunt**\u{1b}[0m \u{1b}[32mut labore et dolore magna\u{1b}[0m \n aliqua. Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo consequat.\n");
7674
}

src/errors.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ error_chain! {
99
StatusMismatch(cmd: Vec<String>, expected: bool) {
1010
description("Wrong status")
1111
display(
12-
"{}: `(command `{}` expected to {})` (command {})",
12+
"{}: (command `{}` expected to {}) (command {})",
1313
ERROR_PREFIX,
1414
cmd.join(" "),
1515
expected = if *expected { "succeed" } else { "fail" },
@@ -19,33 +19,27 @@ error_chain! {
1919
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
2020
description("Wrong exit code")
2121
display(
22-
"{}: `(exit code of `{}` expected to be `{:?}`)` (exit code was: `{:?}`)",
22+
"{}: (exit code of `{}` expected to be `{:?}`) (exit code was: `{:?}`)",
2323
ERROR_PREFIX,
2424
cmd.join(" "),
2525
expected,
2626
got,
2727
)
2828
}
29-
OutputMismatch(output_name: String, cmd: Vec<String>, expected: String, got: String) {
29+
StdoutMismatch(cmd: Vec<String>, output_err: ::output::Error) {
3030
description("Output was not as expected")
3131
display(
32-
"{}: `({} of `{}` expected to contain `{:?}`)` (output was: `{:?}`)",
33-
ERROR_PREFIX,
34-
output_name,
35-
cmd.join(" "),
36-
expected,
37-
got,
32+
"{}: `{}` stdout mismatch: `{}`)",
33+
ERROR_PREFIX, cmd.join(" "), output_err,
3834
)
3935
}
40-
ExactOutputMismatch(output_name: String, cmd: Vec<String>, diff: String) {
41-
description("Output was not as expected")
36+
StderrMismatch(cmd: Vec<String>, output_err: ::output::Error) {
37+
description("Error output was not as expected")
4238
display(
43-
"{}: `({} of `{}` was not as expected)`\n{}\n",
44-
ERROR_PREFIX,
45-
output_name,
46-
cmd.join(" "),
47-
diff.trim()
39+
"{}: `{}` stderr mismatch: `{}`)",
40+
ERROR_PREFIX, cmd.join(" "), output_err,
4841
)
4942
}
43+
5044
}
5145
}

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,14 @@ impl Assert {
395395
));
396396
}
397397

398-
if let Some(ouput_assertion) = self.expect_stdout {
399-
ouput_assertion.execute(&output)?;
398+
if let Some(ref ouput_assertion) = self.expect_stdout {
399+
ouput_assertion.execute(&output)
400+
.map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e))?;
400401
}
401402

402-
if let Some(ouput_assertion) = self.expect_stderr {
403-
ouput_assertion.execute(&output)?;
403+
if let Some(ref ouput_assertion) = self.expect_stderr {
404+
ouput_assertion.execute(&output)
405+
.map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e))?;
404406
}
405407

406408
Ok(())

src/output.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::process::Output;
33

44
use difference::Changeset;
55

6-
use errors::*;
6+
use self::errors::*;
7+
pub use self::errors::{Error, ErrorKind};
78
use diff;
89

910
#[derive(Debug, Clone)]
@@ -16,12 +17,7 @@ pub struct OutputAssertion<T> {
1617
impl<T: OutputType> OutputAssertion<T> {
1718
fn matches_fuzzy(&self, got: &str) -> Result<()> {
1819
if !got.contains(&self.expect) {
19-
bail!(ErrorKind::OutputMismatch(
20-
self.kind.to_string(),
21-
vec!["Foo".to_string()],
22-
self.expect.clone(),
23-
got.into(),
24-
));
20+
bail!(ErrorKind::OutputMismatch(self.expect.clone(), got.into()));
2521
}
2622

2723
Ok(())
@@ -32,11 +28,7 @@ impl<T: OutputType> OutputAssertion<T> {
3228

3329
if differences.distance > 0 {
3430
let nice_diff = diff::render(&differences)?;
35-
bail!(ErrorKind::ExactOutputMismatch(
36-
self.kind.to_string(),
37-
vec!["Foo".to_string()],
38-
nice_diff
39-
));
31+
bail!(ErrorKind::ExactOutputMismatch(nice_diff));
4032
}
4133

4234
Ok(())
@@ -89,3 +81,22 @@ impl OutputType for StdErr {
8981
&o.stderr
9082
}
9183
}
84+
85+
mod errors {
86+
error_chain! {
87+
foreign_links {
88+
// Io(::std::io::Error);
89+
Fmt(::std::fmt::Error);
90+
}
91+
errors {
92+
OutputMismatch(expected: String, got: String) {
93+
description("Output was not as expected")
94+
display("expected {:?}, got {:?}", expected, got)
95+
}
96+
ExactOutputMismatch(diff: String) {
97+
description("Output was not as expected")
98+
display("{}", diff)
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)