From 49d87e3c40064f252cc1bcd1e5c67da940a0a9a9 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Tue, 20 Mar 2018 17:40:47 +0000 Subject: [PATCH] Improve test suite to catch more cases --- realpath.sh | 2 ++ test.sh | 53 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/realpath.sh b/realpath.sh index 82e4796..d114d30 100755 --- a/realpath.sh +++ b/realpath.sh @@ -49,6 +49,8 @@ while [ -n "$left" ]; do ;; '') # Readlink failed, its not a symlink + # If file does not exist, exit + [ -e "$result" ] || exit 1 ;; *) # Relative symlink: Keep $result, its the symlink base diff --git a/test.sh b/test.sh index 29aeab1..59345f6 100755 --- a/test.sh +++ b/test.sh @@ -1,42 +1,49 @@ #!/bin/sh bin=$PWD/realpath.sh +ref="readlink -f" +#ref="busybox readlink -f" dir="$(mktemp -d)" cd "$dir" do_test() { - name=$dir/$2 - target=$3 - case "$name" in - */*) mkdir -p "${name%/*}";; - esac - - if [ -n "$target" ]; then - ln -sfn "$target" "$name" - else - touch "$name" - fi + name=$2 + should="$3:$4" is=$($bin "$name") - - should=$(readlink -f "$name") + is="$is:$?" if [ "$is" == "$should" ]; then echo "Test $1 success" else echo "Test $1 fail: \"${bin##*/} $name\" returned \"$is\" instead of \"$should\"" - stat "$name" + #stat "$name" 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 2 a/b . -do_test 3 a/b .. -do_test 4 a/b ../t -do_test 5 a -do_test 6 a/ /bin -do_test 7 a/ +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 rm -rf "$dir"