fd/tests/test.sh

236 lines
4.4 KiB
Bash
Raw Normal View History

2017-05-23 00:34:27 +02:00
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fd="${SCRIPT_DIR}/../target/debug/fd"
2017-06-13 21:08:02 +02:00
MKTEMP_TEMPLATE="fd-tests.XXXXXXXXXX"
2017-06-15 15:55:16 +02:00
# Stabilize sort
2017-07-02 15:49:59 +02:00
export LC_ALL="C.UTF-8"
2017-06-15 15:55:16 +02:00
export LC_CTYPE="UTF-8"
2017-05-23 00:34:27 +02:00
export reset='\x1b[0m'
export bold='\x1b[01m'
export green='\x1b[32;01m'
export red='\x1b[31;01m'
set -eou pipefail
suite() {
echo
echo -e "${bold}$1${reset}"
echo
}
expect() {
expected_output="$1"
shift
2017-06-13 21:08:02 +02:00
tmp_expected="$(mktemp -t "$MKTEMP_TEMPLATE")"
tmp_output="$(mktemp -t "$MKTEMP_TEMPLATE")"
2017-05-23 00:34:27 +02:00
echo "$expected_output" > "$tmp_expected"
"$fd" "$@" | sed -e 's/\x0/NULL\n/g' | sort -f > "$tmp_output"
2017-05-23 00:34:27 +02:00
echo -ne " ${bold}${reset} Testing 'fd $*' ... "
if diff -q "$tmp_expected" "$tmp_output" > /dev/null; then
echo -e "${green}✓ okay${reset}"
2017-06-13 21:08:02 +02:00
rm -f "$tmp_expected" "$tmp_output"
2017-05-23 00:34:27 +02:00
else
echo -e "${red}❌FAILED${reset}"
echo -ne "\nShowing diff between ${red}expected${reset} and "
echo -e "${green}actual${reset} output:\n"
2017-06-15 15:46:43 +02:00
diff -C3 --label expected --label actual \
2017-05-23 00:34:27 +02:00
"$tmp_expected" "$tmp_output" || true
2017-06-13 21:08:02 +02:00
rm -f "$tmp_expected" "$tmp_output"
exit 1
2017-05-23 00:34:27 +02:00
fi
}
2017-06-13 21:08:02 +02:00
root=$(mktemp -d -t "$MKTEMP_TEMPLATE")
2017-05-23 00:34:27 +02:00
cd "$root"
# Setup test environment
mkdir -p one/two/three
touch a.foo
touch one/b.foo
touch one/two/c.foo
2017-06-15 15:46:43 +02:00
touch one/two/C.Foo2
touch one/two/three/d.foo
mkdir one/two/three/directory_foo
touch ignored.foo
touch .hidden.foo
2017-05-23 00:34:27 +02:00
ln -s one/two symlink
2017-05-28 19:35:27 +02:00
echo "ignored.foo" > .ignore
2017-05-23 00:34:27 +02:00
# Run the tests
suite "Simple tests"
expect "a.foo" a.foo
expect "one/b.foo" b.foo
expect "one/two/three/d.foo" d.foo
expect "a.foo
one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo" foo
expect "a.foo
one
one/b.foo
one/two
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three
one/two/three/d.foo
one/two/three/directory_foo
symlink" # run 'fd' without arguments
suite "Explicit root path"
expect "one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo" foo one
expect "one/two/three/d.foo
one/two/three/directory_foo" foo one/two/three
(
cd one/two
expect "../../a.foo
../b.foo
c.foo
2017-06-15 15:46:43 +02:00
C.Foo2
three/d.foo
three/directory_foo" foo ../../
)
suite "Regex searches"
expect "a.foo
one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2" '[a-c].foo'
expect "a.foo
one/b.foo
one/two/c.foo" --case-sensitive '[a-c].foo'
2017-05-23 00:34:27 +02:00
suite "Smart case"
expect "one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2" c.foo
expect "one/two/C.Foo2" C.Foo
expect "one/two/C.Foo2" Foo
2017-05-23 00:34:27 +02:00
suite "Case-sensitivity (--case-sensitive)"
expect "one/two/c.foo" --case-sensitive c.foo
2017-06-15 15:46:43 +02:00
expect "one/two/C.Foo2" --case-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$'
2017-05-23 00:34:27 +02:00
suite "Hidden files (--hidden)"
2017-06-15 15:46:43 +02:00
expect ".hidden.foo
a.foo
one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo" --hidden foo
2017-05-23 00:34:27 +02:00
suite "Ignored files (--no-ignore)"
expect "a.foo
ignored.foo
one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo" --no-ignore foo
2017-06-15 15:46:43 +02:00
expect ".hidden.foo
a.foo
ignored.foo
one/b.foo
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo" --hidden --no-ignore foo
2017-05-23 00:34:27 +02:00
suite "Symlinks (--follow)"
expect "one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
symlink/c.foo
2017-06-15 15:46:43 +02:00
symlink/C.Foo2" --follow c.foo
2017-05-23 00:34:27 +02:00
2017-07-19 23:10:06 +02:00
suite "Null separator (--print0)"
expect "a.fooNULL
one/b.fooNULL
one/two/C.Foo2NULL
one/two/c.fooNULL
one/two/three/d.fooNULL
one/two/three/directory_fooNULL" --print0 foo
2017-07-19 23:10:06 +02:00
2017-05-23 00:34:27 +02:00
suite "Maximum depth (--max-depth)"
expect "a.foo
one
one/b.foo
one/two
one/two/c.foo
2017-06-15 15:46:43 +02:00
one/two/C.Foo2
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
2017-05-23 00:34:27 +02:00
2017-06-15 15:46:43 +02:00
abs_path=$(python -c "import os; print(os.path.realpath('$root'))")
suite "Absolute paths (--absolute-path)"
2017-06-15 15:46:43 +02:00
expect "$abs_path/a.foo
$abs_path/one/b.foo
$abs_path/one/two/c.foo
$abs_path/one/two/C.Foo2
$abs_path/one/two/three/d.foo
$abs_path/one/two/three/directory_foo" --absolute-path foo
expect "$abs_path/a.foo
$abs_path/one/b.foo
$abs_path/one/two/c.foo
$abs_path/one/two/C.Foo2
$abs_path/one/two/three/d.foo
$abs_path/one/two/three/directory_foo" foo "$abs_path"
if [[ "$OSTYPE" == "linux-gnu" ]]; then
suite "Invalid UTF-8"
touch "$(printf 'test-invalid-utf8-\xc3.txt')"
expect "$(printf 'test-invalid-utf8-\ufffd.txt')" test-invalid-utf8
fi
2017-06-11 15:28:18 +02:00
2017-05-23 00:34:27 +02:00
# All done
echo