-
Notifications
You must be signed in to change notification settings - Fork 21
WIP: Make cargo bin helpers work with empty env #54
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use environment::Environment; | ||
use error_chain::ChainedError; | ||
use errors::*; | ||
use output::{OutputAssertion, OutputKind}; | ||
use std::default; | ||
|
@@ -7,6 +8,20 @@ use std::path::PathBuf; | |
use std::process::{Command, Stdio}; | ||
use std::vec::Vec; | ||
|
||
fn find_cargo() -> String { | ||
let which_cargo = Command::new("which").arg("cargo") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, we should probably add a Windows CI |
||
.output().expect("Cannot exectute `which` to find `cargo`."); | ||
|
||
if !which_cargo.status.success() { | ||
panic!("Could not find `cargo` command"); | ||
} | ||
|
||
String::from_utf8(which_cargo.stdout) | ||
.expect("Path to `cargo` is not UTF-8. This is currently unsupported by assert_cli.") | ||
.trim() | ||
.to_string() | ||
} | ||
|
||
/// Assertions for a specific command. | ||
#[derive(Debug)] | ||
pub struct Assert { | ||
|
@@ -24,11 +39,13 @@ impl default::Default for Assert { | |
/// | ||
/// Defaults to asserting _successful_ execution. | ||
fn default() -> Self { | ||
let cargo_path = find_cargo(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fn can currently panic. We should deal with the result instead and maybe cache the error in the Assert struct so we can return it only when running execute or unwrap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main value of caching the result would be for when calling Also, should the result of |
||
let args = vec!["run", "--quiet", "--"] | ||
.into_iter() | ||
.map(String::from); | ||
|
||
Assert { | ||
cmd: vec!["cargo", "run", "--"] | ||
.into_iter() | ||
.map(String::from) | ||
.collect(), | ||
cmd: vec![cargo_path].into_iter().chain(args).collect(), | ||
env: Environment::inherit(), | ||
current_dir: None, | ||
expect_success: Some(true), | ||
|
@@ -51,11 +68,13 @@ impl Assert { | |
/// | ||
/// Defaults to asserting _successful_ execution. | ||
pub fn cargo_binary(name: &str) -> Self { | ||
let cargo_path = find_cargo(); | ||
let args = vec!["run", "--quiet", "--bin", name, "--"] | ||
.into_iter() | ||
.map(String::from); | ||
|
||
Assert { | ||
cmd: vec!["cargo", "run", "--bin", name, "--"] | ||
.into_iter() | ||
.map(String::from) | ||
.collect(), | ||
cmd: vec![cargo_path].into_iter().chain(args).collect(), | ||
..Self::default() | ||
} | ||
} | ||
|
@@ -308,7 +327,9 @@ impl Assert { | |
None => command, | ||
}; | ||
|
||
let mut spawned = command.spawn()?; | ||
let mut spawned = command | ||
.spawn() | ||
.chain_err(|| ErrorKind::SpawnFailed(self.cmd.clone()))?; | ||
|
||
if let Some(ref contents) = self.stdin_contents { | ||
spawned | ||
|
@@ -365,7 +386,7 @@ impl Assert { | |
/// ``` | ||
pub fn unwrap(self) { | ||
if let Err(err) = self.execute() { | ||
panic!("{}", err); | ||
panic!("{}", err.display_chain()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#[macro_use] | ||
extern crate error_chain; | ||
|
||
use std::env; | ||
use std::process; | ||
|
||
error_chain! { | ||
foreign_links { | ||
Env(env::VarError); | ||
ParseInt(std::num::ParseIntError); | ||
} | ||
} | ||
|
||
fn run() -> Result<()> { | ||
if let Ok(text) = env::var("stdout") { | ||
println!("{}", text); | ||
} | ||
if let Ok(text) = env::var("stderr") { | ||
eprintln!("{}", text); | ||
} | ||
|
||
let code = env::var("exit") | ||
.ok() | ||
.map(|v| v.parse::<i32>()) | ||
.map_or(Ok(None), |r| r.map(Some)) | ||
.chain_err(|| "Invalid exit code")? | ||
.unwrap_or(0); | ||
process::exit(code); | ||
} | ||
|
||
quick_main!(run); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
extern crate assert_cli; | ||
|
||
#[test] | ||
fn main_binary() { | ||
assert_cli::Assert::main_binary() | ||
.with_env(assert_cli::Environment::inherit().insert("stdout", "42")) | ||
.stdout() | ||
.is("42") | ||
.stderr() | ||
.is("") | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
fn cargo_binary() { | ||
assert_cli::Assert::cargo_binary("assert_fixture") | ||
.with_env(assert_cli::Environment::inherit().insert("stdout", "42")) | ||
.stdout() | ||
.is("42") | ||
.stderr() | ||
.is("") | ||
.unwrap(); | ||
} | ||
|
||
|
||
#[test] | ||
fn works_with_empty_env() { | ||
assert_cli::Assert::main_binary() | ||
.with_env(assert_cli::Environment::empty()) | ||
.unwrap(); | ||
|
||
assert_cli::Assert::cargo_binary("assert_fixture") | ||
.with_env(assert_cli::Environment::empty()) | ||
.unwrap(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should should need to panic in this fn at all