diff options
author | Balaji Sivaraman <balaji@balajisivaraman.com> | 2018-01-07 21:35:58 +0530 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-01-30 09:13:53 -0500 |
commit | f007f940c53a4818ead58f2fe2e0fac95cc3a40a (patch) | |
tree | 4612a86e94ffc22f44c9851925fdf01f18b920ad /tests | |
parent | a8543f798d5cd0ccfb038c2b80a640f02521c370 (diff) |
search: add support for searching compressed files
This commit adds opt-in support for searching compressed files during
recursive search. This behavior is only enabled when the
`-z/--search-zip` flag is passed to ripgrep. When enabled, a limited set
of common compression formats are recognized via file extension, and a
new process is spawned to perform the decompression. ripgrep then
searches the stdout of that spawned process.
Closes #539
Diffstat (limited to 'tests')
-rw-r--r-- | tests/data/sherlock.bz2 | bin | 0 -> 272 bytes | |||
-rw-r--r-- | tests/data/sherlock.gz | bin | 0 -> 263 bytes | |||
-rw-r--r-- | tests/data/sherlock.lzma | bin | 0 -> 286 bytes | |||
-rw-r--r-- | tests/data/sherlock.xz | bin | 0 -> 332 bytes | |||
-rw-r--r-- | tests/tests.rs | 102 |
5 files changed, 102 insertions, 0 deletions
diff --git a/tests/data/sherlock.bz2 b/tests/data/sherlock.bz2 Binary files differnew file mode 100644 index 00000000..e4a6454e --- /dev/null +++ b/tests/data/sherlock.bz2 diff --git a/tests/data/sherlock.gz b/tests/data/sherlock.gz Binary files differnew file mode 100644 index 00000000..5629cbef --- /dev/null +++ b/tests/data/sherlock.gz diff --git a/tests/data/sherlock.lzma b/tests/data/sherlock.lzma Binary files differnew file mode 100644 index 00000000..bfdf7fb4 --- /dev/null +++ b/tests/data/sherlock.lzma diff --git a/tests/data/sherlock.xz b/tests/data/sherlock.xz Binary files differnew file mode 100644 index 00000000..39cca0f5 --- /dev/null +++ b/tests/data/sherlock.xz diff --git a/tests/tests.rs b/tests/tests.rs index 5f8fa2ec..dc19350c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -75,6 +75,10 @@ fn sort_lines(lines: &str) -> String { format!("{}\n", lines.join("\n")) } +fn cmd_exists(name: &str) -> bool { + Command::new(name).arg("--help").output().is_ok() +} + sherlock!(single_file, |wd: WorkDir, mut cmd| { let lines: String = wd.stdout(&mut cmd); let expected = "\ @@ -1610,6 +1614,104 @@ clean!(suggest_fixed_strings_for_invalid_regex, "foo(", ".", }); #[test] +fn compressed_gzip() { + if !cmd_exists("gzip") { + return; + } + let gzip_file = include_bytes!("./data/sherlock.gz"); + + let wd = WorkDir::new("feature_search_compressed"); + wd.create_bytes("sherlock.gz", gzip_file); + + let mut cmd = wd.command(); + cmd.arg("-z").arg("Sherlock").arg("sherlock.gz"); + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +For the Doctor Watsons of this world, as opposed to the Sherlock +be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +} + +#[test] +fn compressed_bzip2() { + if !cmd_exists("bzip2") { + return; + } + let bzip2_file = include_bytes!("./data/sherlock.bz2"); + + let wd = WorkDir::new("feature_search_compressed"); + wd.create_bytes("sherlock.bz2", bzip2_file); + + let mut cmd = wd.command(); + cmd.arg("-z").arg("Sherlock").arg("sherlock.bz2"); + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +For the Doctor Watsons of this world, as opposed to the Sherlock +be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +} + +#[test] +fn compressed_xz() { + if !cmd_exists("xz") { + return; + } + let xz_file = include_bytes!("./data/sherlock.xz"); + + let wd = WorkDir::new("feature_search_compressed"); + wd.create_bytes("sherlock.xz", xz_file); + + let mut cmd = wd.command(); + cmd.arg("-z").arg("Sherlock").arg("sherlock.xz"); + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +For the Doctor Watsons of this world, as opposed to the Sherlock +be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +} + +#[test] +fn compressed_lzma() { + if !cmd_exists("xz") { + return; + } + let lzma_file = include_bytes!("./data/sherlock.lzma"); + + let wd = WorkDir::new("feature_search_compressed"); + wd.create_bytes("sherlock.lzma", lzma_file); + + let mut cmd = wd.command(); + cmd.arg("-z").arg("Sherlock").arg("sherlock.lzma"); + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +For the Doctor Watsons of this world, as opposed to the Sherlock +be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +} + +#[test] +fn compressed_failing_gzip() { + if !cmd_exists("gzip") { + return; + } + let wd = WorkDir::new("feature_search_compressed"); + wd.create("sherlock.gz", hay::SHERLOCK); + + let mut cmd = wd.command(); + cmd.arg("-z").arg("Sherlock").arg("sherlock.gz"); + + wd.assert_non_empty_stderr(&mut cmd); + + let output = cmd.output().unwrap(); + let err = String::from_utf8_lossy(&output.stderr); + assert_eq!(err.contains("not in gzip format"), true); +} + +#[test] fn feature_740_passthru() { let wd = WorkDir::new("feature_740"); wd.create("file", "\nfoo\nbar\nfoobar\n\nbaz\n"); |