tests.rs
This file
Path
tests/tests.rs
1,613 lines · 40.8 KiB · Rust
Last changed by
Code formattingsharkdpbefore adoption726fede
History of this fileTake it
1mod testenv;23use std::fs;4use std::io::Write;5use std::path::Path;6use std::time::{Duration, SystemTime};78use regex::escape;910use crate::testenv::TestEnv;1112static DEFAULT_DIRS: &[&str] = &["one/two/three", "one/two/three/directory_foo"];1314static DEFAULT_FILES: &[&str] = &[15 "a.foo",16 "one/b.foo",17 "one/two/c.foo",18 "one/two/C.Foo2",19 "one/two/three/d.foo",20 "fdignored.foo",21 "gitignored.foo",22 ".hidden.foo",23 "e1 e2",24];2526fn get_absolute_root_path(env: &TestEnv) -> String {27 let path = env28 .test_root()29 .canonicalize()30 .expect("absolute path")31 .to_str()32 .expect("string")33 .to_string();3435 #[cfg(windows)]36 let path = path.trim_start_matches(r"\\?\").to_string();3738 path39}4041#[cfg(test)]42fn get_test_env_with_abs_path(dirs: &[&'static str], files: &[&'static str]) -> (TestEnv, String) {43 let env = TestEnv::new(dirs, files);44 let root_path = get_absolute_root_path(&env);45 (env, root_path)46}4748#[cfg(test)]49fn create_file_with_size<P: AsRef<Path>>(path: P, size_in_bytes: usize) {50 let content = "#".repeat(size_in_bytes);51 let mut f = fs::File::create::<P>(path).unwrap();52 f.write_all(content.as_bytes()).unwrap();53}5455/// Simple test56#[test]57fn test_simple() {58 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);5960 te.assert_output(&["a.foo"], "a.foo");61 te.assert_output(&["b.foo"], "one/b.foo");62 te.assert_output(&["d.foo"], "one/two/three/d.foo");6364 te.assert_output(65 &["foo"],66 "a.foo67 one/b.foo68 one/two/c.foo69 one/two/C.Foo270 one/two/three/d.foo71 one/two/three/directory_foo",72 );73}7475/// Test each pattern type with an empty pattern.76#[test]77fn test_empty_pattern() {78 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);79 let expected = "a.foo80 e1 e281 one82 one/b.foo83 one/two84 one/two/c.foo85 one/two/C.Foo286 one/two/three87 one/two/three/d.foo88 one/two/three/directory_foo89 symlink";9091 te.assert_output(&["--regex"], expected);92 te.assert_output(&["--fixed-strings"], expected);93 te.assert_output(&["--glob"], expected);94}9596/// Test multiple directory searches97#[test]98fn test_multi_file() {99 let dirs = &["test1", "test2"];100 let files = &["test1/a.foo", "test1/b.foo", "test2/a.foo"];101 let te = TestEnv::new(dirs, files);102 te.assert_output(103 &["a.foo", "test1", "test2"],104 "test1/a.foo105 test2/a.foo",106 );107108 te.assert_output(109 &["", "test1", "test2"],110 "test1/a.foo111 test2/a.foo112 test1/b.foo",113 );114115 te.assert_output(&["a.foo", "test1"], "test1/a.foo");116117 te.assert_output(&["b.foo", "test1", "test2"], "test1/b.foo");118}119120/// Test search over multiple directory with missing121#[test]122fn test_multi_file_with_missing() {123 let dirs = &["real"];124 let files = &["real/a.foo", "real/b.foo"];125 let te = TestEnv::new(dirs, files);126 te.assert_output(&["a.foo", "real", "fake"], "real/a.foo");127128 te.assert_error(129 &["a.foo", "real", "fake"],130 "[fd error]: Search path 'fake' is not a directory.",131 );132133 te.assert_output(134 &["", "real", "fake"],135 "real/a.foo136 real/b.foo",137 );138139 te.assert_output(140 &["", "real", "fake1", "fake2"],141 "real/a.foo142 real/b.foo",143 );144145 te.assert_error(146 &["", "real", "fake1", "fake2"],147 "[fd error]: Search path 'fake1' is not a directory.148 [fd error]: Search path 'fake2' is not a directory.",149 );150151 te.assert_failure_with_error(152 &["", "fake1", "fake2"],153 "[fd error]: Search path 'fake1' is not a directory.154 [fd error]: Search path 'fake2' is not a directory.155 [fd error]: No valid search paths given.",156 );157}158159/// Explicit root path160#[test]161fn test_explicit_root_path() {162 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);163164 te.assert_output(165 &["foo", "one"],166 "one/b.foo167 one/two/c.foo168 one/two/C.Foo2169 one/two/three/d.foo170 one/two/three/directory_foo",171 );172173 te.assert_output(174 &["foo", "one/two/three"],175 "one/two/three/d.foo176 one/two/three/directory_foo",177 );178179 te.assert_output_subdirectory(180 "one/two",181 &["foo", "../../"],182 "../../a.foo183 ../../one/b.foo184 ../../one/two/c.foo185 ../../one/two/C.Foo2186 ../../one/two/three/d.foo187 ../../one/two/three/directory_foo",188 );189190 te.assert_output_subdirectory(191 "one/two/three",192 &["", ".."],193 "../c.foo194 ../C.Foo2195 ../three196 ../three/d.foo197 ../three/directory_foo",198 );199}200201/// Regex searches202#[test]203fn test_regex_searches() {204 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);205206 te.assert_output(207 &["[a-c].foo"],208 "a.foo209 one/b.foo210 one/two/c.foo211 one/two/C.Foo2",212 );213214 te.assert_output(215 &["--case-sensitive", "[a-c].foo"],216 "a.foo217 one/b.foo218 one/two/c.foo",219 );220}221222/// Smart case223#[test]224fn test_smart_case() {225 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);226227 te.assert_output(228 &["c.foo"],229 "one/two/c.foo230 one/two/C.Foo2",231 );232233 te.assert_output(&["C.Foo"], "one/two/C.Foo2");234235 te.assert_output(&["Foo"], "one/two/C.Foo2");236237 // Only literal uppercase chars should trigger case sensitivity.238 te.assert_output(239 &["\\Ac"],240 "one/two/c.foo241 one/two/C.Foo2",242 );243 te.assert_output(&["\\AC"], "one/two/C.Foo2");244}245246/// Case sensitivity (--case-sensitive)247#[test]248fn test_case_sensitive() {249 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);250251 te.assert_output(&["--case-sensitive", "c.foo"], "one/two/c.foo");252253 te.assert_output(&["--case-sensitive", "C.Foo"], "one/two/C.Foo2");254255 te.assert_output(256 &["--ignore-case", "--case-sensitive", "C.Foo"],257 "one/two/C.Foo2",258 );259}260261/// Case insensitivity (--ignore-case)262#[test]263fn test_case_insensitive() {264 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);265266 te.assert_output(267 &["--ignore-case", "C.Foo"],268 "one/two/c.foo269 one/two/C.Foo2",270 );271272 te.assert_output(273 &["--case-sensitive", "--ignore-case", "C.Foo"],274 "one/two/c.foo275 one/two/C.Foo2",276 );277}278279/// Glob-based searches (--glob)280#[test]281fn test_glob_searches() {282 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);283284 te.assert_output(285 &["--glob", "*.foo"],286 "a.foo287 one/b.foo288 one/two/c.foo289 one/two/three/d.foo",290 );291292 te.assert_output(293 &["--glob", "[a-c].foo"],294 "a.foo295 one/b.foo296 one/two/c.foo",297 );298299 te.assert_output(300 &["--glob", "[a-c].foo*"],301 "a.foo302 one/b.foo303 one/two/C.Foo2304 one/two/c.foo",305 );306}307308/// Glob-based searches (--glob) in combination with full path searches (--full-path)309#[cfg(not(windows))] // TODO: make this work on Windows310#[test]311fn test_full_path_glob_searches() {312 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);313314 te.assert_output(315 &["--glob", "--full-path", "**/one/**/*.foo"],316 "one/b.foo317 one/two/c.foo318 one/two/three/d.foo",319 );320321 te.assert_output(322 &["--glob", "--full-path", "**/one/*/*.foo"],323 " one/two/c.foo",324 );325326 te.assert_output(327 &["--glob", "--full-path", "**/one/*/*/*.foo"],328 " one/two/three/d.foo",329 );330}331332#[test]333fn test_smart_case_glob_searches() {334 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);335336 te.assert_output(337 &["--glob", "c.foo*"],338 "one/two/C.Foo2339 one/two/c.foo",340 );341342 te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");343}344345/// Glob-based searches (--glob) in combination with --case-sensitive346#[test]347fn test_case_sensitive_glob_searches() {348 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);349350 te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");351}352353/// Glob-based searches (--glob) in combination with --extension354#[test]355fn test_glob_searches_with_extension() {356 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);357358 te.assert_output(359 &["--glob", "--extension", "foo2", "[a-z].*"],360 "one/two/C.Foo2",361 );362}363364/// Make sure that --regex overrides --glob365#[test]366fn test_regex_overrides_glob() {367 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);368369 te.assert_output(&["--glob", "--regex", "Foo2$"], "one/two/C.Foo2");370}371372/// Full path search (--full-path)373#[test]374fn test_full_path() {375 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);376377 let root = te.system_root();378 let prefix = escape(&root.to_string_lossy());379380 te.assert_output(381 &[382 "--full-path",383 &format!("^{prefix}.*three.*foo$", prefix = prefix),384 ],385 "one/two/three/d.foo386 one/two/three/directory_foo",387 );388}389390/// Hidden files (--hidden)391#[test]392fn test_hidden() {393 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);394395 te.assert_output(396 &["--hidden", "foo"],397 ".hidden.foo398 a.foo399 one/b.foo400 one/two/c.foo401 one/two/C.Foo2402 one/two/three/d.foo403 one/two/three/directory_foo",404 );405}406407/// Hidden file attribute on Windows408#[cfg(windows)]409#[test]410fn test_hidden_file_attribute() {411 use std::os::windows::fs::OpenOptionsExt;412413 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);414415 // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa416 const FILE_ATTRIBUTE_HIDDEN: u32 = 2;417418 fs::OpenOptions::new()419 .create(true)420 .write(true)421 .attributes(FILE_ATTRIBUTE_HIDDEN)422 .open(te.test_root().join("hidden-file.txt"))423 .unwrap();424425 te.assert_output(&["--hidden", "hidden-file.txt"], "hidden-file.txt");426 te.assert_output(&["hidden-file.txt"], "");427}428429/// Ignored files (--no-ignore)430#[test]431fn test_no_ignore() {432 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);433434 te.assert_output(435 &["--no-ignore", "foo"],436 "a.foo437 fdignored.foo438 gitignored.foo439 one/b.foo440 one/two/c.foo441 one/two/C.Foo2442 one/two/three/d.foo443 one/two/three/directory_foo",444 );445446 te.assert_output(447 &["--hidden", "--no-ignore", "foo"],448 ".hidden.foo449 a.foo450 fdignored.foo451 gitignored.foo452 one/b.foo453 one/two/c.foo454 one/two/C.Foo2455 one/two/three/d.foo456 one/two/three/directory_foo",457 );458}459460/// .gitignore and .fdignore461#[test]462fn test_gitignore_and_fdignore() {463 let files = &[464 "ignored-by-nothing",465 "ignored-by-fdignore",466 "ignored-by-gitignore",467 "ignored-by-both",468 ];469 let te = TestEnv::new(&[], files);470471 fs::File::create(te.test_root().join(".fdignore"))472 .unwrap()473 .write_all(b"ignored-by-fdignore\nignored-by-both")474 .unwrap();475476 fs::File::create(te.test_root().join(".gitignore"))477 .unwrap()478 .write_all(b"ignored-by-gitignore\nignored-by-both")479 .unwrap();480481 te.assert_output(&["ignored"], "ignored-by-nothing");482483 te.assert_output(484 &["--no-ignore-vcs", "ignored"],485 "ignored-by-nothing486 ignored-by-gitignore",487 );488489 te.assert_output(490 &["--no-ignore", "ignored"],491 "ignored-by-nothing492 ignored-by-fdignore493 ignored-by-gitignore494 ignored-by-both",495 );496}497498/// Precedence of .fdignore files499#[test]500fn test_custom_ignore_precedence() {501 let dirs = &["inner"];502 let files = &["inner/foo"];503 let te = TestEnv::new(dirs, files);504505 // Ignore 'foo' via .gitignore506 fs::File::create(te.test_root().join("inner/.gitignore"))507 .unwrap()508 .write_all(b"foo")509 .unwrap();510511 // Whitelist 'foo' via .fdignore512 fs::File::create(te.test_root().join(".fdignore"))513 .unwrap()514 .write_all(b"!foo")515 .unwrap();516517 te.assert_output(&["foo"], "inner/foo");518519 te.assert_output(&["--no-ignore-vcs", "foo"], "inner/foo");520521 te.assert_output(&["--no-ignore", "foo"], "inner/foo");522}523524/// VCS ignored files (--no-ignore-vcs)525#[test]526fn test_no_ignore_vcs() {527 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);528529 te.assert_output(530 &["--no-ignore-vcs", "foo"],531 "a.foo532 gitignored.foo533 one/b.foo534 one/two/c.foo535 one/two/C.Foo2536 one/two/three/d.foo537 one/two/three/directory_foo",538 );539}540541/// Custom ignore files (--ignore-file)542#[test]543fn test_custom_ignore_files() {544 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);545546 // Ignore 'C.Foo2' and everything in 'three'.547 fs::File::create(te.test_root().join("custom.ignore"))548 .unwrap()549 .write_all(b"C.Foo2\nthree")550 .unwrap();551552 te.assert_output(553 &["--ignore-file", "custom.ignore", "foo"],554 "a.foo555 one/b.foo556 one/two/c.foo",557 );558}559560/// Ignored files with ripgrep aliases (-u / -uu)561#[test]562fn test_no_ignore_aliases() {563 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);564565 te.assert_output(566 &["-u", "foo"],567 "a.foo568 fdignored.foo569 gitignored.foo570 one/b.foo571 one/two/c.foo572 one/two/C.Foo2573 one/two/three/d.foo574 one/two/three/directory_foo",575 );576577 te.assert_output(578 &["-uu", "foo"],579 ".hidden.foo580 a.foo581 fdignored.foo582 gitignored.foo583 one/b.foo584 one/two/c.foo585 one/two/C.Foo2586 one/two/three/d.foo587 one/two/three/directory_foo",588 );589}590591/// Symlinks (--follow)592#[test]593fn test_follow() {594 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);595596 te.assert_output(597 &["--follow", "c.foo"],598 "one/two/c.foo599 one/two/C.Foo2600 symlink/c.foo601 symlink/C.Foo2",602 );603}604605// File system boundaries (--one-file-system)606// Limited to Unix because, to the best of my knowledge, there is no easy way to test a use case607// file systems mounted into the tree on Windows.608// Not limiting depth causes massive delay under Darwin, see BurntSushi/ripgrep#1429609#[test]610#[cfg(unix)]611fn test_file_system_boundaries() {612 // Helper function to get the device ID for a given path613 // Inspired by https://github.com/BurntSushi/ripgrep/blob/8892bf648cfec111e6e7ddd9f30e932b0371db68/ignore/src/walk.rs#L1693614 fn device_num(path: impl AsRef<Path>) -> u64 {615 use std::os::unix::fs::MetadataExt;616617 path.as_ref().metadata().map(|md| md.dev()).unwrap()618 }619620 // Can't simulate file system boundaries621 let te = TestEnv::new(&[], &[]);622623 let dev_null = Path::new("/dev/null");624625 // /dev/null should exist in all sane Unixes. Skip if it doesn't exist for some reason.626 // Also skip should it be on the same device as the root partition for some reason.627 if !dev_null.is_file() || device_num(dev_null) == device_num("/") {628 return;629 }630631 te.assert_output(632 &["--full-path", "--max-depth", "2", "^/dev/null$", "/"],633 "/dev/null",634 );635 te.assert_output(636 &[637 "--one-file-system",638 "--full-path",639 "--max-depth",640 "2",641 "^/dev/null$",642 "/",643 ],644 "",645 );646}647648#[test]649fn test_follow_broken_symlink() {650 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);651 te.create_broken_symlink("broken_symlink")652 .expect("Failed to create broken symlink.");653654 te.assert_output(655 &["symlink"],656 "broken_symlink657 symlink",658 );659 te.assert_output(660 &["--type", "symlink", "symlink"],661 "broken_symlink662 symlink",663 );664665 te.assert_output(&["--type", "file", "symlink"], "");666667 te.assert_output(668 &["--follow", "--type", "symlink", "symlink"],669 "broken_symlink",670 );671 te.assert_output(&["--follow", "--type", "file", "symlink"], "");672}673674/// Null separator (--print0)675#[test]676fn test_print0() {677 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);678679 te.assert_output(680 &["--print0", "foo"],681 "a.fooNULL682 one/b.fooNULL683 one/two/C.Foo2NULL684 one/two/c.fooNULL685 one/two/three/d.fooNULL686 one/two/three/directory_fooNULL",687 );688}689690/// Maximum depth (--max-depth)691#[test]692fn test_max_depth() {693 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);694695 te.assert_output(696 &["--max-depth", "3"],697 "a.foo698 e1 e2699 one700 one/b.foo701 one/two702 one/two/c.foo703 one/two/C.Foo2704 one/two/three705 symlink",706 );707708 te.assert_output(709 &["--max-depth", "2"],710 "a.foo711 e1 e2712 one713 one/b.foo714 one/two715 symlink",716 );717718 te.assert_output(719 &["--max-depth", "1"],720 "a.foo721 e1 e2722 one723 symlink",724 );725}726727/// Minimum depth (--min-depth)728#[test]729fn test_min_depth() {730 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);731732 te.assert_output(733 &["--min-depth", "3"],734 "one/two/c.foo735 one/two/C.Foo2736 one/two/three737 one/two/three/d.foo738 one/two/three/directory_foo",739 );740741 te.assert_output(742 &["--min-depth", "4"],743 "one/two/three/d.foo744 one/two/three/directory_foo",745 );746}747748/// Exact depth (--exact-depth)749#[test]750fn test_exact_depth() {751 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);752753 te.assert_output(754 &["--exact-depth", "3"],755 "one/two/c.foo756 one/two/C.Foo2757 one/two/three",758 );759}760761/// Absolute paths (--absolute-path)762#[test]763fn test_absolute_path() {764 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);765766 te.assert_output(767 &["--absolute-path"],768 &format!(769 "{abs_path}/a.foo770 {abs_path}/e1 e2771 {abs_path}/one772 {abs_path}/one/b.foo773 {abs_path}/one/two774 {abs_path}/one/two/c.foo775 {abs_path}/one/two/C.Foo2776 {abs_path}/one/two/three777 {abs_path}/one/two/three/d.foo778 {abs_path}/one/two/three/directory_foo779 {abs_path}/symlink",780 abs_path = &abs_path781 ),782 );783784 te.assert_output(785 &["--absolute-path", "foo"],786 &format!(787 "{abs_path}/a.foo788 {abs_path}/one/b.foo789 {abs_path}/one/two/c.foo790 {abs_path}/one/two/C.Foo2791 {abs_path}/one/two/three/d.foo792 {abs_path}/one/two/three/directory_foo",793 abs_path = &abs_path794 ),795 );796}797798/// Show absolute paths if the path argument is absolute799#[test]800fn test_implicit_absolute_path() {801 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);802803 te.assert_output(804 &["foo", &abs_path],805 &format!(806 "{abs_path}/a.foo807 {abs_path}/one/b.foo808 {abs_path}/one/two/c.foo809 {abs_path}/one/two/C.Foo2810 {abs_path}/one/two/three/d.foo811 {abs_path}/one/two/three/directory_foo",812 abs_path = &abs_path813 ),814 );815}816817/// Absolute paths should be normalized818#[test]819fn test_normalized_absolute_path() {820 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);821822 te.assert_output_subdirectory(823 "one",824 &["--absolute-path", "foo", ".."],825 &format!(826 "{abs_path}/a.foo827 {abs_path}/one/b.foo828 {abs_path}/one/two/c.foo829 {abs_path}/one/two/C.Foo2830 {abs_path}/one/two/three/d.foo831 {abs_path}/one/two/three/directory_foo",832 abs_path = &abs_path833 ),834 );835}836837/// File type filter (--type)838#[test]839fn test_type() {840 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);841842 te.assert_output(843 &["--type", "f"],844 "a.foo845 e1 e2846 one/b.foo847 one/two/c.foo848 one/two/C.Foo2849 one/two/three/d.foo",850 );851852 te.assert_output(&["--type", "f", "e1"], "e1 e2");853854 te.assert_output(855 &["--type", "d"],856 "one857 one/two858 one/two/three859 one/two/three/directory_foo",860 );861862 te.assert_output(863 &["--type", "d", "--type", "l"],864 "one865 one/two866 one/two/three867 one/two/three/directory_foo868 symlink",869 );870871 te.assert_output(&["--type", "l"], "symlink");872}873874/// Test `--type executable`875#[cfg(unix)]876#[test]877fn test_type_executable() {878 use std::os::unix::fs::OpenOptionsExt;879880 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);881882 fs::OpenOptions::new()883 .create(true)884 .write(true)885 .mode(0o777)886 .open(te.test_root().join("executable-file.sh"))887 .unwrap();888889 te.assert_output(&["--type", "executable"], "executable-file.sh");890891 te.assert_output(892 &["--type", "executable", "--type", "directory"],893 "executable-file.sh894 one895 one/two896 one/two/three897 one/two/three/directory_foo",898 );899}900901/// Test `--type empty`902#[test]903fn test_type_empty() {904 let te = TestEnv::new(&["dir_empty", "dir_nonempty"], &[]);905906 create_file_with_size(te.test_root().join("0_bytes.foo"), 0);907 create_file_with_size(te.test_root().join("5_bytes.foo"), 5);908909 create_file_with_size(te.test_root().join("dir_nonempty").join("2_bytes.foo"), 2);910911 te.assert_output(912 &["--type", "empty"],913 "0_bytes.foo914 dir_empty",915 );916917 te.assert_output(918 &["--type", "empty", "--type", "file", "--type", "directory"],919 "0_bytes.foo920 dir_empty",921 );922923 te.assert_output(&["--type", "empty", "--type", "file"], "0_bytes.foo");924925 te.assert_output(&["--type", "empty", "--type", "directory"], "dir_empty");926}927928/// File extension (--extension)929#[test]930fn test_extension() {931 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);932933 te.assert_output(934 &["--extension", "foo"],935 "a.foo936 one/b.foo937 one/two/c.foo938 one/two/three/d.foo",939 );940941 te.assert_output(942 &["--extension", ".foo"],943 "a.foo944 one/b.foo945 one/two/c.foo946 one/two/three/d.foo",947 );948949 te.assert_output(950 &["--extension", ".foo", "--extension", "foo2"],951 "a.foo952 one/b.foo953 one/two/c.foo954 one/two/three/d.foo955 one/two/C.Foo2",956 );957958 te.assert_output(&["--extension", ".foo", "a"], "a.foo");959960 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");961962 let te2 = TestEnv::new(&[], &["spam.bar.baz", "egg.bar.baz", "yolk.bar.baz.sig"]);963964 te2.assert_output(965 &["--extension", ".bar.baz"],966 "spam.bar.baz967 egg.bar.baz",968 );969970 te2.assert_output(&["--extension", "sig"], "yolk.bar.baz.sig");971972 te2.assert_output(&["--extension", "bar.baz.sig"], "yolk.bar.baz.sig");973974 let te3 = TestEnv::new(&[], &["latin1.e\u{301}xt", "smiley.☻"]);975976 te3.assert_output(&["--extension", "☻"], "smiley.☻");977978 te3.assert_output(&["--extension", ".e\u{301}xt"], "latin1.e\u{301}xt");979980 let te4 = TestEnv::new(&[], &[".hidden", "test.hidden"]);981982 te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");983}984985/// Symlink as search directory986#[test]987fn test_symlink_as_root() {988 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);989 te.create_broken_symlink("broken_symlink")990 .expect("Failed to create broken symlink.");991992 // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html993 // The getcwd() function shall place an absolute pathname of the current working directory in994 // the array pointed to by buf, and return buf. The pathname shall contain no components that995 // are dot or dot-dot, or are symbolic links.996 //997 // Key points:998 // 1. The path of the current working directory of a Unix process cannot contain symlinks.999 // 2. The path of the current working directory of a Windows process can contain symlinks.1000 //1001 // More:1002 // 1. On Windows, symlinks are resolved after the ".." component.1003 // 2. On Unix, symlinks are resolved immediately as encountered.10041005 let parent_parent = if cfg!(windows) { ".." } else { "../.." };1006 te.assert_output_subdirectory(1007 "symlink",1008 &["", parent_parent],1009 &format!(1010 "{dir}/a.foo1011 {dir}/broken_symlink1012 {dir}/e1 e21013 {dir}/one1014 {dir}/one/b.foo1015 {dir}/one/two1016 {dir}/one/two/c.foo1017 {dir}/one/two/C.Foo21018 {dir}/one/two/three1019 {dir}/one/two/three/d.foo1020 {dir}/one/two/three/directory_foo1021 {dir}/symlink",1022 dir = &parent_parent1023 ),1024 );1025}10261027#[test]1028fn test_symlink_and_absolute_path() {1029 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);10301031 te.assert_output_subdirectory(1032 "symlink",1033 &["--absolute-path"],1034 &format!(1035 "{abs_path}/one/two/c.foo1036 {abs_path}/one/two/C.Foo21037 {abs_path}/one/two/three1038 {abs_path}/one/two/three/d.foo1039 {abs_path}/one/two/three/directory_foo",1040 abs_path = &abs_path1041 ),1042 );1043}10441045#[test]1046fn test_symlink_as_absolute_root() {1047 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);10481049 te.assert_output(1050 &["", &format!("{abs_path}/symlink", abs_path = abs_path)],1051 &format!(1052 "{abs_path}/symlink/c.foo1053 {abs_path}/symlink/C.Foo21054 {abs_path}/symlink/three1055 {abs_path}/symlink/three/d.foo1056 {abs_path}/symlink/three/directory_foo",1057 abs_path = &abs_path1058 ),1059 );1060}10611062#[test]1063fn test_symlink_and_full_path() {1064 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1065 let root = te.system_root();1066 let prefix = escape(&root.to_string_lossy());10671068 te.assert_output_subdirectory(1069 "symlink",1070 &[1071 "--absolute-path",1072 "--full-path",1073 &format!("^{prefix}.*three", prefix = prefix),1074 ],1075 &format!(1076 "{abs_path}/one/two/three1077 {abs_path}/one/two/three/d.foo1078 {abs_path}/one/two/three/directory_foo",1079 abs_path = &abs_path1080 ),1081 );1082}10831084#[test]1085fn test_symlink_and_full_path_abs_path() {1086 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1087 let root = te.system_root();1088 let prefix = escape(&root.to_string_lossy());1089 te.assert_output(1090 &[1091 "--full-path",1092 &format!("^{prefix}.*symlink.*three", prefix = prefix),1093 &format!("{abs_path}/symlink", abs_path = abs_path),1094 ],1095 &format!(1096 "{abs_path}/symlink/three1097 {abs_path}/symlink/three/d.foo1098 {abs_path}/symlink/three/directory_foo",1099 abs_path = &abs_path1100 ),1101 );1102}1103/// Exclude patterns (--exclude)1104#[test]1105fn test_excludes() {1106 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);11071108 te.assert_output(1109 &["--exclude", "*.foo"],1110 "one1111 one/two1112 one/two/C.Foo21113 one/two/three1114 one/two/three/directory_foo1115 e1 e21116 symlink",1117 );11181119 te.assert_output(1120 &["--exclude", "*.foo", "--exclude", "*.Foo2"],1121 "one1122 one/two1123 one/two/three1124 one/two/three/directory_foo1125 e1 e21126 symlink",1127 );11281129 te.assert_output(1130 &["--exclude", "*.foo", "--exclude", "*.Foo2", "foo"],1131 "one/two/three/directory_foo",1132 );11331134 te.assert_output(1135 &["--exclude", "one/two", "foo"],1136 "a.foo1137 one/b.foo",1138 );11391140 te.assert_output(1141 &["--exclude", "one/**/*.foo"],1142 "a.foo1143 e1 e21144 one1145 one/two1146 one/two/C.Foo21147 one/two/three1148 one/two/three/directory_foo1149 symlink",1150 );1151}11521153/// Shell script execution (--exec)1154#[test]1155fn test_exec() {1156 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1157 // TODO Windows tests: D:file.txt \file.txt \\server\share\file.txt ...1158 if !cfg!(windows) {1159 te.assert_output(1160 &["--absolute-path", "foo", "--exec", "echo"],1161 &format!(1162 "{abs_path}/a.foo1163 {abs_path}/one/b.foo1164 {abs_path}/one/two/C.Foo21165 {abs_path}/one/two/c.foo1166 {abs_path}/one/two/three/d.foo1167 {abs_path}/one/two/three/directory_foo",1168 abs_path = &abs_path1169 ),1170 );11711172 te.assert_output(1173 &["foo", "--exec", "echo", "{}"],1174 "a.foo1175 one/b.foo1176 one/two/C.Foo21177 one/two/c.foo1178 one/two/three/d.foo1179 one/two/three/directory_foo",1180 );11811182 te.assert_output(1183 &["foo", "--exec", "echo", "{.}"],1184 "a1185 one/b1186 one/two/C1187 one/two/c1188 one/two/three/d1189 one/two/three/directory_foo",1190 );11911192 te.assert_output(1193 &["foo", "--exec", "echo", "{/}"],1194 "a.foo1195 b.foo1196 C.Foo21197 c.foo1198 d.foo1199 directory_foo",1200 );12011202 te.assert_output(1203 &["foo", "--exec", "echo", "{/.}"],1204 "a1205 b1206 C1207 c1208 d1209 directory_foo",1210 );12111212 te.assert_output(1213 &["foo", "--exec", "echo", "{//}"],1214 ".1215 one1216 one/two1217 one/two1218 one/two/three1219 one/two/three",1220 );12211222 te.assert_output(&["e1", "--exec", "printf", "%s.%s\n"], "e1 e2.");1223 }1224}12251226#[test]1227fn test_exec_batch() {1228 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1229 let te = te.normalize_line(true);12301231 // TODO Test for windows1232 if !cfg!(windows) {1233 te.assert_output(1234 &["--absolute-path", "foo", "--exec-batch", "echo"],1235 &format!(1236 "{abs_path}/a.foo {abs_path}/one/b.foo {abs_path}/one/two/C.Foo2 {abs_path}/one/two/c.foo {abs_path}/one/two/three/d.foo {abs_path}/one/two/three/directory_foo",1237 abs_path = &abs_path1238 ),1239 );12401241 te.assert_output(1242 &["foo", "--exec-batch", "echo", "{}"],1243 "a.foo one/b.foo one/two/C.Foo2 one/two/c.foo one/two/three/d.foo one/two/three/directory_foo",1244 );12451246 te.assert_output(1247 &["foo", "--exec-batch", "echo", "{/}"],1248 "a.foo b.foo C.Foo2 c.foo d.foo directory_foo",1249 );12501251 te.assert_output(1252 &["no_match", "--exec-batch", "echo", "Matched: ", "{/}"],1253 "",1254 );12551256 te.assert_failure_with_error(1257 &["foo", "--exec-batch", "echo", "{}", "{}"],1258 "[fd error]: Only one placeholder allowed for batch commands",1259 );12601261 te.assert_failure_with_error(1262 &["foo", "--exec-batch", "echo", "{/}", ";", "-x", "echo"],1263 "error: The argument '--exec <cmd>' cannot be used with '--exec-batch <cmd>'",1264 );12651266 te.assert_failure_with_error(1267 &["foo", "--exec-batch"],1268 "error: The argument '--exec-batch <cmd>' requires a value but none was supplied",1269 );12701271 te.assert_failure_with_error(1272 &["foo", "--exec-batch", "echo {}"],1273 "[fd error]: First argument of exec-batch is expected to be a fixed executable",1274 );1275 }1276}12771278/// Literal search (--fixed-strings)1279#[test]1280fn test_fixed_strings() {1281 let dirs = &["test1", "test2"];1282 let files = &["test1/a.foo", "test1/a_foo", "test2/Download (1).tar.gz"];1283 let te = TestEnv::new(dirs, files);12841285 // Regex search, dot is treated as "any character"1286 te.assert_output(1287 &["a.foo"],1288 "test1/a.foo1289 test1/a_foo",1290 );12911292 // Literal search, dot is treated as character1293 te.assert_output(&["--fixed-strings", "a.foo"], "test1/a.foo");12941295 // Regex search, parens are treated as group1296 te.assert_output(&["download (1)"], "");12971298 // Literal search, parens are treated as characters1299 te.assert_output(1300 &["--fixed-strings", "download (1)"],1301 "test2/Download (1).tar.gz",1302 );13031304 // Combine with --case-sensitive1305 te.assert_output(&["--fixed-strings", "--case-sensitive", "download (1)"], "");1306}13071308/// Filenames with invalid UTF-8 sequences1309#[cfg(target_os = "linux")]1310#[test]1311fn test_invalid_utf8() {1312 use std::ffi::OsStr;1313 use std::os::unix::ffi::OsStrExt;13141315 let dirs = &["test1"];1316 let files = &[];1317 let te = TestEnv::new(dirs, files);13181319 fs::File::create(1320 te.test_root()1321 .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),1322 )1323 .unwrap();13241325 te.assert_output(&["", "test1/"], "test1/test_�invalid.txt");13261327 te.assert_output(&["invalid", "test1/"], "test1/test_�invalid.txt");13281329 // Should not be found under a different extension1330 te.assert_output(&["-e", "zip", "", "test1/"], "");1331}13321333/// Filtering for file size (--size)1334#[test]1335fn test_size() {1336 let te = TestEnv::new(&[], &[]);13371338 create_file_with_size(te.test_root().join("0_bytes.foo"), 0);1339 create_file_with_size(te.test_root().join("11_bytes.foo"), 11);1340 create_file_with_size(te.test_root().join("30_bytes.foo"), 30);1341 create_file_with_size(te.test_root().join("3_kilobytes.foo"), 3 * 1000);1342 create_file_with_size(te.test_root().join("4_kibibytes.foo"), 4 * 1024);13431344 // Zero and non-zero sized files.1345 te.assert_output(1346 &["", "--size", "+0B"],1347 "0_bytes.foo1348 11_bytes.foo1349 30_bytes.foo1350 3_kilobytes.foo1351 4_kibibytes.foo",1352 );13531354 // Zero sized files.1355 te.assert_output(&["", "--size", "-0B"], "0_bytes.foo");13561357 // Files with 2 bytes or more.1358 te.assert_output(1359 &["", "--size", "+2B"],1360 "11_bytes.foo1361 30_bytes.foo1362 3_kilobytes.foo1363 4_kibibytes.foo",1364 );13651366 // Files with 2 bytes or less.1367 te.assert_output(&["", "--size", "-2B"], "0_bytes.foo");13681369 // Files with size between 1 byte and 11 bytes.1370 te.assert_output(&["", "--size", "+1B", "--size", "-11B"], "11_bytes.foo");13711372 // Files with size between 1 byte and 30 bytes.1373 te.assert_output(1374 &["", "--size", "+1B", "--size", "-30B"],1375 "11_bytes.foo1376 30_bytes.foo",1377 );13781379 // Combine with a search pattern1380 te.assert_output(&["^11_", "--size", "+1B", "--size", "-30B"], "11_bytes.foo");13811382 // Files with size between 12 and 30 bytes.1383 te.assert_output(&["", "--size", "+12B", "--size", "-30B"], "30_bytes.foo");13841385 // Files with size between 31 and 100 bytes.1386 te.assert_output(&["", "--size", "+31B", "--size", "-100B"], "");13871388 // Files with size between 3 kibibytes and 5 kibibytes.1389 te.assert_output(&["", "--size", "+3ki", "--size", "-5ki"], "4_kibibytes.foo");13901391 // Files with size between 3 kilobytes and 5 kilobytes.1392 te.assert_output(1393 &["", "--size", "+3k", "--size", "-5k"],1394 "3_kilobytes.foo1395 4_kibibytes.foo",1396 );13971398 // Files with size greater than 3 kilobytes and less than 3 kibibytes.1399 te.assert_output(&["", "--size", "+3k", "--size", "-3ki"], "3_kilobytes.foo");14001401 // Files with size equal 4 kibibytes.1402 te.assert_output(&["", "--size", "+4ki", "--size", "-4ki"], "4_kibibytes.foo");1403}14041405#[cfg(test)]1406fn create_file_with_modified<P: AsRef<Path>>(path: P, duration_in_secs: u64) {1407 let st = SystemTime::now() - Duration::from_secs(duration_in_secs);1408 let ft = filetime::FileTime::from_system_time(st);1409 fs::File::create(&path).expect("creation failed");1410 filetime::set_file_times(&path, ft, ft).expect("time modification failed");1411}14121413#[test]1414fn test_modified_relative() {1415 let te = TestEnv::new(&[], &[]);1416 create_file_with_modified(te.test_root().join("foo_0_now"), 0);1417 create_file_with_modified(te.test_root().join("bar_1_min"), 60);1418 create_file_with_modified(te.test_root().join("foo_10_min"), 600);1419 create_file_with_modified(te.test_root().join("bar_1_h"), 60 * 60);1420 create_file_with_modified(te.test_root().join("foo_2_h"), 2 * 60 * 60);1421 create_file_with_modified(te.test_root().join("bar_1_day"), 24 * 60 * 60);14221423 te.assert_output(1424 &["", "--changed-within", "15min"],1425 "foo_0_now1426 bar_1_min1427 foo_10_min",1428 );14291430 te.assert_output(1431 &["", "--change-older-than", "15min"],1432 "bar_1_h1433 foo_2_h1434 bar_1_day",1435 );14361437 te.assert_output(1438 &["foo", "--changed-within", "12h"],1439 "foo_0_now1440 foo_10_min1441 foo_2_h",1442 );1443}14441445#[cfg(test)]1446fn change_file_modified<P: AsRef<Path>>(path: P, iso_date: &str) {1447 let st = humantime::parse_rfc3339(iso_date).expect("invalid date");1448 let ft = filetime::FileTime::from_system_time(st);1449 filetime::set_file_times(path, ft, ft).expect("time modification failde");1450}14511452#[test]1453fn test_modified_asolute() {1454 let te = TestEnv::new(&[], &["15mar2018", "30dec2017"]);1455 change_file_modified(te.test_root().join("15mar2018"), "2018-03-15T12:00:00Z");1456 change_file_modified(te.test_root().join("30dec2017"), "2017-12-30T23:59:00Z");14571458 te.assert_output(1459 &["", "--change-newer-than", "2018-01-01 00:00:00"],1460 "15mar2018",1461 );1462 te.assert_output(1463 &["", "--changed-before", "2018-01-01 00:00:00"],1464 "30dec2017",1465 );1466}14671468#[test]1469fn test_custom_path_separator() {1470 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14711472 te.assert_output(1473 &["foo", "one", "--path-separator", "="],1474 "one=b.foo1475 one=two=c.foo1476 one=two=C.Foo21477 one=two=three=d.foo1478 one=two=three=directory_foo",1479 );1480}14811482#[test]1483fn test_base_directory() {1484 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14851486 te.assert_output(1487 &["--base-directory", "one"],1488 "b.foo1489 two1490 two/c.foo1491 two/C.Foo21492 two/three1493 two/three/d.foo1494 two/three/directory_foo",1495 );14961497 te.assert_output(1498 &["--base-directory", "one/two", "foo"],1499 "c.foo1500 C.Foo21501 three/d.foo1502 three/directory_foo",1503 );15041505 // Explicit root path1506 te.assert_output(1507 &["--base-directory", "one", "foo", "two"],1508 "two/c.foo1509 two/C.Foo21510 two/three/d.foo1511 two/three/directory_foo",1512 );15131514 // Ignore base directory when absolute path is used1515 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1516 let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);1517 te.assert_output(1518 &["--base-directory", &abs_base_dir, "foo", &abs_path],1519 &format!(1520 "{abs_path}/a.foo1521 {abs_path}/one/b.foo1522 {abs_path}/one/two/c.foo1523 {abs_path}/one/two/C.Foo21524 {abs_path}/one/two/three/d.foo1525 {abs_path}/one/two/three/directory_foo",1526 abs_path = &abs_path1527 ),1528 );1529}15301531#[test]1532fn test_max_results() {1533 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);15341535 // Unrestricted1536 te.assert_output(1537 &["--max-results=0", "c.foo"],1538 "one/two/C.Foo21539 one/two/c.foo",1540 );15411542 // Limited to two results1543 te.assert_output(1544 &["--max-results=2", "c.foo"],1545 "one/two/C.Foo21546 one/two/c.foo",1547 );15481549 // Limited to one result. We could find either C.Foo2 or c.foo1550 let assert_just_one_result_with_option = |option| {1551 let output = te.assert_success_and_get_output(".", &[option, "c.foo"]);1552 let stdout = String::from_utf8_lossy(&output.stdout)1553 .trim()1554 .replace(&std::path::MAIN_SEPARATOR.to_string(), "/");1555 assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo");1556 };1557 assert_just_one_result_with_option("--max-results=1");1558 assert_just_one_result_with_option("-1");1559}15601561/// Filenames with non-utf8 paths are passed to the executed program unchanged1562///1563/// Note:1564/// - the test is disabled on Darwin/OSX, since it coerces file names to UTF-8,1565/// even when the requested file name is not valid UTF-8.1566/// - the test is currently disabled on Windows because I'm not sure how to create1567/// invalid UTF-8 files on Windows1568#[cfg(all(unix, not(target_os = "macos")))]1569#[test]1570fn test_exec_invalid_utf8() {1571 use std::ffi::OsStr;1572 use std::os::unix::ffi::OsStrExt;15731574 let dirs = &["test1"];1575 let files = &[];1576 let te = TestEnv::new(dirs, files);15771578 fs::File::create(1579 te.test_root()1580 .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),1581 )1582 .unwrap();15831584 te.assert_output_raw(1585 &["", "test1/", "--exec", "echo", "{}"],1586 b"test1/test_\xFEinvalid.txt\n",1587 );15881589 te.assert_output_raw(1590 &["", "test1/", "--exec", "echo", "{/}"],1591 b"test_\xFEinvalid.txt\n",1592 );15931594 te.assert_output_raw(&["", "test1/", "--exec", "echo", "{//}"], b"test1\n");15951596 te.assert_output_raw(1597 &["", "test1/", "--exec", "echo", "{.}"],1598 b"test1/test_\xFEinvalid\n",1599 );16001601 te.assert_output_raw(1602 &["", "test1/", "--exec", "echo", "{/.}"],1603 b"test_\xFEinvalid\n",1604 );1605}16061607#[test]1608fn test_list_details() {1609 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);16101611 // Make sure we can execute 'fd --list-details' without any errors.1612 te.assert_success_and_get_output(".", &["--list-details"]);1613}