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

Commit 2f0e943

Browse files
committed
Make cargo bin helpers work with empty env
Fixes #51
1 parent fae7c90 commit 2f0e943

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/assert.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ use std::path::PathBuf;
88
use std::process::{Command, Stdio};
99
use std::vec::Vec;
1010

11+
fn find_cargo() -> String {
12+
let which_cargo = Command::new("which").arg("cargo")
13+
.output().expect("Cannot exectute `which` to find `cargo`.");
14+
15+
if !which_cargo.status.success() {
16+
panic!("Could not find `cargo` command");
17+
}
18+
19+
String::from_utf8(which_cargo.stdout)
20+
.expect("Path to `cargo` is not UTF-8. This is currently unsupported by assert_cli.")
21+
.trim()
22+
.to_string()
23+
}
24+
1125
/// Assertions for a specific command.
1226
#[derive(Debug)]
1327
pub struct Assert {
@@ -25,11 +39,13 @@ impl default::Default for Assert {
2539
///
2640
/// Defaults to asserting _successful_ execution.
2741
fn default() -> Self {
42+
let cargo_path = find_cargo();
43+
let args = vec!["run", "--quiet", "--"]
44+
.into_iter()
45+
.map(String::from);
46+
2847
Assert {
29-
cmd: vec!["cargo", "run", "--quiet", "--"]
30-
.into_iter()
31-
.map(String::from)
32-
.collect(),
48+
cmd: vec![cargo_path].into_iter().chain(args).collect(),
3349
env: Environment::inherit(),
3450
current_dir: None,
3551
expect_success: Some(true),
@@ -52,11 +68,13 @@ impl Assert {
5268
///
5369
/// Defaults to asserting _successful_ execution.
5470
pub fn cargo_binary(name: &str) -> Self {
71+
let cargo_path = find_cargo();
72+
let args = vec!["run", "--quiet", "--bin", name, "--"]
73+
.into_iter()
74+
.map(String::from);
75+
5576
Assert {
56-
cmd: vec!["cargo", "run", "--quiet", "--bin", name, "--"]
57-
.into_iter()
58-
.map(String::from)
59-
.collect(),
77+
cmd: vec![cargo_path].into_iter().chain(args).collect(),
6078
..Self::default()
6179
}
6280
}

tests/cargo.rs

+12
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ fn cargo_binary() {
2121
.is("")
2222
.unwrap();
2323
}
24+
25+
26+
#[test]
27+
fn works_with_empty_env() {
28+
assert_cli::Assert::main_binary()
29+
.with_env(assert_cli::Environment::empty())
30+
.unwrap();
31+
32+
assert_cli::Assert::cargo_binary("assert_fixture")
33+
.with_env(assert_cli::Environment::empty())
34+
.unwrap();
35+
}

0 commit comments

Comments
 (0)