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

Commit 992fbfe

Browse files
committed
Auto merge of #11 - johannhof:return-output, r=killercup
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.
2 parents d137c10 + 2cf0cf6 commit 992fbfe

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ assert_cli::assert_cli("echo", &["42"]).unwrap();
5757
assert_cli!("echo", &["42"] => Success).unwrap();
5858
```
5959

60+
All exported functions and the macro return a `Result` containing the
61+
`Output` of the process, allowing you to do further custom assertions:
62+
63+
```rust
64+
#[macro_use] extern crate assert_cli;
65+
let output = assert_cli!("echo", &["Number 42"] => Success).unwrap();
66+
let stdout = std::str::from_utf8(&output.stdout).unwrap();
67+
assert!(stdout.contains("42"));
68+
```
69+
6070
## License
6171

6272
Licensed under either of

src/lib.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
//! # }
3636
//! ```
3737
//!
38+
//! All exported functions and the macro return a `Result` containing the
39+
//! `Output` of the process, allowing you to do further custom assertions:
40+
//!
41+
//! ```rust
42+
//! # #[macro_use] extern crate assert_cli;
43+
//! # fn main() {
44+
//! let output = assert_cli!("echo", &["Number 42"] => Success).unwrap();
45+
//! let stdout = std::str::from_utf8(&output.stdout).unwrap();
46+
//! assert!(stdout.contains("42"));
47+
//! # }
48+
//! ```
49+
//!
3850
//! Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
3951
4052

@@ -72,7 +84,7 @@ use cli_error::CliError;
7284
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
7385
/// assert_cli::assert_cli("bash", &["-c", BLACK_BOX]);
7486
/// ```
75-
pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
87+
pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<Output, Box<Error>>
7688
where S: AsRef<OsStr>
7789
{
7890
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -85,7 +97,7 @@ pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
8597
return Err(From::from(CliError::WrongExitCode(output)));
8698
}
8799

88-
Ok(())
100+
Ok(output)
89101
})
90102
.map_err(From::from)
91103
}
@@ -113,7 +125,7 @@ pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
113125
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
114126
/// assert_cli::assert_cli_output("bash", &["-c", BLACK_BOX], "Launch sequence initiated.");
115127
/// ```
116-
pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Result<(), Box<Error>>
128+
pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Result<Output, Box<Error>>
117129
where S: AsRef<OsStr>
118130
{
119131
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -126,15 +138,17 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
126138
return Err(From::from(CliError::WrongExitCode(output)));
127139
}
128140

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)));
141+
{
142+
let stdout = String::from_utf8_lossy(&output.stdout);
143+
let (distance, changes) = difference::diff(expected_output.trim(),
144+
&stdout.trim(),
145+
"\n");
146+
if distance > 0 {
147+
return Err(From::from(CliError::OutputMissmatch(changes)));
148+
}
135149
}
136150

137-
Ok(())
151+
Ok(output)
138152
})
139153
.map_err(From::from)
140154
}
@@ -160,7 +174,7 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
160174
pub fn assert_cli_error<S>(cmd: &str,
161175
args: &[S],
162176
error_code: Option<i32>)
163-
-> Result<(), Box<Error>>
177+
-> Result<Output, Box<Error>>
164178
where S: AsRef<OsStr>
165179
{
166180
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -179,7 +193,7 @@ pub fn assert_cli_error<S>(cmd: &str,
179193
_ => {}
180194
}
181195

182-
Ok(())
196+
Ok(output)
183197
})
184198
.map_err(From::from)
185199
}
@@ -210,7 +224,7 @@ pub fn assert_cli_output_error<S>(cmd: &str,
210224
args: &[S],
211225
error_code: Option<i32>,
212226
expected_output: &str)
213-
-> Result<(), Box<Error>>
227+
-> Result<Output, Box<Error>>
214228
where S: AsRef<OsStr>
215229
{
216230
let call: Result<Output, Box<Error>> = Command::new(cmd)
@@ -229,15 +243,17 @@ pub fn assert_cli_output_error<S>(cmd: &str,
229243
_ => {}
230244
}
231245

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)));
246+
{
247+
let stdout = String::from_utf8_lossy(&output.stderr);
248+
let (distance, changes) = difference::diff(expected_output.trim(),
249+
&stdout.trim(),
250+
"\n");
251+
if distance > 0 {
252+
return Err(From::from(CliError::OutputMissmatch(changes)));
253+
}
238254
}
239255

240-
Ok(())
256+
Ok(output)
241257
})
242258
.map_err(From::from)
243259
}

0 commit comments

Comments
 (0)