]> git.ipfire.org Git - thirdparty/git.git/commitdiff
tests: add 'test_bool_env' to catch non-bool GIT_TEST_* values
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 22 Nov 2019 13:14:36 +0000 (14:14 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 23 Nov 2019 02:16:08 +0000 (11:16 +0900)
Since 3b072c577b (tests: replace test_tristate with "git env--helper",
2019-06-21) we get the normalized bool values of various GIT_TEST_*
environment variables via 'git env--helper'.  Now, while the 'git
env--helper' command itself does catch invalid values in the
environment variable or in the given --default and exits with error
(exit code 128 or 129, respectively), it's invoked in conditions like
'if ! git env--helper ...', which means that all invalid bool values
are interpreted the same as the ordinary 'false' (exit code 1).  This
has led to inadvertently skipped httpd tests in our CI builds for a
couple of weeks, see 3960290675 (ci: restore running httpd tests,
2019-09-06).

Let's be more careful about what the test suite accepts as bool values
in GIT_TEST_* environment variables, and error out loud and clear on
invalid values instead of simply skipping tests.  Add the
'test_bool_env' helper function to encapsulate the invocation of 'git
env--helper' and the verification of its exit code, and replace all
invocations of that command in our test framework and test suite with
a call to this new helper (except in 't0017-env-helper.sh', of
course).

  $ GIT_TEST_GIT_DAEMON=YesPlease ./t5570-git-daemon.sh
  fatal: bad numeric config value 'YesPlease' for 'GIT_TEST_GIT_DAEMON': invalid unit
  error: test_bool_env requires bool values both for $GIT_TEST_GIT_DAEMON and for the default fallback

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/README
t/lib-git-daemon.sh
t/lib-git-svn.sh
t/lib-httpd.sh
t/t0000-basic.sh
t/t5512-ls-remote.sh
t/test-lib-functions.sh
t/test-lib.sh

index 60d5b77bccd952ffe8128a62827d7a15e728f614..94e09d025e3e70227deddb70cbd385384b8acc52 100644 (file)
--- a/t/README
+++ b/t/README
@@ -978,6 +978,15 @@ library for your script to use.
    output to the downstream---unlike the real version, it generates
    only up to 99 lines.
 
+ - test_bool_env <env-variable-name> <default-value>
+
+   Given the name of an environment variable with a bool value,
+   normalize its value to a 0 (true) or 1 (false or empty string)
+   return code.  Return with code corresponding to the given default
+   value if the variable is unset.
+   Abort the test script if either the value of the variable or the
+   default are not valid bool values.
+
 
 Prerequisites
 -------------
index fb8f8870801eb56e1ada818fd1b2ccdf10a4600d..e62569222b55aa3d3036eb9e61562af8d13159ba 100644 (file)
@@ -15,7 +15,7 @@
 #
 #      test_done
 
-if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
+if ! test_bool_env GIT_TEST_GIT_DAEMON true
 then
        skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
        test_done
index bc0b9c71f824dd31984d05ef8e39bb48fade2db6..7d248e6588063565596681e6e306f4f6b74f1177 100644 (file)
@@ -69,7 +69,7 @@ svn_cmd () {
 maybe_start_httpd () {
        loc=${1-svn}
 
-       if git env--helper --type=bool --default=false --exit-code GIT_TEST_SVN_HTTPD
+       if test_bool_env GIT_TEST_SVN_HTTPD false
        then
                . "$TEST_DIRECTORY"/lib-httpd.sh
                LIB_HTTPD_SVN="$loc"
@@ -104,7 +104,7 @@ EOF
 }
 
 require_svnserve () {
-       if ! git env--helper --type=bool --default=false --exit-code GIT_TEST_SVNSERVE
+       if ! test_bool_env GIT_TEST_SVNSERVE false
        then
                skip_all='skipping svnserve test. (set $GIT_TEST_SVNSERVE to enable)'
                test_done
index 0d985758c6dd85e801404860ee82c5e93f1b51d9..656997b4d66904fc5b7a45309d67e8d60f1ceeee 100644 (file)
@@ -41,7 +41,7 @@ then
        test_done
 fi
 
-if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_HTTPD
+if ! test_bool_env GIT_TEST_HTTPD true
 then
        skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
        test_done
index 4d3f7ba295967e752dcf38f1fb050152ca679eef..03b208b12646360844f2e8eba75b095a0ebec54a 100755 (executable)
@@ -916,6 +916,40 @@ test_expect_success 'test_oid can look up data for SHA-256' '
        test "$hexsz" -eq 64
 '
 
+test_expect_success 'test_bool_env' '
+       (
+               sane_unset envvar &&
+
+               test_bool_env envvar true &&
+               ! test_bool_env envvar false &&
+
+               envvar= &&
+               export envvar &&
+               ! test_bool_env envvar true &&
+               ! test_bool_env envvar false &&
+
+               envvar=true &&
+               test_bool_env envvar true &&
+               test_bool_env envvar false &&
+
+               envvar=false &&
+               ! test_bool_env envvar true &&
+               ! test_bool_env envvar false &&
+
+               envvar=invalid &&
+               # When encountering an invalid bool value, test_bool_env
+               # prints its error message to the original stderr of the
+               # test script, hence the redirection of fd 7, and aborts
+               # with "exit 1", hence the subshell.
+               ! ( test_bool_env envvar true ) 7>err &&
+               grep "error: test_bool_env requires bool values" err &&
+
+               envvar=true &&
+               ! ( test_bool_env envvar invalid ) 7>err &&
+               grep "error: test_bool_env requires bool values" err
+       )
+'
+
 ################################################################
 # Basics of the basics
 
index 43e1d8d4d2a45c5cff87d64b32ccc2814f5579e3..d7b9f9078f6f95e866c6a0dec68511681f1957d6 100755 (executable)
@@ -267,7 +267,7 @@ test_expect_success 'ls-remote --symref omits filtered-out matches' '
 '
 
 test_lazy_prereq GIT_DAEMON '
-       git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
+       test_bool_env GIT_TEST_GIT_DAEMON true
 '
 
 # This test spawns a daemon, so run it only if the user would be OK with
index b299ecc3265357bfb38e2131854b02868b5e9e0e..5136a6724adf7a5b6cf320a920993b8a9c8a1266 100644 (file)
@@ -1175,6 +1175,34 @@ perl () {
        command "$PERL_PATH" "$@" 2>&7
 } 7>&2 2>&4
 
+# Given the name of an environment variable with a bool value, normalize
+# its value to a 0 (true) or 1 (false or empty string) return code.
+#
+#   test_bool_env GIT_TEST_HTTPD <default-value>
+#
+# Return with code corresponding to the given default value if the variable
+# is unset.
+# Abort the test script if either the value of the variable or the default
+# are not valid bool values.
+
+test_bool_env () {
+       if test $# != 2
+       then
+               BUG "test_bool_env requires two parameters (variable name and default value)"
+       fi
+
+       git env--helper --type=bool --default="$2" --exit-code "$1"
+       ret=$?
+       case $ret in
+       0|1)    # unset or valid bool value
+               ;;
+       *)      # invalid bool value or something unexpected
+               error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback"
+               ;;
+       esac
+       return $ret
+}
+
 # Exit the test suite, either by skipping all remaining tests or by
 # exiting with an error. If our prerequisite variable $1 falls back
 # on a default assume we were opportunistically trying to set up some
@@ -1183,7 +1211,7 @@ perl () {
 # The error/skip message should be given by $2.
 #
 test_skip_or_die () {
-       if ! git env--helper --type=bool --default=false --exit-code $1
+       if ! test_bool_env "$1" false
        then
                skip_all=$2
                test_done
index 46c44408432ed93dbb2126def0fa05b4159e0548..145184863b5dbb88b775c0b07901091f0e4ac396 100644 (file)
@@ -1406,19 +1406,19 @@ yes () {
 # The GIT_TEST_FAIL_PREREQS code hooks into test_set_prereq(), and
 # thus needs to be set up really early, and set an internal variable
 # for convenience so the hot test_set_prereq() codepath doesn't need
-# to call "git env--helper". Only do that work if needed by seeing if
-# GIT_TEST_FAIL_PREREQS is set at all.
+# to call "git env--helper" (via test_bool_env). Only do that work
+# if needed by seeing if GIT_TEST_FAIL_PREREQS is set at all.
 GIT_TEST_FAIL_PREREQS_INTERNAL=
 if test -n "$GIT_TEST_FAIL_PREREQS"
 then
-       if git env--helper --type=bool --default=0 --exit-code GIT_TEST_FAIL_PREREQS
+       if test_bool_env GIT_TEST_FAIL_PREREQS false
        then
                GIT_TEST_FAIL_PREREQS_INTERNAL=true
                test_set_prereq FAIL_PREREQS
        fi
 else
        test_lazy_prereq FAIL_PREREQS '
-               git env--helper --type=bool --default=0 --exit-code GIT_TEST_FAIL_PREREQS
+               test_bool_env GIT_TEST_FAIL_PREREQS false
        '
 fi
 
@@ -1477,7 +1477,7 @@ then
 fi
 
 test_lazy_prereq C_LOCALE_OUTPUT '
-       ! git env--helper --type=bool --default=0 --exit-code GIT_TEST_GETTEXT_POISON
+       ! test_bool_env GIT_TEST_GETTEXT_POISON false
 '
 
 if test -z "$GIT_TEST_CHECK_CACHE_TREE"