Skip to content
Myr

tests.rs

This file

Path

tests/tests.rs

3,139 lines · 81.4 KiB · Rust

Last changed by

Merge pull request #2039 from hexbinoct/fix/min-depth-broken-symlinkThayne McCombsbefore adoptionce97e47

History of this file

Take it

1mod testenv;23#[cfg(unix)]4use nix::unistd::{Gid, Group, Uid, User};5use std::fs;6use std::io::Write;7use std::path::Path;8use std::time::{Duration, SystemTime};9use test_case::test_case;1011use jiff::Timestamp;12use normpath::PathExt;13use regex::escape;1415use crate::testenv::TestEnv;1617static DEFAULT_DIRS: &[&str] = &["one/two/three", "one/two/three/directory_foo"];1819static DEFAULT_FILES: &[&str] = &[20    "a.foo",21    "one/b.foo",22    "one/two/c.foo",23    "one/two/C.Foo2",24    "one/two/three/d.foo",25    "fdignored.foo",26    "gitignored.foo",27    ".hidden.foo",28    "e1 e2",29];3031#[allow(clippy::let_and_return)]32fn get_absolute_root_path(env: &TestEnv) -> String {33    let path = env34        .test_root()35        .normalize()36        .expect("absolute path")37        .as_path()38        .to_str()39        .expect("string")40        .to_string();4142    #[cfg(windows)]43    let path = path.trim_start_matches(r"\\?\").to_string();4445    path46}4748#[cfg(test)]49fn get_test_env_with_abs_path(dirs: &[&'static str], files: &[&'static str]) -> (TestEnv, String) {50    let env = TestEnv::new(dirs, files);51    let root_path = get_absolute_root_path(&env);52    (env, root_path)53}5455#[cfg(test)]56fn create_file_with_size<P: AsRef<Path>>(path: P, size_in_bytes: usize) {57    let content = "#".repeat(size_in_bytes);58    let mut f = fs::File::create::<P>(path).unwrap();59    f.write_all(content.as_bytes()).unwrap();60}6162/// Simple test63#[test]64fn test_simple() {65    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);6667    te.assert_output(&["a.foo"], "a.foo");68    te.assert_output(&["b.foo"], "one/b.foo");69    te.assert_output(&["d.foo"], "one/two/three/d.foo");7071    te.assert_output(72        &["foo"],73        "a.foo74        one/b.foo75        one/two/c.foo76        one/two/C.Foo277        one/two/three/d.foo78        one/two/three/directory_foo/",79    );80}8182static AND_EXTRA_FILES: &[&str] = &[83    "a.foo",84    "one/b.foo",85    "one/two/c.foo",86    "one/two/C.Foo2",87    "one/two/three/baz-quux",88    "one/two/three/Baz-Quux2",89    "one/two/three/d.foo",90    "fdignored.foo",91    "gitignored.foo",92    ".hidden.foo",93    "A-B.jpg",94    "A-C.png",95    "B-A.png",96    "B-C.png",97    "C-A.jpg",98    "C-B.png",99    "e1 e2",100];101102/// AND test103#[test]104fn test_and_basic() {105    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);106107    te.assert_output(108        &["foo", "--and", "c"],109        "one/two/C.Foo2110        one/two/c.foo111        one/two/three/directory_foo/",112    );113114    te.assert_output(115        &["f", "--and", "[ad]", "--and", "[_]"],116        "one/two/three/directory_foo/",117    );118119    te.assert_output(120        &["f", "--and", "[ad]", "--and", "[.]"],121        "a.foo122        one/two/three/d.foo",123    );124125    te.assert_output(&["Foo", "--and", "C"], "one/two/C.Foo2");126127    te.assert_output(&["foo", "--and", "asdasdasdsadasd"], "");128}129130#[test]131fn test_and_empty_pattern() {132    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);133    te.assert_output(&["Foo", "--and", "2", "--and", ""], "one/two/C.Foo2");134}135136#[test]137fn test_and_bad_pattern() {138    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);139140    te.assert_failure(&["Foo", "--and", "2", "--and", "[", "--and", "C"]);141    te.assert_failure(&["Foo", "--and", "[", "--and", "2", "--and", "C"]);142    te.assert_failure(&["Foo", "--and", "2", "--and", "C", "--and", "["]);143    te.assert_failure(&["[", "--and", "2", "--and", "C", "--and", "Foo"]);144}145146#[test]147fn test_and_pattern_starts_with_dash() {148    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);149150    te.assert_output(151        &["baz", "--and", "quux"],152        "one/two/three/Baz-Quux2153        one/two/three/baz-quux",154    );155    te.assert_output(156        &["baz", "--and", "-"],157        "one/two/three/Baz-Quux2158        one/two/three/baz-quux",159    );160    te.assert_output(161        &["Quu", "--and", "x", "--and", "-"],162        "one/two/three/Baz-Quux2",163    );164}165166#[test]167fn test_and_plus_extension() {168    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);169170    te.assert_output(171        &[172            "A",173            "--and",174            "B",175            "--extension",176            "jpg",177            "--extension",178            "png",179        ],180        "A-B.jpg181        B-A.png",182    );183184    te.assert_output(185        &[186            "A",187            "--extension",188            "jpg",189            "--and",190            "B",191            "--extension",192            "png",193        ],194        "A-B.jpg195        B-A.png",196    );197}198199#[test]200fn test_and_plus_type() {201    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);202203    te.assert_output(204        &["c", "--type", "d", "--and", "foo"],205        "one/two/three/directory_foo/",206    );207208    te.assert_output(209        &["c", "--type", "f", "--and", "foo"],210        "one/two/C.Foo2211        one/two/c.foo",212    );213}214215#[test]216fn test_and_plus_glob() {217    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);218219    te.assert_output(&["*foo", "--glob", "--and", "c*"], "one/two/c.foo");220}221222#[test]223fn test_and_plus_fixed_strings() {224    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);225226    te.assert_output(227        &["foo", "--fixed-strings", "--and", "c", "--and", "."],228        "one/two/c.foo229        one/two/C.Foo2",230    );231232    te.assert_output(233        &["foo", "--fixed-strings", "--and", "[c]", "--and", "."],234        "",235    );236237    te.assert_output(238        &["Foo", "--fixed-strings", "--and", "C", "--and", "."],239        "one/two/C.Foo2",240    );241}242243#[test]244fn test_and_plus_ignore_case() {245    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);246247    te.assert_output(248        &["Foo", "--ignore-case", "--and", "C", "--and", "[.]"],249        "one/two/C.Foo2250        one/two/c.foo",251    );252}253254#[test]255fn test_and_plus_case_sensitive() {256    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);257258    te.assert_output(259        &["foo", "--case-sensitive", "--and", "c", "--and", "[.]"],260        "one/two/c.foo",261    );262}263264#[test]265fn test_and_plus_full_path() {266    let te = TestEnv::new(DEFAULT_DIRS, AND_EXTRA_FILES);267268    te.assert_output(269        &[270            "three",271            "--full-path",272            "--and",273            "_foo",274            "--and",275            r"[/\\]dir",276        ],277        "one/two/three/directory_foo/",278    );279280    te.assert_output(281        &[282            "three",283            "--full-path",284            "--and",285            r"[/\\]two",286            "--and",287            r"[/\\]dir",288        ],289        "one/two/three/directory_foo/",290    );291}292293/// Test each pattern type with an empty pattern.294#[test]295fn test_empty_pattern() {296    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);297    let expected = "a.foo298    e1 e2299    one/300    one/b.foo301    one/two/302    one/two/c.foo303    one/two/C.Foo2304    one/two/three/305    one/two/three/d.foo306    one/two/three/directory_foo/307    symlink";308309    te.assert_output(&["--regex"], expected);310    te.assert_output(&["--fixed-strings"], expected);311    te.assert_output(&["--glob"], expected);312}313314/// Test multiple directory searches315#[test]316fn test_multi_file() {317    let dirs = &["test1", "test2"];318    let files = &["test1/a.foo", "test1/b.foo", "test2/a.foo"];319    let te = TestEnv::new(dirs, files);320    te.assert_output(321        &["a.foo", "test1", "test2"],322        "test1/a.foo323        test2/a.foo",324    );325326    te.assert_output(327        &["", "test1", "test2"],328        "test1/a.foo329        test2/a.foo330        test1/b.foo",331    );332333    te.assert_output(&["a.foo", "test1"], "test1/a.foo");334335    te.assert_output(&["b.foo", "test1", "test2"], "test1/b.foo");336}337338/// Test search over multiple directory with missing339#[test]340fn test_multi_file_with_missing() {341    let dirs = &["real"];342    let files = &["real/a.foo", "real/b.foo"];343    let te = TestEnv::new(dirs, files);344    te.assert_output(&["a.foo", "real", "fake"], "real/a.foo");345346    te.assert_error(347        &["a.foo", "real", "fake"],348        "[fd error]: Search path \"fake\" is not a directory.",349    );350351    te.assert_output(352        &["", "real", "fake"],353        "real/a.foo354        real/b.foo",355    );356357    te.assert_output(358        &["", "real", "fake1", "fake2"],359        "real/a.foo360        real/b.foo",361    );362363    te.assert_error(364        &["", "real", "fake1", "fake2"],365        "[fd error]: Search path \"fake1\" is not a directory.366        [fd error]: Search path \"fake2\" is not a directory.",367    );368369    te.assert_failure_with_error(370        &["", "fake1", "fake2"],371        "[fd error]: Search path \"fake1\" is not a directory.372        [fd error]: Search path \"fake2\" is not a directory.373        [fd error]: No valid search paths given.",374    );375}376377/// Without --full-path, a pattern containing '/' should always produce the378/// path-separator diagnostic, even if the pattern does not name an existing379/// directory. Before the fix for sharkdp/fd#1873 this only fired when the380/// pattern happened to resolve to a real directory, so the common typo of381/// pasting a full path silently returned zero matches.382#[test]383fn test_pattern_with_forward_slash_is_rejected() {384    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);385386    // Pattern that is NOT a real directory; old behaviour: no warning.387    te.assert_failure_with_error(388        &["nonexistent/path"],389        "[fd error]: The search pattern 'nonexistent/path' contains a path-separation character and will not lead to any search results.",390    );391392    // Pattern that IS a real directory; old behaviour: warning. Must still fire.393    te.assert_failure_with_error(394        &["one/two/three"],395        "[fd error]: The search pattern 'one/two/three' contains a path-separation character and will not lead to any search results.",396    );397}398399/// --full-path is the user's explicit opt-in to regex-over-full-path matching,400/// so a path-separation character in the pattern is expected and must not401/// trigger the diagnostic.402///403/// Gated off Windows: the actual match is regex-over-the-full-path, so a404/// forward-slash pattern only matches Unix-style paths. On Windows the OS405/// uses `\` and `one/two/c` (as a literal regex) does not match a real406/// entry — the behaviour this test is pinning (the diagnostic does not407/// fire) is covered by the fact that the invocation does not error.408#[test]409#[cfg(not(windows))]410fn test_pattern_with_forward_slash_allowed_with_full_path() {411    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);412413    te.assert_output(414        &["--full-path", "one/two/c"],415        "one/two/c.foo416        one/two/C.Foo2",417    );418}419420/// `--and` patterns are matched against the file name exactly like the primary421/// pattern, so a path separator in any of them is the same silent "no results"422/// footgun and must trigger the same diagnostic. Regression for the sibling of423/// #1873 left unchecked by #1975 (only the positional pattern was validated).424#[test]425fn test_and_pattern_with_forward_slash_is_rejected() {426    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);427428    // Clean primary pattern, but a `--and` pattern carrying a path separator.429    te.assert_failure_with_error(430        &["foo", "--and", "nonexistent/path"],431        "[fd error]: The search pattern 'nonexistent/path' contains a path-separation character and will not lead to any search results.",432    );433434    // `--full-path` is the explicit opt-in and must still suppress the435    // diagnostic for `--and` patterns too (the invocation runs and matches436    // instead of erroring on the path separator).437    #[cfg(not(windows))]438    te.assert_output(439        &["--full-path", "one/two/c", "--and", "two/c"],440        "one/two/c.foo441        one/two/C.Foo2",442    );443}444445/// Explicit root path446#[test]447fn test_explicit_root_path() {448    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);449450    te.assert_output(451        &["foo", "one"],452        "one/b.foo453        one/two/c.foo454        one/two/C.Foo2455        one/two/three/d.foo456        one/two/three/directory_foo/",457    );458459    te.assert_output(460        &["foo", "one/two/three"],461        "one/two/three/d.foo462        one/two/three/directory_foo/",463    );464465    te.assert_output_subdirectory(466        "one/two/",467        &["foo", "../../"],468        "../../a.foo469        ../../one/b.foo470        ../../one/two/c.foo471        ../../one/two/C.Foo2472        ../../one/two/three/d.foo473        ../../one/two/three/directory_foo/",474    );475476    te.assert_output_subdirectory(477        "one/two/three",478        &["", ".."],479        "../c.foo480        ../C.Foo2481        ../three/482        ../three/d.foo483        ../three/directory_foo/",484    );485}486487#[test]488fn test_single_dash_root_path() {489    let te = TestEnv::new(&["-"], &["-/foo"]);490491    te.assert_output(&[".", "-"], "./-/foo");492}493494/// Regex searches495#[test]496fn test_regex_searches() {497    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);498499    te.assert_output(500        &["[a-c].foo"],501        "a.foo502        one/b.foo503        one/two/c.foo504        one/two/C.Foo2",505    );506507    te.assert_output(508        &["--case-sensitive", "[a-c].foo"],509        "a.foo510        one/b.foo511        one/two/c.foo",512    );513}514515/// Smart case516#[test]517fn test_smart_case() {518    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);519520    te.assert_output(521        &["c.foo"],522        "one/two/c.foo523        one/two/C.Foo2",524    );525526    te.assert_output(&["C.Foo"], "one/two/C.Foo2");527528    te.assert_output(&["Foo"], "one/two/C.Foo2");529530    // Only literal uppercase chars should trigger case sensitivity.531    te.assert_output(532        &["\\Ac"],533        "one/two/c.foo534        one/two/C.Foo2",535    );536    te.assert_output(&["\\AC"], "one/two/C.Foo2");537}538539/// Case sensitivity (--case-sensitive)540#[test]541fn test_case_sensitive() {542    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);543544    te.assert_output(&["--case-sensitive", "c.foo"], "one/two/c.foo");545546    te.assert_output(&["--case-sensitive", "C.Foo"], "one/two/C.Foo2");547548    te.assert_output(549        &["--ignore-case", "--case-sensitive", "C.Foo"],550        "one/two/C.Foo2",551    );552}553554/// Case insensitivity (--ignore-case)555#[test]556fn test_case_insensitive() {557    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);558559    te.assert_output(560        &["--ignore-case", "C.Foo"],561        "one/two/c.foo562        one/two/C.Foo2",563    );564565    te.assert_output(566        &["--case-sensitive", "--ignore-case", "C.Foo"],567        "one/two/c.foo568        one/two/C.Foo2",569    );570}571572/// Glob-based searches (--glob)573#[test]574fn test_glob_searches() {575    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);576577    te.assert_output(578        &["--glob", "*.foo"],579        "a.foo580        one/b.foo581        one/two/c.foo582        one/two/three/d.foo",583    );584585    te.assert_output(586        &["--glob", "[a-c].foo"],587        "a.foo588        one/b.foo589        one/two/c.foo",590    );591592    te.assert_output(593        &["--glob", "[a-c].foo*"],594        "a.foo595        one/b.foo596        one/two/C.Foo2597        one/two/c.foo",598    );599}600601/// Glob-based searches (--glob) in combination with full path searches (--full-path)602#[cfg(not(windows))] // TODO: make this work on Windows603#[test]604fn test_full_path_glob_searches() {605    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);606607    te.assert_output(608        &["--glob", "--full-path", "**/one/**/*.foo"],609        "one/b.foo610        one/two/c.foo611        one/two/three/d.foo",612    );613614    te.assert_output(615        &["--glob", "--full-path", "**/one/*/*.foo"],616        " one/two/c.foo",617    );618619    te.assert_output(620        &["--glob", "--full-path", "**/one/*/*/*.foo"],621        " one/two/three/d.foo",622    );623}624625#[test]626fn test_smart_case_glob_searches() {627    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);628629    te.assert_output(630        &["--glob", "c.foo*"],631        "one/two/C.Foo2632        one/two/c.foo",633    );634635    te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");636}637638/// Glob-based searches (--glob) in combination with --case-sensitive639#[test]640fn test_case_sensitive_glob_searches() {641    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);642643    te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");644}645646/// Glob-based searches (--glob) in combination with --extension647#[test]648fn test_glob_searches_with_extension() {649    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);650651    te.assert_output(652        &["--glob", "--extension", "foo2", "[a-z].*"],653        "one/two/C.Foo2",654    );655}656657/// Make sure that --regex overrides --glob658#[test]659fn test_regex_overrides_glob() {660    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);661662    te.assert_output(&["--glob", "--regex", "Foo2$"], "one/two/C.Foo2");663}664665/// Full path search (--full-path)666#[test]667fn test_full_path() {668    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);669670    let root = te.system_root();671    let prefix = escape(&root.to_string_lossy());672673    te.assert_output(674        &["--full-path", &format!("^{prefix}.*three.*foo$")],675        "one/two/three/d.foo676        one/two/three/directory_foo/",677    );678}679680/// Hidden files (--hidden)681#[test]682fn test_hidden() {683    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);684685    te.assert_output(686        &["--hidden", "foo"],687        ".hidden.foo688        a.foo689        one/b.foo690        one/two/c.foo691        one/two/C.Foo2692        one/two/three/d.foo693        one/two/three/directory_foo/",694    );695}696697/// Hidden file attribute on Windows698#[cfg(windows)]699#[test]700fn test_hidden_file_attribute() {701    use std::os::windows::fs::OpenOptionsExt;702703    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);704705    // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa706    const FILE_ATTRIBUTE_HIDDEN: u32 = 2;707708    fs::OpenOptions::new()709        .create(true)710        .write(true)711        .attributes(FILE_ATTRIBUTE_HIDDEN)712        .open(te.test_root().join("hidden-file.txt"))713        .unwrap();714715    te.assert_output(&["--hidden", "hidden-file.txt"], "hidden-file.txt");716    te.assert_output(&["hidden-file.txt"], "");717}718719/// Ignored files (--no-ignore)720#[test]721fn test_no_ignore() {722    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);723724    te.assert_output(725        &["--no-ignore", "foo"],726        "a.foo727        fdignored.foo728        gitignored.foo729        one/b.foo730        one/two/c.foo731        one/two/C.Foo2732        one/two/three/d.foo733        one/two/three/directory_foo/",734    );735736    te.assert_output(737        &["--hidden", "--no-ignore", "foo"],738        ".hidden.foo739        a.foo740        fdignored.foo741        gitignored.foo742        one/b.foo743        one/two/c.foo744        one/two/C.Foo2745        one/two/three/d.foo746        one/two/three/directory_foo/",747    );748}749750/// .gitignore and .fdignore751#[test]752fn test_gitignore_and_fdignore() {753    let files = &[754        "ignored-by-nothing",755        "ignored-by-fdignore",756        "ignored-by-gitignore",757        "ignored-by-both",758    ];759    let te = TestEnv::new(&[], files);760761    fs::File::create(te.test_root().join(".fdignore"))762        .unwrap()763        .write_all(b"ignored-by-fdignore\nignored-by-both")764        .unwrap();765766    fs::File::create(te.test_root().join(".gitignore"))767        .unwrap()768        .write_all(b"ignored-by-gitignore\nignored-by-both")769        .unwrap();770771    te.assert_output(&["ignored"], "ignored-by-nothing");772773    te.assert_output(774        &["--no-ignore-vcs", "ignored"],775        "ignored-by-nothing776        ignored-by-gitignore",777    );778779    te.assert_output(780        &["--no-ignore", "ignored"],781        "ignored-by-nothing782        ignored-by-fdignore783        ignored-by-gitignore784        ignored-by-both",785    );786}787788/// Ignore parent ignore files (--no-ignore-parent)789#[test]790fn test_no_ignore_parent() {791    let dirs = &["inner"];792    let files = &[793        "inner/parent-ignored",794        "inner/child-ignored",795        "inner/not-ignored",796    ];797    let te = TestEnv::new(dirs, files);798799    // Ignore 'parent-ignored' in root800    fs::File::create(te.test_root().join(".gitignore"))801        .unwrap()802        .write_all(b"parent-ignored")803        .unwrap();804    // Ignore 'child-ignored' in inner805    fs::File::create(te.test_root().join("inner/.gitignore"))806        .unwrap()807        .write_all(b"child-ignored")808        .unwrap();809810    te.assert_output_subdirectory("inner", &[], "not-ignored");811812    te.assert_output_subdirectory(813        "inner",814        &["--no-ignore-parent"],815        "parent-ignored816        not-ignored",817    );818}819820/// Ignore parent ignore files (--no-ignore-parent) with an inner git repo821#[test]822fn test_no_ignore_parent_inner_git() {823    let dirs = &["inner"];824    let files = &[825        "inner/parent-ignored",826        "inner/child-ignored",827        "inner/not-ignored",828    ];829    let te = TestEnv::new(dirs, files);830831    // Make the inner folder also appear as a git repo832    fs::create_dir_all(te.test_root().join("inner/.git")).unwrap();833834    // Ignore 'parent-ignored' in root835    fs::File::create(te.test_root().join(".gitignore"))836        .unwrap()837        .write_all(b"parent-ignored")838        .unwrap();839    // Ignore 'child-ignored' in inner840    fs::File::create(te.test_root().join("inner/.gitignore"))841        .unwrap()842        .write_all(b"child-ignored")843        .unwrap();844845    te.assert_output_subdirectory(846        "inner",847        &[],848        "not-ignored849        parent-ignored",850    );851852    te.assert_output_subdirectory(853        "inner",854        &["--no-ignore-parent"],855        "not-ignored856        parent-ignored",857    );858}859860/// Precedence of .fdignore files861#[test]862fn test_custom_ignore_precedence() {863    let dirs = &["inner"];864    let files = &["inner/foo"];865    let te = TestEnv::new(dirs, files);866867    // Ignore 'foo' via .gitignore868    fs::File::create(te.test_root().join("inner/.gitignore"))869        .unwrap()870        .write_all(b"foo")871        .unwrap();872873    // Whitelist 'foo' via .fdignore874    fs::File::create(te.test_root().join(".fdignore"))875        .unwrap()876        .write_all(b"!foo")877        .unwrap();878879    te.assert_output(&["foo"], "inner/foo");880881    te.assert_output(&["--no-ignore-vcs", "foo"], "inner/foo");882883    te.assert_output(&["--no-ignore", "foo"], "inner/foo");884}885886/// Don't require git to respect gitignore (--no-require-git)887#[test]888fn test_respect_ignore_files() {889    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);890891    // Not in a git repo anymore892    fs::remove_dir(te.test_root().join(".git")).unwrap();893894    // don't respect gitignore because we're not in a git repo895    te.assert_output(896        &["foo"],897        "a.foo898        gitignored.foo899        one/b.foo900        one/two/c.foo901        one/two/C.Foo2902        one/two/three/d.foo903        one/two/three/directory_foo/",904    );905906    // respect gitignore because we set `--no-require-git`907    te.assert_output(908        &["--no-require-git", "foo"],909        "a.foo910        one/b.foo911        one/two/c.foo912        one/two/C.Foo2913        one/two/three/d.foo914        one/two/three/directory_foo/",915    );916917    // make sure overriding works918    te.assert_output(919        &["--no-require-git", "--require-git", "foo"],920        "a.foo921        gitignored.foo922        one/b.foo923        one/two/c.foo924        one/two/C.Foo2925        one/two/three/d.foo926        one/two/three/directory_foo/",927    );928929    te.assert_output(930        &["--no-require-git", "--no-ignore", "foo"],931        "a.foo932        gitignored.foo933        fdignored.foo934        one/b.foo935        one/two/c.foo936        one/two/C.Foo2937        one/two/three/d.foo938        one/two/three/directory_foo/",939    );940}941942/// VCS ignored files (--no-ignore-vcs)943#[test]944fn test_no_ignore_vcs() {945    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);946947    te.assert_output(948        &["--no-ignore-vcs", "foo"],949        "a.foo950        gitignored.foo951        one/b.foo952        one/two/c.foo953        one/two/C.Foo2954        one/two/three/d.foo955        one/two/three/directory_foo/",956    );957}958959/// Test that --no-ignore-vcs still respects .fdignored in parent directory960#[test]961fn test_no_ignore_vcs_child_dir() {962    let te = TestEnv::new(963        &["inner"],964        &["inner/fdignored.foo", "inner/foo", "inner/gitignored.foo"],965    );966967    te.assert_output_subdirectory(968        "inner",969        &["--no-ignore-vcs", "foo"],970        "foo971        gitignored.foo",972    );973}974975/// Custom ignore files (--ignore-file)976#[test]977fn test_custom_ignore_files() {978    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);979980    // Ignore 'C.Foo2' and everything in 'three'.981    fs::File::create(te.test_root().join("custom.ignore"))982        .unwrap()983        .write_all(b"C.Foo2\nthree")984        .unwrap();985986    te.assert_output(987        &["--ignore-file", "custom.ignore", "foo"],988        "a.foo989        one/b.foo990        one/two/c.foo",991    );992}993994/// Ignored files with ripgrep aliases (-u / -uu)995#[test]996fn test_no_ignore_aliases() {997    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);998999    te.assert_output(1000        &["-u", "foo"],1001        ".hidden.foo1002        a.foo1003        fdignored.foo1004        gitignored.foo1005        one/b.foo1006        one/two/c.foo1007        one/two/C.Foo21008        one/two/three/d.foo1009        one/two/three/directory_foo/",1010    );1011}10121013#[cfg(not(windows))]1014#[test]1015fn test_global_ignore() {1016    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");1017    te.assert_output(1018        &[],1019        "a.foo1020    e1 e21021    symlink",1022    );1023}10241025#[cfg(not(windows))]1026#[test_case("--unrestricted", ".hidden.foo1027a.foo1028fdignored.foo1029gitignored.foo1030one/b.foo1031one/two/c.foo1032one/two/C.Foo21033one/two/three/d.foo1034one/two/three/directory_foo/"; "unrestricted")]1035#[test_case("--no-ignore", "a.foo1036fdignored.foo1037gitignored.foo1038one/b.foo1039one/two/c.foo1040one/two/C.Foo21041one/two/three/d.foo1042one/two/three/directory_foo/"; "no-ignore")]1043#[test_case("--no-global-ignore-file", "a.foo1044one/b.foo1045one/two/c.foo1046one/two/C.Foo21047one/two/three/d.foo1048one/two/three/directory_foo/"; "no-global-ignore-file")]1049fn test_no_global_ignore(flag: &str, expected_output: &str) {1050    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES).global_ignore_file("one");1051    te.assert_output(&[flag, "foo"], expected_output);1052}10531054/// Symlinks (--follow)1055#[test]1056fn test_follow() {1057    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);10581059    te.assert_output(1060        &["--follow", "c.foo"],1061        "one/two/c.foo1062        one/two/C.Foo21063        symlink/c.foo1064        symlink/C.Foo2",1065    );1066}10671068// File system boundaries (--one-file-system)1069// Limited to Unix because, to the best of my knowledge, there is no easy way to test a use case1070// file systems mounted into the tree on Windows.1071// Not limiting depth causes massive delay under Darwin, see BurntSushi/ripgrep#14291072#[test]1073#[cfg(unix)]1074fn test_file_system_boundaries() {1075    // Helper function to get the device ID for a given path1076    // Inspired by https://github.com/BurntSushi/ripgrep/blob/8892bf648cfec111e6e7ddd9f30e932b0371db68/ignore/src/walk.rs#L16931077    fn device_num(path: impl AsRef<Path>) -> u64 {1078        use std::os::unix::fs::MetadataExt;10791080        path.as_ref().metadata().map(|md| md.dev()).unwrap()1081    }10821083    // Can't simulate file system boundaries1084    let te = TestEnv::new(&[], &[]);10851086    let dev_null = Path::new("/dev/null");10871088    // /dev/null should exist in all sane Unixes. Skip if it doesn't exist for some reason.1089    // Also skip should it be on the same device as the root partition for some reason.1090    if !dev_null.is_file() || device_num(dev_null) == device_num("/") {1091        return;1092    }10931094    te.assert_output(1095        &["--full-path", "--max-depth", "2", "^/dev/null$", "/"],1096        "/dev/null",1097    );1098    te.assert_output(1099        &[1100            "--one-file-system",1101            "--full-path",1102            "--max-depth",1103            "2",1104            "^/dev/null$",1105            "/",1106        ],1107        "",1108    );1109}11101111#[test]1112fn test_follow_broken_symlink() {1113    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1114    te.create_broken_symlink("broken_symlink")1115        .expect("Failed to create broken symlink.");11161117    te.assert_output(1118        &["symlink"],1119        "broken_symlink1120        symlink",1121    );1122    te.assert_output(1123        &["--type", "symlink", "symlink"],1124        "broken_symlink1125        symlink",1126    );11271128    te.assert_output(&["--type", "file", "symlink"], "");11291130    te.assert_output(1131        &["--follow", "--type", "symlink", "symlink"],1132        "broken_symlink",1133    );1134    te.assert_output(&["--follow", "--type", "file", "symlink"], "");1135}11361137/// Null separator (--print0)1138#[test]1139fn test_print0() {1140    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);11411142    te.assert_output(1143        &["--print0", "foo"],1144        "./a.fooNULL1145        ./one/b.fooNULL1146        ./one/two/C.Foo2NULL1147        ./one/two/c.fooNULL1148        ./one/two/three/d.fooNULL1149        ./one/two/three/directory_foo/NULL",1150    );1151}11521153/// Maximum depth (--max-depth)1154#[test]1155fn test_max_depth() {1156    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);11571158    te.assert_output(1159        &["--max-depth", "3"],1160        "a.foo1161        e1 e21162        one/1163        one/b.foo1164        one/two/1165        one/two/c.foo1166        one/two/C.Foo21167        one/two/three/1168        symlink",1169    );11701171    te.assert_output(1172        &["--max-depth", "2"],1173        "a.foo1174        e1 e21175        one/1176        one/b.foo1177        one/two/1178        symlink",1179    );11801181    te.assert_output(1182        &["--max-depth", "1"],1183        "a.foo1184        e1 e21185        one/1186        symlink",1187    );1188}11891190/// Minimum depth (--min-depth)1191#[test]1192fn test_min_depth() {1193    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);11941195    te.assert_output(1196        &["--min-depth", "3"],1197        "one/two/c.foo1198        one/two/C.Foo21199        one/two/three/1200        one/two/three/d.foo1201        one/two/three/directory_foo/",1202    );12031204    te.assert_output(1205        &["--min-depth", "4"],1206        "one/two/three/d.foo1207        one/two/three/directory_foo/",1208    );1209}12101211/// Minimum depth with a broken symlink (regression test for #1017)1212///1213/// A broken symlink, surfaced while following links, has no depth reported by1214/// the walker, so --min-depth used to drop it unconditionally.1215#[test]1216fn test_min_depth_broken_symlink() {1217    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1218    te.create_broken_symlink("one/two/broken_symlink")1219        .expect("Failed to create broken symlink.");12201221    // The broken symlink sits at depth 3, so it is kept up to that depth.1222    te.assert_output(1223        &[1224            "--follow",1225            "--type",1226            "symlink",1227            "--min-depth",1228            "3",1229            "broken_symlink",1230        ],1231        "one/two/broken_symlink",1232    );12331234    // A --min-depth beyond its actual depth must exclude it.1235    te.assert_output(1236        &[1237            "--follow",1238            "--type",1239            "symlink",1240            "--min-depth",1241            "4",1242            "broken_symlink",1243        ],1244        "",1245    );1246}12471248/// Minimum depth with a broken symlink combined with --absolute-path (#1017)1249///1250/// With --absolute-path the search root is made absolute before walking, so the1251/// broken symlink's depth must still be computed relative to that root rather1252/// than from the absolute path's full component count.1253#[test]1254fn test_min_depth_broken_symlink_absolute_path() {1255    let (mut te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1256    te.create_broken_symlink("one/two/broken_symlink")1257        .expect("Failed to create broken symlink.");12581259    // The broken symlink sits at depth 3 relative to the (absolute) root.1260    te.assert_output(1261        &[1262            "--follow",1263            "--absolute-path",1264            "--type",1265            "symlink",1266            "--min-depth",1267            "3",1268            "broken_symlink",1269        ],1270        &format!("{abs_path}/one/two/broken_symlink"),1271    );12721273    // A --min-depth beyond its actual depth must exclude it.1274    te.assert_output(1275        &[1276            "--follow",1277            "--absolute-path",1278            "--type",1279            "symlink",1280            "--min-depth",1281            "4",1282            "broken_symlink",1283        ],1284        "",1285    );1286}12871288/// Minimum depth with a broken symlink under overlapping search roots (#1017)1289///1290/// When two search roots overlap, the walker visits the same broken symlink once1291/// per root, at a different depth each time. Here `one/two/broken_symlink` is at1292/// depth 2 under root `one` and at depth 1 under root `one/two`, so --min-depth 21293/// must keep the entry the walker reached via `one` and drop the one it reached1294/// via `one/two`. A depth derived from the entry's path cannot distinguish the1295/// two visits, since both carry the same path.1296#[test]1297fn test_min_depth_broken_symlink_overlapping_roots() {1298    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1299    te.create_broken_symlink("one/two/broken_symlink")1300        .expect("Failed to create broken symlink.");13011302    // Only the route through `one` reaches depth 2.1303    te.assert_output(1304        &[1305            "--follow",1306            "--type",1307            "symlink",1308            "--min-depth",1309            "2",1310            "broken_symlink",1311            "one",1312            "one/two",1313        ],1314        "one/two/broken_symlink",1315    );13161317    // Both routes clear --min-depth 1, so it is reported once per root.1318    te.assert_output(1319        &[1320            "--follow",1321            "--type",1322            "symlink",1323            "--min-depth",1324            "1",1325            "broken_symlink",1326            "one",1327            "one/two",1328        ],1329        "one/two/broken_symlink1330        one/two/broken_symlink",1331    );1332}13331334/// Maximum depth with a broken symlink (#1017)1335///1336/// A broken symlink must be filtered by --max-depth like any other entry. The1337/// default environment also exposes it through the followed `symlink` directory1338/// (`symlink -> one/two`), so it is reachable at depth 2 as well as depth 3.1339#[test]1340fn test_max_depth_broken_symlink() {1341    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1342    te.create_broken_symlink("one/two/broken_symlink")1343        .expect("Failed to create broken symlink.");13441345    // --max-depth 3 keeps both routes to the broken symlink.1346    te.assert_output(1347        &[1348            "--follow",1349            "--type",1350            "symlink",1351            "--max-depth",1352            "3",1353            "broken_symlink",1354        ],1355        "one/two/broken_symlink1356        symlink/broken_symlink",1357    );13581359    // A --max-depth below either route must exclude it.1360    te.assert_output(1361        &[1362            "--follow",1363            "--type",1364            "symlink",1365            "--max-depth",1366            "1",1367            "broken_symlink",1368        ],1369        "",1370    );1371}13721373/// Exact depth with a broken symlink (#1017)1374///1375/// A broken symlink must be kept only at its exact depth. It is reachable at1376/// depth 3 (`one/two/broken_symlink`) and, through the followed `symlink`1377/// directory, at depth 2 (`symlink/broken_symlink`).1378#[test]1379fn test_exact_depth_broken_symlink() {1380    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1381    te.create_broken_symlink("one/two/broken_symlink")1382        .expect("Failed to create broken symlink.");13831384    // Only the depth-3 route matches --exact-depth 3.1385    te.assert_output(1386        &[1387            "--follow",1388            "--type",1389            "symlink",1390            "--exact-depth",1391            "3",1392            "broken_symlink",1393        ],1394        "one/two/broken_symlink",1395    );13961397    // Only the depth-2 route (via the followed symlink) matches --exact-depth 2,1398    // which confirms the depth is computed relative to the search root.1399    te.assert_output(1400        &[1401            "--follow",1402            "--type",1403            "symlink",1404            "--exact-depth",1405            "2",1406            "broken_symlink",1407        ],1408        "symlink/broken_symlink",1409    );1410}14111412/// Exact depth (--exact-depth)1413#[test]1414fn test_exact_depth() {1415    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14161417    te.assert_output(1418        &["--exact-depth", "3"],1419        "one/two/c.foo1420        one/two/C.Foo21421        one/two/three/",1422    );1423}14241425/// Pruning (--prune)1426#[test]1427fn test_prune() {1428    let dirs = &["foo/bar", "bar/foo", "baz"];1429    let files = &[1430        "foo/foo.file",1431        "foo/bar/foo.file",1432        "bar/foo.file",1433        "bar/foo/foo.file",1434        "baz/foo.file",1435    ];14361437    let te = TestEnv::new(dirs, files);14381439    te.assert_output(1440        &["foo"],1441        "foo/1442        foo/foo.file1443        foo/bar/foo.file1444        bar/foo.file1445        bar/foo/1446        bar/foo/foo.file1447        baz/foo.file",1448    );14491450    te.assert_output(1451        &["--prune", "foo"],1452        "foo/1453        bar/foo/1454        bar/foo.file1455        baz/foo.file",1456    );1457}14581459/// Absolute paths (--absolute-path)1460#[test]1461fn test_absolute_path() {1462    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);14631464    te.assert_output(1465        &["--absolute-path"],1466        &format!(1467            "{abs_path}/a.foo1468            {abs_path}/e1 e21469            {abs_path}/one/1470            {abs_path}/one/b.foo1471            {abs_path}/one/two/1472            {abs_path}/one/two/c.foo1473            {abs_path}/one/two/C.Foo21474            {abs_path}/one/two/three/1475            {abs_path}/one/two/three/d.foo1476            {abs_path}/one/two/three/directory_foo/1477            {abs_path}/symlink",1478            abs_path = abs_path1479        ),1480    );14811482    te.assert_output(1483        &["--absolute-path", "foo"],1484        &format!(1485            "{abs_path}/a.foo1486            {abs_path}/one/b.foo1487            {abs_path}/one/two/c.foo1488            {abs_path}/one/two/C.Foo21489            {abs_path}/one/two/three/d.foo1490            {abs_path}/one/two/three/directory_foo/",1491            abs_path = abs_path1492        ),1493    );1494}14951496/// Show absolute paths if the path argument is absolute1497#[test]1498fn test_implicit_absolute_path() {1499    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);15001501    te.assert_output(1502        &["foo", &abs_path],1503        &format!(1504            "{abs_path}/a.foo1505            {abs_path}/one/b.foo1506            {abs_path}/one/two/c.foo1507            {abs_path}/one/two/C.Foo21508            {abs_path}/one/two/three/d.foo1509            {abs_path}/one/two/three/directory_foo/",1510            abs_path = abs_path1511        ),1512    );1513}15141515/// Absolute paths should be normalized1516#[test]1517fn test_normalized_absolute_path() {1518    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);15191520    te.assert_output_subdirectory(1521        "one",1522        &["--absolute-path", "foo", ".."],1523        &format!(1524            "{abs_path}/a.foo1525            {abs_path}/one/b.foo1526            {abs_path}/one/two/c.foo1527            {abs_path}/one/two/C.Foo21528            {abs_path}/one/two/three/d.foo1529            {abs_path}/one/two/three/directory_foo/",1530            abs_path = abs_path1531        ),1532    );1533}15341535/// File type filter (--type)1536#[test]1537fn test_type() {1538    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);15391540    te.assert_output(1541        &["--type", "f"],1542        "a.foo1543        e1 e21544        one/b.foo1545        one/two/c.foo1546        one/two/C.Foo21547        one/two/three/d.foo",1548    );15491550    te.assert_output(&["--type", "f", "e1"], "e1 e2");15511552    te.assert_output(1553        &["--type", "d"],1554        "one/1555        one/two/1556        one/two/three/1557        one/two/three/directory_foo/",1558    );15591560    te.assert_output(1561        &["--type", "d", "--type", "l"],1562        "one/1563        one/two/1564        one/two/three/1565        one/two/three/directory_foo/1566        symlink",1567    );15681569    te.assert_output(&["--type", "l"], "symlink");1570}15711572/// Test `--type executable`1573#[cfg(unix)]1574#[test]1575fn test_type_executable() {1576    use std::os::unix::fs::OpenOptionsExt;15771578    // This test assumes the current user isn't root1579    // (otherwise if the executable bit is set for any level, it is executable for the current1580    // user)1581    if Uid::current().is_root() {1582        return;1583    }15841585    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);15861587    fs::OpenOptions::new()1588        .create_new(true)1589        .truncate(true)1590        .write(true)1591        .mode(0o777)1592        .open(te.test_root().join("executable-file.sh"))1593        .unwrap();15941595    fs::OpenOptions::new()1596        .create(true)1597        .truncate(true)1598        .write(true)1599        .mode(0o645)1600        .open(te.test_root().join("not-user-executable-file.sh"))1601        .unwrap();16021603    te.assert_output(&["--type", "executable"], "executable-file.sh");16041605    te.assert_output(1606        &["--type", "executable", "--type", "directory"],1607        "executable-file.sh1608        one/1609        one/two/1610        one/two/three/1611        one/two/three/directory_foo/",1612    );1613}16141615/// Test `--type empty`1616#[test]1617fn test_type_empty() {1618    let te = TestEnv::new(&["dir_empty", "dir_nonempty"], &[]);16191620    create_file_with_size(te.test_root().join("0_bytes.foo"), 0);1621    create_file_with_size(te.test_root().join("5_bytes.foo"), 5);16221623    create_file_with_size(te.test_root().join("dir_nonempty").join("2_bytes.foo"), 2);16241625    te.assert_output(1626        &["--type", "empty"],1627        "0_bytes.foo1628        dir_empty/",1629    );16301631    te.assert_output(1632        &["--type", "empty", "--type", "file", "--type", "directory"],1633        "0_bytes.foo1634        dir_empty/",1635    );16361637    te.assert_output(&["--type", "empty", "--type", "file"], "0_bytes.foo");16381639    te.assert_output(&["--type", "empty", "--type", "directory"], "dir_empty/");1640}16411642/// File extension (--extension)1643#[test]1644fn test_extension() {1645    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);16461647    te.assert_output(1648        &["--extension", "foo"],1649        "a.foo1650        one/b.foo1651        one/two/c.foo1652        one/two/three/d.foo",1653    );16541655    te.assert_output(1656        &["--extension", ".foo"],1657        "a.foo1658        one/b.foo1659        one/two/c.foo1660        one/two/three/d.foo",1661    );16621663    te.assert_output(1664        &["--extension", ".foo", "--extension", "foo2"],1665        "a.foo1666        one/b.foo1667        one/two/c.foo1668        one/two/three/d.foo1669        one/two/C.Foo2",1670    );16711672    te.assert_output(&["--extension", ".foo", "a"], "a.foo");16731674    te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");16751676    let te2 = TestEnv::new(&[], &["spam.bar.baz", "egg.bar.baz", "yolk.bar.baz.sig"]);16771678    te2.assert_output(1679        &["--extension", ".bar.baz"],1680        "spam.bar.baz1681        egg.bar.baz",1682    );16831684    te2.assert_output(&["--extension", "sig"], "yolk.bar.baz.sig");16851686    te2.assert_output(&["--extension", "bar.baz.sig"], "yolk.bar.baz.sig");16871688    let te3 = TestEnv::new(&[], &["latin1.e\u{301}xt", "smiley.☻"]);16891690    te3.assert_output(&["--extension", "☻"], "smiley.☻");16911692    te3.assert_output(&["--extension", ".e\u{301}xt"], "latin1.e\u{301}xt");16931694    let te4 = TestEnv::new(&[], &[".hidden", "test.hidden"]);16951696    te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");1697}16981699/// No file extension (test for the pattern provided in the --help text)1700#[test]1701fn test_no_extension() {1702    let te = TestEnv::new(1703        DEFAULT_DIRS,1704        &["a.foo", "aa", "one/b.foo", "one/bb", "one/two/three/d"],1705    );17061707    te.assert_output(1708        &["^[^.]+$"],1709        "aa1710        one/1711        one/bb1712        one/two/1713        one/two/three/1714        one/two/three/d1715        one/two/three/directory_foo/1716        symlink",1717    );17181719    te.assert_output(1720        &["^[^.]+$", "--type", "file"],1721        "aa1722        one/bb1723        one/two/three/d",1724    );1725}17261727/// Symlink as search directory1728#[test]1729fn test_symlink_as_root() {1730    let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1731    te.create_broken_symlink("broken_symlink")1732        .expect("Failed to create broken symlink.");17331734    // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html1735    // The getcwd() function shall place an absolute pathname of the current working directory in1736    // the array pointed to by buf, and return buf. The pathname shall contain no components that1737    // are dot or dot-dot, or are symbolic links.1738    //1739    // Key points:1740    // 1. The path of the current working directory of a Unix process cannot contain symlinks.1741    // 2. The path of the current working directory of a Windows process can contain symlinks.1742    //1743    // More:1744    // 1. On Windows, symlinks are resolved after the ".." component.1745    // 2. On Unix, symlinks are resolved immediately as encountered.17461747    let parent_parent = if cfg!(windows) { ".." } else { "../.." };1748    te.assert_output_subdirectory(1749        "symlink",1750        &["", parent_parent],1751        &format!(1752            "{dir}/a.foo1753            {dir}/broken_symlink1754            {dir}/e1 e21755            {dir}/one/1756            {dir}/one/b.foo1757            {dir}/one/two/1758            {dir}/one/two/c.foo1759            {dir}/one/two/C.Foo21760            {dir}/one/two/three/1761            {dir}/one/two/three/d.foo1762            {dir}/one/two/three/directory_foo/1763            {dir}/symlink",1764            dir = parent_parent1765        ),1766    );1767}17681769#[test]1770fn test_symlink_and_absolute_path() {1771    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);17721773    let expected_path = if cfg!(windows) { "symlink" } else { "one/two" };17741775    te.assert_output_subdirectory(1776        "symlink",1777        &["--absolute-path"],1778        &format!(1779            "{abs_path}/{expected_path}/c.foo1780            {abs_path}/{expected_path}/C.Foo21781            {abs_path}/{expected_path}/three/1782            {abs_path}/{expected_path}/three/d.foo1783            {abs_path}/{expected_path}/three/directory_foo/",1784            abs_path = abs_path,1785            expected_path = expected_path1786        ),1787    );1788}17891790#[test]1791fn test_symlink_as_absolute_root() {1792    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);17931794    te.assert_output(1795        &["", &format!("{abs_path}/symlink")],1796        &format!(1797            "{abs_path}/symlink/c.foo1798            {abs_path}/symlink/C.Foo21799            {abs_path}/symlink/three/1800            {abs_path}/symlink/three/d.foo1801            {abs_path}/symlink/three/directory_foo/",1802            abs_path = abs_path1803        ),1804    );1805}18061807#[test]1808fn test_symlink_and_full_path() {1809    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1810    let root = te.system_root();1811    let prefix = escape(&root.to_string_lossy());18121813    let expected_path = if cfg!(windows) { "symlink" } else { "one/two" };18141815    te.assert_output_subdirectory(1816        "symlink",1817        &[1818            "--absolute-path",1819            "--full-path",1820            &format!("^{prefix}.*three"),1821        ],1822        &format!(1823            "{abs_path}/{expected_path}/three/1824            {abs_path}/{expected_path}/three/d.foo1825            {abs_path}/{expected_path}/three/directory_foo/",1826            abs_path = abs_path,1827            expected_path = expected_path1828        ),1829    );1830}18311832#[test]1833fn test_symlink_and_full_path_abs_path() {1834    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1835    let root = te.system_root();1836    let prefix = escape(&root.to_string_lossy());1837    te.assert_output(1838        &[1839            "--full-path",1840            &format!("^{prefix}.*symlink.*three"),1841            &format!("{abs_path}/symlink"),1842        ],1843        &format!(1844            "{abs_path}/symlink/three/1845            {abs_path}/symlink/three/d.foo1846            {abs_path}/symlink/three/directory_foo/",1847            abs_path = abs_path1848        ),1849    );1850}1851/// Exclude patterns (--exclude)1852#[test]1853fn test_excludes() {1854    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);18551856    te.assert_output(1857        &["--exclude", "*.foo"],1858        "one/1859        one/two/1860        one/two/C.Foo21861        one/two/three/1862        one/two/three/directory_foo/1863        e1 e21864        symlink",1865    );18661867    te.assert_output(1868        &["--exclude", "*.foo", "--exclude", "*.Foo2"],1869        "one/1870        one/two/1871        one/two/three/1872        one/two/three/directory_foo/1873        e1 e21874        symlink",1875    );18761877    te.assert_output(1878        &["--exclude", "*.foo", "--exclude", "*.Foo2", "foo"],1879        "one/two/three/directory_foo/",1880    );18811882    te.assert_output(1883        &["--exclude", "one/two/", "foo"],1884        "a.foo1885        one/b.foo",1886    );18871888    te.assert_output(1889        &["--exclude", "one/**/*.foo"],1890        "a.foo1891        e1 e21892        one/1893        one/two/1894        one/two/C.Foo21895        one/two/three/1896        one/two/three/directory_foo/1897        symlink",1898    );1899}19001901#[test]1902fn format() {1903    let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);19041905    te.assert_output(1906        &["--format", "path={}", "--path-separator=/"],1907        "path=a.foo1908        path=e1 e21909        path=one1910        path=one/b.foo1911        path=one/two1912        path=one/two/C.Foo21913        path=one/two/c.foo1914        path=one/two/three1915        path=one/two/three/d.foo1916        path=one/two/three/directory_foo1917        path=symlink",1918    );19191920    te.assert_output(1921        &["foo", "--format", "noExt={.}", "--path-separator=/"],1922        "noExt=a1923        noExt=one/b1924        noExt=one/two/C1925        noExt=one/two/c1926        noExt=one/two/three/d1927        noExt=one/two/three/directory_foo",1928    );19291930    te.assert_output(1931        &["foo", "--format", "basename={/}", "--path-separator=/"],1932        "basename=a.foo1933        basename=b.foo1934        basename=C.Foo21935        basename=c.foo1936        basename=d.foo1937        basename=directory_foo",1938    );19391940    te.assert_output(1941        &["foo", "--format", "name={/.}", "--path-separator=/"],1942        "name=a1943        name=b1944        name=C1945        name=c1946        name=d1947        name=directory_foo",1948    );19491950    te.assert_output(1951        &["foo", "--format", "parent={//}", "--path-separator=/"],1952        "parent=.1953        parent=one1954        parent=one/two1955        parent=one/two1956        parent=one/two/three1957        parent=one/two/three",1958    );19591960    // Templates may start with '-' (e.g. markdown list items); see #2126.1961    te.assert_output(1962        &["foo", "--format", "- {/.}", "--path-separator=/"],1963        "- a1964        - b1965        - C1966        - c1967        - d1968        - directory_foo",1969    );1970}19711972/// Shell script execution (--exec)1973#[test]1974fn test_exec() {1975    let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1976    // TODO Windows tests: D:file.txt \file.txt \\server\share\file.txt ...1977    if !cfg!(windows) {1978        te.assert_output(1979            &["--absolute-path", "foo", "--exec", "echo"],1980            &format!(1981                "{abs_path}/a.foo1982                {abs_path}/one/b.foo1983                {abs_path}/one/two/C.Foo21984                {abs_path}/one/two/c.foo1985                {abs_path}/one/two/three/d.foo1986                {abs_path}/one/two/three/directory_foo",1987                abs_path = abs_path1988            ),1989        );19901991        te.assert_output(1992            &["foo", "--exec", "echo", "{}"],1993            "./a.foo1994            ./one/b.foo1995            ./one/two/C.Foo21996            ./one/two/c.foo1997            ./one/two/three/d.foo1998            ./one/two/three/directory_foo",1999        );2000

The first 2,000 of 3,139 lines.