2019-03-04 23:52:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-05-03 12:43:04 +00:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
if command -v greadlink >/dev/null; then
|
|
|
|
bats_readlinkf() {
|
|
|
|
greadlink -f "$1"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bats_readlinkf() {
|
|
|
|
readlink -f "$1"
|
|
|
|
}
|
2019-03-04 23:52:53 +00:00
|
|
|
fi
|
|
|
|
|
2021-05-03 12:43:04 +00:00
|
|
|
fallback_to_readlinkf_posix() {
|
|
|
|
bats_readlinkf() {
|
|
|
|
[ "${1:-}" ] || return 1
|
|
|
|
max_symlinks=40
|
|
|
|
CDPATH='' # to avoid changing to an unexpected directory
|
|
|
|
|
|
|
|
target=$1
|
|
|
|
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
|
|
|
|
[ -d "${target:-/}" ] && target="$target/"
|
|
|
|
|
|
|
|
cd -P . 2>/dev/null || return 1
|
|
|
|
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
|
|
|
|
if [ ! "$target" = "${target%/*}" ]; then
|
|
|
|
case $target in
|
|
|
|
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
|
|
|
|
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
|
|
|
|
esac
|
|
|
|
target=${target##*/}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -L "$target" ]; then
|
|
|
|
target="${PWD%/}${target:+/}${target}"
|
|
|
|
printf '%s\n' "${target:-/}"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# `ls -dl` format: "%s %u %s %s %u %s %s -> %s\n",
|
|
|
|
# <file mode>, <number of links>, <owner name>, <group name>,
|
|
|
|
# <size>, <date and time>, <pathname of link>, <contents of link>
|
|
|
|
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
|
|
|
|
link=$(ls -dl -- "$target" 2>/dev/null) || break
|
|
|
|
target=${link#*" $target -> "}
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
2019-03-04 23:52:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 12:43:04 +00:00
|
|
|
if ! BATS_PATH=$(bats_readlinkf "${BASH_SOURCE[0]}" 2>/dev/null); then
|
|
|
|
fallback_to_readlinkf_posix
|
|
|
|
BATS_PATH=$(bats_readlinkf "${BASH_SOURCE[0]}")
|
|
|
|
fi
|
2019-03-04 23:52:53 +00:00
|
|
|
|
2021-05-03 12:43:04 +00:00
|
|
|
export BATS_ROOT=${BATS_PATH%/*/*}
|
|
|
|
export -f bats_readlinkf
|
|
|
|
exec env BATS_ROOT="$BATS_ROOT" "$BATS_ROOT/libexec/bats-core/bats" "$@"
|