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

WIP: Make cargo bin helpers work with empty env #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::vec::Vec;

fn find_cargo() -> String {
Copy link
Collaborator Author

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

let which_cargo = Command::new("which").arg("cargo")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 {
Expand All @@ -25,11 +39,13 @@ impl default::Default for Assert {
///
/// Defaults to asserting _successful_ execution.
fn default() -> Self {
let cargo_path = find_cargo();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main value of caching the result would be for when calling .execute() rather than .unwrap(), right?

Also, should the result of find_cargo be shoved off into a lazy_static!?

let args = vec!["run", "--quiet", "--"]
.into_iter()
.map(String::from);

Assert {
cmd: vec!["cargo", "run", "--quiet", "--"]
.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),
Expand All @@ -52,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", "--quiet", "--bin", name, "--"]
.into_iter()
.map(String::from)
.collect(),
cmd: vec![cargo_path].into_iter().chain(args).collect(),
..Self::default()
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ fn cargo_binary() {
.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();
}