1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 11:42:33 +01:00
bats/libexec/bats-preprocess
Mike Bland f5acd28612
bats-exec-suite: Count tests w/ BATS_TEST_PATTERN
Under Bash 3.2.57(1)-release and 4.4.12(1)-release on a MacBook Pro with
a 2.9GHz Intel Core i5 CPU and 8GB 1867MHz DDR3 RAM, this shaves off
O(0.16s) from the current test suite.

Before this change:

  46 tests, 0 failures

  real    0m3.541s
  user    0m2.125s
  sys     0m0.937s

After this change:

  real    0m3.372s
  user    0m2.031s
  sys     0m0.894s
2017-09-30 15:12:38 -04:00

55 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
encode_name() {
local name="$1"
local result="test_"
local hex_code
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
printf -v 'hex_code' -- "-%02x" \'"$char"
result+="$hex_code"
fi
done
fi
printf -v "$2" '%s' "$result"
}
tests=()
index=0
while IFS= read -r line; do
line="${line//$'\r'}"
let index+=1
if [[ "$line" =~ $BATS_TEST_PATTERN ]]; then
name="${BASH_REMATCH[1]#[\'\"]}"
name="${name%[\'\"]}"
body="${BASH_REMATCH[2]}"
encode_name "$name" 'encoded_name'
tests["${#tests[@]}"]="$encoded_name"
echo "${encoded_name}() { bats_test_begin \"${name}\" ${index}; ${body}"
else
printf "%s\n" "$line"
fi
done
for test_name in "${tests[@]}"; do
echo "bats_test_function ${test_name}"
done