Skip to content
Myr

misc.rs

This file

Path

tests/misc.rs

1,198 lines · 37.7 KiB · Rust

Last changed by

ignore: add multi-root parent matcher regression testsNipan Dasbefore adoption653d7f5

History of this file

Take it

1use crate::hay::SHERLOCK;2use crate::util::{Dir, TestCommand, cmd_exists, sort_lines};34// This file contains "miscellaneous" tests that were either written before5// features were tracked more explicitly, or were simply written without6// linking them to a specific issue number. We should try to minimize the7// addition of more tests in this file and instead add them to either the8// regression test suite or the feature test suite (found in regression.rs and9// feature.rs, respectively).1011rgtest!(single_file, |dir: Dir, mut cmd: TestCommand| {12    dir.create("sherlock", SHERLOCK);1314    let expected = "\15For the Doctor Watsons of this world, as opposed to the Sherlock16be, to a very large extent, the result of luck. Sherlock Holmes17";18    eqnice!(expected, cmd.arg("Sherlock").arg("sherlock").stdout());19});2021rgtest!(dir, |dir: Dir, mut cmd: TestCommand| {22    dir.create("sherlock", SHERLOCK);2324    let expected = "\25sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock26sherlock:be, to a very large extent, the result of luck. Sherlock Holmes27";28    eqnice!(expected, cmd.arg("Sherlock").stdout());29});3031rgtest!(line_numbers, |dir: Dir, mut cmd: TestCommand| {32    dir.create("sherlock", SHERLOCK);3334    let expected = "\351:For the Doctor Watsons of this world, as opposed to the Sherlock363:be, to a very large extent, the result of luck. Sherlock Holmes37";38    eqnice!(expected, cmd.arg("-n").arg("Sherlock").arg("sherlock").stdout());39});4041rgtest!(columns, |dir: Dir, mut cmd: TestCommand| {42    dir.create("sherlock", SHERLOCK);43    cmd.arg("--column").arg("Sherlock").arg("sherlock");4445    let expected = "\461:57:For the Doctor Watsons of this world, as opposed to the Sherlock473:49:be, to a very large extent, the result of luck. Sherlock Holmes48";49    eqnice!(expected, cmd.stdout());50});5152rgtest!(with_filename, |dir: Dir, mut cmd: TestCommand| {53    dir.create("sherlock", SHERLOCK);54    cmd.arg("-H").arg("Sherlock").arg("sherlock");5556    let expected = "\57sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock58sherlock:be, to a very large extent, the result of luck. Sherlock Holmes59";60    eqnice!(expected, cmd.stdout());61});6263rgtest!(with_heading, |dir: Dir, mut cmd: TestCommand| {64    dir.create("sherlock", SHERLOCK);65    cmd.args(&[66        // This forces the issue since --with-filename is disabled by default67        // when searching one file.68        "--with-filename",69        "--heading",70        "Sherlock",71        "sherlock",72    ]);7374    let expected = "\75sherlock76For the Doctor Watsons of this world, as opposed to the Sherlock77be, to a very large extent, the result of luck. Sherlock Holmes78";79    eqnice!(expected, cmd.stdout());80});8182rgtest!(with_heading_default, |dir: Dir, mut cmd: TestCommand| {83    // Search two or more and get --with-filename enabled by default.84    // Use -j1 to get deterministic results.85    dir.create("sherlock", SHERLOCK);86    dir.create("foo", "Sherlock Holmes lives on Baker Street.");87    cmd.arg("-j1").arg("--heading").arg("Sherlock");8889    let expected = "\90foo91Sherlock Holmes lives on Baker Street.9293sherlock94For the Doctor Watsons of this world, as opposed to the Sherlock95be, to a very large extent, the result of luck. Sherlock Holmes96";97    eqnice!(sort_lines(expected), sort_lines(&cmd.stdout()));98});99100rgtest!(inverted, |dir: Dir, mut cmd: TestCommand| {101    dir.create("sherlock", SHERLOCK);102    cmd.arg("-v").arg("Sherlock").arg("sherlock");103104    let expected = "\105Holmeses, success in the province of detective work must always106can extract a clew from a wisp of straw or a flake of cigar ash;107but Doctor Watson has to have it taken out for him and dusted,108and exhibited clearly, with a label attached.109";110    eqnice!(expected, cmd.stdout());111});112113rgtest!(inverted_line_numbers, |dir: Dir, mut cmd: TestCommand| {114    dir.create("sherlock", SHERLOCK);115    cmd.arg("-n").arg("-v").arg("Sherlock").arg("sherlock");116117    let expected = "\1182:Holmeses, success in the province of detective work must always1194:can extract a clew from a wisp of straw or a flake of cigar ash;1205:but Doctor Watson has to have it taken out for him and dusted,1216:and exhibited clearly, with a label attached.122";123    eqnice!(expected, cmd.stdout());124});125126rgtest!(case_insensitive, |dir: Dir, mut cmd: TestCommand| {127    dir.create("sherlock", SHERLOCK);128    cmd.arg("-i").arg("sherlock").arg("sherlock");129130    let expected = "\131For the Doctor Watsons of this world, as opposed to the Sherlock132be, to a very large extent, the result of luck. Sherlock Holmes133";134    eqnice!(expected, cmd.stdout());135});136137rgtest!(word, |dir: Dir, mut cmd: TestCommand| {138    dir.create("sherlock", SHERLOCK);139    cmd.arg("-w").arg("as").arg("sherlock");140141    let expected = "\142For the Doctor Watsons of this world, as opposed to the Sherlock143";144    eqnice!(expected, cmd.stdout());145});146147rgtest!(word_period, |dir: Dir, mut cmd: TestCommand| {148    dir.create("haystack", "...");149    cmd.arg("-ow").arg(".").arg("haystack");150151    let expected = "\152.153.154.155";156    eqnice!(expected, cmd.stdout());157});158159rgtest!(line, |dir: Dir, mut cmd: TestCommand| {160    dir.create("sherlock", SHERLOCK);161    cmd.args(&[162        "-x",163        "Watson|and exhibited clearly, with a label attached.",164        "sherlock",165    ]);166167    let expected = "\168and exhibited clearly, with a label attached.169";170    eqnice!(expected, cmd.stdout());171});172173rgtest!(literal, |dir: Dir, mut cmd: TestCommand| {174    dir.create("sherlock", SHERLOCK);175    dir.create("file", "blib\n()\nblab\n");176    cmd.arg("-F").arg("()").arg("file");177178    eqnice!("()\n", cmd.stdout());179});180181rgtest!(quiet, |dir: Dir, mut cmd: TestCommand| {182    dir.create("sherlock", SHERLOCK);183    cmd.arg("-q").arg("Sherlock").arg("sherlock");184185    assert!(cmd.stdout().is_empty());186});187188rgtest!(replace, |dir: Dir, mut cmd: TestCommand| {189    dir.create("sherlock", SHERLOCK);190    cmd.arg("-r").arg("FooBar").arg("Sherlock").arg("sherlock");191192    let expected = "\193For the Doctor Watsons of this world, as opposed to the FooBar194be, to a very large extent, the result of luck. FooBar Holmes195";196    eqnice!(expected, cmd.stdout());197});198199rgtest!(replace_groups, |dir: Dir, mut cmd: TestCommand| {200    dir.create("sherlock", SHERLOCK);201    cmd.args(&["-r", "$2, $1", "([A-Z][a-z]+) ([A-Z][a-z]+)", "sherlock"]);202203    let expected = "\204For the Watsons, Doctor of this world, as opposed to the Sherlock205be, to a very large extent, the result of luck. Holmes, Sherlock206but Watson, Doctor has to have it taken out for him and dusted,207";208    eqnice!(expected, cmd.stdout());209});210211rgtest!(replace_named_groups, |dir: Dir, mut cmd: TestCommand| {212    dir.create("sherlock", SHERLOCK);213    cmd.args(&[214        "-r",215        "$last, $first",216        "(?P<first>[A-Z][a-z]+) (?P<last>[A-Z][a-z]+)",217        "sherlock",218    ]);219220    let expected = "\221For the Watsons, Doctor of this world, as opposed to the Sherlock222be, to a very large extent, the result of luck. Holmes, Sherlock223but Watson, Doctor has to have it taken out for him and dusted,224";225    eqnice!(expected, cmd.stdout());226});227228rgtest!(replace_with_only_matching, |dir: Dir, mut cmd: TestCommand| {229    dir.create("sherlock", SHERLOCK);230    cmd.arg("-o").arg("-r").arg("$1").arg(r"of (\w+)").arg("sherlock");231232    let expected = "\233this234detective235luck236straw237cigar238";239    eqnice!(expected, cmd.stdout());240});241242rgtest!(file_types, |dir: Dir, mut cmd: TestCommand| {243    dir.create("sherlock", SHERLOCK);244    dir.create("file.py", "Sherlock");245    dir.create("file.rs", "Sherlock");246    cmd.arg("-t").arg("rust").arg("Sherlock");247248    eqnice!("file.rs:Sherlock\n", cmd.stdout());249});250251rgtest!(file_types_all, |dir: Dir, mut cmd: TestCommand| {252    dir.create("sherlock", SHERLOCK);253    dir.create("file.py", "Sherlock");254    cmd.arg("-t").arg("all").arg("Sherlock");255256    eqnice!("file.py:Sherlock\n", cmd.stdout());257});258259rgtest!(file_types_negate, |dir: Dir, mut cmd: TestCommand| {260    dir.create("sherlock", SHERLOCK);261    dir.remove("sherlock");262    dir.create("file.py", "Sherlock");263    dir.create("file.rs", "Sherlock");264    cmd.arg("-T").arg("rust").arg("Sherlock");265266    eqnice!("file.py:Sherlock\n", cmd.stdout());267});268269rgtest!(file_types_negate_all, |dir: Dir, mut cmd: TestCommand| {270    dir.create("sherlock", SHERLOCK);271    dir.create("file.py", "Sherlock");272    cmd.arg("-T").arg("all").arg("Sherlock");273274    let expected = "\275sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock276sherlock:be, to a very large extent, the result of luck. Sherlock Holmes277";278    eqnice!(expected, cmd.stdout());279});280281rgtest!(file_type_clear, |dir: Dir, mut cmd: TestCommand| {282    dir.create("sherlock", SHERLOCK);283    dir.create("file.py", "Sherlock");284    dir.create("file.rs", "Sherlock");285    cmd.arg("--type-clear").arg("rust").arg("-t").arg("rust").arg("Sherlock");286287    cmd.assert_non_empty_stderr();288});289290rgtest!(file_type_add, |dir: Dir, mut cmd: TestCommand| {291    dir.create("sherlock", SHERLOCK);292    dir.create("file.py", "Sherlock");293    dir.create("file.rs", "Sherlock");294    dir.create("file.wat", "Sherlock");295    cmd.args(&["--type-add", "wat:*.wat", "-t", "wat", "Sherlock"]);296297    eqnice!("file.wat:Sherlock\n", cmd.stdout());298});299300rgtest!(file_type_add_compose, |dir: Dir, mut cmd: TestCommand| {301    dir.create("sherlock", SHERLOCK);302    dir.create("file.py", "Sherlock");303    dir.create("file.rs", "Sherlock");304    dir.create("file.wat", "Sherlock");305    cmd.args(&[306        "--type-add",307        "wat:*.wat",308        "--type-add",309        "combo:include:wat,py",310        "-t",311        "combo",312        "Sherlock",313    ]);314315    let expected = "\316file.py:Sherlock317file.wat:Sherlock318";319    eqnice!(expected, sort_lines(&cmd.stdout()));320});321322rgtest!(glob, |dir: Dir, mut cmd: TestCommand| {323    dir.create("sherlock", SHERLOCK);324    dir.create("file.py", "Sherlock");325    dir.create("file.rs", "Sherlock");326    cmd.arg("-g").arg("*.rs").arg("Sherlock");327328    eqnice!("file.rs:Sherlock\n", cmd.stdout());329});330331rgtest!(glob_negate, |dir: Dir, mut cmd: TestCommand| {332    dir.create("sherlock", SHERLOCK);333    dir.remove("sherlock");334    dir.create("file.py", "Sherlock");335    dir.create("file.rs", "Sherlock");336    cmd.arg("-g").arg("!*.rs").arg("Sherlock");337338    eqnice!("file.py:Sherlock\n", cmd.stdout());339});340341rgtest!(glob_case_insensitive, |dir: Dir, mut cmd: TestCommand| {342    dir.create("sherlock", SHERLOCK);343    dir.create("file.HTML", "Sherlock");344    cmd.arg("--iglob").arg("*.html").arg("Sherlock");345346    eqnice!("file.HTML:Sherlock\n", cmd.stdout());347});348349rgtest!(glob_case_sensitive, |dir: Dir, mut cmd: TestCommand| {350    dir.create("sherlock", SHERLOCK);351    dir.create("file1.HTML", "Sherlock");352    dir.create("file2.html", "Sherlock");353    cmd.arg("--glob").arg("*.html").arg("Sherlock");354355    eqnice!("file2.html:Sherlock\n", cmd.stdout());356});357358rgtest!(glob_always_case_insensitive, |dir: Dir, mut cmd: TestCommand| {359    dir.create("sherlock", SHERLOCK);360    dir.create("file.HTML", "Sherlock");361    cmd.args(&["--glob-case-insensitive", "--glob", "*.html", "Sherlock"]);362363    eqnice!("file.HTML:Sherlock\n", cmd.stdout());364});365366rgtest!(byte_offset_only_matching, |dir: Dir, mut cmd: TestCommand| {367    dir.create("sherlock", SHERLOCK);368    cmd.arg("-b").arg("-o").arg("Sherlock");369370    let expected = "\371sherlock:56:Sherlock372sherlock:177:Sherlock373";374    eqnice!(expected, cmd.stdout());375});376377rgtest!(count, |dir: Dir, mut cmd: TestCommand| {378    dir.create("sherlock", SHERLOCK);379    cmd.arg("--count").arg("Sherlock");380381    let expected = "sherlock:2\n";382    eqnice!(expected, cmd.stdout());383});384385rgtest!(count_matches, |dir: Dir, mut cmd: TestCommand| {386    dir.create("sherlock", SHERLOCK);387    cmd.arg("--count-matches").arg("the");388389    let expected = "sherlock:4\n";390    eqnice!(expected, cmd.stdout());391});392393rgtest!(count_matches_inverted, |dir: Dir, mut cmd: TestCommand| {394    dir.create("sherlock", SHERLOCK);395    cmd.arg("--count-matches").arg("--invert-match").arg("Sherlock");396397    let expected = "sherlock:4\n";398    eqnice!(expected, cmd.stdout());399});400401rgtest!(count_matches_via_only, |dir: Dir, mut cmd: TestCommand| {402    dir.create("sherlock", SHERLOCK);403    cmd.arg("--count").arg("--only-matching").arg("the");404405    let expected = "sherlock:4\n";406    eqnice!(expected, cmd.stdout());407});408409rgtest!(include_zero, |dir: Dir, mut cmd: TestCommand| {410    dir.create("sherlock", SHERLOCK);411    cmd.args(&["--count", "--include-zero", "nada"]);412    cmd.assert_err();413414    let output = cmd.raw_output();415    let stdout = String::from_utf8_lossy(&output.stdout);416    let expected = "sherlock:0\n";417418    eqnice!(expected, stdout);419});420421rgtest!(include_zero_override, |dir: Dir, mut cmd: TestCommand| {422    dir.create("sherlock", SHERLOCK);423    cmd.args(&["--count", "--include-zero", "--no-include-zero", "nada"]);424    cmd.assert_err();425426    let output = cmd.raw_output();427    let stdout = String::from_utf8_lossy(&output.stdout);428    assert!(stdout.is_empty());429});430431rgtest!(files_with_matches, |dir: Dir, mut cmd: TestCommand| {432    dir.create("sherlock", SHERLOCK);433    cmd.arg("--files-with-matches").arg("Sherlock");434435    let expected = "sherlock\n";436    eqnice!(expected, cmd.stdout());437});438439rgtest!(files_without_match, |dir: Dir, mut cmd: TestCommand| {440    dir.create("sherlock", SHERLOCK);441    dir.create("file.py", "foo");442    cmd.arg("--files-without-match").arg("Sherlock");443444    let expected = "file.py\n";445    eqnice!(expected, cmd.stdout());446});447448rgtest!(after_context, |dir: Dir, mut cmd: TestCommand| {449    dir.create("sherlock", SHERLOCK);450    cmd.arg("-A").arg("1").arg("Sherlock").arg("sherlock");451452    let expected = "\453For the Doctor Watsons of this world, as opposed to the Sherlock454Holmeses, success in the province of detective work must always455be, to a very large extent, the result of luck. Sherlock Holmes456can extract a clew from a wisp of straw or a flake of cigar ash;457";458    eqnice!(expected, cmd.stdout());459});460461rgtest!(after_context_line_numbers, |dir: Dir, mut cmd: TestCommand| {462    dir.create("sherlock", SHERLOCK);463    cmd.arg("-A").arg("1").arg("-n").arg("Sherlock").arg("sherlock");464465    let expected = "\4661:For the Doctor Watsons of this world, as opposed to the Sherlock4672-Holmeses, success in the province of detective work must always4683:be, to a very large extent, the result of luck. Sherlock Holmes4694-can extract a clew from a wisp of straw or a flake of cigar ash;470";471    eqnice!(expected, cmd.stdout());472});473474rgtest!(before_context, |dir: Dir, mut cmd: TestCommand| {475    dir.create("sherlock", SHERLOCK);476    cmd.arg("-B").arg("1").arg("Sherlock").arg("sherlock");477478    let expected = "\479For the Doctor Watsons of this world, as opposed to the Sherlock480Holmeses, success in the province of detective work must always481be, to a very large extent, the result of luck. Sherlock Holmes482";483    eqnice!(expected, cmd.stdout());484});485486rgtest!(before_context_line_numbers, |dir: Dir, mut cmd: TestCommand| {487    dir.create("sherlock", SHERLOCK);488    cmd.arg("-B").arg("1").arg("-n").arg("Sherlock").arg("sherlock");489490    let expected = "\4911:For the Doctor Watsons of this world, as opposed to the Sherlock4922-Holmeses, success in the province of detective work must always4933:be, to a very large extent, the result of luck. Sherlock Holmes494";495    eqnice!(expected, cmd.stdout());496});497498rgtest!(context, |dir: Dir, mut cmd: TestCommand| {499    dir.create("sherlock", SHERLOCK);500    cmd.arg("-C").arg("1").arg("world|attached").arg("sherlock");501502    let expected = "\503For the Doctor Watsons of this world, as opposed to the Sherlock504Holmeses, success in the province of detective work must always505--506but Doctor Watson has to have it taken out for him and dusted,507and exhibited clearly, with a label attached.508";509    eqnice!(expected, cmd.stdout());510});511512rgtest!(context_line_numbers, |dir: Dir, mut cmd: TestCommand| {513    dir.create("sherlock", SHERLOCK);514    cmd.arg("-C").arg("1").arg("-n").arg("world|attached").arg("sherlock");515516    let expected = "\5171:For the Doctor Watsons of this world, as opposed to the Sherlock5182-Holmeses, success in the province of detective work must always519--5205-but Doctor Watson has to have it taken out for him and dusted,5216:and exhibited clearly, with a label attached.522";523    eqnice!(expected, cmd.stdout());524});525526rgtest!(max_filesize_parse_error_length, |_: Dir, mut cmd: TestCommand| {527    cmd.arg("--max-filesize").arg("44444444444444444444");528    cmd.assert_non_empty_stderr();529});530531rgtest!(max_filesize_parse_error_suffix, |_: Dir, mut cmd: TestCommand| {532    cmd.arg("--max-filesize").arg("45k");533    cmd.assert_non_empty_stderr();534});535536rgtest!(max_filesize_parse_no_suffix, |dir: Dir, mut cmd: TestCommand| {537    dir.create_size("foo", 40);538    dir.create_size("bar", 60);539    cmd.arg("--max-filesize").arg("50").arg("--files");540541    eqnice!("foo\n", cmd.stdout());542});543544rgtest!(max_filesize_parse_k_suffix, |dir: Dir, mut cmd: TestCommand| {545    dir.create_size("foo", 3048);546    dir.create_size("bar", 4100);547    cmd.arg("--max-filesize").arg("4K").arg("--files");548549    eqnice!("foo\n", cmd.stdout());550});551552rgtest!(max_filesize_parse_m_suffix, |dir: Dir, mut cmd: TestCommand| {553    dir.create_size("foo", 1000000);554    dir.create_size("bar", 1400000);555    cmd.arg("--max-filesize").arg("1M").arg("--files");556557    eqnice!("foo\n", cmd.stdout());558});559560rgtest!(max_filesize_suffix_overflow, |dir: Dir, mut cmd: TestCommand| {561    dir.create_size("foo", 1000000);562563    // 2^35 * 2^30 would otherwise overflow564    cmd.arg("--max-filesize").arg("34359738368G").arg("--files");565    cmd.assert_non_empty_stderr();566});567568rgtest!(ignore_hidden, |dir: Dir, mut cmd: TestCommand| {569    dir.create(".sherlock", SHERLOCK);570    cmd.arg("Sherlock").assert_err();571});572573rgtest!(no_ignore_hidden, |dir: Dir, mut cmd: TestCommand| {574    dir.create(".sherlock", SHERLOCK);575    cmd.arg("--hidden").arg("Sherlock");576577    let expected = "\578.sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock579.sherlock:be, to a very large extent, the result of luck. Sherlock Holmes580";581    eqnice!(expected, cmd.stdout());582});583584rgtest!(ignore_git, |dir: Dir, mut cmd: TestCommand| {585    dir.create("sherlock", SHERLOCK);586    dir.create_dir(".git");587    dir.create(".gitignore", "sherlock\n");588    cmd.arg("Sherlock");589590    cmd.assert_err();591});592593rgtest!(ignore_generic, |dir: Dir, mut cmd: TestCommand| {594    dir.create("sherlock", SHERLOCK);595    dir.create(".ignore", "sherlock\n");596    cmd.arg("Sherlock");597598    cmd.assert_err();599});600601rgtest!(ignore_ripgrep, |dir: Dir, mut cmd: TestCommand| {602    dir.create("sherlock", SHERLOCK);603    dir.create(".rgignore", "sherlock\n");604    cmd.arg("Sherlock");605606    cmd.assert_err();607});608609rgtest!(no_ignore, |dir: Dir, mut cmd: TestCommand| {610    dir.create("sherlock", SHERLOCK);611    dir.create(".gitignore", "sherlock\n");612    cmd.arg("--no-ignore").arg("Sherlock");613614    let expected = "\615sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock616sherlock:be, to a very large extent, the result of luck. Sherlock Holmes617";618    eqnice!(expected, cmd.stdout());619});620621rgtest!(ignore_git_parent, |dir: Dir, mut cmd: TestCommand| {622    dir.create_dir(".git");623    dir.create(".gitignore", "sherlock\n");624    dir.create_dir("foo");625    dir.create("foo/sherlock", SHERLOCK);626    cmd.arg("Sherlock");627628    // Even though we search in foo/, which has no .gitignore, ripgrep will629    // traverse parent directories and respect the gitignore files found.630    cmd.current_dir("foo");631    cmd.assert_err();632});633634rgtest!(ignore_git_parent_stop, |dir: Dir, mut cmd: TestCommand| {635    // This tests that searching parent directories for .gitignore files stops636    // after it sees a .git directory. To test this, we create this directory637    // hierarchy:638    //639    // .gitignore (contains `sherlock`)640    // foo/641    //   .git/642    //   bar/643    //      sherlock644    //645    // And we perform the search inside `foo/bar/`. ripgrep will stop looking646    // for .gitignore files after it sees `foo/.git/`, and therefore not647    // respect the top-level `.gitignore` containing `sherlock`.648    dir.create(".gitignore", "sherlock\n");649    dir.create_dir("foo");650    dir.create_dir("foo/.git");651    dir.create_dir("foo/bar");652    dir.create("foo/bar/sherlock", SHERLOCK);653    cmd.arg("Sherlock");654    cmd.current_dir("foo/bar");655656    let expected = "\657sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock658sherlock:be, to a very large extent, the result of luck. Sherlock Holmes659";660    eqnice!(expected, cmd.stdout());661});662663// Like ignore_git_parent_stop, but with a .git file instead of a .git664// directory.665rgtest!(ignore_git_parent_stop_file, |dir: Dir, mut cmd: TestCommand| {666    // This tests that searching parent directories for .gitignore files stops667    // after it sees a .git *file*. A .git file is used for submodules. To test668    // this, we create this directory hierarchy:669    //670    // .gitignore (contains `sherlock`)671    // foo/672    //   .git673    //   bar/674    //      sherlock675    //676    // And we perform the search inside `foo/bar/`. ripgrep will stop looking677    // for .gitignore files after it sees `foo/.git`, and therefore not678    // respect the top-level `.gitignore` containing `sherlock`.679    dir.create(".gitignore", "sherlock\n");680    dir.create_dir("foo");681    dir.create("foo/.git", "");682    dir.create_dir("foo/bar");683    dir.create("foo/bar/sherlock", SHERLOCK);684    cmd.arg("Sherlock");685    cmd.current_dir("foo/bar");686687    let expected = "\688sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock689sherlock:be, to a very large extent, the result of luck. Sherlock Holmes690";691    eqnice!(expected, cmd.stdout());692});693694rgtest!(ignore_ripgrep_parent_no_stop, |dir: Dir, mut cmd: TestCommand| {695    // This is like the `ignore_git_parent_stop` test, except it checks that696    // ripgrep *doesn't* stop checking for .rgignore files.697    dir.create(".rgignore", "sherlock\n");698    dir.create_dir("foo");699    dir.create_dir("foo/.git");700    dir.create_dir("foo/bar");701    dir.create("foo/bar/sherlock", SHERLOCK);702    cmd.arg("Sherlock");703    cmd.current_dir("foo/bar");704705    // The top-level .rgignore applies.706    cmd.assert_err();707});708709rgtest!(no_parent_ignore_git, |dir: Dir, mut cmd: TestCommand| {710    // Set up a directory hierarchy like this:711    //712    // .git/713    // .gitignore714    // foo/715    //   .gitignore716    //   sherlock717    //   watson718    //719    // Where `.gitignore` contains `sherlock` and `foo/.gitignore` contains720    // `watson`.721    //722    // Now *do the search* from the foo directory. By default, ripgrep will723    // search parent directories for .gitignore files. The --no-ignore-parent724    // flag should prevent that. At the same time, the `foo/.gitignore` file725    // will still be respected (since the search is happening in `foo/`).726    //727    // In other words, we should only see results from `sherlock`, not from728    // `watson`.729    dir.create_dir(".git");730    dir.create(".gitignore", "sherlock\n");731    dir.create_dir("foo");732    dir.create("foo/.gitignore", "watson\n");733    dir.create("foo/sherlock", SHERLOCK);734    dir.create("foo/watson", SHERLOCK);735    cmd.arg("--no-ignore-parent").arg("Sherlock");736    cmd.current_dir("foo");737738    let expected = "\739sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock740sherlock:be, to a very large extent, the result of luck. Sherlock Holmes741";742    eqnice!(expected, cmd.stdout());743});744745// Multiple explicit search roots must apply parent .gitignore rules using each746// root's own path base. Searching `src` before `tests` used to leak the wrong747// absolute base into cached parent matchers so `src/invalid` was not ignored.748// See: https://github.com/BurntSushi/ripgrep/issues/3376749rgtest!(ignore_git_multi_root_order, |dir: Dir, mut cmd: TestCommand| {750    dir.create_dir(".git");751    dir.create(".gitignore", "src/invalid\n");752    dir.create_dir("src");753    dir.create_dir("tests");754    dir.create("src/invalid", "this\n");755    dir.create("src/valid", "this\n");756    dir.create("tests/valid", "this\n");757758    cmd.args(&["--files-with-matches", "this", "src", "tests"]);759    let got = cmd.stdout();760    assert!(761        !got.contains("invalid"),762        "src/invalid must stay ignored with multiple roots, got:\n{}",763        got764    );765    assert!(got.contains("src/valid"), "missing src/valid in:\n{}", got);766    assert!(got.contains("tests/valid"), "missing tests/valid in:\n{}", got);767768    // Reverse CLI order should behave the same.769    let mut cmd = dir.command();770    cmd.args(&["--files-with-matches", "this", "tests", "src"]);771    let got = cmd.stdout();772    assert!(773        !got.contains("invalid"),774        "src/invalid must stay ignored with roots reversed, got:\n{}",775        got776    );777});778779// Same multi-root path-base issue for `.rgignore`.780// See: https://github.com/BurntSushi/ripgrep/issues/3320781rgtest!(ignore_rgignore_multi_root_order, |dir: Dir, mut cmd: TestCommand| {782    dir.create(".rgignore", "beta/**/*.svg\n");783    dir.create_dir("alpha");784    dir.create_dir("beta");785    dir.create("alpha/a.txt", "AWS\n");786    dir.create("beta/x.svg", "AWS\n");787788    cmd.args(&["--files-with-matches", "AWS", "alpha", "beta"]);789    let got = cmd.stdout();790    assert!(791        !got.contains("x.svg"),792        "beta/x.svg must be ignored with multiple roots, got:\n{}",793        got794    );795    assert!(got.contains("alpha/a.txt"), "missing alpha/a.txt in:\n{}", got);796797    let mut cmd = dir.command();798    cmd.args(&["--files-with-matches", "AWS", "beta", "alpha"]);799    let got = cmd.stdout();800    assert!(801        !got.contains("x.svg"),802        "beta/x.svg must be ignored with roots reversed, got:\n{}",803        got804    );805});806807rgtest!(symlink_nofollow, |dir: Dir, mut cmd: TestCommand| {808    dir.create_dir("foo");809    dir.create_dir("foo/bar");810    dir.link_dir("foo/baz", "foo/bar/baz");811    dir.create_dir("foo/baz");812    dir.create("foo/baz/sherlock", SHERLOCK);813    cmd.arg("Sherlock");814    cmd.current_dir("foo/bar");815816    cmd.assert_err();817});818819#[cfg(not(windows))]820rgtest!(symlink_follow, |dir: Dir, mut cmd: TestCommand| {821    dir.create_dir("foo");822    dir.create_dir("foo/bar");823    dir.create_dir("foo/baz");824    dir.create("foo/baz/sherlock", SHERLOCK);825    dir.link_dir("foo/baz", "foo/bar/baz");826    cmd.arg("-L").arg("Sherlock");827    cmd.current_dir("foo/bar");828829    let expected = "\830baz/sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock831baz/sherlock:be, to a very large extent, the result of luck. Sherlock Holmes832";833    eqnice!(expected, cmd.stdout());834});835836rgtest!(unrestricted1, |dir: Dir, mut cmd: TestCommand| {837    dir.create("sherlock", SHERLOCK);838    dir.create(".gitignore", "sherlock\n");839    cmd.arg("-u").arg("Sherlock");840841    let expected = "\842sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock843sherlock:be, to a very large extent, the result of luck. Sherlock Holmes844";845    eqnice!(expected, cmd.stdout());846});847848rgtest!(unrestricted2, |dir: Dir, mut cmd: TestCommand| {849    dir.create(".sherlock", SHERLOCK);850    cmd.arg("-uu").arg("Sherlock");851852    let expected = "\853.sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock854.sherlock:be, to a very large extent, the result of luck. Sherlock Holmes855";856    eqnice!(expected, cmd.stdout());857});858859rgtest!(unrestricted3, |dir: Dir, mut cmd: TestCommand| {860    dir.create("sherlock", SHERLOCK);861    dir.create("hay", "foo\x00bar\nfoo\x00baz\n");862    cmd.arg("-uuu").arg("foo");863864    let expected = "\865hay: binary file matches (found \"\\0\" byte around offset 3)866";867    eqnice!(expected, cmd.stdout());868});869870rgtest!(vimgrep, |dir: Dir, mut cmd: TestCommand| {871    dir.create("sherlock", SHERLOCK);872    cmd.arg("--vimgrep").arg("Sherlock|Watson");873874    let expected = "\875sherlock:1:16:For the Doctor Watsons of this world, as opposed to the Sherlock876sherlock:1:57:For the Doctor Watsons of this world, as opposed to the Sherlock877sherlock:3:49:be, to a very large extent, the result of luck. Sherlock Holmes878sherlock:5:12:but Doctor Watson has to have it taken out for him and dusted,879";880    eqnice!(expected, cmd.stdout());881});882883rgtest!(vimgrep_no_line, |dir: Dir, mut cmd: TestCommand| {884    dir.create("sherlock", SHERLOCK);885    cmd.arg("--vimgrep").arg("-N").arg("Sherlock|Watson");886887    let expected = "\888sherlock:16:For the Doctor Watsons of this world, as opposed to the Sherlock889sherlock:57:For the Doctor Watsons of this world, as opposed to the Sherlock890sherlock:49:be, to a very large extent, the result of luck. Sherlock Holmes891sherlock:12:but Doctor Watson has to have it taken out for him and dusted,892";893    eqnice!(expected, cmd.stdout());894});895896rgtest!(vimgrep_no_line_no_column, |dir: Dir, mut cmd: TestCommand| {897    dir.create("sherlock", SHERLOCK);898    cmd.arg("--vimgrep").arg("-N").arg("--no-column").arg("Sherlock|Watson");899900    let expected = "\901sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock902sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock903sherlock:be, to a very large extent, the result of luck. Sherlock Holmes904sherlock:but Doctor Watson has to have it taken out for him and dusted,905";906    eqnice!(expected, cmd.stdout());907});908909rgtest!(preprocessing, |dir: Dir, mut cmd: TestCommand| {910    if !cmd_exists("xzcat") {911        return;912    }913914    dir.create_bytes("sherlock.xz", include_bytes!("./data/sherlock.xz"));915    cmd.arg("--pre").arg("xzcat").arg("Sherlock").arg("sherlock.xz");916917    let expected = "\918For the Doctor Watsons of this world, as opposed to the Sherlock919be, to a very large extent, the result of luck. Sherlock Holmes920";921    eqnice!(expected, cmd.stdout());922});923924rgtest!(preprocessing_glob, |dir: Dir, mut cmd: TestCommand| {925    if !cmd_exists("xzcat") {926        return;927    }928929    dir.create("sherlock", SHERLOCK);930    dir.create_bytes("sherlock.xz", include_bytes!("./data/sherlock.xz"));931    cmd.args(&["--pre", "xzcat", "--pre-glob", "*.xz", "Sherlock"]);932933    let expected = "\934sherlock.xz:For the Doctor Watsons of this world, as opposed to the Sherlock935sherlock.xz:be, to a very large extent, the result of luck. Sherlock Holmes936sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock937sherlock:be, to a very large extent, the result of luck. Sherlock Holmes938";939    eqnice!(sort_lines(expected), sort_lines(&cmd.stdout()));940});941942rgtest!(compressed_gzip, |dir: Dir, mut cmd: TestCommand| {943    if !cmd_exists("gzip") {944        return;945    }946947    dir.create_bytes("sherlock.gz", include_bytes!("./data/sherlock.gz"));948    cmd.arg("-z").arg("Sherlock").arg("sherlock.gz");949950    let expected = "\951For the Doctor Watsons of this world, as opposed to the Sherlock952be, to a very large extent, the result of luck. Sherlock Holmes953";954    eqnice!(expected, cmd.stdout());955});956957rgtest!(compressed_bzip2, |dir: Dir, mut cmd: TestCommand| {958    if !cmd_exists("bzip2") {959        return;960    }961962    dir.create_bytes("sherlock.bz2", include_bytes!("./data/sherlock.bz2"));963    cmd.arg("-z").arg("Sherlock").arg("sherlock.bz2");964965    let expected = "\966For the Doctor Watsons of this world, as opposed to the Sherlock967be, to a very large extent, the result of luck. Sherlock Holmes968";969    eqnice!(expected, cmd.stdout());970});971972rgtest!(compressed_xz, |dir: Dir, mut cmd: TestCommand| {973    if !cmd_exists("xz") {974        return;975    }976977    dir.create_bytes("sherlock.xz", include_bytes!("./data/sherlock.xz"));978    cmd.arg("-z").arg("Sherlock").arg("sherlock.xz");979980    let expected = "\981For the Doctor Watsons of this world, as opposed to the Sherlock982be, to a very large extent, the result of luck. Sherlock Holmes983";984    eqnice!(expected, cmd.stdout());985});986987rgtest!(compressed_lz4, |dir: Dir, mut cmd: TestCommand| {988    if !cmd_exists("lz4") {989        return;990    }991992    dir.create_bytes("sherlock.lz4", include_bytes!("./data/sherlock.lz4"));993    cmd.arg("-z").arg("Sherlock").arg("sherlock.lz4");994995    let expected = "\996For the Doctor Watsons of this world, as opposed to the Sherlock997be, to a very large extent, the result of luck. Sherlock Holmes998";999    eqnice!(expected, cmd.stdout());1000});10011002rgtest!(compressed_lzma, |dir: Dir, mut cmd: TestCommand| {1003    if !cmd_exists("xz") {1004        return;1005    }10061007    dir.create_bytes("sherlock.lzma", include_bytes!("./data/sherlock.lzma"));1008    cmd.arg("-z").arg("Sherlock").arg("sherlock.lzma");10091010    let expected = "\1011For the Doctor Watsons of this world, as opposed to the Sherlock1012be, to a very large extent, the result of luck. Sherlock Holmes1013";1014    eqnice!(expected, cmd.stdout());1015});10161017rgtest!(compressed_brotli, |dir: Dir, mut cmd: TestCommand| {1018    if !cmd_exists("brotli") {1019        return;1020    }10211022    dir.create_bytes("sherlock.br", include_bytes!("./data/sherlock.br"));1023    cmd.arg("-z").arg("Sherlock").arg("sherlock.br");10241025    let expected = "\1026For the Doctor Watsons of this world, as opposed to the Sherlock1027be, to a very large extent, the result of luck. Sherlock Holmes1028";1029    eqnice!(expected, cmd.stdout());1030});10311032rgtest!(compressed_zstd, |dir: Dir, mut cmd: TestCommand| {1033    if !cmd_exists("zstd") {1034        return;1035    }10361037    dir.create_bytes("sherlock.zst", include_bytes!("./data/sherlock.zst"));1038    cmd.arg("-z").arg("Sherlock").arg("sherlock.zst");10391040    let expected = "\1041For the Doctor Watsons of this world, as opposed to the Sherlock1042be, to a very large extent, the result of luck. Sherlock Holmes1043";1044    eqnice!(expected, cmd.stdout());1045});10461047rgtest!(compressed_uncompress, |dir: Dir, mut cmd: TestCommand| {1048    if !cmd_exists("uncompress") {1049        return;1050    }10511052    dir.create_bytes("sherlock.Z", include_bytes!("./data/sherlock.Z"));1053    cmd.arg("-z").arg("Sherlock").arg("sherlock.Z");10541055    let expected = "\1056    For the Doctor Watsons of this world, as opposed to the Sherlock1057be, to a very large extent, the result of luck. Sherlock Holmes1058";1059    eqnice!(expected, cmd.stdout());1060});10611062rgtest!(compressed_failing_gzip, |dir: Dir, mut cmd: TestCommand| {1063    if !cmd_exists("gzip") {1064        return;1065    }10661067    dir.create("sherlock.gz", SHERLOCK);1068    cmd.arg("-z").arg("Sherlock").arg("sherlock.gz");10691070    cmd.assert_non_empty_stderr();1071});10721073rgtest!(binary_convert, |dir: Dir, mut cmd: TestCommand| {1074    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1075    cmd.arg("--no-mmap").arg("foo").arg("file");10761077    let expected = "\1078binary file matches (found \"\\0\" byte around offset 3)1079";1080    eqnice!(expected, cmd.stdout());1081});10821083rgtest!(binary_convert_mmap, |dir: Dir, mut cmd: TestCommand| {1084    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1085    cmd.arg("--mmap").arg("foo").arg("file");10861087    let expected = "\1088binary file matches (found \"\\0\" byte around offset 3)1089";1090    eqnice!(expected, cmd.stdout());1091});10921093rgtest!(binary_quit, |dir: Dir, mut cmd: TestCommand| {1094    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1095    cmd.arg("--no-mmap").arg("foo").arg("-gfile");1096    cmd.assert_err();1097});10981099rgtest!(binary_quit_mmap, |dir: Dir, mut cmd: TestCommand| {1100    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1101    cmd.arg("--mmap").arg("foo").arg("-gfile");1102    cmd.assert_err();1103});11041105// The following two tests show a discrepancy in search results between1106// searching with memory mapped files and stream searching. Stream searching1107// uses a heuristic (that GNU grep also uses) where NUL bytes are replaced with1108// the EOL terminator, which tends to avoid allocating large amounts of memory1109// for really long "lines." The memory map searcher has no need to worry about1110// such things, and more than that, it would be pretty hard for it to match the1111// semantics of streaming search in this case.1112//1113// Binary files with lots of NULs aren't really part of the use case of ripgrep1114// (or any other grep-like tool for that matter), so we shouldn't feel too bad1115// about it.1116rgtest!(binary_search_mmap, |dir: Dir, mut cmd: TestCommand| {1117    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1118    cmd.arg("-a").arg("--mmap").arg("foo").arg("file");1119    eqnice!("foo\x00bar\nfoo\x00baz\n", cmd.stdout());1120});11211122rgtest!(binary_search_no_mmap, |dir: Dir, mut cmd: TestCommand| {1123    dir.create("file", "foo\x00bar\nfoo\x00baz\n");1124    cmd.arg("-a").arg("--no-mmap").arg("foo").arg("file");1125    eqnice!("foo\x00bar\nfoo\x00baz\n", cmd.stdout());1126});11271128rgtest!(files, |dir: Dir, mut cmd: TestCommand| {1129    dir.create("file", "");1130    dir.create_dir("dir");1131    dir.create("dir/file", "");1132    cmd.arg("--files");11331134    eqnice!(sort_lines("file\ndir/file\n"), sort_lines(&cmd.stdout()));1135});11361137rgtest!(type_list, |_: Dir, mut cmd: TestCommand| {1138    cmd.arg("--type-list");1139    // This can change over time, so just make sure we print something.1140    assert!(!cmd.stdout().is_empty());1141});11421143// The following series of tests seeks to test all permutations of ripgrep's1144// sorted queries.1145//1146// They all rely on this setup function, which sets up this particular file1147// structure with a particular creation order:1148//  ├── a             # 11149//  ├── b             # 41150//  └── dir           # 21151//     ├── c          # 31152//     └── d          # 51153//1154// This order is important when sorting them by system time-stamps.1155fn sort_setup(dir: Dir) {1156    use std::{thread::sleep, time::Duration};1157    // As reported in https://github.com/BurntSushi/ripgrep/issues/30711158    // this test fails if sufficient delay is not given on Windows/Aarch64.1159    let delay = if cfg!(all(windows, target_arch = "aarch64")) {1160        Duration::from_millis(1000)1161    } else {1162        Duration::from_millis(100)1163    };1164    let sub_dir = dir.path().join("dir");1165    dir.create("a", "test");1166    sleep(delay);1167    dir.create_dir(&sub_dir);1168    sleep(delay);1169    dir.create(sub_dir.join("c"), "test");1170    sleep(delay);1171    dir.create("b", "test");1172    sleep(delay);1173    dir.create(sub_dir.join("d"), "test");1174}11751176rgtest!(sort_files, |dir: Dir, mut cmd: TestCommand| {1177    sort_setup(dir);1178    let expected = "a:test\nb:test\ndir/c:test\ndir/d:test\n";1179    eqnice!(expected, cmd.args(["--sort", "path", "test"]).stdout());1180});11811182rgtest!(sort_accessed, |dir: Dir, mut cmd: TestCommand| {1183    if crate::util::is_cross() {1184        return;1185    }1186    sort_setup(dir);1187    let expected = "a:test\ndir/c:test\nb:test\ndir/d:test\n";1188    eqnice!(expected, cmd.args(["--sort", "accessed", "test"]).stdout());1189});11901191rgtest!(sortr_accessed, |dir: Dir, mut cmd: TestCommand| {1192    if crate::util::is_cross() {1193        return;1194    }1195    sort_setup(dir);1196    let expected = "dir/d:test\nb:test\ndir/c:test\na:test\n";1197    eqnice!(expected, cmd.args(["--sortr", "accessed", "test"]).stdout());1198});