git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 11/11] test-lib-functions: assert correct parameter count

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Feb 12, 2021, 13:29 UTC
Message-ID
<20210212132942.17325-12-avarab@gmail.com>
In-Reply-To
<20210209214159.22815-1-avarab@gmail.com>

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>
---
 t/test-lib-functions.sh | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index a5f63c0af6b..a52eb16846c 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -728,6 +728,7 @@ test_path_is_file () {
 }
 
 test_path_is_dir () {
+	test "$#" -ne 1 && BUG "1 param"
 	if ! test -d "$1"
 	then
 		echo "Directory $1 doesn't exist"
@@ -746,6 +747,7 @@ test_path_exists () {
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
+	test "$#" -ne 1 && BUG "1 param"
 	test_path_is_dir "$1" &&
 	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
 	then
@@ -757,6 +759,7 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
+	test "$#" = 2 && BUG "2 param"
 	if ! test -s "$1"
 	then
 		echo "'$1' is not a non-empty file."
@@ -765,6 +768,7 @@ test_file_not_empty () {
 }
 
 test_path_is_missing () {
+	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
 		echo "Path exists:"
@@ -801,6 +805,7 @@ test_line_count () {
 }
 
 test_file_size () {
+	test "$#" -ne 1 && BUG "1 param"
 	test-tool path-utils file-size "$1"
 }
 
@@ -973,6 +978,7 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
+	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1002,6 +1008,7 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
+	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1062,6 +1069,7 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
+	test "$#" -ne 1 && BUG "1 param"
 	test_path_is_file "$1" &&
 	if test -s "$1"
 	then
-- 
2.30.0.284.gd98b1dd5eaa7
Previous: Ævar Arnfjörð Bjarmason
Message 38 of 38 in “test-lib: misc improvements”
  1. 00/12 test-lib: misc improvementsÆvar Arnfjörð Bjarmason, Feb 9, 2021
  2. 01/12 test-lib: remove check_var_migrationÆvar Arnfjörð Bjarmason, Feb 9, 2021
  3. Junio C HamanoFeb 10, 2021
  4. 02/12 test lib: change "error" to "BUG" as appropriateÆvar Arnfjörð Bjarmason, Feb 9, 2021
  5. 03/12 test-lib-functions: move test_set_index_version() to its userÆvar Arnfjörð Bjarmason, Feb 9, 2021
  6. 04/12 test-lib-functions: remove generate_zero_bytes() wrapperÆvar Arnfjörð Bjarmason, Feb 9, 2021
  7. 12/12 test-lib-functions: split out {debug,path,text} helpersÆvar Arnfjörð Bjarmason, Feb 9, 2021
  8. Denton LiuFeb 9, 2021
  9. Junio C HamanoFeb 10, 2021
  10. SZEDER GáborFeb 11, 2021
  11. Johannes SchindelinFeb 11, 2021
  12. Ævar Arnfjörð BjarmasonFeb 13, 2021
  13. 05/12 test libs: rename bundle helper to "lib-bundle.sh"Ævar Arnfjörð Bjarmason, Feb 9, 2021
  14. 10/12 test-lib-functions: remove bug-inducing "diagnostics" helper paramÆvar Arnfjörð Bjarmason, Feb 9, 2021
  15. 06/12 test libs: rename gitweb-lib.sh to lib-gitweb.shÆvar Arnfjörð Bjarmason, Feb 9, 2021
  16. 09/12 test libs: rename "diff-lib" to "lib-diff"Ævar Arnfjörð Bjarmason, Feb 9, 2021
  17. Junio C HamanoFeb 10, 2021
  18. Johannes SchindelinFeb 11, 2021
  19. Junio C HamanoFeb 11, 2021
  20. 11/12 test-lib-functions: assert correct parameter countÆvar Arnfjörð Bjarmason, Feb 9, 2021
  21. 08/12 t/.gitattributes: sort linesÆvar Arnfjörð Bjarmason, Feb 9, 2021
  22. 07/12 test-lib-functions: move function to lib-bitmap.shÆvar Arnfjörð Bjarmason, Feb 9, 2021
  23. SZEDER GáborFeb 10, 2021
  24. Jeff KingFeb 10, 2021
  25. SZEDER GáborFeb 11, 2021
  26. 00/11 test-lib: misc improvementsÆvar Arnfjörð Bjarmason, Feb 12, 2021
  27. Junio C HamanoFeb 12, 2021
  28. 02/11 test lib: change "error" to "BUG" as appropriateÆvar Arnfjörð Bjarmason, Feb 12, 2021
  29. 01/11 test-lib: remove check_var_migrationÆvar Arnfjörð Bjarmason, Feb 12, 2021
  30. 03/11 test-lib-functions: move test_set_index_version() to its userÆvar Arnfjörð Bjarmason, Feb 12, 2021
  31. 04/11 test-lib-functions: remove generate_zero_bytes() wrapperÆvar Arnfjörð Bjarmason, Feb 12, 2021
  32. 05/11 test libs: rename bundle helper to "lib-bundle.sh"Ævar Arnfjörð Bjarmason, Feb 12, 2021
  33. 06/11 test libs: rename gitweb-lib.sh to lib-gitweb.shÆvar Arnfjörð Bjarmason, Feb 12, 2021
  34. 07/11 test-lib-functions: move function to lib-bitmap.shÆvar Arnfjörð Bjarmason, Feb 12, 2021
  35. 08/11 t/.gitattributes: sort linesÆvar Arnfjörð Bjarmason, Feb 12, 2021
  36. 09/11 test libs: rename "diff-lib" to "lib-diff"Ævar Arnfjörð Bjarmason, Feb 12, 2021
  37. 10/11 test-lib-functions: remove bug-inducing "diagnostics" helper paramÆvar Arnfjörð Bjarmason, Feb 12, 2021
  38. 11/11 test-lib-functions: assert correct parameter countÆvar Arnfjörð Bjarmason, Feb 12, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.