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

Commit

Permalink
Merge #33
Browse files Browse the repository at this point in the history
33: Add ability to set current_dir r=killercup a=epage

Some tests need to run in certain directories but if you call `std::env::set_current_dir` within your test, you can hit race conditions with other tests running at the same time.  This allows you to set the current_dir only on the child process.
  • Loading branch information
bors[bot] committed Sep 11, 2017
2 parents aed7aa6 + 3b937b3 commit 7eed1b1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ extern crate difference;
extern crate rustc_serialize;

use std::process::Command;
use std::path::PathBuf;

mod errors;
use errors::*;
Expand All @@ -125,6 +126,7 @@ mod diff;
#[derive(Debug)]
pub struct Assert {
cmd: Vec<String>,
current_dir: Option<PathBuf>,
expect_success: Option<bool>,
expect_exit_code: Option<i32>,
expect_stdout: Option<OutputAssertion<StdOut>>,
Expand All @@ -139,6 +141,7 @@ impl std::default::Default for Assert {
Assert {
cmd: vec!["cargo", "run", "--"]
.into_iter().map(String::from).collect(),
current_dir: None,
expect_success: Some(true),
expect_exit_code: None,
expect_stdout: None,
Expand Down Expand Up @@ -202,6 +205,24 @@ impl Assert {
self
}

/// Sets the working directory for the command.
///
/// # Examples
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command(&["wc", "lib.rs"])
/// .current_dir(std::path::Path::new("src"))
/// .prints("lib.rs")
/// .execute()
/// .unwrap();
/// ```
pub fn current_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
self.current_dir = Some(dir.into());
self
}

/// Small helper to make chains more readable.
///
/// # Examples
Expand Down Expand Up @@ -374,6 +395,10 @@ impl Assert {
let args: Vec<_> = self.cmd.iter().skip(1).collect();
let mut command = Command::new(cmd);
let command = command.args(&args);
let command = match self.current_dir {
Some(ref dir) => command.current_dir(dir),
None => command
};
let output = command.output()?;


Expand Down

0 comments on commit 7eed1b1

Please sign in to comment.