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

[PATCH 05/16] test-lib: Infrastructure to test and check for prerequisites

From
Johannes Sixt <j6t@kdbg.org>
Date
Mar 21, 2009, 21:26 UTC
Message-ID
<a7bb394037e1c32d47d0b04da025bdbe2eb78d66.1237667830.git.j6t@kdbg.org>
In-Reply-To
<cover.1237667830.git.j6t@kdbg.org>

Some tests can be run only if a particular prerequisite is available. For example, some tests require that an UTF-8 locale is available. Here we introduce functions that are used in this way:

1. Insert code that checks whether the prerequisite is available. If it is,
   call test_set_prereq with an arbitrary tag name that subsequently can be
   used to check for the prerequisite:
      case $LANG in
      *.utf-8)
            test_set_prereq UTF8
            ;;
      esac
2. In the calls to test_expect_success pass the tag name:
      test_expect_success UTF8 '...description...' '...tests...'
3. There is an auxiliary predicate that can be used anywhere to test for
   a prerequisite explicitly:
      if test_have_prereq UTF8
      then
            ...code to be skipped if prerequisite is not available...
      fi
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 t/t0000-basic.sh |   15 +++++++++++++++
 t/test-lib.sh    |   44 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 54 insertions(+), 5 deletions(-)
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index ddcd5b0..c53de1f 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -57,6 +57,21 @@ test_expect_failure 'pretend we have a known breakage' '
 test_expect_failure 'pretend we have fixed a known breakage' '
     :
 '
+test_set_prereq HAVEIT
+haveit=no
+test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
+    test_have_prereq HAVEIT &&
+    haveit=yes
+'
+donthaveit=yes
+test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
+    donthaveit=no
+'
+if test $haveit$donthaveit != yesyes
+then
+	say "bug in test framework: prerequisite tags do not work reliably"
+	exit 1
+fi
 
 ################################################################
 # Basics of the basics
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 0a0696a..3c65cfe 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -247,6 +247,31 @@ test_chmod () {
 	git update-index --add "--chmod=$@"
 }
 
+# Use test_set_prereq to tell that a particular prerequisite is available.
+# The prerequisite can later be checked for in two ways:
+#
+# - Explicitly using test_have_prereq.
+#
+# - Implicitly by specifying the prerequisite tag in the calls to
+#   test_expect_{success,failure,code}.
+#
+# The single parameter is the prerequisite tag (a simple word, in all
+# capital letters by convention).
+
+test_set_prereq () {
+	satisfied="$satisfied$1 "
+}
+satisfied=" "
+
+test_have_prereq () {
+	case $satisfied in
+	*" $1 "*)
+		: yes, have it ;;
+	*)
+		! : nope ;;
+	esac
+}
+
 # You are not expected to call test_ok_ and test_failure_ directly, use
 # the text_expect_* functions instead.
 
@@ -293,6 +318,11 @@ test_skip () {
 			to_skip=t
 		esac
 	done
+	if test -z "$to_skip" && test -n "$prereq" &&
+	   ! test_have_prereq "$prereq"
+	then
+		to_skip=t
+	fi
 	case "$to_skip" in
 	t)
 		say_color skip >&3 "skipping test: $@"
@@ -306,8 +336,9 @@ test_skip () {
 }
 
 test_expect_failure () {
+	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 	test "$#" = 2 ||
-	error "bug in the test script: not 2 parameters to test-expect-failure"
+	error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
 	if ! test_skip "$@"
 	then
 		say >&3 "checking known breakage: $2"
@@ -323,8 +354,9 @@ test_expect_failure () {
 }
 
 test_expect_success () {
+	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 	test "$#" = 2 ||
-	error "bug in the test script: not 2 parameters to test-expect-success"
+	error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 	if ! test_skip "$@"
 	then
 		say >&3 "expecting success: $2"
@@ -340,8 +372,9 @@ test_expect_success () {
 }
 
 test_expect_code () {
+	test "$#" = 4 && { prereq=$1; shift; } || prereq=
 	test "$#" = 3 ||
-	error "bug in the test script: not 3 parameters to test-expect-code"
+	error "bug in the test script: not 3 or 4 parameters to test-expect-code"
 	if ! test_skip "$@"
 	then
 		say >&3 "expecting exit code $1: $3"
@@ -365,8 +398,9 @@ test_expect_code () {
 # Usage: test_external description command arguments...
 # Example: test_external 'Perl API' perl ../path/to/test.pl
 test_external () {
-	test "$#" -eq 3 ||
-	error >&5 "bug in the test script: not 3 parameters to test_external"
+	test "$#" = 4 && { prereq=$1; shift; } || prereq=
+	test "$#" = 3 ||
+	error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
 	descr="$1"
 	shift
 	if ! test_skip "$descr" "$@"
-- 
1.6.2.1.224.g2225f
Previous: Johannes SixtNext: Junio C Hamano
Message 6 of 23 in “Tests on Windows - main part”
  1. 00/16 Tests on Windows - main partJohannes Sixt, Mar 21, 2009
  2. 01/16 test-lib: Work around incompatible sort and find on WindowsJohannes Sixt, Mar 21, 2009
  3. 02/16 test-lib: Work around missing sum on WindowsJohannes Sixt, Mar 21, 2009
  4. 03/16 Tests on Windows: $(pwd) must return Windows-style pathsJohannes Sixt, Mar 21, 2009
  5. 04/16 t0050: Check whether git init detected symbolic link support correctlyJohannes Sixt, Mar 21, 2009
  6. 05/16 test-lib: Infrastructure to test and check for prerequisitesJohannes Sixt, Mar 21, 2009
  7. Junio C HamanoMar 21, 2009
  8. 06/16 t3600: Use test prerequisite tagsJohannes Sixt, Mar 21, 2009
  9. Junio C HamanoMar 21, 2009
  10. 07/16 Skip tests that fail if the executable bit is not handled by the filesystemJohannes Sixt, Mar 21, 2009
  11. 08/16 t5302: Use prerequisite tags to skip 64-bit offset testsJohannes Sixt, Mar 21, 2009
  12. 09/16 t9100, t9129: Use prerequisite tags for UTF-8 testsJohannes Sixt, Mar 21, 2009
  13. 10/16 Use prerequisite tags to skip tests that depend on symbolic linksJohannes Sixt, Mar 21, 2009
  14. 11/16 t0060: Fix tests on WindowsJohannes Sixt, Mar 21, 2009
  15. 12/16 Skip tests that require a filesystem that obeys POSIX permissionsJohannes Sixt, Mar 21, 2009
  16. 13/16 t3700: Skip a test with backslashes in pathspecJohannes Sixt, Mar 21, 2009
  17. Junio C HamanoMar 21, 2009
  18. Johannes SixtMar 22, 2009
  19. Junio C HamanoMar 22, 2009
  20. 14/16 Use prerequisites to skip tests that need unzipJohannes Sixt, Mar 21, 2009
  21. 15/16 t7004: Use prerequisite tags to skip tests that need gpgJohannes Sixt, Mar 21, 2009
  22. 16/16 t5503: GIT_DEBUG_SEND_PACK is not supported on MinGWJohannes Sixt, Mar 21, 2009
  23. Johannes SixtMar 21, 2009

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.