Merge pull request #4234 from dokku/use-bats-filter

Switch to bats --filter for running a single test
This commit is contained in:
Jose Diaz-Gonzalez
2020-12-11 14:14:51 -05:00
committed by GitHub
2 changed files with 4 additions and 79 deletions

View File

@@ -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
bats 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.
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
```
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.
```shell
tests/bats-exec-test-single tests/unit/10_apps.bats clone_-2d-2dskip-2ddeploy
bats --filter clone tests/unit/10_apps.bats
```

View File

@@ -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" </usr/local/libexec/bats-core/bats)"
/usr/local/libexec/bats-core/bats-preprocess <"$TEST_FILE" >"$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 "$@"