mirror of
https://github.com/sstephenson/bats.git
synced 2025-03-04 07:49:50 +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:
parent
daf76c27c1
commit
d4443adeb7
22
libexec/bats
22
libexec/bats
|
@ -56,11 +56,19 @@ abs_dirname() {
|
||||||
}
|
}
|
||||||
|
|
||||||
expand_path() {
|
expand_path() {
|
||||||
{ cd "$(dirname "$1")" 2>/dev/null
|
local path="${1%/}"
|
||||||
local dirname="$PWD"
|
local dirname="${path%/*}"
|
||||||
|
|
||||||
|
if [[ "$dirname" == "$path" ]]; then
|
||||||
|
dirname="$PWD"
|
||||||
|
elif cd "$dirname" 2>/dev/null; then
|
||||||
|
dirname="$PWD"
|
||||||
cd "$OLDPWD"
|
cd "$OLDPWD"
|
||||||
echo "$dirname/$(basename "$1")"
|
else
|
||||||
} || echo "$1"
|
printf '%s' "$path"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
printf -v "$2" '%s/%s' "$dirname" "${path##*/}"
|
||||||
}
|
}
|
||||||
|
|
||||||
abs_dirname "$0" 'BATS_LIBEXEC'
|
abs_dirname "$0" 'BATS_LIBEXEC'
|
||||||
|
@ -127,14 +135,16 @@ fi
|
||||||
|
|
||||||
filenames=()
|
filenames=()
|
||||||
for filename in "${arguments[@]}"; do
|
for filename in "${arguments[@]}"; do
|
||||||
|
expand_path "$filename" 'filename'
|
||||||
|
|
||||||
if [ -d "$filename" ]; then
|
if [ -d "$filename" ]; then
|
||||||
shopt -s nullglob
|
shopt -s nullglob
|
||||||
for suite_filename in "$(expand_path "$filename")"/*.bats; do
|
for suite_filename in "$filename"/*.bats; do
|
||||||
filenames["${#filenames[@]}"]="$suite_filename"
|
filenames["${#filenames[@]}"]="$suite_filename"
|
||||||
done
|
done
|
||||||
shopt -u nullglob
|
shopt -u nullglob
|
||||||
else
|
else
|
||||||
filenames["${#filenames[@]}"]="$(expand_path "$filename")"
|
filenames["${#filenames[@]}"]="$filename"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user