Skip to content

Commit 023ff4c

Browse files
committed
Merge branch 'ab/test-env'
Many GIT_TEST_* environment variables control various aspects of how our tests are run, but a few followed "non-empty is true, empty or unset is false" while others followed the usual "there are a few ways to spell true, like yes, on, etc., and also ways to spell false, like no, off, etc." convention. * ab/test-env: env--helper: mark a file-local symbol as static tests: make GIT_TEST_FAIL_PREREQS a boolean tests: replace test_tristate with "git env--helper" tests README: re-flow a previously changed paragraph tests: make GIT_TEST_GETTEXT_POISON a boolean t6040 test: stop using global "script" variable config.c: refactor die_bad_number() to not call gettext() early env--helper: new undocumented builtin wrapping git_env_*() config tests: simplify include cycle test
2 parents 9c9b961 + 08a8ac8 commit 023ff4c

24 files changed

+298
-127
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
/git-difftool
5959
/git-difftool--helper
6060
/git-describe
61+
/git-env--helper
6162
/git-fast-export
6263
/git-fast-import
6364
/git-fetch

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ BUILTIN_OBJS += builtin/diff-index.o
10621062
BUILTIN_OBJS += builtin/diff-tree.o
10631063
BUILTIN_OBJS += builtin/diff.o
10641064
BUILTIN_OBJS += builtin/difftool.o
1065+
BUILTIN_OBJS += builtin/env--helper.o
10651066
BUILTIN_OBJS += builtin/fast-export.o
10661067
BUILTIN_OBJS += builtin/fetch-pack.o
10671068
BUILTIN_OBJS += builtin/fetch.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix);
160160
int cmd_diff(int argc, const char **argv, const char *prefix);
161161
int cmd_diff_tree(int argc, const char **argv, const char *prefix);
162162
int cmd_difftool(int argc, const char **argv, const char *prefix);
163+
int cmd_env__helper(int argc, const char **argv, const char *prefix);
163164
int cmd_fast_export(int argc, const char **argv, const char *prefix);
164165
int cmd_fetch(int argc, const char **argv, const char *prefix);
165166
int cmd_fetch_pack(int argc, const char **argv, const char *prefix);

builtin/env--helper.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "builtin.h"
2+
#include "config.h"
3+
#include "parse-options.h"
4+
5+
static char const * const env__helper_usage[] = {
6+
N_("git env--helper --type=[bool|ulong] <options> <env-var>"),
7+
NULL
8+
};
9+
10+
static enum {
11+
ENV_HELPER_TYPE_BOOL = 1,
12+
ENV_HELPER_TYPE_ULONG
13+
} cmdmode = 0;
14+
15+
static int option_parse_type(const struct option *opt, const char *arg,
16+
int unset)
17+
{
18+
if (!strcmp(arg, "bool"))
19+
cmdmode = ENV_HELPER_TYPE_BOOL;
20+
else if (!strcmp(arg, "ulong"))
21+
cmdmode = ENV_HELPER_TYPE_ULONG;
22+
else
23+
die(_("unrecognized --type argument, %s"), arg);
24+
25+
return 0;
26+
}
27+
28+
int cmd_env__helper(int argc, const char **argv, const char *prefix)
29+
{
30+
int exit_code = 0;
31+
const char *env_variable = NULL;
32+
const char *env_default = NULL;
33+
int ret;
34+
int ret_int, default_int;
35+
unsigned long ret_ulong, default_ulong;
36+
struct option opts[] = {
37+
OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
38+
N_("value is given this type"), PARSE_OPT_NONEG,
39+
option_parse_type),
40+
OPT_STRING(0, "default", &env_default, N_("value"),
41+
N_("default for git_env_*(...) to fall back on")),
42+
OPT_BOOL(0, "exit-code", &exit_code,
43+
N_("be quiet only use git_env_*() value as exit code")),
44+
OPT_END(),
45+
};
46+
47+
argc = parse_options(argc, argv, prefix, opts, env__helper_usage,
48+
PARSE_OPT_KEEP_UNKNOWN);
49+
if (env_default && !*env_default)
50+
usage_with_options(env__helper_usage, opts);
51+
if (!cmdmode)
52+
usage_with_options(env__helper_usage, opts);
53+
if (argc != 1)
54+
usage_with_options(env__helper_usage, opts);
55+
env_variable = argv[0];
56+
57+
switch (cmdmode) {
58+
case ENV_HELPER_TYPE_BOOL:
59+
if (env_default) {
60+
default_int = git_parse_maybe_bool(env_default);
61+
if (default_int == -1) {
62+
error(_("option `--default' expects a boolean value with `--type=bool`, not `%s`"),
63+
env_default);
64+
usage_with_options(env__helper_usage, opts);
65+
}
66+
} else {
67+
default_int = 0;
68+
}
69+
ret_int = git_env_bool(env_variable, default_int);
70+
if (!exit_code)
71+
puts(ret_int ? "true" : "false");
72+
ret = ret_int;
73+
break;
74+
case ENV_HELPER_TYPE_ULONG:
75+
if (env_default) {
76+
if (!git_parse_ulong(env_default, &default_ulong)) {
77+
error(_("option `--default' expects an unsigned long value with `--type=ulong`, not `%s`"),
78+
env_default);
79+
usage_with_options(env__helper_usage, opts);
80+
}
81+
} else {
82+
default_ulong = 0;
83+
}
84+
ret_ulong = git_env_ulong(env_variable, default_ulong);
85+
if (!exit_code)
86+
printf("%lu\n", ret_ulong);
87+
ret = ret_ulong;
88+
break;
89+
default:
90+
BUG("unknown <type> value");
91+
break;
92+
}
93+
94+
return !ret;
95+
}

ci/lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ osx-clang|osx-gcc)
186186
export GIT_SKIP_TESTS="t9810 t9816"
187187
;;
188188
GIT_TEST_GETTEXT_POISON)
189-
export GIT_TEST_GETTEXT_POISON=YesPlease
189+
export GIT_TEST_GETTEXT_POISON=true
190190
;;
191191
esac
192192

config.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -973,34 +973,44 @@ int git_parse_ssize_t(const char *value, ssize_t *ret)
973973
NORETURN
974974
static void die_bad_number(const char *name, const char *value)
975975
{
976-
const char * error_type = (errno == ERANGE)? _("out of range"):_("invalid unit");
976+
const char *error_type = (errno == ERANGE) ?
977+
N_("out of range") : N_("invalid unit");
978+
const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
977979

978980
if (!value)
979981
value = "";
980982

983+
if (!strcmp(name, "GIT_TEST_GETTEXT_POISON"))
984+
/*
985+
* We explicitly *don't* use _() here since it would
986+
* cause an infinite loop with _() needing to call
987+
* use_gettext_poison(). This is why marked up
988+
* translations with N_() above.
989+
*/
990+
die(bad_numeric, value, name, error_type);
991+
981992
if (!(cf && cf->name))
982-
die(_("bad numeric config value '%s' for '%s': %s"),
983-
value, name, error_type);
993+
die(_(bad_numeric), value, name, _(error_type));
984994

985995
switch (cf->origin_type) {
986996
case CONFIG_ORIGIN_BLOB:
987997
die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
988-
value, name, cf->name, error_type);
998+
value, name, cf->name, _(error_type));
989999
case CONFIG_ORIGIN_FILE:
9901000
die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
991-
value, name, cf->name, error_type);
1001+
value, name, cf->name, _(error_type));
9921002
case CONFIG_ORIGIN_STDIN:
9931003
die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
994-
value, name, error_type);
1004+
value, name, _(error_type));
9951005
case CONFIG_ORIGIN_SUBMODULE_BLOB:
9961006
die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
997-
value, name, cf->name, error_type);
1007+
value, name, cf->name, _(error_type));
9981008
case CONFIG_ORIGIN_CMDLINE:
9991009
die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
1000-
value, name, cf->name, error_type);
1010+
value, name, cf->name, _(error_type));
10011011
default:
10021012
die(_("bad numeric config value '%s' for '%s' in %s: %s"),
1003-
value, name, cf->name, error_type);
1013+
value, name, cf->name, _(error_type));
10041014
}
10051015
}
10061016

gettext.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ const char *get_preferred_languages(void)
6868
int use_gettext_poison(void)
6969
{
7070
static int poison_requested = -1;
71-
if (poison_requested == -1) {
72-
const char *v = getenv("GIT_TEST_GETTEXT_POISON");
73-
poison_requested = v && strlen(v) ? 1 : 0;
74-
}
71+
if (poison_requested == -1)
72+
poison_requested = git_env_bool("GIT_TEST_GETTEXT_POISON", 0);
7573
return poison_requested;
7674
}
7775

git-sh-i18n.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export TEXTDOMAINDIR
1717

1818
# First decide what scheme to use...
1919
GIT_INTERNAL_GETTEXT_SH_SCHEME=fallthrough
20-
if test -n "$GIT_TEST_GETTEXT_POISON"
20+
if test -n "$GIT_TEST_GETTEXT_POISON" &&
21+
git env--helper --type=bool --default=0 --exit-code \
22+
GIT_TEST_GETTEXT_POISON
2123
then
2224
GIT_INTERNAL_GETTEXT_SH_SCHEME=poison
2325
elif test -n "@@USE_GETTEXT_SCHEME@@"

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ static struct cmd_struct commands[] = {
500500
{ "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
501501
{ "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
502502
{ "difftool", cmd_difftool, RUN_SETUP_GENTLY },
503+
{ "env--helper", cmd_env__helper },
503504
{ "fast-export", cmd_fast_export, RUN_SETUP },
504505
{ "fetch", cmd_fetch, RUN_SETUP },
505506
{ "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },

po/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ To smoke out issues like these, Git tested with a translation mode that
293293
emits gibberish on every call to gettext. To use it run the test suite
294294
with it, e.g.:
295295

296-
cd t && GIT_TEST_GETTEXT_POISON=YesPlease prove -j 9 ./t[0-9]*.sh
296+
cd t && GIT_TEST_GETTEXT_POISON=true prove -j 9 ./t[0-9]*.sh
297297

298298
If tests break with it you should inspect them manually and see if
299299
what you're translating is sane, i.e. that you're not translating

t/README

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ that cannot be easily covered by a few specific test cases. These
334334
could be enabled by running the test suite with correct GIT_TEST_
335335
environment set.
336336

337-
GIT_TEST_FAIL_PREREQS<non-empty?> fails all prerequisites. This is
337+
GIT_TEST_FAIL_PREREQS=<boolean> fails all prerequisites. This is
338338
useful for discovering issues with the tests where say a later test
339339
implicitly depends on an optional earlier test.
340340

@@ -343,11 +343,11 @@ whether this mode is active, and e.g. skip some tests that are hard to
343343
refactor to deal with it. The "SYMLINKS" prerequisite is currently
344344
excluded as so much relies on it, but this might change in the future.
345345

346-
GIT_TEST_GETTEXT_POISON=<non-empty?> turns all strings marked for
347-
translation into gibberish if non-empty (think "test -n"). Used for
348-
spotting those tests that need to be marked with a C_LOCALE_OUTPUT
349-
prerequisite when adding more strings for translation. See "Testing
350-
marked strings" in po/README for details.
346+
GIT_TEST_GETTEXT_POISON=<boolean> turns all strings marked for
347+
translation into gibberish if true. Used for spotting those tests that
348+
need to be marked with a C_LOCALE_OUTPUT prerequisite when adding more
349+
strings for translation. See "Testing marked strings" in po/README for
350+
details.
351351

352352
GIT_TEST_SPLIT_INDEX=<boolean> forces split-index mode on the whole
353353
test suite. Accept any boolean values that are accepted by git-config.

t/lib-git-daemon.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
#
1616
# test_done
1717

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
2019
then
2120
skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
2221
test_done
2322
fi
2423

2524
if test_have_prereq !PIPE
2625
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"
2827
fi
2928

3029
test_set_port LIB_GIT_DAEMON_PORT
@@ -73,7 +72,7 @@ start_git_daemon() {
7372
kill "$GIT_DAEMON_PID"
7473
wait "$GIT_DAEMON_PID"
7574
unset GIT_DAEMON_PID
76-
test_skip_or_die $GIT_TEST_GIT_DAEMON \
75+
test_skip_or_die GIT_TEST_GIT_DAEMON \
7776
"git daemon failed to start"
7877
fi
7978
}

t/lib-git-svn.sh

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

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
7574
. "$TEST_DIRECTORY"/lib-httpd.sh
7675
LIB_HTTPD_SVN="$loc"
7776
start_httpd
78-
;;
79-
esac
77+
fi
8078
}
8179

8280
convert_to_rev_db () {
@@ -106,8 +104,7 @@ EOF
106104
}
107105

108106
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
111108
then
112109
skip_all='skipping svnserve test. (set $GIT_TEST_SVNSERVE to enable)'
113110
test_done

t/lib-httpd.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ then
4141
test_done
4242
fi
4343

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
4645
then
4746
skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
4847
test_done
4948
fi
5049

5150
if ! test_have_prereq NOT_ROOT; then
52-
test_skip_or_die $GIT_TEST_HTTPD \
51+
test_skip_or_die GIT_TEST_HTTPD \
5352
"Cannot run httpd tests as root"
5453
fi
5554

@@ -95,7 +94,7 @@ GIT_TRACE=$GIT_TRACE; export GIT_TRACE
9594

9695
if ! test -x "$LIB_HTTPD_PATH"
9796
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'"
9998
fi
10099

101100
HTTPD_VERSION=$($LIB_HTTPD_PATH -v | \
@@ -107,19 +106,19 @@ then
107106
then
108107
if ! test $HTTPD_VERSION -ge 2
109108
then
110-
test_skip_or_die $GIT_TEST_HTTPD \
109+
test_skip_or_die GIT_TEST_HTTPD \
111110
"at least Apache version 2 is required"
112111
fi
113112
if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
114113
then
115-
test_skip_or_die $GIT_TEST_HTTPD \
114+
test_skip_or_die GIT_TEST_HTTPD \
116115
"Apache module directory not found"
117116
fi
118117

119118
LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
120119
fi
121120
else
122-
test_skip_or_die $GIT_TEST_HTTPD \
121+
test_skip_or_die GIT_TEST_HTTPD \
123122
"Could not identify web server at '$LIB_HTTPD_PATH'"
124123
fi
125124

@@ -184,7 +183,7 @@ start_httpd() {
184183
if test $? -ne 0
185184
then
186185
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"
188187
fi
189188
}
190189

0 commit comments

Comments
 (0)