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

Commit aa89b08

Browse files
committed
tests: Validate main_binary / cargo_binary
`clap` couldn't be used for command line arguments. I didn't want to add it as a dependency for clients, which meant it had to be optional. The problem is that `main_binary` and `cargo_binary` run with optional features enabled. Note: you cannot use `main_binary` or `cargo_binary` - With `Environment::empty` because the executable can't be found - In skeptic tests (like the README) because the working dir is changed
1 parent aac5ec0 commit aa89b08

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ categories = ["development-tools::testing"]
1212
keywords = ["cli", "testing", "assert"]
1313
build = "build.rs"
1414

15+
[[bin]]
16+
name = "assert_fixture"
17+
1518
[dependencies]
1619
colored = "1.5"
1720
difference = "1.0"

src/bin/assert_fixture.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#[macro_use]
2+
extern crate error_chain;
3+
4+
use std::env;
5+
use std::process;
6+
7+
error_chain! {
8+
foreign_links {
9+
Env(env::VarError);
10+
ParseInt(std::num::ParseIntError);
11+
}
12+
}
13+
14+
fn run() -> Result<()> {
15+
if let Ok(text) = env::var("stdout") {
16+
println!("{}", text);
17+
}
18+
if let Ok(text) = env::var("stderr") {
19+
eprintln!("{}", text);
20+
}
21+
22+
let code = env::var("exit")
23+
.ok()
24+
.map(|v| v.parse::<i32>())
25+
.map_or(Ok(None), |r| r.map(Some))
26+
.chain_err(|| "Invalid exit code")?
27+
.unwrap_or(0);
28+
process::exit(code);
29+
}
30+
31+
quick_main!(run);

tests/cargo.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extern crate assert_cli;
2+
3+
#[test]
4+
fn main_binary() {
5+
assert_cli::Assert::main_binary()
6+
.with_env(assert_cli::Environment::inherit().insert("stdout", "42"))
7+
.stdout()
8+
.contains("42")
9+
.unwrap();
10+
}
11+
12+
#[test]
13+
fn cargo_binary() {
14+
assert_cli::Assert::cargo_binary("assert_fixture")
15+
.with_env(assert_cli::Environment::inherit().insert("stdout", "42"))
16+
.stdout()
17+
.contains("42")
18+
.unwrap();
19+
}

0 commit comments

Comments
 (0)