tests.rs
This file
Path
tests/tests.rs
1,568 lines · 39.5 KiB · Rust
Last changed by
Add testsharkdpbefore adoption4287cdd
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: &'static [&'static str] = &["one/two/three", "one/two/three/directory_foo"];1314static DEFAULT_FILES: &'static [&'static 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 tests56#[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 );7374 te.assert_output(75 &[],76 "a.foo77 e1 e278 one79 one/b.foo80 one/two81 one/two/c.foo82 one/two/C.Foo283 one/two/three84 one/two/three/d.foo85 one/two/three/directory_foo86 symlink",87 );88}8990/// Test multiple directory searches91#[test]92fn test_multi_file() {93 let dirs = &["test1", "test2"];94 let files = &["test1/a.foo", "test1/b.foo", "test2/a.foo"];95 let te = TestEnv::new(dirs, files);96 te.assert_output(97 &["a.foo", "test1", "test2"],98 "test1/a.foo99 test2/a.foo",100 );101102 te.assert_output(103 &["", "test1", "test2"],104 "test1/a.foo105 test2/a.foo106 test1/b.foo",107 );108109 te.assert_output(&["a.foo", "test1"], "test1/a.foo");110111 te.assert_output(&["b.foo", "test1", "test2"], "test1/b.foo");112}113114/// Explicit root path115#[test]116fn test_explicit_root_path() {117 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);118119 te.assert_output(120 &["foo", "one"],121 "one/b.foo122 one/two/c.foo123 one/two/C.Foo2124 one/two/three/d.foo125 one/two/three/directory_foo",126 );127128 te.assert_output(129 &["foo", "one/two/three"],130 "one/two/three/d.foo131 one/two/three/directory_foo",132 );133134 te.assert_output_subdirectory(135 "one/two",136 &["foo", "../../"],137 "../../a.foo138 ../../one/b.foo139 ../../one/two/c.foo140 ../../one/two/C.Foo2141 ../../one/two/three/d.foo142 ../../one/two/three/directory_foo",143 );144145 te.assert_output_subdirectory(146 "one/two/three",147 &["", ".."],148 "../c.foo149 ../C.Foo2150 ../three151 ../three/d.foo152 ../three/directory_foo",153 );154}155156/// Regex searches157#[test]158fn test_regex_searches() {159 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);160161 te.assert_output(162 &["[a-c].foo"],163 "a.foo164 one/b.foo165 one/two/c.foo166 one/two/C.Foo2",167 );168169 te.assert_output(170 &["--case-sensitive", "[a-c].foo"],171 "a.foo172 one/b.foo173 one/two/c.foo",174 );175}176177/// Smart case178#[test]179fn test_smart_case() {180 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);181182 te.assert_output(183 &["c.foo"],184 "one/two/c.foo185 one/two/C.Foo2",186 );187188 te.assert_output(&["C.Foo"], "one/two/C.Foo2");189190 te.assert_output(&["Foo"], "one/two/C.Foo2");191192 // Only literal uppercase chars should trigger case sensitivity.193 te.assert_output(194 &["\\Ac"],195 "one/two/c.foo196 one/two/C.Foo2",197 );198 te.assert_output(&["\\AC"], "one/two/C.Foo2");199}200201/// Case sensitivity (--case-sensitive)202#[test]203fn test_case_sensitive() {204 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);205206 te.assert_output(&["--case-sensitive", "c.foo"], "one/two/c.foo");207208 te.assert_output(&["--case-sensitive", "C.Foo"], "one/two/C.Foo2");209210 te.assert_output(211 &["--ignore-case", "--case-sensitive", "C.Foo"],212 "one/two/C.Foo2",213 );214}215216/// Case insensitivity (--ignore-case)217#[test]218fn test_case_insensitive() {219 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);220221 te.assert_output(222 &["--ignore-case", "C.Foo"],223 "one/two/c.foo224 one/two/C.Foo2",225 );226227 te.assert_output(228 &["--case-sensitive", "--ignore-case", "C.Foo"],229 "one/two/c.foo230 one/two/C.Foo2",231 );232}233234/// Glob-based searches (--glob)235#[test]236fn test_glob_searches() {237 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);238239 te.assert_output(240 &["--glob", "*.foo"],241 "a.foo242 one/b.foo243 one/two/c.foo244 one/two/three/d.foo",245 );246247 te.assert_output(248 &["--glob", "[a-c].foo"],249 "a.foo250 one/b.foo251 one/two/c.foo",252 );253254 te.assert_output(255 &["--glob", "[a-c].foo*"],256 "a.foo257 one/b.foo258 one/two/C.Foo2259 one/two/c.foo",260 );261}262263/// Glob-based searches (--glob) in combination with full path searches (--full-path)264#[cfg(not(windows))] // TODO: make this work on Windows265#[test]266fn test_full_path_glob_searches() {267 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);268269 te.assert_output(270 &["--glob", "--full-path", "**/one/**/*.foo"],271 "one/b.foo272 one/two/c.foo273 one/two/three/d.foo",274 );275276 te.assert_output(277 &["--glob", "--full-path", "**/one/*/*.foo"],278 " one/two/c.foo",279 );280281 te.assert_output(282 &["--glob", "--full-path", "**/one/*/*/*.foo"],283 " one/two/three/d.foo",284 );285}286287#[test]288fn test_smart_case_glob_searches() {289 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);290291 te.assert_output(292 &["--glob", "c.foo*"],293 "one/two/C.Foo2294 one/two/c.foo",295 );296297 te.assert_output(&["--glob", "C.Foo*"], "one/two/C.Foo2");298}299300/// Glob-based searches (--glob) in combination with --case-sensitive301#[test]302fn test_case_sensitive_glob_searches() {303 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);304305 te.assert_output(&["--glob", "--case-sensitive", "c.foo*"], "one/two/c.foo");306}307308/// Glob-based searches (--glob) in combination with --extension309#[test]310fn test_glob_searches_with_extension() {311 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);312313 te.assert_output(314 &["--glob", "--extension", "foo2", "[a-z].*"],315 "one/two/C.Foo2",316 );317}318319/// Make sure that --regex overrides --glob320#[test]321fn test_regex_overrides_glob() {322 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);323324 te.assert_output(&["--glob", "--regex", "Foo2$"], "one/two/C.Foo2");325}326327/// Full path search (--full-path)328#[test]329fn test_full_path() {330 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);331332 let root = te.system_root();333 let prefix = escape(&root.to_string_lossy());334335 te.assert_output(336 &[337 "--full-path",338 &format!("^{prefix}.*three.*foo$", prefix = prefix),339 ],340 "one/two/three/d.foo341 one/two/three/directory_foo",342 );343}344345/// Hidden files (--hidden)346#[test]347fn test_hidden() {348 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);349350 te.assert_output(351 &["--hidden", "foo"],352 ".hidden.foo353 a.foo354 one/b.foo355 one/two/c.foo356 one/two/C.Foo2357 one/two/three/d.foo358 one/two/three/directory_foo",359 );360}361362/// Hidden file attribute on Windows363#[cfg(windows)]364#[test]365fn test_hidden_file_attribute() {366 use std::os::windows::fs::OpenOptionsExt;367368 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);369370 // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa371 const FILE_ATTRIBUTE_HIDDEN: u32 = 2;372373 fs::OpenOptions::new()374 .create(true)375 .write(true)376 .attributes(FILE_ATTRIBUTE_HIDDEN)377 .open(te.test_root().join("hidden-file.txt"))378 .unwrap();379380 te.assert_output(&["--hidden", "hidden-file.txt"], "hidden-file.txt");381 te.assert_output(&["hidden-file.txt"], "");382}383384/// Ignored files (--no-ignore)385#[test]386fn test_no_ignore() {387 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);388389 te.assert_output(390 &["--no-ignore", "foo"],391 "a.foo392 fdignored.foo393 gitignored.foo394 one/b.foo395 one/two/c.foo396 one/two/C.Foo2397 one/two/three/d.foo398 one/two/three/directory_foo",399 );400401 te.assert_output(402 &["--hidden", "--no-ignore", "foo"],403 ".hidden.foo404 a.foo405 fdignored.foo406 gitignored.foo407 one/b.foo408 one/two/c.foo409 one/two/C.Foo2410 one/two/three/d.foo411 one/two/three/directory_foo",412 );413}414415/// .gitignore and .fdignore416#[test]417fn test_gitignore_and_fdignore() {418 let files = &[419 "ignored-by-nothing",420 "ignored-by-fdignore",421 "ignored-by-gitignore",422 "ignored-by-both",423 ];424 let te = TestEnv::new(&[], files);425426 fs::File::create(te.test_root().join(".fdignore"))427 .unwrap()428 .write_all(b"ignored-by-fdignore\nignored-by-both")429 .unwrap();430431 fs::File::create(te.test_root().join(".gitignore"))432 .unwrap()433 .write_all(b"ignored-by-gitignore\nignored-by-both")434 .unwrap();435436 te.assert_output(&["ignored"], "ignored-by-nothing");437438 te.assert_output(439 &["--no-ignore-vcs", "ignored"],440 "ignored-by-nothing441 ignored-by-gitignore",442 );443444 te.assert_output(445 &["--no-ignore", "ignored"],446 "ignored-by-nothing447 ignored-by-fdignore448 ignored-by-gitignore449 ignored-by-both",450 );451}452453/// Precedence of .fdignore files454#[test]455fn test_custom_ignore_precedence() {456 let dirs = &["inner"];457 let files = &["inner/foo"];458 let te = TestEnv::new(dirs, files);459460 // Ignore 'foo' via .gitignore461 fs::File::create(te.test_root().join("inner/.gitignore"))462 .unwrap()463 .write_all(b"foo")464 .unwrap();465466 // Whitelist 'foo' via .fdignore467 fs::File::create(te.test_root().join(".fdignore"))468 .unwrap()469 .write_all(b"!foo")470 .unwrap();471472 te.assert_output(&["foo"], "inner/foo");473474 te.assert_output(&["--no-ignore-vcs", "foo"], "inner/foo");475476 te.assert_output(&["--no-ignore", "foo"], "inner/foo");477}478479/// VCS ignored files (--no-ignore-vcs)480#[test]481fn test_no_ignore_vcs() {482 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);483484 te.assert_output(485 &["--no-ignore-vcs", "foo"],486 "a.foo487 gitignored.foo488 one/b.foo489 one/two/c.foo490 one/two/C.Foo2491 one/two/three/d.foo492 one/two/three/directory_foo",493 );494}495496/// Custom ignore files (--ignore-file)497#[test]498fn test_custom_ignore_files() {499 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);500501 // Ignore 'C.Foo2' and everything in 'three'.502 fs::File::create(te.test_root().join("custom.ignore"))503 .unwrap()504 .write_all(b"C.Foo2\nthree")505 .unwrap();506507 te.assert_output(508 &["--ignore-file", "custom.ignore", "foo"],509 "a.foo510 one/b.foo511 one/two/c.foo",512 );513}514515/// Ignored files with ripgrep aliases (-u / -uu)516#[test]517fn test_no_ignore_aliases() {518 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);519520 te.assert_output(521 &["-u", "foo"],522 "a.foo523 fdignored.foo524 gitignored.foo525 one/b.foo526 one/two/c.foo527 one/two/C.Foo2528 one/two/three/d.foo529 one/two/three/directory_foo",530 );531532 te.assert_output(533 &["-uu", "foo"],534 ".hidden.foo535 a.foo536 fdignored.foo537 gitignored.foo538 one/b.foo539 one/two/c.foo540 one/two/C.Foo2541 one/two/three/d.foo542 one/two/three/directory_foo",543 );544}545546/// Symlinks (--follow)547#[test]548fn test_follow() {549 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);550551 te.assert_output(552 &["--follow", "c.foo"],553 "one/two/c.foo554 one/two/C.Foo2555 symlink/c.foo556 symlink/C.Foo2",557 );558}559560// File system boundaries (--one-file-system)561// Limited to Unix because, to the best of my knowledge, there is no easy way to test a use case562// file systems mounted into the tree on Windows.563// Not limiting depth causes massive delay under Darwin, see BurntSushi/ripgrep#1429564#[test]565#[cfg(unix)]566fn test_file_system_boundaries() {567 // Helper function to get the device ID for a given path568 // Inspired by https://github.com/BurntSushi/ripgrep/blob/8892bf648cfec111e6e7ddd9f30e932b0371db68/ignore/src/walk.rs#L1693569 fn device_num(path: impl AsRef<Path>) -> u64 {570 use std::os::unix::fs::MetadataExt;571572 path.as_ref().metadata().map(|md| md.dev()).unwrap()573 }574575 // Can't simulate file system boundaries576 let te = TestEnv::new(&[], &[]);577578 let dev_null = Path::new("/dev/null");579580 // /dev/null should exist in all sane Unixes. Skip if it doesn't exist for some reason.581 // Also skip should it be on the same device as the root partition for some reason.582 if !dev_null.is_file() || device_num(dev_null) == device_num("/") {583 return;584 }585586 te.assert_output(587 &["--full-path", "--max-depth", "2", "^/dev/null$", "/"],588 "/dev/null",589 );590 te.assert_output(591 &[592 "--one-file-system",593 "--full-path",594 "--max-depth",595 "2",596 "^/dev/null$",597 "/",598 ],599 "",600 );601}602603#[test]604fn test_follow_broken_symlink() {605 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);606 te.create_broken_symlink("broken_symlink")607 .expect("Failed to create broken symlink.");608609 te.assert_output(610 &["symlink"],611 "broken_symlink612 symlink",613 );614 te.assert_output(615 &["--type", "symlink", "symlink"],616 "broken_symlink617 symlink",618 );619620 te.assert_output(&["--type", "file", "symlink"], "");621622 te.assert_output(623 &["--follow", "--type", "symlink", "symlink"],624 "broken_symlink",625 );626 te.assert_output(&["--follow", "--type", "file", "symlink"], "");627}628629/// Null separator (--print0)630#[test]631fn test_print0() {632 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);633634 te.assert_output(635 &["--print0", "foo"],636 "a.fooNULL637 one/b.fooNULL638 one/two/C.Foo2NULL639 one/two/c.fooNULL640 one/two/three/d.fooNULL641 one/two/three/directory_fooNULL",642 );643}644645/// Maximum depth (--max-depth)646#[test]647fn test_max_depth() {648 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);649650 te.assert_output(651 &["--max-depth", "3"],652 "a.foo653 e1 e2654 one655 one/b.foo656 one/two657 one/two/c.foo658 one/two/C.Foo2659 one/two/three660 symlink",661 );662663 te.assert_output(664 &["--max-depth", "2"],665 "a.foo666 e1 e2667 one668 one/b.foo669 one/two670 symlink",671 );672673 te.assert_output(674 &["--max-depth", "1"],675 "a.foo676 e1 e2677 one678 symlink",679 );680}681682/// Minimum depth (--min-depth)683#[test]684fn test_min_depth() {685 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);686687 te.assert_output(688 &["--min-depth", "3"],689 "one/two/c.foo690 one/two/C.Foo2691 one/two/three692 one/two/three/d.foo693 one/two/three/directory_foo",694 );695696 te.assert_output(697 &["--min-depth", "4"],698 "one/two/three/d.foo699 one/two/three/directory_foo",700 );701}702703/// Exact depth (--exact-depth)704#[test]705fn test_exact_depth() {706 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);707708 te.assert_output(709 &["--exact-depth", "3"],710 "one/two/c.foo711 one/two/C.Foo2712 one/two/three",713 );714}715716/// Absolute paths (--absolute-path)717#[test]718fn test_absolute_path() {719 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);720721 te.assert_output(722 &["--absolute-path"],723 &format!(724 "{abs_path}/a.foo725 {abs_path}/e1 e2726 {abs_path}/one727 {abs_path}/one/b.foo728 {abs_path}/one/two729 {abs_path}/one/two/c.foo730 {abs_path}/one/two/C.Foo2731 {abs_path}/one/two/three732 {abs_path}/one/two/three/d.foo733 {abs_path}/one/two/three/directory_foo734 {abs_path}/symlink",735 abs_path = &abs_path736 ),737 );738739 te.assert_output(740 &["--absolute-path", "foo"],741 &format!(742 "{abs_path}/a.foo743 {abs_path}/one/b.foo744 {abs_path}/one/two/c.foo745 {abs_path}/one/two/C.Foo2746 {abs_path}/one/two/three/d.foo747 {abs_path}/one/two/three/directory_foo",748 abs_path = &abs_path749 ),750 );751}752753/// Show absolute paths if the path argument is absolute754#[test]755fn test_implicit_absolute_path() {756 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);757758 te.assert_output(759 &["foo", &abs_path],760 &format!(761 "{abs_path}/a.foo762 {abs_path}/one/b.foo763 {abs_path}/one/two/c.foo764 {abs_path}/one/two/C.Foo2765 {abs_path}/one/two/three/d.foo766 {abs_path}/one/two/three/directory_foo",767 abs_path = &abs_path768 ),769 );770}771772/// Absolute paths should be normalized773#[test]774fn test_normalized_absolute_path() {775 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);776777 te.assert_output_subdirectory(778 "one",779 &["--absolute-path", "foo", ".."],780 &format!(781 "{abs_path}/a.foo782 {abs_path}/one/b.foo783 {abs_path}/one/two/c.foo784 {abs_path}/one/two/C.Foo2785 {abs_path}/one/two/three/d.foo786 {abs_path}/one/two/three/directory_foo",787 abs_path = &abs_path788 ),789 );790}791792/// File type filter (--type)793#[test]794fn test_type() {795 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);796797 te.assert_output(798 &["--type", "f"],799 "a.foo800 e1 e2801 one/b.foo802 one/two/c.foo803 one/two/C.Foo2804 one/two/three/d.foo",805 );806807 te.assert_output(&["--type", "f", "e1"], "e1 e2");808809 te.assert_output(810 &["--type", "d"],811 "one812 one/two813 one/two/three814 one/two/three/directory_foo",815 );816817 te.assert_output(818 &["--type", "d", "--type", "l"],819 "one820 one/two821 one/two/three822 one/two/three/directory_foo823 symlink",824 );825826 te.assert_output(&["--type", "l"], "symlink");827}828829/// Test `--type executable`830#[cfg(unix)]831#[test]832fn test_type_executable() {833 use std::os::unix::fs::OpenOptionsExt;834835 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);836837 fs::OpenOptions::new()838 .create(true)839 .write(true)840 .mode(0o777)841 .open(te.test_root().join("executable-file.sh"))842 .unwrap();843844 te.assert_output(&["--type", "executable"], "executable-file.sh");845846 te.assert_output(847 &["--type", "executable", "--type", "directory"],848 "executable-file.sh849 one850 one/two851 one/two/three852 one/two/three/directory_foo",853 );854}855856/// Test `--type empty`857#[test]858fn test_type_empty() {859 let te = TestEnv::new(&["dir_empty", "dir_nonempty"], &[]);860861 create_file_with_size(te.test_root().join("0_bytes.foo"), 0);862 create_file_with_size(te.test_root().join("5_bytes.foo"), 5);863864 create_file_with_size(te.test_root().join("dir_nonempty").join("2_bytes.foo"), 2);865866 te.assert_output(867 &["--type", "empty"],868 "0_bytes.foo869 dir_empty",870 );871872 te.assert_output(873 &["--type", "empty", "--type", "file", "--type", "directory"],874 "0_bytes.foo875 dir_empty",876 );877878 te.assert_output(&["--type", "empty", "--type", "file"], "0_bytes.foo");879880 te.assert_output(&["--type", "empty", "--type", "directory"], "dir_empty");881}882883/// File extension (--extension)884#[test]885fn test_extension() {886 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);887888 te.assert_output(889 &["--extension", "foo"],890 "a.foo891 one/b.foo892 one/two/c.foo893 one/two/three/d.foo",894 );895896 te.assert_output(897 &["--extension", ".foo"],898 "a.foo899 one/b.foo900 one/two/c.foo901 one/two/three/d.foo",902 );903904 te.assert_output(905 &["--extension", ".foo", "--extension", "foo2"],906 "a.foo907 one/b.foo908 one/two/c.foo909 one/two/three/d.foo910 one/two/C.Foo2",911 );912913 te.assert_output(&["--extension", ".foo", "a"], "a.foo");914915 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");916917 let te2 = TestEnv::new(&[], &["spam.bar.baz", "egg.bar.baz", "yolk.bar.baz.sig"]);918919 te2.assert_output(920 &["--extension", ".bar.baz"],921 "spam.bar.baz922 egg.bar.baz",923 );924925 te2.assert_output(&["--extension", "sig"], "yolk.bar.baz.sig");926927 te2.assert_output(&["--extension", "bar.baz.sig"], "yolk.bar.baz.sig");928929 let te3 = TestEnv::new(&[], &["latin1.e\u{301}xt", "smiley.☻"]);930931 te3.assert_output(&["--extension", "☻"], "smiley.☻");932933 te3.assert_output(&["--extension", ".e\u{301}xt"], "latin1.e\u{301}xt");934935 let te4 = TestEnv::new(&[], &[".hidden", "test.hidden"]);936937 te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");938}939940/// Symlink as search directory941#[test]942fn test_symlink_as_root() {943 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);944 te.create_broken_symlink("broken_symlink")945 .expect("Failed to create broken symlink.");946947 // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html948 // The getcwd() function shall place an absolute pathname of the current working directory in949 // the array pointed to by buf, and return buf. The pathname shall contain no components that950 // are dot or dot-dot, or are symbolic links.951 //952 // Key points:953 // 1. The path of the current working directory of a Unix process cannot contain symlinks.954 // 2. The path of the current working directory of a Windows process can contain symlinks.955 //956 // More:957 // 1. On Windows, symlinks are resolved after the ".." component.958 // 2. On Unix, symlinks are resolved immediately as encountered.959960 let parent_parent = if cfg!(windows) { ".." } else { "../.." };961 te.assert_output_subdirectory(962 "symlink",963 &["", parent_parent],964 &format!(965 "{dir}/a.foo966 {dir}/broken_symlink967 {dir}/e1 e2968 {dir}/one969 {dir}/one/b.foo970 {dir}/one/two971 {dir}/one/two/c.foo972 {dir}/one/two/C.Foo2973 {dir}/one/two/three974 {dir}/one/two/three/d.foo975 {dir}/one/two/three/directory_foo976 {dir}/symlink",977 dir = &parent_parent978 ),979 );980}981982#[test]983fn test_symlink_and_absolute_path() {984 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);985986 te.assert_output_subdirectory(987 "symlink",988 &["--absolute-path"],989 &format!(990 "{abs_path}/one/two/c.foo991 {abs_path}/one/two/C.Foo2992 {abs_path}/one/two/three993 {abs_path}/one/two/three/d.foo994 {abs_path}/one/two/three/directory_foo",995 abs_path = &abs_path996 ),997 );998}9991000#[test]1001fn test_symlink_as_absolute_root() {1002 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);10031004 te.assert_output(1005 &["", &format!("{abs_path}/symlink", abs_path = abs_path)],1006 &format!(1007 "{abs_path}/symlink/c.foo1008 {abs_path}/symlink/C.Foo21009 {abs_path}/symlink/three1010 {abs_path}/symlink/three/d.foo1011 {abs_path}/symlink/three/directory_foo",1012 abs_path = &abs_path1013 ),1014 );1015}10161017#[test]1018fn test_symlink_and_full_path() {1019 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1020 let root = te.system_root();1021 let prefix = escape(&root.to_string_lossy());10221023 te.assert_output_subdirectory(1024 "symlink",1025 &[1026 "--absolute-path",1027 "--full-path",1028 &format!("^{prefix}.*three", prefix = prefix),1029 ],1030 &format!(1031 "{abs_path}/one/two/three1032 {abs_path}/one/two/three/d.foo1033 {abs_path}/one/two/three/directory_foo",1034 abs_path = &abs_path1035 ),1036 );1037}10381039#[test]1040fn test_symlink_and_full_path_abs_path() {1041 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1042 let root = te.system_root();1043 let prefix = escape(&root.to_string_lossy());1044 te.assert_output(1045 &[1046 "--full-path",1047 &format!("^{prefix}.*symlink.*three", prefix = prefix),1048 &format!("{abs_path}/symlink", abs_path = abs_path),1049 ],1050 &format!(1051 "{abs_path}/symlink/three1052 {abs_path}/symlink/three/d.foo1053 {abs_path}/symlink/three/directory_foo",1054 abs_path = &abs_path1055 ),1056 );1057}1058/// Exclude patterns (--exclude)1059#[test]1060fn test_excludes() {1061 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);10621063 te.assert_output(1064 &["--exclude", "*.foo"],1065 "one1066 one/two1067 one/two/C.Foo21068 one/two/three1069 one/two/three/directory_foo1070 e1 e21071 symlink",1072 );10731074 te.assert_output(1075 &["--exclude", "*.foo", "--exclude", "*.Foo2"],1076 "one1077 one/two1078 one/two/three1079 one/two/three/directory_foo1080 e1 e21081 symlink",1082 );10831084 te.assert_output(1085 &["--exclude", "*.foo", "--exclude", "*.Foo2", "foo"],1086 "one/two/three/directory_foo",1087 );10881089 te.assert_output(1090 &["--exclude", "one/two", "foo"],1091 "a.foo1092 one/b.foo",1093 );10941095 te.assert_output(1096 &["--exclude", "one/**/*.foo"],1097 "a.foo1098 e1 e21099 one1100 one/two1101 one/two/C.Foo21102 one/two/three1103 one/two/three/directory_foo1104 symlink",1105 );1106}11071108/// Shell script execution (--exec)1109#[test]1110fn test_exec() {1111 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1112 // TODO Windows tests: D:file.txt \file.txt \\server\share\file.txt ...1113 if !cfg!(windows) {1114 te.assert_output(1115 &["--absolute-path", "foo", "--exec", "echo"],1116 &format!(1117 "{abs_path}/a.foo1118 {abs_path}/one/b.foo1119 {abs_path}/one/two/C.Foo21120 {abs_path}/one/two/c.foo1121 {abs_path}/one/two/three/d.foo1122 {abs_path}/one/two/three/directory_foo",1123 abs_path = &abs_path1124 ),1125 );11261127 te.assert_output(1128 &["foo", "--exec", "echo", "{}"],1129 "a.foo1130 one/b.foo1131 one/two/C.Foo21132 one/two/c.foo1133 one/two/three/d.foo1134 one/two/three/directory_foo",1135 );11361137 te.assert_output(1138 &["foo", "--exec", "echo", "{.}"],1139 "a1140 one/b1141 one/two/C1142 one/two/c1143 one/two/three/d1144 one/two/three/directory_foo",1145 );11461147 te.assert_output(1148 &["foo", "--exec", "echo", "{/}"],1149 "a.foo1150 b.foo1151 C.Foo21152 c.foo1153 d.foo1154 directory_foo",1155 );11561157 te.assert_output(1158 &["foo", "--exec", "echo", "{/.}"],1159 "a1160 b1161 C1162 c1163 d1164 directory_foo",1165 );11661167 te.assert_output(1168 &["foo", "--exec", "echo", "{//}"],1169 ".1170 one1171 one/two1172 one/two1173 one/two/three1174 one/two/three",1175 );11761177 te.assert_output(&["e1", "--exec", "printf", "%s.%s\n"], "e1 e2.");1178 }1179}11801181#[test]1182fn test_exec_batch() {1183 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1184 let te = te.normalize_line(true);11851186 // TODO Test for windows1187 if !cfg!(windows) {1188 te.assert_output(1189 &["--absolute-path", "foo", "--exec-batch", "echo"],1190 &format!(1191 "{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",1192 abs_path = &abs_path1193 ),1194 );11951196 te.assert_output(1197 &["foo", "--exec-batch", "echo", "{}"],1198 "a.foo one/b.foo one/two/C.Foo2 one/two/c.foo one/two/three/d.foo one/two/three/directory_foo",1199 );12001201 te.assert_output(1202 &["foo", "--exec-batch", "echo", "{/}"],1203 "a.foo b.foo C.Foo2 c.foo d.foo directory_foo",1204 );12051206 te.assert_output(1207 &["no_match", "--exec-batch", "echo", "Matched: ", "{/}"],1208 "",1209 );12101211 te.assert_error(1212 &["foo", "--exec-batch", "echo", "{}", "{}"],1213 "[fd error]: Only one placeholder allowed for batch commands",1214 );12151216 te.assert_error(1217 &["foo", "--exec-batch", "echo", "{/}", ";", "-x", "echo"],1218 "error: The argument '--exec <cmd>' cannot be used with '--exec-batch <cmd>'",1219 );12201221 te.assert_error(1222 &["foo", "--exec-batch"],1223 "error: The argument '--exec-batch <cmd>' requires a value but none was supplied",1224 );12251226 te.assert_error(1227 &["foo", "--exec-batch", "echo {}"],1228 "[fd error]: First argument of exec-batch is expected to be a fixed executable",1229 );1230 }1231}12321233/// Literal search (--fixed-strings)1234#[test]1235fn test_fixed_strings() {1236 let dirs = &["test1", "test2"];1237 let files = &["test1/a.foo", "test1/a_foo", "test2/Download (1).tar.gz"];1238 let te = TestEnv::new(dirs, files);12391240 // Regex search, dot is treated as "any character"1241 te.assert_output(1242 &["a.foo"],1243 "test1/a.foo1244 test1/a_foo",1245 );12461247 // Literal search, dot is treated as character1248 te.assert_output(&["--fixed-strings", "a.foo"], "test1/a.foo");12491250 // Regex search, parens are treated as group1251 te.assert_output(&["download (1)"], "");12521253 // Literal search, parens are treated as characters1254 te.assert_output(1255 &["--fixed-strings", "download (1)"],1256 "test2/Download (1).tar.gz",1257 );12581259 // Combine with --case-sensitive1260 te.assert_output(&["--fixed-strings", "--case-sensitive", "download (1)"], "");1261}12621263/// Filenames with invalid UTF-8 sequences1264#[cfg(target_os = "linux")]1265#[test]1266fn test_invalid_utf8() {1267 use std::ffi::OsStr;1268 use std::os::unix::ffi::OsStrExt;12691270 let dirs = &["test1"];1271 let files = &[];1272 let te = TestEnv::new(dirs, files);12731274 fs::File::create(1275 te.test_root()1276 .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),1277 )1278 .unwrap();12791280 te.assert_output(&["", "test1/"], "test1/test_�invalid.txt");12811282 te.assert_output(&["invalid", "test1/"], "test1/test_�invalid.txt");12831284 // Should not be found under a different extension1285 te.assert_output(&["-e", "zip", "", "test1/"], "");1286}12871288/// Filtering for file size (--size)1289#[test]1290fn test_size() {1291 let te = TestEnv::new(&[], &[]);12921293 create_file_with_size(te.test_root().join("0_bytes.foo"), 0);1294 create_file_with_size(te.test_root().join("11_bytes.foo"), 11);1295 create_file_with_size(te.test_root().join("30_bytes.foo"), 30);1296 create_file_with_size(te.test_root().join("3_kilobytes.foo"), 3 * 1000);1297 create_file_with_size(te.test_root().join("4_kibibytes.foo"), 4 * 1024);12981299 // Zero and non-zero sized files.1300 te.assert_output(1301 &["", "--size", "+0B"],1302 "0_bytes.foo1303 11_bytes.foo1304 30_bytes.foo1305 3_kilobytes.foo1306 4_kibibytes.foo",1307 );13081309 // Zero sized files.1310 te.assert_output(&["", "--size", "-0B"], "0_bytes.foo");13111312 // Files with 2 bytes or more.1313 te.assert_output(1314 &["", "--size", "+2B"],1315 "11_bytes.foo1316 30_bytes.foo1317 3_kilobytes.foo1318 4_kibibytes.foo",1319 );13201321 // Files with 2 bytes or less.1322 te.assert_output(&["", "--size", "-2B"], "0_bytes.foo");13231324 // Files with size between 1 byte and 11 bytes.1325 te.assert_output(&["", "--size", "+1B", "--size", "-11B"], "11_bytes.foo");13261327 // Files with size between 1 byte and 30 bytes.1328 te.assert_output(1329 &["", "--size", "+1B", "--size", "-30B"],1330 "11_bytes.foo1331 30_bytes.foo",1332 );13331334 // Combine with a search pattern1335 te.assert_output(&["^11_", "--size", "+1B", "--size", "-30B"], "11_bytes.foo");13361337 // Files with size between 12 and 30 bytes.1338 te.assert_output(&["", "--size", "+12B", "--size", "-30B"], "30_bytes.foo");13391340 // Files with size between 31 and 100 bytes.1341 te.assert_output(&["", "--size", "+31B", "--size", "-100B"], "");13421343 // Files with size between 3 kibibytes and 5 kibibytes.1344 te.assert_output(&["", "--size", "+3ki", "--size", "-5ki"], "4_kibibytes.foo");13451346 // Files with size between 3 kilobytes and 5 kilobytes.1347 te.assert_output(1348 &["", "--size", "+3k", "--size", "-5k"],1349 "3_kilobytes.foo1350 4_kibibytes.foo",1351 );13521353 // Files with size greater than 3 kilobytes and less than 3 kibibytes.1354 te.assert_output(&["", "--size", "+3k", "--size", "-3ki"], "3_kilobytes.foo");13551356 // Files with size equal 4 kibibytes.1357 te.assert_output(&["", "--size", "+4ki", "--size", "-4ki"], "4_kibibytes.foo");1358}13591360#[cfg(test)]1361fn create_file_with_modified<P: AsRef<Path>>(path: P, duration_in_secs: u64) {1362 let st = SystemTime::now() - Duration::from_secs(duration_in_secs);1363 let ft = filetime::FileTime::from_system_time(st);1364 fs::File::create(&path).expect("creation failed");1365 filetime::set_file_times(&path, ft, ft).expect("time modification failed");1366}13671368#[test]1369fn test_modified_relative() {1370 let te = TestEnv::new(&[], &[]);1371 create_file_with_modified(te.test_root().join("foo_0_now"), 0);1372 create_file_with_modified(te.test_root().join("bar_1_min"), 60);1373 create_file_with_modified(te.test_root().join("foo_10_min"), 600);1374 create_file_with_modified(te.test_root().join("bar_1_h"), 60 * 60);1375 create_file_with_modified(te.test_root().join("foo_2_h"), 2 * 60 * 60);1376 create_file_with_modified(te.test_root().join("bar_1_day"), 24 * 60 * 60);13771378 te.assert_output(1379 &["", "--changed-within", "15min"],1380 "foo_0_now1381 bar_1_min1382 foo_10_min",1383 );13841385 te.assert_output(1386 &["", "--change-older-than", "15min"],1387 "bar_1_h1388 foo_2_h1389 bar_1_day",1390 );13911392 te.assert_output(1393 &["foo", "--changed-within", "12h"],1394 "foo_0_now1395 foo_10_min1396 foo_2_h",1397 );1398}13991400#[cfg(test)]1401fn change_file_modified<P: AsRef<Path>>(path: P, iso_date: &str) {1402 let st = humantime::parse_rfc3339(iso_date).expect("invalid date");1403 let ft = filetime::FileTime::from_system_time(st);1404 filetime::set_file_times(path, ft, ft).expect("time modification failde");1405}14061407#[test]1408fn test_modified_asolute() {1409 let te = TestEnv::new(&[], &["15mar2018", "30dec2017"]);1410 change_file_modified(te.test_root().join("15mar2018"), "2018-03-15T12:00:00Z");1411 change_file_modified(te.test_root().join("30dec2017"), "2017-12-30T23:59:00Z");14121413 te.assert_output(1414 &["", "--change-newer-than", "2018-01-01 00:00:00"],1415 "15mar2018",1416 );1417 te.assert_output(1418 &["", "--changed-before", "2018-01-01 00:00:00"],1419 "30dec2017",1420 );1421}14221423#[test]1424fn test_custom_path_separator() {1425 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14261427 te.assert_output(1428 &["foo", "one", "--path-separator", "="],1429 "one=b.foo1430 one=two=c.foo1431 one=two=C.Foo21432 one=two=three=d.foo1433 one=two=three=directory_foo",1434 );1435}14361437#[test]1438fn test_base_directory() {1439 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14401441 te.assert_output(1442 &["--base-directory", "one"],1443 "b.foo1444 two1445 two/c.foo1446 two/C.Foo21447 two/three1448 two/three/d.foo1449 two/three/directory_foo",1450 );14511452 te.assert_output(1453 &["--base-directory", "one/two", "foo"],1454 "c.foo1455 C.Foo21456 three/d.foo1457 three/directory_foo",1458 );14591460 // Explicit root path1461 te.assert_output(1462 &["--base-directory", "one", "foo", "two"],1463 "two/c.foo1464 two/C.Foo21465 two/three/d.foo1466 two/three/directory_foo",1467 );14681469 // Ignore base directory when absolute path is used1470 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1471 let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);1472 te.assert_output(1473 &["--base-directory", &abs_base_dir, "foo", &abs_path],1474 &format!(1475 "{abs_path}/a.foo1476 {abs_path}/one/b.foo1477 {abs_path}/one/two/c.foo1478 {abs_path}/one/two/C.Foo21479 {abs_path}/one/two/three/d.foo1480 {abs_path}/one/two/three/directory_foo",1481 abs_path = &abs_path1482 ),1483 );1484}14851486#[test]1487fn test_max_results() {1488 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);14891490 // Unrestricted1491 te.assert_output(1492 &["--max-results=0", "c.foo"],1493 "one/two/C.Foo21494 one/two/c.foo",1495 );14961497 // Limited to two results1498 te.assert_output(1499 &["--max-results=2", "c.foo"],1500 "one/two/C.Foo21501 one/two/c.foo",1502 );15031504 // Limited to one result. We could find either C.Foo2 or c.foo1505 let assert_just_one_result_with_option = |option| {1506 let output = te.assert_success_and_get_output(".", &[option, "c.foo"]);1507 let stdout = String::from_utf8_lossy(&output.stdout)1508 .trim()1509 .replace(&std::path::MAIN_SEPARATOR.to_string(), "/");1510 assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo");1511 };1512 assert_just_one_result_with_option("--max-results=1");1513 assert_just_one_result_with_option("-1");1514}15151516/// Filenames with non-utf8 paths are passed to the executed program unchanged1517///1518/// Note:1519/// - the test is disabled on Darwin/OSX, since it coerces file names to UTF-8,1520/// even when the requested file name is not valid UTF-8.1521/// - the test is currently disabled on Windows because I'm not sure how to create1522/// invalid UTF-8 files on Windows1523#[cfg(all(unix, not(target_os = "macos")))]1524#[test]1525fn test_exec_invalid_utf8() {1526 use std::ffi::OsStr;1527 use std::os::unix::ffi::OsStrExt;15281529 let dirs = &["test1"];1530 let files = &[];1531 let te = TestEnv::new(dirs, files);15321533 fs::File::create(1534 te.test_root()1535 .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),1536 )1537 .unwrap();15381539 te.assert_output_raw(1540 &["", "test1/", "--exec", "echo", "{}"],1541 b"test1/test_\xFEinvalid.txt\n",1542 );15431544 te.assert_output_raw(1545 &["", "test1/", "--exec", "echo", "{/}"],1546 b"test_\xFEinvalid.txt\n",1547 );15481549 te.assert_output_raw(&["", "test1/", "--exec", "echo", "{//}"], b"test1\n");15501551 te.assert_output_raw(1552 &["", "test1/", "--exec", "echo", "{.}"],1553 b"test1/test_\xFEinvalid\n",1554 );15551556 te.assert_output_raw(1557 &["", "test1/", "--exec", "echo", "{/.}"],1558 b"test_\xFEinvalid\n",1559 );1560}15611562#[test]1563fn test_list_details() {1564 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);15651566 // Make sure we can execute 'fd --list-details' without any errors.1567 te.assert_success_and_get_output(".", &["--list-details"]);1568}