From f536a05e0639d88e1c0865cacf6ddf48fad49fc1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 11 Dec 2020 11:45:15 -0500 Subject: [PATCH] fix: switch to bats --filter for running a single test --- docs/development/testing.md | 14 +++----- tests/bats-exec-test-single | 69 ------------------------------------- 2 files changed, 4 insertions(+), 79 deletions(-) delete mode 100755 tests/bats-exec-test-single diff --git a/docs/development/testing.md b/docs/development/testing.md index b3de27330ee..76ef79e835b 100644 --- a/docs/development/testing.md +++ b/docs/development/testing.md @@ -75,20 +75,14 @@ bats tests/unit/10_apps.bats tests/unit/10_certs.bats In order to increase testing velocity, a wrapper script around Bats is available that can be used to run a single test case within a suite. -Tests within a suite may be listed by specifying the suite as a parameter to the `tests/bats-exec-test-single` script. +Tests within a suite may be listed by specifying the suite as a parameter to `bats`. ```shell -tests/bats-exec-test-single tests/unit/10_apps.bats -``` - -A single test can be specified as a second parameter. The test is selected by fuzzy-match, and only the first match is executed. - -```shell -tests/bats-exec-test-single tests/unit/10_apps.bats clone +bats tests/unit/10_apps.bats ``` -Some special characters are translated in the test listing, specifically the characters `( ) :`, while others are not. The fuzzy matching happens on test names as listed by this script, so executing a test with a more specific name will work as expected. +A single test can be specified via the `--filter` argument. The tests are selected via regex match, and all matches are executed. ```shell -tests/bats-exec-test-single tests/unit/10_apps.bats clone_-2d-2dskip-2ddeploy +bats --filter clone tests/unit/10_apps.bats ``` diff --git a/tests/bats-exec-test-single b/tests/bats-exec-test-single deleted file mode 100755 index 426f30581a7..00000000000 --- a/tests/bats-exec-test-single +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash - -fn-available-tests() { - declare PROCESSED_TEST_FILE="$1" - AVAILABLE_TESTS="$(grep -o -E '^test_[^ ]+' "$PROCESSED_TEST_FILE")" - AVAILABLE_TESTS=${AVAILABLE_TESTS//-28/(} - AVAILABLE_TESTS=${AVAILABLE_TESTS//-29/)} - AVAILABLE_TESTS=${AVAILABLE_TESTS//-3a/:} - echo "$AVAILABLE_TESTS" -} - -fn-matched-test() { - declare TEST_NAME="$1" AVAILABLE_TESTS="$2" - - if [[ -z "$TEST_NAME" ]]; then - return - fi - - echo "$AVAILABLE_TESTS" | grep "$TEST_NAME" | head -1 -} - -fn-real-test-name() { - declare TEST_NAME="$1" - - REAL_TEST_NAME=${TEST_NAME::-2} - REAL_TEST_NAME=${REAL_TEST_NAME//(/-28} - REAL_TEST_NAME=${REAL_TEST_NAME//)/-29} - REAL_TEST_NAME=${REAL_TEST_NAME//:/-3a} - echo "$REAL_TEST_NAME" -} - -main() { - declare TEST_FILE="$1" TEST_NAME="$2" - local AVAILABLE_TESTS - - if [[ -z "$TEST_FILE" ]]; then - echo "No test file specified" - exit 1 - fi - - if [[ ! -e "$TEST_FILE" ]]; then - echo "not found: '$TEST_FILE'" - exit 1 - fi - - TMP_OUTPUT=$(mktemp "/tmp/${FUNCNAME[0]}.XXXXXX") - trap 'rm -rf "$TMP_OUTPUT" >/dev/null' RETURN INT TERM EXIT - - eval "$(grep "export BATS_TEST_PATTERN" "$TMP_OUTPUT" - AVAILABLE_TESTS="$(fn-available-tests "$TMP_OUTPUT")" - MATCHED_TEST=$(fn-matched-test "$TEST_NAME" "$AVAILABLE_TESTS") - - if [[ -z "$MATCHED_TEST" ]]; then - if [[ -n "$TEST_NAME" ]]; then - echo "test '$TEST_NAME' not found in ${TEST_FILE}" - fi - - echo "available test are:" - echo "$AVAILABLE_TESTS" - exit 1 - fi - - echo "Running test: ${MATCHED_TEST}" - export BATS_TEST_SOURCE="$TMP_OUTPUT" - /usr/local/libexec/bats-core/bats-exec-test "$TEST_FILE" "$(fn-real-test-name "$MATCHED_TEST")" -} - -main "$@"