Add more unit tests, search directories and symlinks too

This commit is contained in:
sharkdp 2017-05-24 22:16:55 +02:00
parent 10cd964c1d
commit 0757a50c31
2 changed files with 96 additions and 57 deletions

View File

@ -116,8 +116,6 @@ fn scan(root: &Path, pattern: &Regex, config: &FdOptions) {
if config.search_full_path { if config.search_full_path {
path_rel.to_str() path_rel.to_str()
} else { } else {
if !path_rel.is_file() { continue }
path_rel.file_name() path_rel.file_name()
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
}; };
@ -198,7 +196,7 @@ fn main() {
opts.optflag("s", "sensitive", opts.optflag("s", "sensitive",
"case-sensitive search (default: smart case)"); "case-sensitive search (default: smart case)");
opts.optflag("p", "full-path", opts.optflag("p", "full-path",
"search full path (default: filename only)"); "search full path (default: file-/dirname only)");
opts.optflag("H", "hidden", opts.optflag("H", "hidden",
"search hidden files/directories"); "search hidden files/directories");
opts.optflag("I", "no-ignore", opts.optflag("I", "no-ignore",

View File

@ -40,7 +40,7 @@ expect() {
diff -C3 --label expected --label actual --color \ diff -C3 --label expected --label actual --color \
"$tmp_expected" "$tmp_output" || true "$tmp_expected" "$tmp_output" || true
# exit 1 exit 1
fi fi
} }
@ -52,84 +52,125 @@ cd "$root"
mkdir -p one/two/three mkdir -p one/two/three
touch a.cpp touch a.foo
touch one/b.cpp touch one/b.foo
touch one/two/c.cpp touch one/two/c.foo
touch one/two/C.cpp touch one/two/C.Foo
touch one/two/three/d.cpp touch one/two/three/d.foo
touch ignored.cpp mkdir one/two/three/directory_foo
touch ignored.foo
touch .hidden.cpp touch .hidden.foo
echo "ignored.cpp" > .gitignore
ln -s one/two symlink ln -s one/two symlink
echo "ignored.foo" > .gitignore
# Run the tests # Run the tests
suite "Simple tests" suite "Simple tests"
expect "a.cpp" a.cpp expect "a.foo" a.foo
expect "one/b.cpp" b.cpp expect "one/b.foo" b.foo
expect "one/two/three/d.cpp" d.cpp expect "one/two/three/d.foo" d.foo
expect "a.cpp expect "a.foo
one/b.cpp one/b.foo
one/two/c.cpp one/two/c.foo
one/two/C.cpp one/two/C.Foo
one/two/three/d.cpp" cpp one/two/three/d.foo
one/two/three/directory_foo" foo
expect "a.foo
one
one/b.foo
one/two
one/two/c.foo
one/two/C.Foo
one/two/three
one/two/three/d.foo
one/two/three/directory_foo
symlink" # run 'fd' without arguments
suite "Regex searches"
expect "a.foo
one/b.foo
one/two/c.foo
one/two/C.Foo" '[a-c].foo'
expect "a.foo
one/b.foo
one/two/c.foo" --sensitive '[a-c].foo'
suite "Smart case" suite "Smart case"
expect "one/two/c.cpp expect "one/two/c.foo
one/two/C.cpp" c.cpp one/two/C.Foo" c.foo
expect "one/two/C.cpp" C.cpp expect "one/two/C.Foo" C.Foo
expect "one/two/C.Foo" Foo
suite "Case-sensitivity (--sensitive)" suite "Case-sensitivity (--sensitive)"
expect "one/two/C.cpp" --sensitive C.cpp expect "one/two/c.foo" --sensitive c.foo
expect "one/two/C.Foo" --sensitive C.Foo
suite "Full path search (--full-path)"
expect "one/two/three/d.foo
one/two/three/directory_foo" --full-path 'three.*foo'
expect "a.foo" --full-path '^a\.foo$'
suite "Hidden files (--hidden)" suite "Hidden files (--hidden)"
expect "a.cpp expect "a.foo
.hidden.cpp .hidden.foo
one/b.cpp one/b.foo
one/two/c.cpp one/two/c.foo
one/two/C.cpp one/two/C.Foo
one/two/three/d.cpp" --hidden cpp one/two/three/d.foo
one/two/three/directory_foo" --hidden foo
suite "Ignored files (--no-ignore)" suite "Ignored files (--no-ignore)"
expect "a.cpp expect "a.foo
ignored.cpp ignored.foo
one/b.cpp one/b.foo
one/two/c.cpp one/two/c.foo
one/two/C.cpp one/two/C.Foo
one/two/three/d.cpp" --no-ignore cpp one/two/three/d.foo
one/two/three/directory_foo" --no-ignore foo
expect "a.cpp expect "a.foo
.hidden.cpp .hidden.foo
ignored.cpp ignored.foo
one/b.cpp one/b.foo
one/two/c.cpp one/two/c.foo
one/two/C.cpp one/two/C.Foo
one/two/three/d.cpp" --hidden --no-ignore cpp one/two/three/d.foo
one/two/three/directory_foo" --hidden --no-ignore foo
suite "Symlinks (--follow)" suite "Symlinks (--follow)"
expect "one/two/c.cpp expect "one/two/c.foo
one/two/C.cpp one/two/C.Foo
symlink/c.cpp symlink/c.foo
symlink/C.cpp" --follow c.cpp symlink/C.Foo" --follow c.foo
suite "Maximum depth (--max-depth)" suite "Maximum depth (--max-depth)"
expect "a.cpp expect "a.foo
one/b.cpp one
one/two/c.cpp one/b.foo
one/two/C.cpp" --max-depth 3 one/two
expect "a.cpp one/two/c.foo
one/b.cpp" --max-depth 2 one/two/C.Foo
expect "a.cpp" --max-depth 1 one/two/three
symlink" --max-depth 3
expect "a.foo
one
one/b.foo
one/two
symlink" --max-depth 2
expect "a.foo
one
symlink" --max-depth 1
# All done # All done
echo echo