1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-09-29 20:48:27 +02:00

preprocess: Eliminate eval in subshell

This is part of the effort to improve performance by reducing the number
of command substitutions/subshells.

Under Bash 3.2.57(1)-release on a MacBook Pro with a 2.9GHz Intel Core
i5 CPU and 8GB 1867MHz DDR3 RAM, this shaves off O(0.15s) from the test
suite at the previous commit, but I anticipate this effect being
magnified on Windows platforms.
This commit is contained in:
Mike Bland 2017-02-15 11:53:01 -05:00
parent 3ab495fda2
commit 6beea07a0b
No known key found for this signature in database
GPG Key ID: 5121C73A6E07384B
3 changed files with 6 additions and 6 deletions

View File

@ -33,18 +33,18 @@ encode_name() {
tests=() tests=()
index=0 index=0
pattern='^ *@test *([^ ].*) *\{ *(.*)$' pattern='^ *@test +(.+) +\{ *(.*)$'
while IFS= read -r line; do while IFS= read -r line; do
line="${line//$'\r'}" line="${line//$'\r'}"
let index+=1 let index+=1
if [[ "$line" =~ $pattern ]]; then if [[ "$line" =~ $pattern ]]; then
quoted_name="${BASH_REMATCH[1]}" name="${BASH_REMATCH[1]#[\'\"]}"
name="${name%[\'\"]}"
body="${BASH_REMATCH[2]}" body="${BASH_REMATCH[2]}"
name="$(eval echo "$quoted_name")"
encode_name "$name" 'encoded_name' encode_name "$name" 'encoded_name'
tests["${#tests[@]}"]="$encoded_name" tests["${#tests[@]}"]="$encoded_name"
echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}; ${body}" echo "${encoded_name}() { bats_test_begin \"${name}\" ${index}; ${body}"
else else
printf "%s\n" "$line" printf "%s\n" "$line"
fi fi

View File

@ -280,5 +280,5 @@ fixtures bats
[ $status -eq 0 ] [ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 single-quoted name" ] [ "${lines[1]}" = "ok 1 single-quoted name" ]
[ "${lines[2]}" = "ok 2 double-quoted name" ] [ "${lines[2]}" = "ok 2 double-quoted name" ]
[ "${lines[3]}" = "ok 3 unquoted" ] [ "${lines[3]}" = "ok 3 unquoted name" ]
} }

View File

@ -6,6 +6,6 @@
true true
} }
@test unquoted { @test unquoted name {
true true
} }