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

Support log file and filtering tests #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 83 additions & 43 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] <test> [<test> ...]"
echo "Usage: bats [-c] [-p | -t] [-l <file>] [-f <pattern>] <test> [<test> ...]"
}

help() {
Expand All @@ -21,6 +21,8 @@ help() {
echo " -p, --pretty Show results in pretty format (default for terminals)"
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 @@ -57,55 +59,93 @@ export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
export BATS_CWD="$(abs_dirname .)"
export PATH="$BATS_LIBEXEC:$PATH"

options=()
unset count_flag pretty log_file filter
[ -t 0 ] && [ -t 1 ] && pretty="1"
[ -n "$CI" ] && pretty=""

arguments=()
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
options[${#options[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
options[${#options[*]}]="$option"
let index+=1
done
fi
else

while [[ $# -gt 0 ]]; do
arg="$1"

if [ ! ${arg:0:1} = "-" ]; then
arguments[${#arguments[*]}]="$arg"
shift
continue
fi
done

unset count_flag pretty
[ -t 0 ] && [ -t 1 ] && pretty="1"
[ -n "$CI" ] && pretty=""
options=()
pat="^-[a-z][a-z]+"
opt_grouped="false" #whether a option is in a group or not
if [[ $arg =~ $pat ]]; then
index=1
while opt="${arg:$index:1}"; do
[ -n "$opt" ] || break
options[${#options[*]}]="-$opt"
let index+=1
done
opt_grouped="true"
else
options[${#options[*]}]=$arg
fi

for opt in ${options[@]}; do
case $opt in
"-h" | "--help" )
help
exit 0
;;
"-v" | "--version" )
version
exit 0
;;
"-c" | "--count" )
count_flag="-c"
;;
"-t" | "--tap" )
pretty=""
;;
"-p" | "--pretty" )
pretty="1"
;;
"-l" | "--logfile" )
# 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
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
;;
esac
done

for option in "${options[@]}"; do
case "$option" in
"h" | "help" )
help
exit 0
;;
"v" | "version" )
version
exit 0
;;
"c" | "count" )
count_flag="-c"
;;
"t" | "tap" )
pretty=""
;;
"p" | "pretty" )
pretty="1"
;;
* )
usage >&2
exit 1
;;
esac
shift
continue
done

if [ -n "$log_file" ]; then
echo -n > $log_file
fi

export BATS_LOG_FILE="$log_file"
export BATS_FILTER="$filter"

if [ "${#arguments[@]}" -eq 0 ]; then
usage >&2
exit 1
Expand Down
13 changes: 11 additions & 2 deletions libexec/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ bats_exit_trap() {
status=0
fi

rm -f "$BATS_OUT"
if [ -n "$BATS_OUT_TEMP" ]; then
rm -f $BATS_OUT_TEMP
fi

exit "$status"
}

Expand Down Expand Up @@ -309,7 +312,13 @@ fi

BATS_TMPNAME="$BATS_TMPDIR/bats.$$"
BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID"
BATS_OUT="${BATS_TMPNAME}.out"

if [ -z "$BATS_LOG_FILE" ]; then
BATS_OUT="${BATS_TMPNAME}.out"
BATS_OUT_TEMP=$BATS_OUT
else
BATS_OUT=$BATS_LOG_FILE
fi

bats_preprocess_source() {
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
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