Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Support filtering tests
Browse files Browse the repository at this point in the history
Support filtering tests based on the regex pattern given by user.
A new option named -f (or --filter) is added for this feature.

For example, to run tests whose name contain word foo or Foo:
$ bats --tap --filter '[Ff]oo'
  • Loading branch information
jinuxstyle committed Jul 1, 2016
1 parent d7aea2a commit 734a049
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions libexec/bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version() {

usage() {
version
echo "Usage: bats [-c] [-p | -t] [-l <file>] <test> [<test> ...]"
echo "Usage: bats [-c] [-p | -t] [-l <file>] [-f <pattern>] <test> [<test> ...]"
}

help() {
Expand All @@ -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
Expand Down Expand Up @@ -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=""

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion libexec/bats-preprocess
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ encode_name() {
tests=()
index=0
pattern='^ *@test *([^ ].*) *\{ *(.*)$'
filter=${BATS_FILTER:-.*}

while IFS= read -r line; do
let index+=1
Expand All @@ -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"
Expand Down

0 comments on commit 734a049

Please sign in to comment.