Skip to content

Commit 43a2afe

Browse files
szedergitster
authored andcommitted
tests: add 'test_bool_env' to catch non-bool GIT_TEST_* values
Since 3b072c5 (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 3960290 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9f6f3b commit 43a2afe

File tree

8 files changed

+82
-11
lines changed

8 files changed

+82
-11
lines changed

t/README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,15 @@ library for your script to use.
978978
output to the downstream---unlike the real version, it generates
979979
only up to 99 lines.
980980

981+
- test_bool_env <env-variable-name> <default-value>
982+
983+
Given the name of an environment variable with a bool value,
984+
normalize its value to a 0 (true) or 1 (false or empty string)
985+
return code. Return with code corresponding to the given default
986+
value if the variable is unset.
987+
Abort the test script if either the value of the variable or the
988+
default are not valid bool values.
989+
981990

982991
Prerequisites
983992
-------------

t/lib-git-daemon.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
# test_done
1717

18-
if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
18+
if ! test_bool_env GIT_TEST_GIT_DAEMON true
1919
then
2020
skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
2121
test_done

t/lib-git-svn.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ svn_cmd () {
6969
maybe_start_httpd () {
7070
loc=${1-svn}
7171

72-
if git env--helper --type=bool --default=false --exit-code GIT_TEST_SVN_HTTPD
72+
if test_bool_env GIT_TEST_SVN_HTTPD false
7373
then
7474
. "$TEST_DIRECTORY"/lib-httpd.sh
7575
LIB_HTTPD_SVN="$loc"
@@ -104,7 +104,7 @@ EOF
104104
}
105105

106106
require_svnserve () {
107-
if ! git env--helper --type=bool --default=false --exit-code GIT_TEST_SVNSERVE
107+
if ! test_bool_env GIT_TEST_SVNSERVE false
108108
then
109109
skip_all='skipping svnserve test. (set $GIT_TEST_SVNSERVE to enable)'
110110
test_done

t/lib-httpd.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ then
4141
test_done
4242
fi
4343

44-
if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_HTTPD
44+
if ! test_bool_env GIT_TEST_HTTPD true
4545
then
4646
skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
4747
test_done

t/t0000-basic.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,40 @@ test_expect_success 'test_oid can look up data for SHA-256' '
916916
test "$hexsz" -eq 64
917917
'
918918

919+
test_expect_success 'test_bool_env' '
920+
(
921+
sane_unset envvar &&
922+
923+
test_bool_env envvar true &&
924+
! test_bool_env envvar false &&
925+
926+
envvar= &&
927+
export envvar &&
928+
! test_bool_env envvar true &&
929+
! test_bool_env envvar false &&
930+
931+
envvar=true &&
932+
test_bool_env envvar true &&
933+
test_bool_env envvar false &&
934+
935+
envvar=false &&
936+
! test_bool_env envvar true &&
937+
! test_bool_env envvar false &&
938+
939+
envvar=invalid &&
940+
# When encountering an invalid bool value, test_bool_env
941+
# prints its error message to the original stderr of the
942+
# test script, hence the redirection of fd 7, and aborts
943+
# with "exit 1", hence the subshell.
944+
! ( test_bool_env envvar true ) 7>err &&
945+
grep "error: test_bool_env requires bool values" err &&
946+
947+
envvar=true &&
948+
! ( test_bool_env envvar invalid ) 7>err &&
949+
grep "error: test_bool_env requires bool values" err
950+
)
951+
'
952+
919953
################################################################
920954
# Basics of the basics
921955

t/t5512-ls-remote.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ test_expect_success 'ls-remote --symref omits filtered-out matches' '
267267
'
268268

269269
test_lazy_prereq GIT_DAEMON '
270-
git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
270+
test_bool_env GIT_TEST_GIT_DAEMON true
271271
'
272272

273273
# This test spawns a daemon, so run it only if the user would be OK with

t/test-lib-functions.sh

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,34 @@ perl () {
11751175
command "$PERL_PATH" "$@" 2>&7
11761176
} 7>&2 2>&4
11771177

1178+
# Given the name of an environment variable with a bool value, normalize
1179+
# its value to a 0 (true) or 1 (false or empty string) return code.
1180+
#
1181+
# test_bool_env GIT_TEST_HTTPD <default-value>
1182+
#
1183+
# Return with code corresponding to the given default value if the variable
1184+
# is unset.
1185+
# Abort the test script if either the value of the variable or the default
1186+
# are not valid bool values.
1187+
1188+
test_bool_env () {
1189+
if test $# != 2
1190+
then
1191+
BUG "test_bool_env requires two parameters (variable name and default value)"
1192+
fi
1193+
1194+
git env--helper --type=bool --default="$2" --exit-code "$1"
1195+
ret=$?
1196+
case $ret in
1197+
0|1) # unset or valid bool value
1198+
;;
1199+
*) # invalid bool value or something unexpected
1200+
error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback"
1201+
;;
1202+
esac
1203+
return $ret
1204+
}
1205+
11781206
# Exit the test suite, either by skipping all remaining tests or by
11791207
# exiting with an error. If our prerequisite variable $1 falls back
11801208
# on a default assume we were opportunistically trying to set up some
@@ -1183,7 +1211,7 @@ perl () {
11831211
# The error/skip message should be given by $2.
11841212
#
11851213
test_skip_or_die () {
1186-
if ! git env--helper --type=bool --default=false --exit-code $1
1214+
if ! test_bool_env "$1" false
11871215
then
11881216
skip_all=$2
11891217
test_done

t/test-lib.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,19 +1406,19 @@ yes () {
14061406
# The GIT_TEST_FAIL_PREREQS code hooks into test_set_prereq(), and
14071407
# thus needs to be set up really early, and set an internal variable
14081408
# for convenience so the hot test_set_prereq() codepath doesn't need
1409-
# to call "git env--helper". Only do that work if needed by seeing if
1410-
# GIT_TEST_FAIL_PREREQS is set at all.
1409+
# to call "git env--helper" (via test_bool_env). Only do that work
1410+
# if needed by seeing if GIT_TEST_FAIL_PREREQS is set at all.
14111411
GIT_TEST_FAIL_PREREQS_INTERNAL=
14121412
if test -n "$GIT_TEST_FAIL_PREREQS"
14131413
then
1414-
if git env--helper --type=bool --default=0 --exit-code GIT_TEST_FAIL_PREREQS
1414+
if test_bool_env GIT_TEST_FAIL_PREREQS false
14151415
then
14161416
GIT_TEST_FAIL_PREREQS_INTERNAL=true
14171417
test_set_prereq FAIL_PREREQS
14181418
fi
14191419
else
14201420
test_lazy_prereq FAIL_PREREQS '
1421-
git env--helper --type=bool --default=0 --exit-code GIT_TEST_FAIL_PREREQS
1421+
test_bool_env GIT_TEST_FAIL_PREREQS false
14221422
'
14231423
fi
14241424

@@ -1477,7 +1477,7 @@ then
14771477
fi
14781478

14791479
test_lazy_prereq C_LOCALE_OUTPUT '
1480-
! git env--helper --type=bool --default=0 --exit-code GIT_TEST_GETTEXT_POISON
1480+
! test_bool_env GIT_TEST_GETTEXT_POISON false
14811481
'
14821482

14831483
if test -z "$GIT_TEST_CHECK_CACHE_TREE"

0 commit comments

Comments
 (0)