realpath.sh/test.sh

56 lines
1.2 KiB
Bash
Raw Normal View History

2018-03-14 00:26:12 +01:00
#!/bin/sh
# Give in the command to test against as argument(s)
2018-03-20 19:05:56 +01:00
# Path needs to be absolute!
# Examples: ./test.sh readlink -f
# ./test.sh cat
2018-03-20 19:05:56 +01:00
# ./test.sh $PWD/otherscript.sh
bin="$@"
[ -z "$bin" ] && bin=$PWD/realpath.sh
2018-03-14 00:26:12 +01:00
dir="$(mktemp -d)"
cd "$dir"
do_test() {
2018-03-20 18:40:47 +01:00
name=$2
should="$3:$4"
2018-03-14 00:26:12 +01:00
is=$($bin "$name")
2018-03-20 18:40:47 +01:00
is="$is:$?"
2018-03-14 00:26:12 +01:00
if [ "$is" = "$should" ]; then
2018-03-14 00:26:12 +01:00
echo "Test $1 success"
else
echo "Test $1 fail: \"${bin##*/} $name\" returned \"$is\" instead of \"$should\""
2018-03-20 18:40:47 +01:00
#stat "$name"
2018-03-14 00:26:12 +01:00
fi
}
2018-03-20 18:40:47 +01:00
touch file
mkdir dir
ln -s dir dir_symlink
ln -s file file_symlink
ln -s none invalid_symlink
ln -s . dir/samedir
ln -s .. dir/parent
do_test 1 file "$dir"/file 0
do_test 2 dir "$dir"/dir 0
do_test 3 dir_symlink "$dir"/dir 0
do_test 4 file_symlink "$dir"/file 0
do_test 5 invalid_symlink "" 1
do_test 6 invalid_symlink/fail "" 1
do_test 7 dir/samedir "$dir"/dir 0
do_test 8 dir/parent "$dir" 0
do_test 9 "$dir"/file "$dir"/file 0
do_test 10 "$dir"/dir "$dir"/dir 0
do_test 11 "$dir"/dir_symlink "$dir"/dir 0
do_test 12 "$dir"/file_symlink "$dir"/file 0
do_test 13 "$dir"/invalid_symlink "" 1
do_test 14 "$dir"/invalid_symlink/fail "" 1
do_test 15 "$dir"/dir/samedir "$dir"/dir 0
do_test 16 "$dir"/dir/parent "$dir" 0
2018-03-14 00:26:12 +01:00
rm -rf "$dir"