1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-26 06:29:47 +01:00

Initial commit

This commit is contained in:
Sam Stephenson 2011-12-28 12:40:14 -06:00
commit c850527cce
4 changed files with 181 additions and 0 deletions

1
bin/bats Symbolic link
View File

@ -0,0 +1 @@
../libexec/bats

38
libexec/bats Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -e
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
expand_path() {
echo "$(abs_dirname "$(dirname "$1")")/$(basename "$1")"
}
BATS_LIBEXEC="$(abs_dirname "$0")"
export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
export PATH="$BATS_LIBEXEC:$PATH"
filename="$1"
if [ -z "$filename" ]; then
echo "usage: $0 <filename>" >&2
exit 1
else
shift
fi
exec bats-exec "$(expand_path "$filename")" "$@"

91
libexec/bats-exec Executable file
View File

@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -e
BATS_TEST_FILENAME="$1"
if [ -z "$BATS_TEST_FILENAME" ]; then
echo "usage: bats-exec <filename>" >&2
exit 1
elif [ ! -f "$BATS_TEST_FILENAME" ]; then
echo "bats: $BATS_TEST_FILENAME does not exist"
exit 1
else
shift
fi
BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
BATS_TEST_NAMES=()
load() {
source "$BATS_TEST_DIRNAME/$1.bash"
}
abort() {
[ "$#" -eq 0 ] || echo error: "$@" >&2
exit 1
}
setup() {
true
}
teardown() {
true
}
bats_test_info() {
BATS_TEST_DESCRIPTION="$1"
BATS_LINE_NUMBER="$2"
}
bats_test_function() {
local test_name="$1"
BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
}
bats_exit_trap() {
if [ -z "$BATS_TEST_COMPLETED" ]; then
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION"
exit 1
else
echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION"
exit 0
fi
}
bats_perform_tests() {
echo "1..$#"
test_number=1
for test_name in "$@"; do
"$0" "$BATS_TEST_FILENAME" "$test_name" "$test_number" || true
test_number=$(($test_number + 1))
done
}
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=""
trap bats_exit_trap err exit
setup
"$BATS_TEST_NAME"
BATS_TEST_COMPLETED=1
else
echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2
exit 1
fi
}
eval "$(bats-preprocess < "$BATS_TEST_FILENAME")"
if [ "$#" -eq 0 ]; then
bats_perform_tests "${BATS_TEST_NAMES[@]}"
else
bats_perform_test "$@"
fi

51
libexec/bats-preprocess Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -e
encode_name() {
local name="$1"
local result="test_"
if [[ ! "$name" =~ [^[:alnum:]\ _-] ]]; then
name="${name//_/-5f}"
name="${name//-/-2d}"
name="${name// /_}"
result+="$name"
else
local length="${#name}"
local char i
for ((i=0; i<length; i++)); do
char="${name:$i:1}"
if [ "$char" = " " ]; then
result+="_"
elif [[ "$char" =~ [[:alnum:]] ]]; then
result+="$char"
else
result+="$(printf -- "-%02x" \'"$char")"
fi
done
fi
echo "$result"
}
tests=()
index=0
while IFS= read -r line; do
index=$(($index + 1))
quoted_name="$(expr "$line" : '^ *@test *\([^ ].*\) *{ *$' || true)"
if [ -n "$quoted_name" ]; then
name="$(eval echo "$quoted_name")"
encoded_name="$(encode_name "$name")"
tests["${#tests[@]}"]="$encoded_name"
echo "${encoded_name}() { bats_test_info ${quoted_name} ${index}"
else
echo "$line"
fi
done
for test_name in "${tests[@]}"; do
echo "bats_test_function ${test_name}"
done