1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-26 14:39:46 +01:00
bats/libexec/bats-preprocess
BinaryBabel OSS d2067db1b4 Remove redundant anchor in preprocess expression
Expr patterns are anchored to the beginning by default. Specifying
the carrot is undefined behavior and generates warnings on some versions, obscuring the
output.
2013-04-12 14:35:15 -07:00

52 lines
1.0 KiB
Bash
Executable File

#!/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