From 3e28a098ad33fdea04cd172008b80d43033ea461 Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Mon, 21 May 2018 14:59:00 +0200 Subject: [PATCH] Make dependencies transitive/inherited --- README.md | 16 +++++++++------- libexec/bats-exec-test | 13 +++++++++---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5c1f4ba..f71ec70 100644 --- a/README.md +++ b/README.md @@ -195,14 +195,14 @@ same name to different tests, ### Test dependencies Some tests may have one or more tests as pre-requisites, meaning that tests -are irrelevant if the pre-requisites fail. A common case is test A checking that a -given service is running, while test B and C use this service to retrieve more -information. If service is not running, we know for sure that test B and C will -fail. In order to avoid a bunch of errors when such dependency exists, you can -instruct bash to skip test B and C if A fails. +are irrelevant if the pre-requisites fail. A common case is test A checking that +service 'foo' is running, while test B and C use this service to retrieve more +information. If service 'foo' is not running, we know for sure that tests B and C will +fail. In such cases, readability is much better if we display test A as failed +and tests B and C as skipped. In order to implement such a dependency, you will use the 'bats_test_succeeds()' -function. This function returns true (0) if all its argument every test names +function. This function returns true (0) if all the test names given as arguments succeeded, and false if any of them failed. Example: @@ -227,12 +227,14 @@ Example: Notes: -* You cannot define cross-file dependencies. The test(s) you refer too must +* You cannot define cross-file dependencies. The test(s) you refer to must be located in the same test file. * As tests are executed in the order they appear in the file, you can only depend on tests that appear in the test file BEFORE/ABOVE the current test. If this is not the case, a warning message is issued on stderr and the parent test is supposed to be successful (depending test will run). +* Dependencies are transitive/inherited. If a test depends on a test skipped for any reason, +it will be skipped too, along with its descendants. * You need to set explicit names for the tests you want to create dependencies on. ### `setup` and `teardown`: Pre- and post-test hooks diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 5f9e234..7320621 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -241,7 +241,7 @@ bats_teardown_trap() { } bats_exit_trap() { - local status + local status # Exit code: 0=OK, 1=error, 2=skipped local skipped trap - err exit @@ -262,6 +262,7 @@ bats_exit_trap() { else echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 status=0 + [ -n "$BATS_TEST_SKIPPED" ] && status=2 fi rm -f "$BATS_OUT" @@ -290,7 +291,7 @@ bats_test_succeeds() { if [ -z $status ] ; then echo "Warning: $name: Cannot check status as test didn't run yet" >&2 else - let ret+=$status + [ $status = 0 ] && ret=1 fi fi done @@ -299,15 +300,19 @@ bats_test_succeeds() { } bats_perform_tests() { + local ret status test_number test_name + + ret=0 echo "1..$#" test_number=1 status=0 for test_name in "$@"; do - "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 + "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=$? + [ $status = 1 ] && ret=1 # Skip -> OK BATS_TEST_RESULTS[$test_number]=$status let test_number+=1 done - exit "$status" + exit "$ret" } bats_perform_test() {