tests: allow testing if a path is truly a file or a directory
Add test_path_is_file_not_symlink(), test_path_is_dir_not_symlink() and test_path_is_symlink(). Case of use for the first one in test t/t3903-stash.sh to replace "test -f" because that function explicitly want the file not to be a symlink. Give more friendly error message. Signed-off-by: COGONI Guillaume <cogoni.guillaume@gmail.com> Co-authored-by: BRESSAT Jonathan <git.jonathan.bressat@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
COGONI Guillaume committed
Feb 22, 2022 at 22:54 UTC
456296b5d1f05ca16949e7d37ae87f5750118564
2 files changed
+32
-3
t/t3903-stash.sh
+3
-3
@@ -390,7 +390,7 @@ test_expect_success SYMLINKS 'stash file to symlink' '
390
rm file &&
391
ln -s file2 file &&
392
git stash save "file to symlink" &&
393
- test -f file &&
393
+ test_path_is_file_not_symlink file &&
394
test bar = "$(cat file)" &&
395
git stash apply &&
396
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
@@ -401,7 +401,7 @@ test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
401
git rm file &&
402
ln -s file2 file &&
403
git stash save "file to symlink (stage rm)" &&
404
- test -f file &&
404
+ test_path_is_file_not_symlink file &&
405
test bar = "$(cat file)" &&
406
git stash apply &&
407
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
@@ -413,7 +413,7 @@ test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
413
ln -s file2 file &&
414
git add file &&
415
git stash save "file to symlink (full stage)" &&
416
- test -f file &&
416
+ test_path_is_file_not_symlink file &&
417
test bar = "$(cat file)" &&
418
git stash apply &&
419
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
t/test-lib-functions.sh
+29
@@ -856,6 +856,16 @@ test_path_is_file () {
856
fi
857
}
858
859
+test_path_is_file_not_symlink () {
860
+ test "$#" -ne 1 && BUG "1 param"
861
+ test_path_is_file "$1" &&
862
+ if test -h "$1"
863
+ then
864
+ echo "$1 shouldn't be a symbolic link"
865
+ false
866
+ fi
867
+}
868
+
869
test_path_is_dir () {
870
test "$#" -ne 1 && BUG "1 param"
871
if ! test -d "$1"
@@ -865,6 +875,16 @@ test_path_is_dir () {
875
fi
876
}
877
878
+test_path_is_dir_not_symlink () {
879
+ test "$#" -ne 1 && BUG "1 param"
880
+ test_path_is_dir "$1" &&
881
+ if test -h "$1"
882
+ then
883
+ echo "$1 shouldn't be a symbolic link"
884
+ false
885
+ fi
886
+}
887
+
888
test_path_exists () {
889
test "$#" -ne 1 && BUG "1 param"
890
if ! test -e "$1"
@@ -874,6 +894,15 @@ test_path_exists () {
894
fi
895
}
896
897
+test_path_is_symlink () {
898
+ test "$#" -ne 1 && BUG "1 param"
899
+ if ! test -h "$1"
900
+ then
901
+ echo "Symbolic link $1 doesn't exist"
902
+ false
903
+ fi
904
+}
905
+
906
# Check if the directory exists and is empty as expected, barf otherwise.
907
test_dir_is_empty () {
908
test "$#" -ne 1 && BUG "1 param"