Skip to content

feat(stdin): Support piping in files #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Changes from all commits
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
75 changes: 68 additions & 7 deletions src/stdin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fs;
use std::io;
use std::io::Read;
use std::io::Write;
use std::path;
use std::process;

use assert::Assert;
Expand All @@ -22,24 +25,81 @@ pub trait CommandStdInExt {
///
/// Command::new("cat")
/// .arg("-A")
/// .with_stdin("42")
/// .with_stdin()
/// .buffer("42")
/// .unwrap();
/// ```
fn with_stdin<S>(&mut self, buffer: S) -> StdInCommand
where
S: Into<Vec<u8>>;
fn with_stdin(&mut self) -> StdInCommandBuilder;
}

impl CommandStdInExt for process::Command {
fn with_stdin<S>(&mut self, buffer: S) -> StdInCommand
fn with_stdin(&mut self) -> StdInCommandBuilder {
StdInCommandBuilder { cmd: self }
}
}

/// For adding a stdin to a `Command`.
#[derive(Debug)]
pub struct StdInCommandBuilder<'a> {
cmd: &'a mut process::Command,
}

impl<'a> StdInCommandBuilder<'a> {
/// Write `buffer` to `stdin` when the command is run.
///
/// # Examples
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::new("cat")
/// .arg("-A")
/// .with_stdin()
/// .buffer("42")
/// .unwrap();
/// ```
pub fn buffer<S>(&mut self, buffer: S) -> StdInCommand
where
S: Into<Vec<u8>>,
{
StdInCommand {
cmd: self,
cmd: self.cmd,
stdin: buffer.into(),
}
}

/// Write `path`s content to `stdin` when the command is run.
///
/// Paths are relative to the `env::current_dir` and not `Command::current_dir`.
///
/// # Examples
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// Command::new("cat")
/// .arg("-A")
/// .with_stdin()
/// .path("Cargo.toml")
/// .unwrap()
/// .unwrap();
/// ```
pub fn path<P>(&mut self, file: P) -> io::Result<StdInCommand>
where
P: AsRef<path::Path>,
{
let file = file.as_ref();
let mut buffer = Vec::new();
fs::File::open(file)?.read_to_end(&mut buffer)?;
Ok(StdInCommand {
cmd: self.cmd,
stdin: buffer,
})
}
}

/// `Command` that carries the `stdin` buffer.
Expand All @@ -54,7 +114,8 @@ impl CommandStdInExt for process::Command {
/// use std::process::Command;
///
/// Command::new("cat")
/// .with_stdin("42")
/// .with_stdin()
/// .buffer("42")
/// .unwrap();
/// ```
#[derive(Debug)]
Expand Down