diff --git a/libexec/bats b/libexec/bats index 71f392f7..697efac5 100755 --- a/libexec/bats +++ b/libexec/bats @@ -7,7 +7,7 @@ version() { usage() { version - echo "Usage: bats [-c] [-p | -t] [ ...]" + echo "Usage: bats [-c] [-p | -t] [-l ] [-f ] [ ...]" } help() { @@ -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 @@ -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 diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8f3bd510..84b33fab 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -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" } @@ -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" 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"