1
0
mirror of https://github.com/sstephenson/bats.git synced 2025-03-03 15:29:52 +01:00

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
This commit is contained in:
Mike Bland 2017-03-16 13:15:38 -04:00
parent daf76c27c1
commit d4443adeb7
No known key found for this signature in database
GPG Key ID: 5121C73A6E07384B

View File

@ -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