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

Commit ff82660

Browse files
committed
Add stderr output handling
1 parent 9900f41 commit ff82660

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

src/errors.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,17 @@ error_chain! {
3232
description("Output was not as expected")
3333
display("{}", diff)
3434
}
35+
ErrorOutputMismatch(expected: String, got: String) {
36+
description("Stderr output was not as expected")
37+
display(
38+
"Expected stderr output to contain\n{}\nbut could not find it in\n{}",
39+
expected,
40+
got,
41+
)
42+
}
43+
ExactErrorOutputMismatch(diff: String) {
44+
description("Stderr output was not as expected")
45+
display("{}", diff)
46+
}
3547
}
3648
}

src/lib.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ pub struct Assert {
8181
expect_success: bool,
8282
expect_exit_code: Option<i32>,
8383
expect_output: Option<String>,
84-
fuzzy: bool,
84+
fuzzy_output: bool,
85+
expect_error_output: Option<String>,
86+
fuzzy_error_output: bool,
8587
}
8688

8789
impl std::default::Default for Assert {
@@ -93,7 +95,9 @@ impl std::default::Default for Assert {
9395
expect_success: true,
9496
expect_exit_code: None,
9597
expect_output: None,
96-
fuzzy: false,
98+
fuzzy_output: false,
99+
expect_error_output: None,
100+
fuzzy_error_output: false,
97101
}
98102
}
99103
}
@@ -226,7 +230,7 @@ impl Assert {
226230
/// ```
227231
pub fn prints<O: Into<String>>(mut self, output: O) -> Self {
228232
self.expect_output = Some(output.into());
229-
self.fuzzy = true;
233+
self.fuzzy_output = true;
230234
self
231235
}
232236

@@ -243,7 +247,43 @@ impl Assert {
243247
/// ```
244248
pub fn prints_exactly<O: Into<String>>(mut self, output: O) -> Self {
245249
self.expect_output = Some(output.into());
246-
self.fuzzy = false;
250+
self.fuzzy_output = false;
251+
self
252+
}
253+
254+
/// Expect the command's stderr output to contain `output`
255+
///
256+
/// # Examples
257+
///
258+
/// ```rust
259+
/// extern crate assert_cli;
260+
///
261+
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
262+
/// .fails()
263+
/// .prints_error("non-exisiting-file")
264+
/// .unwrap();
265+
/// ```
266+
pub fn prints_error<O: Into<String>>(mut self, output: O) -> Self {
267+
self.expect_error_output = Some(output.into());
268+
self.fuzzy_error_output = true;
269+
self
270+
}
271+
272+
/// Expect the command to output exactly this `output` to stderr
273+
///
274+
/// # Examples
275+
///
276+
/// ```rust
277+
/// extern crate assert_cli;
278+
///
279+
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
280+
/// .fails()
281+
/// .prints_error_exactly("cat: non-exisiting-file: No such file or directory")
282+
/// .unwrap();
283+
/// ```
284+
pub fn prints_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
285+
self.expect_error_output = Some(output.into());
286+
self.fuzzy_error_output = false;
247287
self
248288
}
249289

@@ -284,7 +324,7 @@ impl Assert {
284324
}
285325

286326
let stdout = String::from_utf8_lossy(&output.stdout);
287-
match (self.expect_output, self.fuzzy) {
327+
match (self.expect_output, self.fuzzy_output) {
288328
(Some(ref expected_output), true) if !stdout.contains(expected_output) => {
289329
bail!(ErrorKind::OutputMismatch(
290330
expected_output.clone(),
@@ -301,6 +341,24 @@ impl Assert {
301341
_ => {},
302342
}
303343

344+
let stderr = String::from_utf8_lossy(&output.stderr);
345+
match (self.expect_error_output, self.fuzzy_error_output) {
346+
(Some(ref expected_output), true) if !stderr.contains(expected_output) => {
347+
bail!(ErrorKind::ErrorOutputMismatch(
348+
expected_output.clone(),
349+
stderr.into(),
350+
));
351+
},
352+
(Some(ref expected_output), false) => {
353+
let differences = Changeset::new(expected_output.trim(), stderr.trim(), "\n");
354+
if differences.distance > 0 {
355+
let nice_diff = diff::render(&differences)?;
356+
bail!(ErrorKind::ExactErrorOutputMismatch(nice_diff));
357+
}
358+
},
359+
_ => {},
360+
}
361+
304362
Ok(())
305363
}
306364

0 commit comments

Comments
 (0)