test-lib-functions: assert correct parameter count

Add assertions of the correct parameter count of various functions, in particularly the wrappers for the shell "test" built-in. In an earlier commit we fixed a bug with an incorrect number of arguments being passed to "test_path_is_{file,missing}". Let's also guard other similar functions from the same sort of misuse. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 12, 2021 at 14:29 UTC e7884b353b7f3b61c2b8ace086bc7e030946e270
1 file changed +8
t/test-lib-functions.sh
+8
@@ -728,6 +728,7 @@ test_path_is_file () {
728 }
729
730 test_path_is_dir () {
731 + test "$#" -ne 1 && BUG "1 param"
732 if ! test -d "$1"
733 then
734 echo "Directory $1 doesn't exist"
@@ -746,6 +747,7 @@ test_path_exists () {
747
748 # Check if the directory exists and is empty as expected, barf otherwise.
749 test_dir_is_empty () {
750 + test "$#" -ne 1 && BUG "1 param"
751 test_path_is_dir "$1" &&
752 if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
753 then
@@ -757,6 +759,7 @@ test_dir_is_empty () {
759
760 # Check if the file exists and has a size greater than zero
761 test_file_not_empty () {
762 + test "$#" = 2 && BUG "2 param"
763 if ! test -s "$1"
764 then
765 echo "'$1' is not a non-empty file."
@@ -765,6 +768,7 @@ test_file_not_empty () {
768 }
769
770 test_path_is_missing () {
771 + test "$#" -ne 1 && BUG "1 param"
772 if test -e "$1"
773 then
774 echo "Path exists:"
@@ -801,6 +805,7 @@ test_line_count () {
805 }
806
807 test_file_size () {
808 + test "$#" -ne 1 && BUG "1 param"
809 test-tool path-utils file-size "$1"
810 }
811
@@ -973,6 +978,7 @@ test_expect_code () {
978 # - not all diff versions understand "-u"
979
980 test_cmp () {
981 + test "$#" -ne 2 && BUG "2 param"
982 eval "$GIT_TEST_CMP" '"$@"'
983 }
984
@@ -1002,6 +1008,7 @@ test_cmp_config () {
1008 # test_cmp_bin - helper to compare binary files
1009
1010 test_cmp_bin () {
1011 + test "$#" -ne 2 && BUG "2 param"
1012 cmp "$@"
1013 }
1014
@@ -1071,6 +1078,7 @@ verbose () {
1078 # otherwise.
1079
1080 test_must_be_empty () {
1081 + test "$#" -ne 1 && BUG "1 param"
1082 test_path_is_file "$1" &&
1083 if test -s "$1"
1084 then