Skip to content

Commit f94a996

Browse files
authored
Merge pull request assert-rs#50 from epage/fix/main_binary
fix: Remove unrelated output in main_binary
2 parents a01f933 + 45a4aa6 commit f94a996

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
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/assert.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use environment::Environment;
2+
use error_chain::ChainedError;
23
use errors::*;
34
use output::{OutputAssertion, OutputKind};
45
use std::default;
@@ -25,7 +26,7 @@ impl default::Default for Assert {
2526
/// Defaults to asserting _successful_ execution.
2627
fn default() -> Self {
2728
Assert {
28-
cmd: vec!["cargo", "run", "--"]
29+
cmd: vec!["cargo", "run", "--quiet", "--"]
2930
.into_iter()
3031
.map(String::from)
3132
.collect(),
@@ -52,7 +53,7 @@ impl Assert {
5253
/// Defaults to asserting _successful_ execution.
5354
pub fn cargo_binary(name: &str) -> Self {
5455
Assert {
55-
cmd: vec!["cargo", "run", "--bin", name, "--"]
56+
cmd: vec!["cargo", "run", "--quiet", "--bin", name, "--"]
5657
.into_iter()
5758
.map(String::from)
5859
.collect(),
@@ -332,7 +333,9 @@ impl Assert {
332333
None => command,
333334
};
334335

335-
let mut spawned = command.spawn()?;
336+
let mut spawned = command
337+
.spawn()
338+
.chain_err(|| ErrorKind::SpawnFailed(self.cmd.clone()))?;
336339

337340
if let Some(ref contents) = self.stdin_contents {
338341
spawned
@@ -389,7 +392,7 @@ impl Assert {
389392
/// ```
390393
pub fn unwrap(self) {
391394
if let Err(err) = self.execute() {
392-
panic!("{}", err);
395+
panic!("{}", err.display_chain());
393396
}
394397
}
395398
}

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);

src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ error_chain! {
66
Fmt(::std::fmt::Error);
77
}
88
errors {
9+
SpawnFailed(cmd: Vec<String>) {
10+
description("Spawn failed")
11+
display(
12+
"{}: (command `{}` failed to run)",
13+
ERROR_PREFIX,
14+
cmd.join(" "),
15+
)
16+
}
917
StatusMismatch(cmd: Vec<String>, expected: bool, out: String, err: String) {
1018
description("Wrong status")
1119
display(

tests/cargo.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
.is("42")
9+
.stderr()
10+
.is("")
11+
.unwrap();
12+
}
13+
14+
#[test]
15+
fn cargo_binary() {
16+
assert_cli::Assert::cargo_binary("assert_fixture")
17+
.with_env(assert_cli::Environment::inherit().insert("stdout", "42"))
18+
.stdout()
19+
.is("42")
20+
.stderr()
21+
.is("")
22+
.unwrap();
23+
}

0 commit comments

Comments
 (0)