mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-11-23 21:54:45 +02:00
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.
This commit is contained in:
@@ -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| {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user