diff options
author | Eric Kidd <git@randomhacks.net> | 2016-11-09 06:07:53 -0500 |
---|---|---|
committer | Eric Kidd <git@randomhacks.net> | 2016-11-15 13:00:16 -0500 |
commit | e9cd0a1cc39a2087600976189bf3b7c489d7ca1c (patch) | |
tree | 4f42b988de8b44ea2e96380dceb4165cc9d4d121 /tests | |
parent | cc35ae074839dc074f2241475274d4be6fbf602f (diff) |
Allow specifying patterns with `-f FILE` and `-f-`
This is a somewhat basic implementation of `-f-` (#7), with unit tests.
Changes include:
1. The internals of the `pattern` function have been refactored to avoid
code duplication, but there's a lot more we could do. Right now we
read the entire pattern list into a `Vec`.
2. There's now a `WorkDir::pipe` command that allows sending standard
input to `rg` when testing.
Not implemented: aho-corasick.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tests.rs | 23 | ||||
-rw-r--r-- | tests/workdir.rs | 36 |
2 files changed, 58 insertions, 1 deletions
diff --git a/tests/tests.rs b/tests/tests.rs index 2a53c479..3481121b 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -902,6 +902,29 @@ clean!(regression_228, "test", ".", |wd: WorkDir, mut cmd: Command| { wd.assert_err(&mut cmd); }); +// See: https://github.com/BurntSushi/ripgrep/issues/7 +sherlock!(feature_7, "-fpat", "sherlock", |wd: WorkDir, mut cmd: Command| { + wd.create("pat", "Sherlock\nHolmes"); + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +For the Doctor Watsons of this world, as opposed to the Sherlock +Holmeses, success in the province of detective work must always +be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +}); + +// See: https://github.com/BurntSushi/ripgrep/issues/7 +sherlock!(feature_7_dash, "-f-", ".", |wd: WorkDir, mut cmd: Command| { + let output = wd.pipe(&mut cmd, "Sherlock"); + let lines = String::from_utf8_lossy(&output.stdout); + let expected = "\ +sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock +sherlock:be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +}); + // See: https://github.com/BurntSushi/ripgrep/issues/20 sherlock!(feature_20_no_filename, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| { diff --git a/tests/workdir.rs b/tests/workdir.rs index 5ef3b72e..d9de0f8a 100644 --- a/tests/workdir.rs +++ b/tests/workdir.rs @@ -153,7 +153,41 @@ impl WorkDir { /// Gets the output of a command. If the command failed, then this panics. pub fn output(&self, cmd: &mut process::Command) -> process::Output { - let o = cmd.output().unwrap(); + let output = cmd.output().unwrap(); + self.expect_success(cmd, output) + } + + /// Pipe `input` to a command, and collect the output. + pub fn pipe( + &self, + cmd: &mut process::Command, + input: &str + ) -> process::Output { + cmd.stdin(process::Stdio::piped()); + cmd.stdout(process::Stdio::piped()); + cmd.stderr(process::Stdio::piped()); + + let mut child = cmd.spawn().unwrap(); + + // Pipe input to child process using a separate thread to avoid + // risk of deadlock between parent and child process. + let mut stdin = child.stdin.take().expect("expected standard input"); + let input = input.to_owned(); + let worker = thread::spawn(move || { + write!(stdin, "{}", input) + }); + + let output = self.expect_success(cmd, child.wait_with_output().unwrap()); + worker.join().unwrap().unwrap(); + output + } + + /// If `o` is not the output of a successful process run + fn expect_success( + &self, + cmd: &process::Command, + o: process::Output + ) -> process::Output { if !o.status.success() { let suggest = if o.stderr.is_empty() { |