mirror of
https://github.com/sstephenson/bats.git
synced 2025-03-03 23:39:48 +01: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:
parent
3ab495fda2
commit
6beea07a0b
|
@ -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
|
||||||
|
|
|
@ -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" ]
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,6 @@
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
@test unquoted {
|
@test unquoted name {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user