From 87ae363144c7ec21fcf50211b0bbe24d4357755d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 23 Jun 2017 23:49:00 +0200 Subject: [PATCH 1/3] Fix abs_dirname There are no tests for this unfortunately and it is copied into two places. Ref: https://github.com/rbenv/rbenv/pull/868 --- install.sh | 19 ++++++++++++------- libexec/bats | 21 +++++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/install.sh b/install.sh index 8bbdd16b..a676b373 100755 --- a/install.sh +++ b/install.sh @@ -6,17 +6,22 @@ resolve_link() { } abs_dirname() { - local cwd="$(pwd)" local path="$1" + local save_cwd="$PWD" + local dir name - while [ -n "$path" ]; do - cd "${path%/*}" - local name="${path##*/}" + while [[ -n "$path" ]]; do + dir="${path%/*}" + if [[ "$dir" != "$path" ]]; then + cd "$dir" + name="${path##*/}" + else + name="$path" + fi path="$(resolve_link "$name" || true)" done - - pwd - cd "$cwd" + echo "$PWD" + cd "$save_cwd" } PREFIX="$1" diff --git a/libexec/bats b/libexec/bats index 71f392f7..4e87e369 100755 --- a/libexec/bats +++ b/libexec/bats @@ -31,17 +31,22 @@ resolve_link() { } abs_dirname() { - local cwd="$(pwd)" local path="$1" - - while [ -n "$path" ]; do - cd "${path%/*}" - local name="${path##*/}" + local save_cwd="$PWD" + local dir name + + while [[ -n "$path" ]]; do + dir="${path%/*}" + if [[ "$dir" != "$path" ]]; then + cd "$dir" + name="${path##*/}" + else + name="$path" + fi path="$(resolve_link "$name" || true)" done - - pwd - cd "$cwd" + echo "$PWD" + cd "$save_cwd" } expand_path() { From ff3b8d011571f7d9c3a0bde73e4cd291f048587d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 19 Sep 2018 09:41:34 +0200 Subject: [PATCH 2/3] fixup! Fix abs_dirname --- install.sh | 15 +++++++-------- libexec/bats | 15 +++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/install.sh b/install.sh index a676b373..2b746636 100755 --- a/install.sh +++ b/install.sh @@ -8,18 +8,17 @@ resolve_link() { abs_dirname() { local path="$1" local save_cwd="$PWD" - local dir name + local cd_path name - while [[ -n "$path" ]]; do - dir="${path%/*}" - if [[ "$dir" != "$path" ]]; then - cd "$dir" - name="${path##*/}" - else - name="$path" + while [ -n "$path" ]; do + cd_path="${path%/*}" + if [[ "$cd_path" != "$path" ]]; then + cd "$cd_path" fi + name="${path##*/}" path="$(resolve_link "$name" || true)" done + echo "$PWD" cd "$save_cwd" } diff --git a/libexec/bats b/libexec/bats index 4e87e369..44cac198 100755 --- a/libexec/bats +++ b/libexec/bats @@ -33,18 +33,17 @@ resolve_link() { abs_dirname() { local path="$1" local save_cwd="$PWD" - local dir name + local cd_path name - while [[ -n "$path" ]]; do - dir="${path%/*}" - if [[ "$dir" != "$path" ]]; then - cd "$dir" - name="${path##*/}" - else - name="$path" + while [ -n "$path" ]; do + cd_path="${path%/*}" + if [[ "$cd_path" != "$path" ]]; then + cd "$cd_path" fi + name="${path##*/}" path="$(resolve_link "$name" || true)" done + echo "$PWD" cd "$save_cwd" } From 2878724ce3ac8cc0ead67f0481f6679cda9e6da1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 19 Sep 2018 10:02:53 +0200 Subject: [PATCH 3/3] Add test --- test/bats.bats | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/bats.bats b/test/bats.bats index f1aff293..19c7beab 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -262,3 +262,13 @@ fixtures bats [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 loop_func" ] } + +@test "abs_dirname with symlink to same current directory" { + cd "$TMP" + ln -s $(which bats) bats + ln -s bats bats-linked + run ./bats-linked "$FIXTURE_ROOT/loop_keep_IFS.bats" + [ $status -eq 0 ] + expected_lines=("1..1" "ok 1 loop_func") + [ "${lines}" = "${expected_lines}" ] +}