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

Commit c22f859

Browse files
committed
Return output information from assert calls.
This is helpful to be able to do further assertions on the result of the call, e.g. to check the stdout using a regex.
1 parent d137c10 commit c22f859

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/lib.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use cli_error::CliError;
7272
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
7373
/// assert_cli::assert_cli("bash", &["-c", BLACK_BOX]);
7474
/// ```
75-
pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
75+
pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<Output, Box<Error>>
7676
where S: AsRef<OsStr>
7777
{
7878
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -85,7 +85,7 @@ pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
8585
return Err(From::from(CliError::WrongExitCode(output)));
8686
}
8787

88-
Ok(())
88+
Ok(output)
8989
})
9090
.map_err(From::from)
9191
}
@@ -113,7 +113,7 @@ pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
113113
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
114114
/// assert_cli::assert_cli_output("bash", &["-c", BLACK_BOX], "Launch sequence initiated.");
115115
/// ```
116-
pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Result<(), Box<Error>>
116+
pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Result<Output, Box<Error>>
117117
where S: AsRef<OsStr>
118118
{
119119
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -126,15 +126,17 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
126126
return Err(From::from(CliError::WrongExitCode(output)));
127127
}
128128

129-
let stdout = String::from_utf8_lossy(&output.stdout);
130-
let (distance, changes) = difference::diff(expected_output.trim(),
131-
&stdout.trim(),
132-
"\n");
133-
if distance > 0 {
134-
return Err(From::from(CliError::OutputMissmatch(changes)));
129+
{
130+
let stdout = String::from_utf8_lossy(&output.stdout);
131+
let (distance, changes) = difference::diff(expected_output.trim(),
132+
&stdout.trim(),
133+
"\n");
134+
if distance > 0 {
135+
return Err(From::from(CliError::OutputMissmatch(changes)));
136+
}
135137
}
136138

137-
Ok(())
139+
Ok(output)
138140
})
139141
.map_err(From::from)
140142
}
@@ -160,7 +162,7 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
160162
pub fn assert_cli_error<S>(cmd: &str,
161163
args: &[S],
162164
error_code: Option<i32>)
163-
-> Result<(), Box<Error>>
165+
-> Result<Output, Box<Error>>
164166
where S: AsRef<OsStr>
165167
{
166168
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -179,7 +181,7 @@ pub fn assert_cli_error<S>(cmd: &str,
179181
_ => {}
180182
}
181183

182-
Ok(())
184+
Ok(output)
183185
})
184186
.map_err(From::from)
185187
}
@@ -210,7 +212,7 @@ pub fn assert_cli_output_error<S>(cmd: &str,
210212
args: &[S],
211213
error_code: Option<i32>,
212214
expected_output: &str)
213-
-> Result<(), Box<Error>>
215+
-> Result<Output, Box<Error>>
214216
where S: AsRef<OsStr>
215217
{
216218
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -229,15 +231,17 @@ pub fn assert_cli_output_error<S>(cmd: &str,
229231
_ => {}
230232
}
231233

232-
let stdout = String::from_utf8_lossy(&output.stderr);
233-
let (distance, changes) = difference::diff(expected_output.trim(),
234-
&stdout.trim(),
235-
"\n");
236-
if distance > 0 {
237-
return Err(From::from(CliError::OutputMissmatch(changes)));
234+
{
235+
let stdout = String::from_utf8_lossy(&output.stderr);
236+
let (distance, changes) = difference::diff(expected_output.trim(),
237+
&stdout.trim(),
238+
"\n");
239+
if distance > 0 {
240+
return Err(From::from(CliError::OutputMissmatch(changes)));
241+
}
238242
}
239243

240-
Ok(())
244+
Ok(output)
241245
})
242246
.map_err(From::from)
243247
}

0 commit comments

Comments
 (0)