Skip to content
Myr

feature.rs

This file

Path

tests/feature.rs

1,174 lines · 37.6 KiB · Rust

Last changed by

test: attempt to fix flaky time-reliant testAndrew Gallantbefore adoptiona5d9e03

History of this file

Take it

1use crate::hay::{SHERLOCK, SHERLOCK_CRLF};2use crate::util::{Dir, TestCommand, sort_lines};34// See: https://github.com/BurntSushi/ripgrep/issues/15rgtest!(f1_sjis, |dir: Dir, mut cmd: TestCommand| {6    dir.create_bytes(7        "foo",8        b"\x84Y\x84u\x84\x82\x84|\x84\x80\x84{ \x84V\x84\x80\x84|\x84}\x84\x83"9    );10    cmd.arg("-Esjis").arg("Шерлок Холмс");11    eqnice!("foo:Шерлок Холмс\n", cmd.stdout());12});1314// See: https://github.com/BurntSushi/ripgrep/issues/115rgtest!(f1_utf16_auto, |dir: Dir, mut cmd: TestCommand| {16    dir.create_bytes(17        "foo",18        b"\xff\xfe(\x045\x04@\x04;\x04>\x04:\x04 \x00%\x04>\x04;\x04<\x04A\x04"19    );20    cmd.arg("Шерлок Холмс");21    eqnice!("foo:Шерлок Холмс\n", cmd.stdout());22});2324// See: https://github.com/BurntSushi/ripgrep/issues/125rgtest!(f1_utf16_explicit, |dir: Dir, mut cmd: TestCommand| {26    dir.create_bytes(27        "foo",28        b"\xff\xfe(\x045\x04@\x04;\x04>\x04:\x04 \x00%\x04>\x04;\x04<\x04A\x04"29    );30    cmd.arg("-Eutf-16le").arg("Шерлок Холмс");31    eqnice!("foo:Шерлок Холмс\n", cmd.stdout());32});3334// See: https://github.com/BurntSushi/ripgrep/issues/135rgtest!(f1_eucjp, |dir: Dir, mut cmd: TestCommand| {36    dir.create_bytes(37        "foo",38        b"\xa7\xba\xa7\xd6\xa7\xe2\xa7\xdd\xa7\xe0\xa7\xdc \xa7\xb7\xa7\xe0\xa7\xdd\xa7\xde\xa7\xe3"39    );40    cmd.arg("-Eeuc-jp").arg("Шерлок Холмс");41    eqnice!("foo:Шерлок Холмс\n", cmd.stdout());42});4344// See: https://github.com/BurntSushi/ripgrep/issues/145rgtest!(f1_unknown_encoding, |_: Dir, mut cmd: TestCommand| {46    cmd.arg("-Efoobar").assert_non_empty_stderr();47});4849// See: https://github.com/BurntSushi/ripgrep/issues/150rgtest!(f1_replacement_encoding, |_: Dir, mut cmd: TestCommand| {51    cmd.arg("-Ecsiso2022kr").assert_non_empty_stderr();52});5354// See: https://github.com/BurntSushi/ripgrep/issues/755rgtest!(f7, |dir: Dir, mut cmd: TestCommand| {56    dir.create("sherlock", SHERLOCK);57    dir.create("pat", "Sherlock\nHolmes");5859    let expected = "\60For the Doctor Watsons of this world, as opposed to the Sherlock61Holmeses, success in the province of detective work must always62be, to a very large extent, the result of luck. Sherlock Holmes63";64    eqnice!(expected, cmd.arg("-fpat").arg("sherlock").stdout());65});6667// See: https://github.com/BurntSushi/ripgrep/issues/768rgtest!(f7_stdin, |dir: Dir, mut cmd: TestCommand| {69    dir.create("sherlock", SHERLOCK);7071    let expected = "\72sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock73sherlock:be, to a very large extent, the result of luck. Sherlock Holmes74";75    eqnice!(expected, cmd.arg("-f-").pipe(b"Sherlock"));76});7778// See: https://github.com/BurntSushi/ripgrep/issues/2079rgtest!(f20_no_filename, |dir: Dir, mut cmd: TestCommand| {80    dir.create("sherlock", SHERLOCK);81    cmd.arg("--no-filename");8283    let expected = "\84For the Doctor Watsons of this world, as opposed to the Sherlock85be, to a very large extent, the result of luck. Sherlock Holmes86";87    eqnice!(expected, cmd.arg("--no-filename").arg("Sherlock").stdout());88});8990// See: https://github.com/BurntSushi/ripgrep/issues/3491rgtest!(f34_only_matching, |dir: Dir, mut cmd: TestCommand| {92    dir.create("sherlock", SHERLOCK);9394    let expected = "\95sherlock:Sherlock96sherlock:Sherlock97";98    eqnice!(expected, cmd.arg("-o").arg("Sherlock").stdout());99});100101// See: https://github.com/BurntSushi/ripgrep/issues/34102rgtest!(f34_only_matching_line_column, |dir: Dir, mut cmd: TestCommand| {103    dir.create("sherlock", SHERLOCK);104105    let expected = "\106sherlock:1:57:Sherlock107sherlock:3:49:Sherlock108";109    cmd.arg("-o").arg("--column").arg("-n").arg("Sherlock");110    eqnice!(expected, cmd.stdout());111});112113// See: https://github.com/BurntSushi/ripgrep/issues/45114rgtest!(f45_relative_cwd, |dir: Dir, mut cmd: TestCommand| {115    dir.create(".not-an-ignore", "foo\n/bar");116    dir.create_dir("bar");117    dir.create_dir("baz/bar");118    dir.create_dir("baz/baz/bar");119    dir.create("bar/test", "test");120    dir.create("baz/bar/test", "test");121    dir.create("baz/baz/bar/test", "test");122    dir.create("baz/foo", "test");123    dir.create("baz/test", "test");124    dir.create("foo", "test");125    dir.create("test", "test");126127    cmd.arg("-l").arg("test");128129    // First, get a baseline without applying ignore rules.130    let expected = "131bar/test132baz/bar/test133baz/baz/bar/test134baz/foo135baz/test136foo137test138";139    eqnice!(sort_lines(expected), sort_lines(&cmd.stdout()));140141    // Now try again with the ignore file activated.142    cmd.arg("--ignore-file").arg(".not-an-ignore");143    let expected = "144baz/bar/test145baz/baz/bar/test146baz/test147test148";149    eqnice!(sort_lines(expected), sort_lines(&cmd.stdout()));150151    // Now do it again, but inside the baz directory. Since the ignore file152    // is interpreted relative to the CWD, this will cause the /bar anchored153    // pattern to filter out baz/bar, which is a subtle difference between true154    // parent ignore files and manually specified ignore files.155    let mut cmd = dir.command();156    cmd.args(&["--ignore-file", "../.not-an-ignore", "-l", "test"]);157    cmd.current_dir("baz");158    let expected = "159baz/bar/test160test161";162    eqnice!(sort_lines(expected), sort_lines(&cmd.stdout()));163});164165// See: https://github.com/BurntSushi/ripgrep/issues/45166rgtest!(f45_precedence_with_others, |dir: Dir, mut cmd: TestCommand| {167    dir.create(".not-an-ignore", "*.log");168    dir.create(".ignore", "!imp.log");169    dir.create("imp.log", "test");170    dir.create("wat.log", "test");171172    cmd.arg("--ignore-file").arg(".not-an-ignore").arg("test");173    eqnice!("imp.log:test\n", cmd.stdout());174});175176// See: https://github.com/BurntSushi/ripgrep/issues/45177rgtest!(f45_precedence_internal, |dir: Dir, mut cmd: TestCommand| {178    dir.create(".not-an-ignore1", "*.log");179    dir.create(".not-an-ignore2", "!imp.log");180    dir.create("imp.log", "test");181    dir.create("wat.log", "test");182183    cmd.args(&[184        "--ignore-file",185        ".not-an-ignore1",186        "--ignore-file",187        ".not-an-ignore2",188        "test",189    ]);190    eqnice!("imp.log:test\n", cmd.stdout());191});192193// See: https://github.com/BurntSushi/ripgrep/issues/68194rgtest!(f68_no_ignore_vcs, |dir: Dir, mut cmd: TestCommand| {195    dir.create_dir(".git");196    dir.create(".gitignore", "foo");197    dir.create(".ignore", "bar");198    dir.create("foo", "test");199    dir.create("bar", "test");200201    eqnice!("foo:test\n", cmd.arg("--no-ignore-vcs").arg("test").stdout());202});203204// See: https://github.com/BurntSushi/ripgrep/issues/70205rgtest!(f70_smart_case, |dir: Dir, mut cmd: TestCommand| {206    dir.create("sherlock", SHERLOCK);207208    let expected = "\209sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock210sherlock:be, to a very large extent, the result of luck. Sherlock Holmes211";212    eqnice!(expected, cmd.arg("-S").arg("sherlock").stdout());213});214215// See: https://github.com/BurntSushi/ripgrep/issues/89216rgtest!(f89_files_with_matches, |dir: Dir, mut cmd: TestCommand| {217    dir.create("sherlock", SHERLOCK);218219    cmd.arg("--null").arg("--files-with-matches").arg("Sherlock");220    eqnice!("sherlock\x00", cmd.stdout());221});222223// See: https://github.com/BurntSushi/ripgrep/issues/89224rgtest!(f89_files_without_match, |dir: Dir, mut cmd: TestCommand| {225    dir.create("sherlock", SHERLOCK);226    dir.create("file.py", "foo");227228    cmd.arg("--null").arg("--files-without-match").arg("Sherlock");229    eqnice!("file.py\x00", cmd.stdout());230});231232// See: https://github.com/BurntSushi/ripgrep/issues/89233rgtest!(f89_count, |dir: Dir, mut cmd: TestCommand| {234    dir.create("sherlock", SHERLOCK);235236    cmd.arg("--null").arg("--count").arg("Sherlock");237    eqnice!("sherlock\x002\n", cmd.stdout());238});239240// See: https://github.com/BurntSushi/ripgrep/issues/89241rgtest!(f89_files, |dir: Dir, mut cmd: TestCommand| {242    dir.create("sherlock", SHERLOCK);243244    eqnice!("sherlock\x00", cmd.arg("--null").arg("--files").stdout());245});246247// See: https://github.com/BurntSushi/ripgrep/issues/89248rgtest!(f89_match, |dir: Dir, mut cmd: TestCommand| {249    dir.create("sherlock", SHERLOCK);250251    let expected = "\252sherlock\x00For the Doctor Watsons of this world, as opposed to the Sherlock253sherlock\x00Holmeses, success in the province of detective work must always254sherlock\x00be, to a very large extent, the result of luck. Sherlock Holmes255sherlock\x00can extract a clew from a wisp of straw or a flake of cigar ash;256";257    eqnice!(expected, cmd.arg("--null").arg("-C1").arg("Sherlock").stdout());258});259260// See: https://github.com/BurntSushi/ripgrep/issues/109261rgtest!(f109_max_depth, |dir: Dir, mut cmd: TestCommand| {262    dir.create_dir("one");263    dir.create("one/pass", "far");264    dir.create_dir("one/too");265    dir.create("one/too/many", "far");266267    cmd.arg("--maxdepth").arg("2").arg("far");268    eqnice!("one/pass:far\n", cmd.stdout());269});270271// See: https://github.com/BurntSushi/ripgrep/issues/124272rgtest!(f109_case_sensitive_part1, |dir: Dir, mut cmd: TestCommand| {273    dir.create("foo", "tEsT");274275    cmd.arg("--smart-case").arg("--case-sensitive").arg("test").assert_err();276});277278// See: https://github.com/BurntSushi/ripgrep/issues/124279rgtest!(f109_case_sensitive_part2, |dir: Dir, mut cmd: TestCommand| {280    dir.create("foo", "tEsT");281    cmd.arg("--ignore-case").arg("--case-sensitive").arg("test").assert_err();282});283284// See: https://github.com/BurntSushi/ripgrep/issues/129285rgtest!(f129_matches, |dir: Dir, mut cmd: TestCommand| {286    dir.create("foo", "test\ntest abcdefghijklmnopqrstuvwxyz test");287288    let expected = "foo:test\nfoo:[Omitted long matching line]\n";289    eqnice!(expected, cmd.arg("-M26").arg("test").stdout());290});291292// See: https://github.com/BurntSushi/ripgrep/issues/129293rgtest!(f129_context, |dir: Dir, mut cmd: TestCommand| {294    dir.create("foo", "test\nabcdefghijklmnopqrstuvwxyz");295296    let expected = "foo:test\nfoo-[Omitted long context line]\n";297    eqnice!(expected, cmd.arg("-M20").arg("-C1").arg("test").stdout());298});299300// See: https://github.com/BurntSushi/ripgrep/issues/129301rgtest!(f129_replace, |dir: Dir, mut cmd: TestCommand| {302    dir.create("foo", "test\ntest abcdefghijklmnopqrstuvwxyz test");303304    let expected = "foo:foo\nfoo:[Omitted long line with 2 matches]\n";305    eqnice!(expected, cmd.arg("-M26").arg("-rfoo").arg("test").stdout());306});307308// See: https://github.com/BurntSushi/ripgrep/issues/159309rgtest!(f159_max_count, |dir: Dir, mut cmd: TestCommand| {310    dir.create("foo", "test\ntest");311312    eqnice!("foo:test\n", cmd.arg("-m1").arg("test").stdout());313});314315// See: https://github.com/BurntSushi/ripgrep/issues/159316rgtest!(f159_max_count_zero, |dir: Dir, mut cmd: TestCommand| {317    dir.create("foo", "test\ntest");318319    cmd.arg("-m0").arg("test").assert_err();320});321322// See: https://github.com/BurntSushi/ripgrep/issues/196323rgtest!(f196_persistent_config, |dir: Dir, mut cmd: TestCommand| {324    dir.create("sherlock", SHERLOCK);325    cmd.arg("sherlock").arg("sherlock");326327    // Make sure we get no matches by default.328    cmd.assert_err();329330    // Now add our config file, and make sure it impacts ripgrep.331    dir.create(".ripgreprc", "--ignore-case");332    cmd.cmd().env("RIPGREP_CONFIG_PATH", ".ripgreprc");333334    let expected = "\335For the Doctor Watsons of this world, as opposed to the Sherlock336be, to a very large extent, the result of luck. Sherlock Holmes337";338    eqnice!(expected, cmd.stdout());339});340341// See: https://github.com/BurntSushi/ripgrep/issues/243342rgtest!(f243_column_line, |dir: Dir, mut cmd: TestCommand| {343    dir.create("foo", "test");344345    eqnice!("foo:1:1:test\n", cmd.arg("--column").arg("test").stdout());346});347348// See: https://github.com/BurntSushi/ripgrep/issues/263349rgtest!(f263_sort_files, |dir: Dir, mut cmd: TestCommand| {350    dir.create("foo", "test");351    dir.create("abc", "test");352    dir.create("zoo", "test");353    dir.create("bar", "test");354355    let expected = "abc:test\nbar:test\nfoo:test\nzoo:test\n";356    eqnice!(expected, cmd.arg("--sort-files").arg("test").stdout());357});358359// See: https://github.com/BurntSushi/ripgrep/issues/263360rgtest!(f263_sort_files_reverse, |dir: Dir, mut cmd: TestCommand| {361    dir.create("foo", "test");362    dir.create("abc", "test");363    dir.create("zoo", "test");364    dir.create("bar", "test");365366    let expected = "zoo:test\nfoo:test\nbar:test\nabc:test\n";367    eqnice!(expected, cmd.arg("--sortr=path").arg("test").stdout());368});369370// See: https://github.com/BurntSushi/ripgrep/issues/275371rgtest!(f275_pathsep, |dir: Dir, mut cmd: TestCommand| {372    dir.create_dir("foo");373    dir.create("foo/bar", "test");374375    cmd.arg("test").arg("--path-separator").arg("Z");376    eqnice!("fooZbar:test\n", cmd.stdout());377});378379// See: https://github.com/BurntSushi/ripgrep/issues/362380rgtest!(f362_dfa_size_limit, |dir: Dir, mut cmd: TestCommand| {381    dir.create("sherlock", SHERLOCK);382383    // This should fall back to the nfa engine but should still produce the384    // expected result.385    cmd.arg("--dfa-size-limit").arg("10").arg(r"For\s").arg("sherlock");386387    let expected = "\388For the Doctor Watsons of this world, as opposed to the Sherlock389";390    eqnice!(expected, cmd.stdout());391});392393// See: https://github.com/BurntSushi/ripgrep/issues/362394rgtest!(f362_exceeds_regex_size_limit, |dir: Dir, mut cmd: TestCommand| {395    // --regex-size-limit doesn't apply to PCRE2.396    if dir.is_pcre2() {397        return;398    }399    cmd.arg("--regex-size-limit").arg("10K").arg(r"[0-9]\w+").assert_err();400});401402// See: https://github.com/BurntSushi/ripgrep/issues/362403#[cfg(target_pointer_width = "32")]404rgtest!(405    f362_u64_to_narrow_usize_overflow,406    |dir: Dir, mut cmd: TestCommand| {407        // --dfa-size-limit doesn't apply to PCRE2.408        if dir.is_pcre2() {409            return;410        }411        dir.create_size("foo", 1000000);412413        // 2^35 * 2^20 is ok for u64, but not for usize414        cmd.arg("--dfa-size-limit").arg("34359738368M").arg("--files");415        cmd.assert_err();416    }417);418419// See: https://github.com/BurntSushi/ripgrep/issues/411420rgtest!(421    f411_single_threaded_search_stats,422    |dir: Dir, mut cmd: TestCommand| {423        dir.create("sherlock", SHERLOCK);424425        let lines = cmd.arg("-j1").arg("--stats").arg("Sherlock").stdout();426        assert!(lines.contains("Sherlock"));427        assert!(lines.contains("2 matched lines"));428        assert!(lines.contains("1 files contained matches"));429        assert!(lines.contains("1 files searched"));430        assert!(lines.contains("seconds"));431    }432);433434rgtest!(f411_parallel_search_stats, |dir: Dir, mut cmd: TestCommand| {435    dir.create("sherlock_1", SHERLOCK);436    dir.create("sherlock_2", SHERLOCK);437438    let lines = cmd.arg("-j2").arg("--stats").arg("Sherlock").stdout();439    assert!(lines.contains("4 matched lines"));440    assert!(lines.contains("2 files contained matches"));441    assert!(lines.contains("2 files searched"));442    assert!(lines.contains("seconds"));443});444445rgtest!(446    f411_single_threaded_quiet_search_stats,447    |dir: Dir, mut cmd: TestCommand| {448        dir.create("sherlock", SHERLOCK);449450        let lines = cmd451            .arg("--quiet")452            .arg("-j1")453            .arg("--stats")454            .arg("Sherlock")455            .stdout();456        assert!(!lines.contains("Sherlock"));457        assert!(lines.contains("2 matched lines"));458        assert!(lines.contains("1 files contained matches"));459        assert!(lines.contains("1 files searched"));460        assert!(lines.contains("seconds"));461    }462);463464rgtest!(f411_parallel_quiet_search_stats, |dir: Dir, mut cmd: TestCommand| {465    dir.create("sherlock_1", SHERLOCK);466    dir.create("sherlock_2", SHERLOCK);467468    let lines =469        cmd.arg("-j2").arg("--quiet").arg("--stats").arg("Sherlock").stdout();470    assert!(!lines.contains("Sherlock"));471    assert!(lines.contains("4 matched lines"));472    assert!(lines.contains("2 files contained matches"));473    assert!(lines.contains("2 files searched"));474    assert!(lines.contains("seconds"));475});476477// See: https://github.com/BurntSushi/ripgrep/issues/416478rgtest!(f416_crlf, |dir: Dir, mut cmd: TestCommand| {479    dir.create("sherlock", SHERLOCK_CRLF);480    cmd.arg("--crlf").arg(r"Sherlock$").arg("sherlock");481482    let expected = "\483For the Doctor Watsons of this world, as opposed to the Sherlock\r484";485    eqnice!(expected, cmd.stdout());486});487488// See: https://github.com/BurntSushi/ripgrep/issues/416489rgtest!(f416_crlf_multiline, |dir: Dir, mut cmd: TestCommand| {490    dir.create("sherlock", SHERLOCK_CRLF);491    cmd.arg("--crlf").arg("-U").arg(r"Sherlock$").arg("sherlock");492493    let expected = "\494For the Doctor Watsons of this world, as opposed to the Sherlock\r495";496    eqnice!(expected, cmd.stdout());497});498499// See: https://github.com/BurntSushi/ripgrep/issues/416500rgtest!(f416_crlf_only_matching, |dir: Dir, mut cmd: TestCommand| {501    dir.create("sherlock", SHERLOCK_CRLF);502    cmd.arg("--crlf").arg("-o").arg(r"Sherlock$").arg("sherlock");503504    let expected = "\505Sherlock\r506";507    eqnice!(expected, cmd.stdout());508});509510// See: https://github.com/BurntSushi/ripgrep/issues/419511rgtest!(f419_zero_as_shortcut_for_null, |dir: Dir, mut cmd: TestCommand| {512    dir.create("sherlock", SHERLOCK);513514    cmd.arg("-0").arg("--count").arg("Sherlock");515    eqnice!("sherlock\x002\n", cmd.stdout());516});517518rgtest!(f740_passthru, |dir: Dir, mut cmd: TestCommand| {519    dir.create("file", "\nfoo\nbar\nfoobar\n\nbaz\n");520    dir.create("patterns", "foo\nbar\n");521522    // We can't assume that the way colour specs are translated to ANSI523    // sequences will remain stable, and --replace doesn't currently work with524    // pass-through, so for now we don't actually test the match sub-strings525    let common_args = &["-n", "--passthru"];526    let foo_expected = "\5271-5282:foo5293-bar5304:foobar5315-5326-baz533";534535    // With single pattern536    cmd.args(common_args).arg("foo").arg("file");537    eqnice!(foo_expected, cmd.stdout());538539    let foo_bar_expected = "\5401-5412:foo5423:bar5434:foobar5445-5456-baz546";547548    // With multiple -e patterns549    let mut cmd = dir.command();550    cmd.args(common_args);551    cmd.args(&["-e", "foo", "-e", "bar", "file"]);552    eqnice!(foo_bar_expected, cmd.stdout());553554    // With multiple -f patterns555    let mut cmd = dir.command();556    cmd.args(common_args);557    cmd.args(&["-f", "patterns", "file"]);558    eqnice!(foo_bar_expected, cmd.stdout());559560    // -c should override561    let mut cmd = dir.command();562    cmd.args(common_args);563    cmd.args(&["-c", "foo", "file"]);564    eqnice!("2\n", cmd.stdout());565566    let only_foo_expected = "\5671-5682:foo5693-bar5704:foo5715-5726-baz573";574575    // -o should work576    let mut cmd = dir.command();577    cmd.args(common_args);578    cmd.args(&["-o", "foo", "file"]);579    eqnice!(only_foo_expected, cmd.stdout());580581    let replace_foo_expected = "\5821-5832:wat5843-bar5854:watbar5865-5876-baz588";589590    // -r should work591    let mut cmd = dir.command();592    cmd.args(common_args);593    cmd.args(&["-r", "wat", "foo", "file"]);594    eqnice!(replace_foo_expected, cmd.stdout());595});596597// See: https://github.com/BurntSushi/ripgrep/issues/948598rgtest!(f948_exit_code_match, |dir: Dir, mut cmd: TestCommand| {599    dir.create("sherlock", SHERLOCK);600    cmd.arg(".");601602    cmd.assert_exit_code(0);603});604605// See: https://github.com/BurntSushi/ripgrep/issues/948606rgtest!(f948_exit_code_no_match, |dir: Dir, mut cmd: TestCommand| {607    dir.create("sherlock", SHERLOCK);608    cmd.arg("NADA");609610    cmd.assert_exit_code(1);611});612613// See: https://github.com/BurntSushi/ripgrep/issues/948614rgtest!(f948_exit_code_error, |dir: Dir, mut cmd: TestCommand| {615    dir.create("sherlock", SHERLOCK);616    cmd.arg("*");617618    cmd.assert_exit_code(2);619});620621// See: https://github.com/BurntSushi/ripgrep/issues/917622rgtest!(f917_trim, |dir: Dir, mut cmd: TestCommand| {623    const SHERLOCK: &'static str = "\624zzz625    For the Doctor Watsons of this world, as opposed to the Sherlock626  Holmeses, success in the province of detective work must always627\tbe, to a very large extent, the result of luck. Sherlock Holmes628     can extract a clew from a wisp of straw or a flake of cigar ash;629but Doctor Watson has to have it taken out for him and dusted,630 and exhibited clearly, with a label attached.631";632    dir.create("sherlock", SHERLOCK);633    cmd.args(&["-n", "-B1", "-A2", "--trim", "Holmeses", "sherlock"]);634635    let expected = "\6362-For the Doctor Watsons of this world, as opposed to the Sherlock6373:Holmeses, success in the province of detective work must always6384-be, to a very large extent, the result of luck. Sherlock Holmes6395-can extract a clew from a wisp of straw or a flake of cigar ash;640";641    eqnice!(expected, cmd.stdout());642});643644// See: https://github.com/BurntSushi/ripgrep/issues/917645//646// This is like f917_trim, except this tests that trimming occurs even when the647// whitespace is part of a match.648rgtest!(f917_trim_match, |dir: Dir, mut cmd: TestCommand| {649    const SHERLOCK: &'static str = "\650zzz651    For the Doctor Watsons of this world, as opposed to the Sherlock652  Holmeses, success in the province of detective work must always653\tbe, to a very large extent, the result of luck. Sherlock Holmes654     can extract a clew from a wisp of straw or a flake of cigar ash;655but Doctor Watson has to have it taken out for him and dusted,656 and exhibited clearly, with a label attached.657";658    dir.create("sherlock", SHERLOCK);659    cmd.args(&["-n", "-B1", "-A2", "--trim", r"\s+Holmeses", "sherlock"]);660661    let expected = "\6622-For the Doctor Watsons of this world, as opposed to the Sherlock6633:Holmeses, success in the province of detective work must always6644-be, to a very large extent, the result of luck. Sherlock Holmes6655-can extract a clew from a wisp of straw or a flake of cigar ash;666";667    eqnice!(expected, cmd.stdout());668});669670rgtest!(f917_trim_multi_standard, |dir: Dir, mut cmd: TestCommand| {671    const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";672    dir.create("haystack", HAYSTACK);673    cmd.args(&["--multiline", "--trim", "-r$0", "--no-filename", r"a\n?bc"]);674675    let expected = "0123456789abcdefghijklmnopqrstuvwxyz\n";676    eqnice!(expected, cmd.stdout());677});678679rgtest!(f917_trim_max_columns_normal, |dir: Dir, mut cmd: TestCommand| {680    const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";681    dir.create("haystack", HAYSTACK);682    cmd.args(&[683        "--trim",684        "--max-columns-preview",685        "-M8",686        "--no-filename",687        "abc",688    ]);689690    let expected = "01234567 [... omitted end of long line]\n";691    eqnice!(expected, cmd.stdout());692});693694rgtest!(f917_trim_max_columns_matches, |dir: Dir, mut cmd: TestCommand| {695    const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";696    dir.create("haystack", HAYSTACK);697    cmd.args(&[698        "--trim",699        "--max-columns-preview",700        "-M8",701        "--color=always",702        "--colors=path:none",703        "--no-filename",704        "abc",705    ]);706707    let expected = "01234567 [... 1 more match]\n";708    eqnice!(expected, cmd.stdout());709});710711rgtest!(712    f917_trim_max_columns_multi_standard,713    |dir: Dir, mut cmd: TestCommand| {714        const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";715        dir.create("haystack", HAYSTACK);716        cmd.args(&[717            "--multiline",718            "--trim",719            "--max-columns-preview",720            "-M8",721            // Force the "slow" printing path without actually722            // putting colors in the output.723            "--color=always",724            "--colors=path:none",725            "--no-filename",726            r"a\n?bc",727        ]);728729        let expected = "01234567 [... 1 more match]\n";730        eqnice!(expected, cmd.stdout());731    }732);733734rgtest!(735    f917_trim_max_columns_multi_only_matching,736    |dir: Dir, mut cmd: TestCommand| {737        const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";738        dir.create("haystack", HAYSTACK);739        cmd.args(&[740            "--multiline",741            "--trim",742            "--max-columns-preview",743            "-M8",744            "--only-matching",745            "--no-filename",746            r".*a\n?bc.*",747        ]);748749        let expected = "01234567 [... 0 more matches]\n";750        eqnice!(expected, cmd.stdout());751    }752);753754rgtest!(755    f917_trim_max_columns_multi_per_match,756    |dir: Dir, mut cmd: TestCommand| {757        const HAYSTACK: &str = "     0123456789abcdefghijklmnopqrstuvwxyz";758        dir.create("haystack", HAYSTACK);759        cmd.args(&[760            "--multiline",761            "--trim",762            "--max-columns-preview",763            "-M8",764            "--vimgrep",765            "--no-filename",766            r".*a\n?bc.*",767        ]);768769        let expected = "1:1:01234567 [... 0 more matches]\n";770        eqnice!(expected, cmd.stdout());771    }772);773774// See: https://github.com/BurntSushi/ripgrep/issues/993775rgtest!(f993_null_data, |dir: Dir, mut cmd: TestCommand| {776    dir.create("test", "foo\x00bar\x00\x00\x00baz\x00");777    cmd.arg("--null-data").arg(r".+").arg("test");778779    // If we just used -a instead of --null-data, then the result would include780    // all NUL bytes.781    let expected = "foo\x00bar\x00baz\x00";782    eqnice!(expected, cmd.stdout());783});784785// See: https://github.com/BurntSushi/ripgrep/issues/1078786//787// N.B. There are many more tests in the grep-printer crate.788rgtest!(f1078_max_columns_preview1, |dir: Dir, mut cmd: TestCommand| {789    dir.create("sherlock", SHERLOCK);790    cmd.args(&[791        "-M46",792        "--max-columns-preview",793        "exhibited|dusted|has to have it",794    ]);795796    let expected = "\797sherlock:but Doctor Watson has to have it taken out for [... omitted end of long line]798sherlock:and exhibited clearly, with a label attached.799";800    eqnice!(expected, cmd.stdout());801});802803rgtest!(f1078_max_columns_preview2, |dir: Dir, mut cmd: TestCommand| {804    dir.create("sherlock", SHERLOCK);805    cmd.args(&[806        "-M43",807        "--max-columns-preview",808        // Doing a replacement forces ripgrep to show the number of remaining809        // matches. Normally, this happens by default when printing a tty with810        // colors.811        "-rxxx",812        "exhibited|dusted|has to have it",813    ]);814815    let expected = "\816sherlock:but Doctor Watson xxx taken out for him and [... 1 more match]817sherlock:and xxx clearly, with a label attached.818";819    eqnice!(expected, cmd.stdout());820});821822// See: https://github.com/BurntSushi/ripgrep/issues/1138823rgtest!(f1138_no_ignore_dot, |dir: Dir, mut cmd: TestCommand| {824    dir.create_dir(".git");825    dir.create(".gitignore", "foo");826    dir.create(".ignore", "bar");827    dir.create(".fzf-ignore", "quux");828    dir.create("foo", "");829    dir.create("bar", "");830    dir.create("quux", "");831832    cmd.arg("--sort").arg("path").arg("--files");833    eqnice!("quux\n", cmd.stdout());834    eqnice!("bar\nquux\n", cmd.arg("--no-ignore-dot").stdout());835    eqnice!("bar\n", cmd.arg("--ignore-file").arg(".fzf-ignore").stdout());836});837838// See: https://github.com/BurntSushi/ripgrep/issues/1155839rgtest!(f1155_auto_hybrid_regex, |dir: Dir, mut cmd: TestCommand| {840    // No sense in testing a hybrid regex engine with only one engine!841    if !dir.is_pcre2() {842        return;843    }844845    dir.create("sherlock", SHERLOCK);846    cmd.arg("--no-pcre2").arg("--auto-hybrid-regex").arg(r"(?<=the )Sherlock");847848    let expected = "\849sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock850";851    eqnice!(expected, cmd.stdout());852});853854// See: https://github.com/BurntSushi/ripgrep/issues/1207855//856// Tests if without encoding 'none' flag null bytes are consumed by automatic857// encoding detection.858rgtest!(f1207_auto_encoding, |dir: Dir, mut cmd: TestCommand| {859    dir.create_bytes("foo", b"\xFF\xFE\x00\x62");860    cmd.arg("-a").arg("\\x00").arg("foo");861    cmd.assert_exit_code(1);862});863864// See: https://github.com/BurntSushi/ripgrep/issues/1207865//866// Tests if encoding 'none' flag does treat file as raw bytes867rgtest!(f1207_ignore_encoding, |dir: Dir, mut cmd: TestCommand| {868    // PCRE2 chokes on this test because it can't search invalid non-UTF-8869    // and the point of this test is to search raw UTF-16.870    if dir.is_pcre2() {871        return;872    }873874    dir.create_bytes("foo", b"\xFF\xFE\x00\x62");875    cmd.arg("--encoding").arg("none").arg("-a").arg("\\x00").arg("foo");876    eqnice!("\u{FFFD}\u{FFFD}\x00b\n", cmd.stdout());877});878879// See: https://github.com/BurntSushi/ripgrep/issues/1414880rgtest!(f1414_no_require_git, |dir: Dir, mut cmd: TestCommand| {881    dir.create(".gitignore", "foo");882    dir.create("foo", "");883    dir.create("bar", "");884885    let stdout = cmd.args(&["--sort", "path", "--files"]).stdout();886    eqnice!("bar\nfoo\n", stdout);887888    let stdout =889        cmd.args(&["--sort", "path", "--files", "--no-require-git"]).stdout();890    eqnice!("bar\n", stdout);891892    let stdout = cmd893        .args(&[894            "--sort",895            "path",896            "--files",897            "--no-require-git",898            "--require-git",899        ])900        .stdout();901    eqnice!("bar\nfoo\n", stdout);902});903904// See: https://github.com/BurntSushi/ripgrep/pull/1420905rgtest!(f1420_no_ignore_exclude, |dir: Dir, mut cmd: TestCommand| {906    dir.create_dir(".git/info");907    dir.create(".git/info/exclude", "foo");908    dir.create("bar", "");909    dir.create("foo", "");910911    cmd.arg("--sort").arg("path").arg("--files");912    eqnice!("bar\n", cmd.stdout());913    eqnice!("bar\nfoo\n", cmd.arg("--no-ignore-exclude").stdout());914});915916// See: https://github.com/BurntSushi/ripgrep/pull/1466917rgtest!(f1466_no_ignore_files, |dir: Dir, mut cmd: TestCommand| {918    dir.create(".myignore", "bar");919    dir.create("bar", "");920    dir.create("foo", "");921922    // Test that --no-ignore-files disables --ignore-file.923    // And that --ignore-files overrides --no-ignore-files.924    cmd.arg("--sort").arg("path").arg("--files");925    eqnice!("bar\nfoo\n", cmd.stdout());926    eqnice!("foo\n", cmd.arg("--ignore-file").arg(".myignore").stdout());927    eqnice!("bar\nfoo\n", cmd.arg("--no-ignore-files").stdout());928    eqnice!("foo\n", cmd.arg("--ignore-files").stdout());929930    // Test that the -u flag does not disable --ignore-file.931    let mut cmd = dir.command();932    cmd.arg("--sort").arg("path").arg("--files");933    cmd.arg("--ignore-file").arg(".myignore");934    eqnice!("foo\n", cmd.stdout());935    eqnice!("foo\n", cmd.arg("-u").stdout());936});937938// See: https://github.com/BurntSushi/ripgrep/pull/2361939rgtest!(f2361_sort_nested_files, |dir: Dir, mut cmd: TestCommand| {940    use std::{thread::sleep, time::Duration};941942    if crate::util::is_cross() {943        return;944    }945    dir.create("foo", "1");946    sleep(Duration::from_millis(200));947    dir.create_dir("dir");948    sleep(Duration::from_millis(200));949    dir.create(dir.path().join("dir").join("bar"), "1");950951    cmd.arg("--sort").arg("accessed").arg("--files");952    eqnice!("foo\ndir/bar\n", cmd.stdout());953954    dir.create("foo", "2");955    sleep(Duration::from_millis(200));956    dir.create(dir.path().join("dir").join("bar"), "2");957    sleep(Duration::from_millis(200));958959    cmd.arg("--sort").arg("accessed").arg("--files");960    eqnice!("foo\ndir/bar\n", cmd.stdout());961});962963// See: https://github.com/BurntSushi/ripgrep/issues/1404964rgtest!(f1404_nothing_searched_warning, |dir: Dir, mut cmd: TestCommand| {965    dir.create(".ignore", "ignored-dir/**");966    dir.create_dir("ignored-dir");967    dir.create("ignored-dir/foo", "needle");968969    // Test that, if ripgrep searches only ignored folders/files, then there970    // is a non-zero exit code.971    cmd.arg("needle");972    cmd.assert_err();973974    // Test that we actually get an error message that we expect.975    let output = cmd.raw_output();976    let stderr = String::from_utf8_lossy(&output.stderr);977    let expected = "\978        rg: No files were searched, which means ripgrep probably applied \979        a filter you didn't expect.\n\980        Running with --debug will show why files are being skipped.\n\981    ";982    eqnice!(expected, stderr);983});984985// See: https://github.com/BurntSushi/ripgrep/issues/1404986rgtest!(f1404_nothing_searched_ignored, |dir: Dir, mut cmd: TestCommand| {987    dir.create(".ignore", "ignored-dir/**");988    dir.create_dir("ignored-dir");989    dir.create("ignored-dir/foo", "needle");990991    // Test that, if ripgrep searches only ignored folders/files, then there992    // is a non-zero exit code.993    cmd.arg("--no-messages").arg("needle");994    cmd.assert_err();995996    // But since --no-messages is given, there should not be any error message997    // printed.998    let output = cmd.raw_output();999    let stderr = String::from_utf8_lossy(&output.stderr);1000    let expected = "";1001    eqnice!(expected, stderr);1002});10031004// See: https://github.com/BurntSushi/ripgrep/issues/18421005rgtest!(f1842_field_context_separator, |dir: Dir, _: TestCommand| {1006    dir.create("sherlock", SHERLOCK);10071008    // Test the default.1009    let base = &["-n", "-A1", "Doctor Watsons", "sherlock"];1010    let expected = "\10111:For the Doctor Watsons of this world, as opposed to the Sherlock10122-Holmeses, success in the province of detective work must always1013";1014    eqnice!(expected, dir.command().args(base).stdout());10151016    // Test that it can be overridden.1017    let mut args = vec!["--field-context-separator", "!"];1018    args.extend(base);1019    let expected = "\10201:For the Doctor Watsons of this world, as opposed to the Sherlock10212!Holmeses, success in the province of detective work must always1022";1023    eqnice!(expected, dir.command().args(&args).stdout());10241025    // Test that it can use multiple bytes.1026    let mut args = vec!["--field-context-separator", "!!"];1027    args.extend(base);1028    let expected = "\10291:For the Doctor Watsons of this world, as opposed to the Sherlock10302!!Holmeses, success in the province of detective work must always1031";1032    eqnice!(expected, dir.command().args(&args).stdout());10331034    // Test that unescaping works.1035    let mut args = vec!["--field-context-separator", r"\x7F"];1036    args.extend(base);1037    let expected = "\10381:For the Doctor Watsons of this world, as opposed to the Sherlock10392\x7FHolmeses, success in the province of detective work must always1040";1041    eqnice!(expected, dir.command().args(&args).stdout());10421043    // Test that an empty separator is OK.1044    let mut args = vec!["--field-context-separator", r""];1045    args.extend(base);1046    let expected = "\10471:For the Doctor Watsons of this world, as opposed to the Sherlock10482Holmeses, success in the province of detective work must always1049";1050    eqnice!(expected, dir.command().args(&args).stdout());1051});10521053// See: https://github.com/BurntSushi/ripgrep/issues/18421054rgtest!(f1842_field_match_separator, |dir: Dir, _: TestCommand| {1055    dir.create("sherlock", SHERLOCK);10561057    // Test the default.1058    let base = &["-n", "Doctor Watsons", "sherlock"];1059    let expected = "\10601:For the Doctor Watsons of this world, as opposed to the Sherlock1061";1062    eqnice!(expected, dir.command().args(base).stdout());10631064    // Test that it can be overridden.1065    let mut args = vec!["--field-match-separator", "!"];1066    args.extend(base);1067    let expected = "\10681!For the Doctor Watsons of this world, as opposed to the Sherlock1069";1070    eqnice!(expected, dir.command().args(&args).stdout());10711072    // Test that it can use multiple bytes.1073    let mut args = vec!["--field-match-separator", "!!"];1074    args.extend(base);1075    let expected = "\10761!!For the Doctor Watsons of this world, as opposed to the Sherlock1077";1078    eqnice!(expected, dir.command().args(&args).stdout());10791080    // Test that unescaping works.1081    let mut args = vec!["--field-match-separator", r"\x7F"];1082    args.extend(base);1083    let expected = "\10841\x7FFor the Doctor Watsons of this world, as opposed to the Sherlock1085";1086    eqnice!(expected, dir.command().args(&args).stdout());10871088    // Test that an empty separator is OK.1089    let mut args = vec!["--field-match-separator", r""];1090    args.extend(base);1091    let expected = "\10921For the Doctor Watsons of this world, as opposed to the Sherlock1093";1094    eqnice!(expected, dir.command().args(&args).stdout());1095});10961097// See: https://github.com/BurntSushi/ripgrep/issues/22881098rgtest!(f2288_context_partial_override, |dir: Dir, mut cmd: TestCommand| {1099    dir.create("test", "1\n2\n3\n4\n5\n6\n7\n8\n9\n");1100    cmd.args(&["-C1", "-A2", "5", "test"]);1101    eqnice!("4\n5\n6\n7\n", cmd.stdout());1102});11031104// See: https://github.com/BurntSushi/ripgrep/issues/22881105rgtest!(1106    f2288_context_partial_override_rev,1107    |dir: Dir, mut cmd: TestCommand| {1108        dir.create("test", "1\n2\n3\n4\n5\n6\n7\n8\n9\n");1109        cmd.args(&["-A2", "-C1", "5", "test"]);1110        eqnice!("4\n5\n6\n7\n", cmd.stdout());1111    }1112);11131114rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| {1115    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1116    cmd.args(&["-A1", "--no-context-separator", "foo", "test"]);1117    eqnice!("foo\nctx\nfoo\nctx\n", cmd.stdout());1118});11191120rgtest!(no_context_sep_overrides, |dir: Dir, mut cmd: TestCommand| {1121    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1122    cmd.args(&[1123        "-A1",1124        "--context-separator",1125        "AAA",1126        "--no-context-separator",1127        "foo",1128        "test",1129    ]);1130    eqnice!("foo\nctx\nfoo\nctx\n", cmd.stdout());1131});11321133rgtest!(no_context_sep_overridden, |dir: Dir, mut cmd: TestCommand| {1134    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1135    cmd.args(&[1136        "-A1",1137        "--no-context-separator",1138        "--context-separator",1139        "AAA",1140        "foo",1141        "test",1142    ]);1143    eqnice!("foo\nctx\nAAA\nfoo\nctx\n", cmd.stdout());1144});11451146rgtest!(context_sep, |dir: Dir, mut cmd: TestCommand| {1147    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1148    cmd.args(&["-A1", "--context-separator", "AAA", "foo", "test"]);1149    eqnice!("foo\nctx\nAAA\nfoo\nctx\n", cmd.stdout());1150});11511152rgtest!(context_sep_default, |dir: Dir, mut cmd: TestCommand| {1153    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1154    cmd.args(&["-A1", "foo", "test"]);1155    eqnice!("foo\nctx\n--\nfoo\nctx\n", cmd.stdout());1156});11571158rgtest!(context_sep_empty, |dir: Dir, mut cmd: TestCommand| {1159    dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");1160    cmd.args(&["-A1", "--context-separator", "", "foo", "test"]);1161    eqnice!("foo\nctx\n\nfoo\nctx\n", cmd.stdout());1162});11631164rgtest!(no_unicode, |dir: Dir, mut cmd: TestCommand| {1165    dir.create("test", "δ");1166    cmd.arg("-i").arg("--no-unicode").arg("Δ").assert_err();1167});11681169// See: https://github.com/BurntSushi/ripgrep/issues/17901170rgtest!(stop_on_nonmatch, |dir: Dir, mut cmd: TestCommand| {1171    dir.create("test", "line1\nline2\nline3\nline4\nline5");1172    cmd.args(&["--stop-on-nonmatch", "[235]"]);1173    eqnice!("test:line2\ntest:line3\n", cmd.stdout());1174});