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

Commit 542c8aa

Browse files
authored
Merge branch 'master' into master
2 parents e09eb41 + 81c0958 commit 542c8aa

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "assert_cli"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
description = "Test CLI Applications."
55
authors = ["Pascal Hertleif <[email protected]>"]
66
license = "MIT OR Apache-2.0"

src/assert.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::default;
2-
use std::process::Command;
2+
use std::process::{Command, Stdio};
33
use std::path::PathBuf;
44
use std::vec::Vec;
55
use std::convert::Into;
6+
use std::io::Write;
7+
68
use errors::*;
79
use output::{OutputAssertion, OutputKind};
810
use environment::Environment;
@@ -16,6 +18,7 @@ pub struct Assert {
1618
expect_success: Option<bool>,
1719
expect_exit_code: Option<i32>,
1820
expect_output: Vec<OutputAssertion>,
21+
stdin_contents: Option<String>,
1922
}
2023

2124
impl default::Default for Assert {
@@ -31,6 +34,7 @@ impl default::Default for Assert {
3134
expect_success: Some(true),
3235
expect_exit_code: None,
3336
expect_output: vec![],
37+
stdin_contents: None,
3438
}
3539
}
3640
}
@@ -91,6 +95,23 @@ impl Assert {
9195
self
9296
}
9397

98+
/// Add stdin to the command.
99+
///
100+
/// # Examples
101+
///
102+
/// ```rust
103+
/// extern crate assert_cli;
104+
///
105+
/// assert_cli::Assert::command(&["cat"])
106+
/// .stdin("42")
107+
/// .stdout().contains("42")
108+
/// .unwrap();
109+
/// ```
110+
pub fn stdin(mut self, contents: &str) -> Self {
111+
self.stdin_contents = Some(String::from(contents));
112+
self
113+
}
114+
94115
/// Sets the working directory for the command.
95116
///
96117
/// # Examples
@@ -257,15 +278,24 @@ impl Assert {
257278
let args: Vec<_> = self.cmd.iter().skip(1).collect();
258279
let mut command = Command::new(cmd);
259280
let command = command
281+
.stdin(Stdio::piped())
282+
.stdout(Stdio::piped())
283+
.stderr(Stdio::piped())
260284
.env_clear()
261-
.envs(::std::env::vars().chain(self.env.clone().vars))
285+
.envs(self.env.clone().compile()))
262286
.args(&args);
263287

264288
let command = match self.current_dir {
265289
Some(ref dir) => command.current_dir(dir),
266290
None => command,
267291
};
268-
let output = command.output()?;
292+
293+
let mut spawned = command.spawn()?;
294+
295+
if let Some(ref contents) = self.stdin_contents {
296+
spawned.stdin.as_mut().expect("Couldn't get mut ref to command stdin").write_all(contents.as_bytes())?;
297+
}
298+
let output = spawned.wait_with_output()?;
269299

270300
if let Some(expect_success) = self.expect_success {
271301
if expect_success != output.status.success() {

0 commit comments

Comments
 (0)