Skip to content

Commit bdccd3c

Browse files
peffgitster
authored andcommitted
test-lib: allow negation of prerequisites
You can set and test a prerequisite like this: test_set_prereq FOO test_have_prereq FOO && echo yes You can negate the test in the shell like this: ! test_have_prereq && echo no However, when you are using the automatic prerequisite checking in test_expect_*, there is no opportunity to use the shell negation. This patch introduces the syntax "!FOO" to indicate that the test should only run if a prerequisite is not meant. One alternative is to set an explicit negative prerequisite, like: if system_has_foo; then test_set_prereq FOO else test_set_prereq NO_FOO fi However, this doesn't work for lazy prerequisites, which associate a single test with a single name. We could teach the lazy prereq evaluator to set both forms, but the code change ends up quite similar to this one (because we still need to convert NO_FOO into FOO to find the correct lazy script). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0b00a3 commit bdccd3c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

t/t0000-basic.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ then
115115
exit 1
116116
fi
117117

118+
test_lazy_prereq LAZY_TRUE true
119+
havetrue=no
120+
test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
121+
havetrue=yes
122+
'
123+
donthavetrue=yes
124+
test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
125+
donthavetrue=no
126+
'
127+
128+
if test "$havetrue$donthavetrue" != yesyes
129+
then
130+
say 'bug in test framework: lazy prerequisites do not work'
131+
exit 1
132+
fi
133+
134+
test_lazy_prereq LAZY_FALSE false
135+
nothavefalse=no
136+
test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
137+
nothavefalse=yes
138+
'
139+
havefalse=yes
140+
test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
141+
havefalse=no
142+
'
143+
144+
if test "$nothavefalse$havefalse" != yesyes
145+
then
146+
say 'bug in test framework: negative lazy prerequisites do not work'
147+
exit 1
148+
fi
149+
118150
clean=no
119151
test_expect_success 'tests clean up after themselves' '
120152
test_when_finished clean=yes

t/test-lib-functions.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ test_have_prereq () {
275275

276276
for prerequisite
277277
do
278+
case "$prerequisite" in
279+
!*)
280+
negative_prereq=t
281+
prerequisite=${prerequisite#!}
282+
;;
283+
*)
284+
negative_prereq=
285+
esac
286+
278287
case " $lazily_tested_prereq " in
279288
*" $prerequisite "*)
280289
;;
@@ -294,10 +303,20 @@ test_have_prereq () {
294303
total_prereq=$(($total_prereq + 1))
295304
case "$satisfied_prereq" in
296305
*" $prerequisite "*)
306+
satisfied_this_prereq=t
307+
;;
308+
*)
309+
satisfied_this_prereq=
310+
esac
311+
312+
case "$satisfied_this_prereq,$negative_prereq" in
313+
t,|,t)
297314
ok_prereq=$(($ok_prereq + 1))
298315
;;
299316
*)
300-
# Keep a list of missing prerequisites
317+
# Keep a list of missing prerequisites; restore
318+
# the negative marker if necessary.
319+
prerequisite=${negative_prereq:+!}$prerequisite
301320
if test -z "$missing_prereq"
302321
then
303322
missing_prereq=$prerequisite

0 commit comments

Comments
 (0)