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

Commit a8650cf

Browse files
committed
Error Check Fn, NonSuccess → WrongExitCode
1 parent c7cc61f commit a8650cf

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/cli_error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::process::Output;
66
use difference::Difference;
77

88
pub enum CliError {
9-
NoSuccess(Output),
9+
WrongExitCode(Output),
1010
OutputMissmatch(Vec<Difference>),
1111
}
1212

@@ -19,8 +19,8 @@ impl fmt::Display for CliError {
1919
impl fmt::Debug for CliError {
2020
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2121
match *self {
22-
CliError::NoSuccess(ref output) => write!(f,
23-
"Non-success error code {code:?} with this stderr:\n{stderr}",
22+
CliError::WrongExitCode(ref output) => write!(f,
23+
"Unexpected error code {code:?} with this stderr:\n{stderr}",
2424
code = output.status.code(),
2525
stderr = String::from_utf8_lossy(&output.stderr)),
2626
CliError::OutputMissmatch(ref diff) => {
@@ -37,7 +37,7 @@ impl fmt::Debug for CliError {
3737
impl Error for CliError {
3838
fn description(&self) -> &str {
3939
match *self {
40-
CliError::NoSuccess(_) => "Command return non-success error code.",
40+
CliError::WrongExitCode(_) => "Command return unexpected error code.",
4141
CliError::OutputMissmatch(_) => "Command output was not as expected.",
4242
}
4343
}

src/lib.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
7575

7676
call.and_then(|output| {
7777
if !output.status.success() {
78-
return Err(From::from(CliError::NoSuccess(output)));
78+
return Err(From::from(CliError::WrongExitCode(output)));
7979
}
8080

8181
let stdout = String::from_utf8_lossy(&output.stdout);
@@ -90,3 +90,40 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
9090
})
9191
.map_err(From::from)
9292
}
93+
94+
/// Assert a CLI call fails with the expected error code and output.
95+
pub fn assert_cli_output_error<S>(cmd: &str,
96+
args: &[S],
97+
error_code: Option<i32>,
98+
expected_output: &str)
99+
-> Result<(), Box<Error>>
100+
where S: AsRef<OsStr>
101+
{
102+
let call: Result<Output, Box<Error>> = Command::new(cmd)
103+
.args(args)
104+
.output()
105+
.map_err(From::from);
106+
107+
call.and_then(|output| {
108+
if output.status.success() {
109+
return Err(From::from(CliError::WrongExitCode(output)));
110+
}
111+
112+
match (error_code, output.status.code()) {
113+
(Some(a), Some(b)) if a != b =>
114+
return Err(From::from(CliError::WrongExitCode(output))),
115+
_ => {}
116+
}
117+
118+
let stdout = String::from_utf8_lossy(&output.stderr);
119+
let (distance, changes) = difference::diff(expected_output.trim(),
120+
&stdout.trim(),
121+
"\n");
122+
if distance > 0 {
123+
return Err(From::from(CliError::OutputMissmatch(changes)));
124+
}
125+
126+
Ok(())
127+
})
128+
.map_err(From::from)
129+
}

0 commit comments

Comments
 (0)