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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 14, 2012 at 16:33 UTC bdccd3c1fb261dc6d4aaf9fae446eea7136b76e2
2 files changed +52 -1
t/t0000-basic.sh
+32
@@ -115,6 +115,38 @@ then
115 exit 1
116 fi
117
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 +
150 clean=no
151 test_expect_success 'tests clean up after themselves' '
152 test_when_finished clean=yes
t/test-lib-functions.sh
+20 -1
@@ -275,6 +275,15 @@ test_have_prereq () {
275
276 for prerequisite
277 do
278 + case "$prerequisite" in
279 + !*)
280 + negative_prereq=t
281 + prerequisite=${prerequisite#!}
282 + ;;
283 + *)
284 + negative_prereq=
285 + esac
286 +
287 case " $lazily_tested_prereq " in
288 *" $prerequisite "*)
289 ;;
@@ -294,10 +303,20 @@ test_have_prereq () {
303 total_prereq=$(($total_prereq + 1))
304 case "$satisfied_prereq" in
305 *" $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)
314 ok_prereq=$(($ok_prereq + 1))
315 ;;
316 *)
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
320 if test -z "$missing_prereq"
321 then
322 missing_prereq=$prerequisite