2011-12-28 19:40:14 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
2013-05-23 19:47:17 +02:00
|
|
|
set -E
|
|
|
|
set -T
|
2011-12-28 19:40:14 +01:00
|
|
|
|
2012-04-09 06:25:19 +02:00
|
|
|
BATS_COUNT_ONLY=""
|
|
|
|
if [ "$1" = "-c" ]; then
|
|
|
|
BATS_COUNT_ONLY=1
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2013-10-21 18:32:09 +02:00
|
|
|
BATS_EXTENDED_SYNTAX=""
|
|
|
|
if [ "$1" = "-x" ]; then
|
|
|
|
BATS_EXTENDED_SYNTAX="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2011-12-28 19:40:14 +01:00
|
|
|
BATS_TEST_FILENAME="$1"
|
|
|
|
if [ -z "$BATS_TEST_FILENAME" ]; then
|
|
|
|
echo "usage: bats-exec <filename>" >&2
|
|
|
|
exit 1
|
|
|
|
elif [ ! -f "$BATS_TEST_FILENAME" ]; then
|
2012-11-16 21:35:48 +01:00
|
|
|
echo "bats: $BATS_TEST_FILENAME does not exist" >&2
|
2011-12-28 19:40:14 +01:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
|
|
|
|
BATS_TEST_NAMES=()
|
|
|
|
|
|
|
|
load() {
|
2012-11-16 21:35:48 +01:00
|
|
|
local filename="$BATS_TEST_DIRNAME/$1.bash"
|
|
|
|
[ -f "$filename" ] || {
|
|
|
|
echo "bats: $filename does not exist" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2011-12-28 19:40:14 +01:00
|
|
|
source "$BATS_TEST_DIRNAME/$1.bash"
|
|
|
|
}
|
|
|
|
|
2011-12-28 23:21:48 +01:00
|
|
|
run() {
|
2013-05-23 19:47:17 +02:00
|
|
|
local e E T
|
2013-11-09 21:36:32 +01:00
|
|
|
|
2011-12-28 23:21:48 +01:00
|
|
|
[[ ! "$-" =~ e ]] || e=1
|
2013-05-23 19:47:17 +02:00
|
|
|
[[ ! "$-" =~ E ]] || E=1
|
|
|
|
[[ ! "$-" =~ T ]] || T=1
|
2013-11-09 21:36:32 +01:00
|
|
|
|
2011-12-28 23:21:48 +01:00
|
|
|
set +e
|
2013-05-23 19:47:17 +02:00
|
|
|
set +E
|
|
|
|
set +T
|
2013-11-09 21:36:32 +01:00
|
|
|
|
|
|
|
output=$(
|
|
|
|
"$@" 2>&1; status=$?
|
|
|
|
bats_capture_vars > "$BATS_VARS"
|
|
|
|
exit $status
|
|
|
|
)
|
|
|
|
|
|
|
|
status=$?
|
2011-12-29 01:45:05 +01:00
|
|
|
IFS=$'\n' lines=($output)
|
2013-11-09 21:36:32 +01:00
|
|
|
|
2011-12-28 23:21:48 +01:00
|
|
|
[ -z "$e" ] || set -e
|
2013-05-23 19:47:17 +02:00
|
|
|
[ -z "$E" ] || set -E
|
|
|
|
[ -z "$T" ] || set -T
|
2011-12-28 23:21:48 +01:00
|
|
|
}
|
|
|
|
|
2013-11-09 21:36:32 +01:00
|
|
|
set_run_vars() {
|
|
|
|
local var_names=("$@")
|
|
|
|
|
|
|
|
for n in "${var_names[@]}"; do
|
|
|
|
eval "$(grep "$n=" "$BATS_VARS")" 2> /dev/null || true
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-12-28 19:40:14 +01:00
|
|
|
setup() {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:05:20 +02:00
|
|
|
skip() {
|
|
|
|
BATS_TEST_SKIPPED=${1:-1}
|
|
|
|
BATS_TEST_COMPLETED=1
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:21:17 +02:00
|
|
|
bats_test_begin() {
|
2011-12-28 19:40:14 +01:00
|
|
|
BATS_TEST_DESCRIPTION="$1"
|
2013-10-21 18:32:09 +02:00
|
|
|
if [ -n "$BATS_EXTENDED_SYNTAX" ]; then
|
|
|
|
echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
|
|
|
|
fi
|
2013-10-21 18:21:17 +02:00
|
|
|
setup
|
2011-12-28 19:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bats_test_function() {
|
|
|
|
local test_name="$1"
|
|
|
|
BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
|
|
|
|
}
|
|
|
|
|
2013-11-09 21:36:32 +01:00
|
|
|
bats_capture_vars() {
|
|
|
|
comm -3 <(declare | sort) <(declare -f | sort)
|
|
|
|
}
|
|
|
|
|
2013-10-29 02:13:45 +01:00
|
|
|
bats_capture_stack_trace() {
|
2013-05-23 19:47:17 +02:00
|
|
|
if [ "$BASH_SOURCE" != "$1" ]; then
|
2013-10-29 02:13:45 +01:00
|
|
|
BATS_STACK_TRACE=()
|
2013-05-23 19:47:17 +02:00
|
|
|
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
|
|
|
|
BATS_LINE_NUMBER_="$2"
|
2013-10-29 02:13:45 +01:00
|
|
|
|
|
|
|
local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE"
|
2013-11-04 19:20:55 +01:00
|
|
|
local setup_pattern=" setup $BATS_TEST_SOURCE"
|
|
|
|
local teardown_pattern=" teardown $BATS_TEST_SOURCE"
|
|
|
|
|
2013-10-29 02:13:45 +01:00
|
|
|
local index=0
|
|
|
|
local frame
|
|
|
|
|
|
|
|
while frame="$(caller "$index")"; do
|
|
|
|
BATS_STACK_TRACE[$index]="$frame"
|
2013-11-04 19:20:55 +01:00
|
|
|
if [[ "$frame" = *"$test_pattern" || \
|
|
|
|
"$frame" = *"$setup_pattern" || \
|
|
|
|
"$frame" = *"$teardown_pattern" ]]; then
|
2013-10-29 02:13:45 +01:00
|
|
|
break
|
|
|
|
else
|
2013-10-29 03:01:51 +01:00
|
|
|
let index+=1
|
2013-10-29 02:13:45 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
bats_print_stack_trace() {
|
|
|
|
local index=1
|
|
|
|
local count="${#BATS_STACK_TRACE[@]}"
|
|
|
|
local line
|
|
|
|
|
|
|
|
for frame in "${BATS_STACK_TRACE[@]}"; do
|
|
|
|
if [ $index -eq 1 ]; then
|
|
|
|
line="$BATS_LINE_NUMBER"
|
|
|
|
echo -n "# ("
|
|
|
|
else
|
|
|
|
line="$(bats_frame_line "$frame")"
|
|
|
|
echo -n "# "
|
|
|
|
fi
|
|
|
|
|
2013-11-04 19:20:55 +01:00
|
|
|
local fn="$(bats_frame_function "$frame")"
|
|
|
|
if [ "$fn" != "$BATS_TEST_NAME" ]; then
|
|
|
|
echo -n "from function \`$fn' "
|
|
|
|
fi
|
|
|
|
|
2013-10-29 02:13:45 +01:00
|
|
|
if [ $index -eq $count ]; then
|
|
|
|
echo "in test file $BATS_TEST_FILENAME, line $line)"
|
|
|
|
else
|
|
|
|
local filename="$(bats_frame_filename "$frame")"
|
2013-11-04 19:20:55 +01:00
|
|
|
echo "in file $filename, line $line,"
|
2013-10-29 02:13:45 +01:00
|
|
|
fi
|
|
|
|
|
2013-10-29 03:01:51 +01:00
|
|
|
let index+=1
|
2013-10-29 02:13:45 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
bats_frame_line() {
|
|
|
|
local frame="$1"
|
|
|
|
local line="${frame%% *}"
|
|
|
|
echo "$line"
|
|
|
|
}
|
|
|
|
|
|
|
|
bats_frame_function() {
|
|
|
|
local frame="$1"
|
|
|
|
local rest="${frame#* }"
|
|
|
|
local fn="${rest%% *}"
|
|
|
|
echo "$fn"
|
|
|
|
}
|
|
|
|
|
|
|
|
bats_frame_filename() {
|
|
|
|
local frame="$1"
|
|
|
|
local rest="${frame#* }"
|
|
|
|
local filename="${rest#* }"
|
|
|
|
|
|
|
|
if [ "$filename" = "$BATS_TEST_SOURCE" ]; then
|
|
|
|
echo "$BATS_TEST_FILENAME"
|
|
|
|
else
|
|
|
|
echo "$filename"
|
2013-05-23 19:47:17 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
bats_error_trap() {
|
|
|
|
trap - debug
|
|
|
|
}
|
|
|
|
|
2011-12-29 04:20:43 +01:00
|
|
|
bats_teardown_trap() {
|
2013-05-23 19:47:17 +02:00
|
|
|
trap bats_exit_trap exit
|
2013-11-04 19:20:55 +01:00
|
|
|
if teardown >>"$BATS_OUT" 2>&1; then
|
|
|
|
BATS_TEARDOWN_COMPLETED=1
|
|
|
|
elif [ -n "$BATS_TEST_COMPLETED" ]; then
|
|
|
|
BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
|
|
|
|
fi
|
2011-12-29 04:20:43 +01:00
|
|
|
bats_exit_trap
|
|
|
|
}
|
|
|
|
|
2011-12-28 19:40:14 +01:00
|
|
|
bats_exit_trap() {
|
2011-12-29 06:50:49 +01:00
|
|
|
local status
|
2013-10-18 23:05:20 +02:00
|
|
|
local skipped
|
2011-12-28 23:21:48 +01:00
|
|
|
trap - err exit
|
2011-12-29 06:50:49 +01:00
|
|
|
|
2013-10-18 23:05:20 +02:00
|
|
|
skipped=""
|
|
|
|
if [ -n "$BATS_TEST_SKIPPED" ]; then
|
|
|
|
skipped=" # skip"
|
|
|
|
if [ "1" != "$BATS_TEST_SKIPPED" ]; then
|
|
|
|
skipped+=" ($BATS_TEST_SKIPPED)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-11-04 19:20:55 +01:00
|
|
|
if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
|
2011-12-29 06:50:49 +01:00
|
|
|
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
|
2013-10-29 02:13:45 +01:00
|
|
|
bats_print_stack_trace >&3
|
2013-10-21 17:58:21 +02:00
|
|
|
sed -e "s/^/# /" < "$BATS_OUT" >&3
|
2011-12-29 06:50:49 +01:00
|
|
|
status=1
|
2011-12-28 19:40:14 +01:00
|
|
|
else
|
2013-10-18 23:05:20 +02:00
|
|
|
echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3
|
2011-12-29 06:50:49 +01:00
|
|
|
status=0
|
2011-12-28 19:40:14 +01:00
|
|
|
fi
|
2011-12-29 06:50:49 +01:00
|
|
|
|
2013-11-09 21:36:32 +01:00
|
|
|
rm -f "$BATS_OUT" "$BATS_VARS"
|
2011-12-29 06:50:49 +01:00
|
|
|
exit "$status"
|
2011-12-28 19:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bats_perform_tests() {
|
|
|
|
echo "1..$#"
|
|
|
|
test_number=1
|
2011-12-29 02:14:10 +01:00
|
|
|
status=0
|
2011-12-28 19:40:14 +01:00
|
|
|
for test_name in "$@"; do
|
2013-10-21 18:32:09 +02:00
|
|
|
"$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1
|
2013-10-29 03:01:51 +01:00
|
|
|
let test_number+=1
|
2011-12-28 19:40:14 +01:00
|
|
|
done
|
2011-12-29 02:14:10 +01:00
|
|
|
exit "$status"
|
2011-12-28 19:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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=""
|
2013-11-04 19:20:55 +01:00
|
|
|
BATS_TEARDOWN_COMPLETED=""
|
2013-10-29 02:13:45 +01:00
|
|
|
trap "bats_capture_stack_trace \"\$BASH_SOURCE\" \$LINENO" debug
|
2013-05-23 19:47:17 +02:00
|
|
|
trap "bats_error_trap" err
|
|
|
|
trap "bats_teardown_trap" exit
|
2013-10-21 18:21:17 +02:00
|
|
|
"$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1
|
2011-12-28 19:40:14 +01:00
|
|
|
BATS_TEST_COMPLETED=1
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-12-29 06:50:49 +01:00
|
|
|
if [ -z "$TMPDIR" ]; then
|
|
|
|
BATS_TMPDIR="/tmp"
|
|
|
|
else
|
|
|
|
BATS_TMPDIR="${TMPDIR%/}"
|
|
|
|
fi
|
2012-04-09 05:08:30 +02:00
|
|
|
|
|
|
|
BATS_TMPNAME="$BATS_TMPDIR/bats.$$"
|
|
|
|
BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID"
|
|
|
|
BATS_OUT="${BATS_TMPNAME}.out"
|
2013-11-09 21:36:32 +01:00
|
|
|
BATS_VARS="${BATS_TMPNAME}.vars"
|
2011-12-29 06:50:49 +01:00
|
|
|
|
2012-04-09 06:13:58 +02:00
|
|
|
bats_preprocess_source() {
|
2012-04-09 05:08:30 +02:00
|
|
|
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
|
2013-10-21 19:45:49 +02:00
|
|
|
{ tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE"
|
2012-04-09 06:13:58 +02:00
|
|
|
trap bats_cleanup_preprocessed_source err exit
|
|
|
|
trap "bats_cleanup_preprocessed_source; exit 1" int
|
|
|
|
}
|
2012-04-09 05:08:30 +02:00
|
|
|
|
2012-04-09 06:13:58 +02:00
|
|
|
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
|
2011-12-28 19:40:14 +01:00
|
|
|
|
|
|
|
if [ "$#" -eq 0 ]; then
|
2012-04-09 06:13:58 +02:00
|
|
|
bats_preprocess_source
|
|
|
|
bats_evaluate_preprocessed_source
|
2012-04-09 06:25:19 +02:00
|
|
|
|
|
|
|
if [ -n "$BATS_COUNT_ONLY" ]; then
|
|
|
|
echo "${#BATS_TEST_NAMES[@]}"
|
|
|
|
else
|
|
|
|
bats_perform_tests "${BATS_TEST_NAMES[@]}"
|
|
|
|
fi
|
2011-12-28 19:40:14 +01:00
|
|
|
else
|
2012-04-09 06:13:58 +02:00
|
|
|
bats_evaluate_preprocessed_source
|
2011-12-28 19:40:14 +01:00
|
|
|
bats_perform_test "$@"
|
|
|
|
fi
|