35
35
//! # }
36
36
//! ```
37
37
//!
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
+ //!
38
50
//! Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
39
51
40
52
@@ -72,7 +84,7 @@ use cli_error::CliError;
72
84
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
73
85
/// assert_cli::assert_cli("bash", &["-c", BLACK_BOX]);
74
86
/// ```
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 > >
76
88
where S : AsRef < OsStr >
77
89
{
78
90
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>>
85
97
return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
86
98
}
87
99
88
- Ok ( ( ) )
100
+ Ok ( output )
89
101
} )
90
102
. map_err ( From :: from)
91
103
}
@@ -113,7 +125,7 @@ pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
113
125
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
114
126
/// assert_cli::assert_cli_output("bash", &["-c", BLACK_BOX], "Launch sequence initiated.");
115
127
/// ```
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 > >
117
129
where S : AsRef < OsStr >
118
130
{
119
131
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
126
138
return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
127
139
}
128
140
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
+ }
135
149
}
136
150
137
- Ok ( ( ) )
151
+ Ok ( output )
138
152
} )
139
153
. map_err ( From :: from)
140
154
}
@@ -160,7 +174,7 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
160
174
pub fn assert_cli_error < S > ( cmd : & str ,
161
175
args : & [ S ] ,
162
176
error_code : Option < i32 > )
163
- -> Result < ( ) , Box < Error > >
177
+ -> Result < Output , Box < Error > >
164
178
where S : AsRef < OsStr >
165
179
{
166
180
let call: Result < Output , Box < Error > > = Command :: new ( cmd)
@@ -179,7 +193,7 @@ pub fn assert_cli_error<S>(cmd: &str,
179
193
_ => { }
180
194
}
181
195
182
- Ok ( ( ) )
196
+ Ok ( output )
183
197
} )
184
198
. map_err ( From :: from)
185
199
}
@@ -210,7 +224,7 @@ pub fn assert_cli_output_error<S>(cmd: &str,
210
224
args : & [ S ] ,
211
225
error_code : Option < i32 > ,
212
226
expected_output : & str )
213
- -> Result < ( ) , Box < Error > >
227
+ -> Result < Output , Box < Error > >
214
228
where S : AsRef < OsStr >
215
229
{
216
230
let call: Result < Output , Box < Error > > = Command :: new ( cmd)
@@ -229,15 +243,17 @@ pub fn assert_cli_output_error<S>(cmd: &str,
229
243
_ => { }
230
244
}
231
245
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
+ }
238
254
}
239
255
240
- Ok ( ( ) )
256
+ Ok ( output )
241
257
} )
242
258
. map_err ( From :: from)
243
259
}
0 commit comments