@matheusbsbs / matheus-bagliano / commits / a7bb394037
Fork 0

test-lib: Infrastructure to test and check for prerequisites

Some tests can be run only if a particular prerequisite is available. For example, some tests require that an UTF-8 locale is available. Here we introduce functions that are used in this way: 1. Insert code that checks whether the prerequisite is available. If it is, call test_set_prereq with an arbitrary tag name that subsequently can be used to check for the prerequisite: case $LANG in *.utf-8) test_set_prereq UTF8 ;; esac 2. In the calls to test_expect_success pass the tag name: test_expect_success UTF8 '...description...' '...tests...' 3. There is an auxiliary predicate that can be used anywhere to test for a prerequisite explicitly: if test_have_prereq UTF8 then ...code to be skipped if prerequisite is not available... fi Signed-off-by: Johannes Sixt <j6t@kdbg.org>

Johannes Sixt committed Mar 1, 2009 at 21:04 UTC a7bb394037e1c32d47d0b04da025bdbe2eb78d66
2 files changed +54 -5
t/t0000-basic.sh
+15
@@ -57,6 +57,21 @@ test_expect_failure 'pretend we have a known breakage' '
57 test_expect_failure 'pretend we have fixed a known breakage' '
58 :
59 '
60 +test_set_prereq HAVEIT
61 +haveit=no
62 +test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
63 + test_have_prereq HAVEIT &&
64 + haveit=yes
65 +'
66 +donthaveit=yes
67 +test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
68 + donthaveit=no
69 +'
70 +if test $haveit$donthaveit != yesyes
71 +then
72 + say "bug in test framework: prerequisite tags do not work reliably"
73 + exit 1
74 +fi
75
76 ################################################################
77 # Basics of the basics
t/test-lib.sh
+39 -5
@@ -247,6 +247,31 @@ test_chmod () {
247 git update-index --add "--chmod=$@"
248 }
249
250 +# Use test_set_prereq to tell that a particular prerequisite is available.
251 +# The prerequisite can later be checked for in two ways:
252 +#
253 +# - Explicitly using test_have_prereq.
254 +#
255 +# - Implicitly by specifying the prerequisite tag in the calls to
256 +# test_expect_{success,failure,code}.
257 +#
258 +# The single parameter is the prerequisite tag (a simple word, in all
259 +# capital letters by convention).
260 +
261 +test_set_prereq () {
262 + satisfied="$satisfied$1 "
263 +}
264 +satisfied=" "
265 +
266 +test_have_prereq () {
267 + case $satisfied in
268 + *" $1 "*)
269 + : yes, have it ;;
270 + *)
271 + ! : nope ;;
272 + esac
273 +}
274 +
275 # You are not expected to call test_ok_ and test_failure_ directly, use
276 # the text_expect_* functions instead.
277
@@ -293,6 +318,11 @@ test_skip () {
318 to_skip=t
319 esac
320 done
321 + if test -z "$to_skip" && test -n "$prereq" &&
322 + ! test_have_prereq "$prereq"
323 + then
324 + to_skip=t
325 + fi
326 case "$to_skip" in
327 t)
328 say_color skip >&3 "skipping test: $@"
@@ -306,8 +336,9 @@ test_skip () {
336 }
337
338 test_expect_failure () {
339 + test "$#" = 3 && { prereq=$1; shift; } || prereq=
340 test "$#" = 2 ||
310 - error "bug in the test script: not 2 parameters to test-expect-failure"
341 + error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
342 if ! test_skip "$@"
343 then
344 say >&3 "checking known breakage: $2"
@@ -323,8 +354,9 @@ test_expect_failure () {
354 }
355
356 test_expect_success () {
357 + test "$#" = 3 && { prereq=$1; shift; } || prereq=
358 test "$#" = 2 ||
327 - error "bug in the test script: not 2 parameters to test-expect-success"
359 + error "bug in the test script: not 2 or 3 parameters to test-expect-success"
360 if ! test_skip "$@"
361 then
362 say >&3 "expecting success: $2"
@@ -340,8 +372,9 @@ test_expect_success () {
372 }
373
374 test_expect_code () {
375 + test "$#" = 4 && { prereq=$1; shift; } || prereq=
376 test "$#" = 3 ||
344 - error "bug in the test script: not 3 parameters to test-expect-code"
377 + error "bug in the test script: not 3 or 4 parameters to test-expect-code"
378 if ! test_skip "$@"
379 then
380 say >&3 "expecting exit code $1: $3"
@@ -365,8 +398,9 @@ test_expect_code () {
398 # Usage: test_external description command arguments...
399 # Example: test_external 'Perl API' perl ../path/to/test.pl
400 test_external () {
368 - test "$#" -eq 3 ||
369 - error >&5 "bug in the test script: not 3 parameters to test_external"
401 + test "$#" = 4 && { prereq=$1; shift; } || prereq=
402 + test "$#" = 3 ||
403 + error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
404 descr="$1"
405 shift
406 if ! test_skip "$descr" "$@"