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

Commit eb2619c

Browse files
committed
Add ability to set current_dir
1 parent 434303a commit eb2619c

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, Output};
112+
use std::path::PathBuf;
112113
use std::fmt;
113114

114115
use difference::Changeset;
@@ -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>,
@@ -169,6 +171,7 @@ impl std::default::Default for Assert {
169171
Assert {
170172
cmd: vec!["cargo", "run", "--"]
171173
.into_iter().map(String::from).collect(),
174+
current_dir: None,
172175
expect_success: Some(true),
173176
expect_exit_code: None,
174177
expect_stdout: None,
@@ -232,6 +235,24 @@ impl Assert {
232235
self
233236
}
234237

238+
/// Sets the working directory for the command.
239+
///
240+
/// # Examples
241+
///
242+
/// ```rust
243+
/// extern crate assert_cli;
244+
///
245+
/// assert_cli::Assert::command(&["wc", "lib.rs"])
246+
/// .current_dir(std::path::Path::new("src"))
247+
/// .prints("lib.rs")
248+
/// .execute()
249+
/// .unwrap();
250+
/// ```
251+
pub fn current_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
252+
self.current_dir = Some(dir.into());
253+
self
254+
}
255+
235256
/// Small helper to make chains more readable.
236257
///
237258
/// # Examples
@@ -400,6 +421,10 @@ impl Assert {
400421
let args: Vec<_> = self.cmd.iter().skip(1).collect();
401422
let mut command = Command::new(cmd);
402423
let command = command.args(&args);
424+
let command = match self.current_dir {
425+
Some(ref dir) => command.current_dir(dir),
426+
None => command
427+
};
403428
let output = command.output()?;
404429

405430

0 commit comments

Comments
 (0)