From a477040abb638846bd3e513720d03c06fc83892a Mon Sep 17 00:00:00 2001 From: Martin Schulze Date: Sun, 20 Sep 2020 01:09:39 +0200 Subject: [PATCH] Allow for seamless switching between old and new bats versions --- test/test_helper/common.bash | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/test_helper/common.bash b/test/test_helper/common.bash index c08621c9..428ddecd 100644 --- a/test/test_helper/common.bash +++ b/test/test_helper/common.bash @@ -80,8 +80,24 @@ function wait_for_finished_setup_in_container() { SETUP_FILE_MARKER="$BATS_TMPDIR/`basename \"$BATS_TEST_FILENAME\"`.setup_file" +function native_setup_teardown_file_support() { + VERSION_REGEX='([0-9]+)\.([0-9]+)\.([0-9]+)' + # bats versions that support setup_file out of the box don't need this + if [[ "$BATS_VERSION" =~ $VERSION_REGEX ]]; then + numeric_version=$(( (BASH_REMATCH[1] * 100 + BASH_REMATCH[2]) * 100 + BASH_REMATCH[3] )) + if [[ $numeric_version -ge 10201 ]]; then + if [ "$BATS_TEST_NAME" == 'test_first' ]; then + skip 'This version natively supports setup/teardown_file' + fi + return 0 + fi + fi + return 1 +} + # use in setup() in conjunction with a `@test "first" {}` to trigger setup_file reliably function run_setup_file_if_necessary() { + native_setup_teardown_file_support && return 0 if [ "$BATS_TEST_NAME" == 'test_first' ]; then # prevent old markers from marking success or get an error if we cannot remove due to permissions rm -f "$SETUP_FILE_MARKER" @@ -99,6 +115,7 @@ function run_setup_file_if_necessary() { # use in teardown() in conjunction with a `@test "last" {}` to trigger teardown_file reliably function run_teardown_file_if_necessary() { + native_setup_teardown_file_support && return 0 if [ "$BATS_TEST_NAME" == 'test_last' ]; then # cleanup setup file marker rm -f "$SETUP_FILE_MARKER"