From d4443adeb790cca11283013fedb28cc2bc12d9cb Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 16 Mar 2017 13:15:38 -0400 Subject: [PATCH] bash: Refactor expand_path 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.25s) from the current test suite. Before the change: 46 tests, 0 failures real 0m3.851s user 0m2.273s sys 0m1.166s After the change: real 0m3.595s user 0m2.171s sys 0m1.048s --- libexec/bats | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libexec/bats b/libexec/bats index 5e90a9e..5cd95c6 100755 --- a/libexec/bats +++ b/libexec/bats @@ -56,11 +56,19 @@ abs_dirname() { } expand_path() { - { cd "$(dirname "$1")" 2>/dev/null - local dirname="$PWD" + local path="${1%/}" + local dirname="${path%/*}" + + if [[ "$dirname" == "$path" ]]; then + dirname="$PWD" + elif cd "$dirname" 2>/dev/null; then + dirname="$PWD" cd "$OLDPWD" - echo "$dirname/$(basename "$1")" - } || echo "$1" + else + printf '%s' "$path" + return + fi + printf -v "$2" '%s/%s' "$dirname" "${path##*/}" } abs_dirname "$0" 'BATS_LIBEXEC' @@ -127,14 +135,16 @@ fi filenames=() for filename in "${arguments[@]}"; do + expand_path "$filename" 'filename' + if [ -d "$filename" ]; then shopt -s nullglob - for suite_filename in "$(expand_path "$filename")"/*.bats; do + for suite_filename in "$filename"/*.bats; do filenames["${#filenames[@]}"]="$suite_filename" done shopt -u nullglob else - filenames["${#filenames[@]}"]="$(expand_path "$filename")" + filenames["${#filenames[@]}"]="$filename" fi done