From 6beea07a0b9b0629b3ddec47899a297f82d9e8b9 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Wed, 15 Feb 2017 11:53:01 -0500 Subject: [PATCH] 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. --- libexec/bats-preprocess | 8 ++++---- test/bats.bats | 2 +- test/fixtures/bats/quoted_and_unquoted_test_names.bats | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libexec/bats-preprocess b/libexec/bats-preprocess index 6cd90eb..30392b6 100755 --- a/libexec/bats-preprocess +++ b/libexec/bats-preprocess @@ -33,18 +33,18 @@ encode_name() { tests=() index=0 -pattern='^ *@test *([^ ].*) *\{ *(.*)$' +pattern='^ *@test +(.+) +\{ *(.*)$' while IFS= read -r line; do line="${line//$'\r'}" let index+=1 if [[ "$line" =~ $pattern ]]; then - quoted_name="${BASH_REMATCH[1]}" + name="${BASH_REMATCH[1]#[\'\"]}" + name="${name%[\'\"]}" body="${BASH_REMATCH[2]}" - name="$(eval echo "$quoted_name")" encode_name "$name" '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 printf "%s\n" "$line" fi diff --git a/test/bats.bats b/test/bats.bats index 60fb017..9bed998 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -280,5 +280,5 @@ fixtures bats [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 single-quoted name" ] [ "${lines[2]}" = "ok 2 double-quoted name" ] - [ "${lines[3]}" = "ok 3 unquoted" ] + [ "${lines[3]}" = "ok 3 unquoted name" ] } diff --git a/test/fixtures/bats/quoted_and_unquoted_test_names.bats b/test/fixtures/bats/quoted_and_unquoted_test_names.bats index 706902b..aa460da 100644 --- a/test/fixtures/bats/quoted_and_unquoted_test_names.bats +++ b/test/fixtures/bats/quoted_and_unquoted_test_names.bats @@ -6,6 +6,6 @@ true } -@test unquoted { +@test unquoted name { true }