#!/usr/bin/env bash set -e set -E set -T BATS_COUNT_ONLY="" if [ "$1" = "-c" ]; then BATS_COUNT_ONLY=1 shift fi BATS_TEST_FILENAME="$1" if [ -z "$BATS_TEST_FILENAME" ]; then echo "usage: bats-exec " >&2 exit 1 elif [ ! -f "$BATS_TEST_FILENAME" ]; then echo "bats: $BATS_TEST_FILENAME does not exist" >&2 exit 1 else shift fi BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")" BATS_TEST_NAMES=() load() { local filename="$BATS_TEST_DIRNAME/$1.bash" [ -f "$filename" ] || { echo "bats: $filename does not exist" >&2 exit 1 } source "$BATS_TEST_DIRNAME/$1.bash" } run() { local e E T [[ ! "$-" =~ e ]] || e=1 [[ ! "$-" =~ E ]] || E=1 [[ ! "$-" =~ T ]] || T=1 set +e set +E set +T output="$("$@" 2>&1)" status="$?" IFS=$'\n' lines=($output) [ -z "$e" ] || set -e [ -z "$E" ] || set -E [ -z "$T" ] || set -T } setup() { true } teardown() { true } bats_test_info() { BATS_TEST_DESCRIPTION="$1" } bats_test_function() { local test_name="$1" BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name" } bats_debug_trap() { if [ "$BASH_SOURCE" != "$1" ]; then BATS_LINE_NUMBER="$BATS_LINE_NUMBER_" BATS_LINE_NUMBER_="$2" fi } bats_error_trap() { trap - debug } bats_teardown_trap() { trap bats_exit_trap exit teardown >>"$BATS_OUT" 2>&1 bats_exit_trap } bats_exit_trap() { local status trap - err exit if [ -z "$BATS_TEST_COMPLETED" ]; then echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3 sed -e "s/^/# /" < "$BATS_OUT" >&3 status=1 elif [ -z "$BATS_TEST_SKIPPED" ]; then echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 status=0 else echo "ok $BATS_TEST_NUMBER # skip $BATS_TEST_DESCRIPTION" >&3 status=0 fi rm -f "$BATS_OUT" exit "$status" } bats_perform_tests() { echo "1..$#" test_number=1 status=0 for test_name in "$@"; do "$0" "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 test_number=$(($test_number + 1)) done exit "$status" } bats_perform_test() { BATS_TEST_NAME="$1" if [ "$(type -t "$BATS_TEST_NAME" || true)" = "function" ]; then BATS_TEST_NUMBER="$2" if [ -z "$BATS_TEST_NUMBER" ]; then echo "1..1" BATS_TEST_NUMBER="1" fi BATS_TEST_COMPLETED="" BATS_TEST_SKIPPED="" BATS_ERROR_LINE="" directive="$(expr "$BATS_TEST_NAME" : '\([^_]*\)' || true)" if [ "skip" = "$directive" ]; then BATS_TEST_SKIPPED=1 BATS_TEST_COMPLETED=1 trap "bats_teardown_trap" exit fi trap "bats_debug_trap \"\$BASH_SOURCE\" \$LINENO" debug trap "bats_error_trap" err trap "bats_teardown_trap" exit { setup "$BATS_TEST_NAME" } >>"$BATS_OUT" 2>&1 BATS_TEST_COMPLETED=1 else echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2 exit 1 fi } if [ -z "$TMPDIR" ]; then BATS_TMPDIR="/tmp" else BATS_TMPDIR="${TMPDIR%/}" fi BATS_TMPNAME="$BATS_TMPDIR/bats.$$" BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID" BATS_OUT="${BATS_TMPNAME}.out" bats_preprocess_source() { BATS_TEST_SOURCE="${BATS_TMPNAME}.src" tr -d '\r' < "$BATS_TEST_FILENAME" | bats-preprocess > "$BATS_TEST_SOURCE" trap bats_cleanup_preprocessed_source err exit trap "bats_cleanup_preprocessed_source; exit 1" int } bats_cleanup_preprocessed_source() { rm -f "$BATS_TEST_SOURCE" } bats_evaluate_preprocessed_source() { if [ -z "$BATS_TEST_SOURCE" ]; then BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src" fi source "$BATS_TEST_SOURCE" } exec 3<&1 if [ "$#" -eq 0 ]; then bats_preprocess_source bats_evaluate_preprocessed_source if [ -n "$BATS_COUNT_ONLY" ]; then echo "${#BATS_TEST_NAMES[@]}" else bats_perform_tests "${BATS_TEST_NAMES[@]}" fi else bats_evaluate_preprocessed_source bats_perform_test "$@" fi