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

Commit 49c56a8

Browse files
committed
Merge #33
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.
2 parents 3fef115 + 3b937b3 commit 49c56a8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extern crate difference;
109109
extern crate rustc_serialize;
110110

111111
use std::process::Command;
112+
use std::path::PathBuf;
112113

113114
mod errors;
114115
use errors::*;
@@ -125,6 +126,7 @@ mod diff;
125126
#[derive(Debug)]
126127
pub struct Assert {
127128
cmd: Vec<String>,
129+
current_dir: Option<PathBuf>,
128130
expect_success: Option<bool>,
129131
expect_exit_code: Option<i32>,
130132
expect_stdout: Option<OutputAssertion<StdOut>>,
@@ -139,6 +141,7 @@ impl std::default::Default for Assert {
139141
Assert {
140142
cmd: vec!["cargo", "run", "--"]
141143
.into_iter().map(String::from).collect(),
144+
current_dir: None,
142145
expect_success: Some(true),
143146
expect_exit_code: None,
144147
expect_stdout: None,
@@ -202,6 +205,24 @@ impl Assert {
202205
self
203206
}
204207

208+
/// Sets the working directory for the command.
209+
///
210+
/// # Examples
211+
///
212+
/// ```rust
213+
/// extern crate assert_cli;
214+
///
215+
/// assert_cli::Assert::command(&["wc", "lib.rs"])
216+
/// .current_dir(std::path::Path::new("src"))
217+
/// .prints("lib.rs")
218+
/// .execute()
219+
/// .unwrap();
220+
/// ```
221+
pub fn current_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
222+
self.current_dir = Some(dir.into());
223+
self
224+
}
225+
205226
/// Small helper to make chains more readable.
206227
///
207228
/// # Examples
@@ -374,6 +395,10 @@ impl Assert {
374395
let args: Vec<_> = self.cmd.iter().skip(1).collect();
375396
let mut command = Command::new(cmd);
376397
let command = command.args(&args);
398+
let command = match self.current_dir {
399+
Some(ref dir) => command.current_dir(dir),
400+
None => command
401+
};
377402
let output = command.output()?;
378403

379404

0 commit comments

Comments
 (0)