diff --git a/libexec/bats b/libexec/bats index 14d3a2b0..697efac5 100755 --- a/libexec/bats +++ b/libexec/bats @@ -7,7 +7,7 @@ version() { usage() { version - echo "Usage: bats [-c] [-p | -t] [-l ] [ ...]" + echo "Usage: bats [-c] [-p | -t] [-l ] [-f ] [ ...]" } help() { @@ -22,6 +22,7 @@ help() { echo " -t, --tap Show results in TAP format" echo " -v, --version Display the version number" echo " -l, --logfile Print logs to specified file" + echo " -f, --filter Filter test cases by specified pattern" echo echo " For more information, see https://github.com/sstephenson/bats" echo @@ -58,7 +59,7 @@ export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")" export BATS_CWD="$(abs_dirname .)" export PATH="$BATS_LIBEXEC:$PATH" -unset count_flag pretty log_file +unset count_flag pretty log_file filter [ -t 0 ] && [ -t 1 ] && pretty="1" [ -n "$CI" ] && pretty="" @@ -117,6 +118,16 @@ while [[ $# -gt 0 ]]; do log_file="$2" shift ;; + "-f" | "--filter" ) + # Bail out if the option is specified in a group or it's not + # given a value + if [ "$opt_grouped" = "true" ] || [ "$2" = "" ]; then + usage >&2 + exit 1 + fi + filter="$2" + shift + ;; * ) usage >&2 exit 1 @@ -133,6 +144,7 @@ if [ -n "$log_file" ]; then fi export BATS_LOG_FILE="$log_file" +export BATS_FILTER="$filter" if [ "${#arguments[@]}" -eq 0 ]; then usage >&2 diff --git a/libexec/bats-preprocess b/libexec/bats-preprocess index 04297ed0..2411b283 100755 --- a/libexec/bats-preprocess +++ b/libexec/bats-preprocess @@ -32,6 +32,7 @@ encode_name() { tests=() index=0 pattern='^ *@test *([^ ].*) *\{ *(.*)$' +filter=${BATS_FILTER:-.*} while IFS= read -r line; do let index+=1 @@ -40,7 +41,9 @@ while IFS= read -r line; do body="${BASH_REMATCH[2]}" name="$(eval echo "$quoted_name")" encoded_name="$(encode_name "$name")" - tests["${#tests[@]}"]="$encoded_name" + if [[ $quoted_name =~ $filter ]]; then + tests["${#tests[@]}"]="$encoded_name" + fi echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}; ${body}" else printf "%s\n" "$line"