tests: replace test_tristate with "git env--helper"

The test_tristate helper introduced in 83d842dc8c ("tests: turn on network daemon tests by default", 2014-02-10) can now be better implemented with "git env--helper" to give the variables in question the standard boolean behavior. The reason for the "tristate" was to have all of false/true/auto, where "auto" meant either "false" or "true" depending on what the fallback was. With the --default option to "git env--helper" we can simply have e.g. GIT_TEST_HTTPD where we know if it's true because the user asked explicitly ("true"), or true implicitly ("auto"). This breaks backwards compatibility for explicitly setting "auto" for these variables, but I don't think anyone cares. That was always intended to be internal. This means the test_normalize_bool() code in test-lib-functions.sh goes away in addition to test_tristate(). We still need the test_skip_or_die() helper, but now it takes the variable name instead of the value, and uses "git env--bool" to distinguish a default "true" from an explicit "true" (in those "explicit true" cases we want to fail the test in question). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 21, 2019 at 12:18 UTC 3b072c577ba594a9fb4ae9426409f1caadafcb08
5 files changed +22 -70
t/lib-git-daemon.sh
+3 -4
@@ -15,8 +15,7 @@
15 #
16 # test_done
17
18 -test_tristate GIT_TEST_GIT_DAEMON
19 -if test "$GIT_TEST_GIT_DAEMON" = false
18 +if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
19 then
20 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
21 test_done
@@ -24,7 +23,7 @@ fi
23
24 if test_have_prereq !PIPE
25 then
27 - test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
26 + test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
27 fi
28
29 test_set_port LIB_GIT_DAEMON_PORT
@@ -73,7 +72,7 @@ start_git_daemon() {
72 kill "$GIT_DAEMON_PID"
73 wait "$GIT_DAEMON_PID"
74 unset GIT_DAEMON_PID
76 - test_skip_or_die $GIT_TEST_GIT_DAEMON \
75 + test_skip_or_die GIT_TEST_GIT_DAEMON \
76 "git daemon failed to start"
77 fi
78 }
t/lib-git-svn.sh
+4 -7
@@ -69,14 +69,12 @@ svn_cmd () {
69 maybe_start_httpd () {
70 loc=${1-svn}
71
72 - test_tristate GIT_SVN_TEST_HTTPD
73 - case $GIT_SVN_TEST_HTTPD in
74 - true)
72 + if git env--helper --type=bool --default=false --exit-code GIT_TEST_HTTPD
73 + then
74 . "$TEST_DIRECTORY"/lib-httpd.sh
75 LIB_HTTPD_SVN="$loc"
76 start_httpd
78 - ;;
79 - esac
77 + fi
78 }
79
80 convert_to_rev_db () {
@@ -106,8 +104,7 @@ EOF
104 }
105
106 require_svnserve () {
109 - test_tristate GIT_TEST_SVNSERVE
110 - if ! test "$GIT_TEST_SVNSERVE" = true
107 + if ! git env--helper --type=bool --default=false --exit-code GIT_TEST_SVNSERVE
108 then
109 skip_all='skipping svnserve test. (set $GIT_TEST_SVNSERVE to enable)'
110 test_done
t/lib-httpd.sh
+7 -8
@@ -41,15 +41,14 @@ then
41 test_done
42 fi
43
44 -test_tristate GIT_TEST_HTTPD
45 -if test "$GIT_TEST_HTTPD" = false
44 +if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_HTTPD
45 then
46 skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
47 test_done
48 fi
49
50 if ! test_have_prereq NOT_ROOT; then
52 - test_skip_or_die $GIT_TEST_HTTPD \
51 + test_skip_or_die GIT_TEST_HTTPD \
52 "Cannot run httpd tests as root"
53 fi
54
@@ -95,7 +94,7 @@ GIT_TRACE=$GIT_TRACE; export GIT_TRACE
94
95 if ! test -x "$LIB_HTTPD_PATH"
96 then
98 - test_skip_or_die $GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
97 + test_skip_or_die GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
98 fi
99
100 HTTPD_VERSION=$($LIB_HTTPD_PATH -v | \
@@ -107,19 +106,19 @@ then
106 then
107 if ! test $HTTPD_VERSION -ge 2
108 then
110 - test_skip_or_die $GIT_TEST_HTTPD \
109 + test_skip_or_die GIT_TEST_HTTPD \
110 "at least Apache version 2 is required"
111 fi
112 if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
113 then
115 - test_skip_or_die $GIT_TEST_HTTPD \
114 + test_skip_or_die GIT_TEST_HTTPD \
115 "Apache module directory not found"
116 fi
117
118 LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
119 fi
120 else
122 - test_skip_or_die $GIT_TEST_HTTPD \
121 + test_skip_or_die GIT_TEST_HTTPD \
122 "Could not identify web server at '$LIB_HTTPD_PATH'"
123 fi
124
@@ -184,7 +183,7 @@ start_httpd() {
183 if test $? -ne 0
184 then
185 cat "$HTTPD_ROOT_PATH"/error.log >&4 2>/dev/null
187 - test_skip_or_die $GIT_TEST_HTTPD "web server setup failed"
186 + test_skip_or_die GIT_TEST_HTTPD "web server setup failed"
187 fi
188 }
189
t/t5512-ls-remote.sh
+1 -2
@@ -267,8 +267,7 @@ test_expect_success 'ls-remote --symref omits filtered-out matches' '
267 '
268
269 test_lazy_prereq GIT_DAEMON '
270 - test_tristate GIT_TEST_GIT_DAEMON &&
271 - test "$GIT_TEST_GIT_DAEMON" != false
270 + git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
271 '
272
273 # This test spawns a daemon, so run it only if the user would be OK with
t/test-lib-functions.sh
+7 -49
@@ -1035,62 +1035,20 @@ perl () {
1035 command "$PERL_PATH" "$@" 2>&7
1036 } 7>&2 2>&4
1037
1038 -# Is the value one of the various ways to spell a boolean true/false?
1039 -test_normalize_bool () {
1040 - git -c magic.variable="$1" config --bool magic.variable 2>/dev/null
1041 -}
1042 -
1043 -# Given a variable $1, normalize the value of it to one of "true",
1044 -# "false", or "auto" and store the result to it.
1045 -#
1046 -# test_tristate GIT_TEST_HTTPD
1047 -#
1048 -# A variable set to an empty string is set to 'false'.
1049 -# A variable set to 'false' or 'auto' keeps its value.
1050 -# Anything else is set to 'true'.
1051 -# An unset variable defaults to 'auto'.
1052 -#
1053 -# The last rule is to allow people to set the variable to an empty
1054 -# string and export it to decline testing the particular feature
1055 -# for versions both before and after this change. We used to treat
1056 -# both unset and empty variable as a signal for "do not test" and
1057 -# took any non-empty string as "please test".
1058 -
1059 -test_tristate () {
1060 - if eval "test x\"\${$1+isset}\" = xisset"
1061 - then
1062 - # explicitly set
1063 - eval "
1064 - case \"\$$1\" in
1065 - '') $1=false ;;
1066 - auto) ;;
1067 - *) $1=\$(test_normalize_bool \$$1 || echo true) ;;
1068 - esac
1069 - "
1070 - else
1071 - eval "$1=auto"
1072 - fi
1073 -}
1074 -
1038 # Exit the test suite, either by skipping all remaining tests or by
1076 -# exiting with an error. If "$1" is "auto", we then we assume we were
1077 -# opportunistically trying to set up some tests and we skip. If it is
1078 -# "true", then we report a failure.
1039 +# exiting with an error. If our prerequisite variable $1 falls back
1040 +# on a default assume we were opportunistically trying to set up some
1041 +# tests and we skip. If it is explicitly "true", then we report a failure.
1042 #
1043 # The error/skip message should be given by $2.
1044 #
1045 test_skip_or_die () {
1083 - case "$1" in
1084 - auto)
1046 + if ! git env--helper --mode-bool --variable=$1 --default=0 --exit-code --quiet
1047 + then
1048 skip_all=$2
1049 test_done
1087 - ;;
1088 - true)
1089 - error "$2"
1090 - ;;
1091 - *)
1092 - error "BUG: test tristate is '$1' (real error: $2)"
1093 - esac
1050 + fi
1051 + error "$2"
1052 }
1053
1054 # The following mingw_* functions obey POSIX shell syntax, but are actually