Improve test suite to catch more cases

This commit is contained in:
Nero 2018-03-20 17:40:47 +00:00
parent c8db61d543
commit 49d87e3c40
2 changed files with 32 additions and 23 deletions

View File

@ -49,6 +49,8 @@ while [ -n "$left" ]; do
;; ;;
'') '')
# Readlink failed, its not a symlink # Readlink failed, its not a symlink
# If file does not exist, exit
[ -e "$result" ] || exit 1
;; ;;
*) *)
# Relative symlink: Keep $result, its the symlink base # Relative symlink: Keep $result, its the symlink base

53
test.sh
View File

@ -1,42 +1,49 @@
#!/bin/sh #!/bin/sh
bin=$PWD/realpath.sh bin=$PWD/realpath.sh
ref="readlink -f"
#ref="busybox readlink -f"
dir="$(mktemp -d)" dir="$(mktemp -d)"
cd "$dir" cd "$dir"
do_test() { do_test() {
name=$dir/$2 name=$2
target=$3 should="$3:$4"
case "$name" in
*/*) mkdir -p "${name%/*}";;
esac
if [ -n "$target" ]; then
ln -sfn "$target" "$name"
else
touch "$name"
fi
is=$($bin "$name") is=$($bin "$name")
is="$is:$?"
should=$(readlink -f "$name")
if [ "$is" == "$should" ]; then if [ "$is" == "$should" ]; then
echo "Test $1 success" echo "Test $1 success"
else else
echo "Test $1 fail: \"${bin##*/} $name\" returned \"$is\" instead of \"$should\"" echo "Test $1 fail: \"${bin##*/} $name\" returned \"$is\" instead of \"$should\""
stat "$name" #stat "$name"
fi fi
rm -rf "$name"
} }
touch t 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 a /bin do_test 1 file "$dir"/file 0
do_test 2 a/b . do_test 2 dir "$dir"/dir 0
do_test 3 a/b .. do_test 3 dir_symlink "$dir"/dir 0
do_test 4 a/b ../t do_test 4 file_symlink "$dir"/file 0
do_test 5 a do_test 5 invalid_symlink "" 1
do_test 6 a/ /bin do_test 6 invalid_symlink/fail "" 1
do_test 7 a/ 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
rm -rf "$dir" rm -rf "$dir"