Skip to content

Commit e0f9ec9

Browse files
committed
Merge branch 'sg/test-bool-env'
Recently we have declared that GIT_TEST_* variables take the usual boolean values (it used to be that some used "non-empty means true" and taking GIT_TEST_VAR=YesPlease as true); make sure we notice and fail when non-bool strings are given to these variables. * sg/test-bool-env: t5608-clone-2gb.sh: turn GIT_TEST_CLONE_2GB into a bool tests: add 'test_bool_env' to catch non-bool GIT_TEST_* values
2 parents fd95230 + a85efb5 commit e0f9ec9

File tree

10 files changed

+84
-13
lines changed

10 files changed

+84
-13
lines changed

ci/lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fi
156156

157157
export DEVELOPER=1
158158
export DEFAULT_TEST_TARGET=prove
159-
export GIT_TEST_CLONE_2GB=YesPlease
159+
export GIT_TEST_CLONE_2GB=true
160160

161161
case "$jobname" in
162162
linux-clang|linux-gcc)

t/README

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

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

986995
Prerequisites
987996
-------------

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
@@ -917,6 +917,40 @@ test_expect_success 'test_oid can look up data for SHA-256' '
917917
test "$hexsz" -eq 64
918918
'
919919

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

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/t5608-clone-2gb.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
test_description='Test cloning a repository larger than 2 gigabyte'
44
. ./test-lib.sh
55

6-
if test -z "$GIT_TEST_CLONE_2GB"
6+
if ! test_bool_env GIT_TEST_CLONE_2GB false
77
then
88
say 'Skipping expensive 2GB clone test; enable it with GIT_TEST_CLONE_2GB=t'
99
else

t/test-lib-functions.sh

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

1189+
# Given the name of an environment variable with a bool value, normalize
1190+
# its value to a 0 (true) or 1 (false or empty string) return code.
1191+
#
1192+
# test_bool_env GIT_TEST_HTTPD <default-value>
1193+
#
1194+
# Return with code corresponding to the given default value if the variable
1195+
# is unset.
1196+
# Abort the test script if either the value of the variable or the default
1197+
# are not valid bool values.
1198+
1199+
test_bool_env () {
1200+
if test $# != 2
1201+
then
1202+
BUG "test_bool_env requires two parameters (variable name and default value)"
1203+
fi
1204+
1205+
git env--helper --type=bool --default="$2" --exit-code "$1"
1206+
ret=$?
1207+
case $ret in
1208+
0|1) # unset or valid bool value
1209+
;;
1210+
*) # invalid bool value or something unexpected
1211+
error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback"
1212+
;;
1213+
esac
1214+
return $ret
1215+
}
1216+
11891217
# Exit the test suite, either by skipping all remaining tests or by
11901218
# exiting with an error. If our prerequisite variable $1 falls back
11911219
# on a default assume we were opportunistically trying to set up some
@@ -1194,7 +1222,7 @@ perl () {
11941222
# The error/skip message should be given by $2.
11951223
#
11961224
test_skip_or_die () {
1197-
if ! git env--helper --type=bool --default=false --exit-code $1
1225+
if ! test_bool_env "$1" false
11981226
then
11991227
skip_all=$2
12001228
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)