From 4f21d9a2eade589d7750109df6a55082feb1d019 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 19 Sep 2018 10:08:46 +0200 Subject: [PATCH] Fix abs_dirname for relative symlinks in same directory Ref (abs_dirname in bats): https://github.com/sstephenson/bats/pull/224 Ref: https://github.com/rbenv/rbenv/pull/868 Fixes https://github.com/pyenv/pyenv/issues/580 --- libexec/pyenv | 9 ++++++--- plugins/python-build/bin/python-build | 13 +++++++------ test/pyenv_ext.bats | 9 +++++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/libexec/pyenv b/libexec/pyenv index fbdd880c..4147d780 100755 --- a/libexec/pyenv +++ b/libexec/pyenv @@ -41,12 +41,15 @@ else # Use a subshell to avoid changing the current path ( while [ -n "$path" ]; do - cd "${path%/*}" - local name="${path##*/}" + cd_path="${path%/*}" + if [[ "$cd_path" != "$path" ]]; then + cd "$cd_path" + fi + name="${path##*/}" path="$(resolve_link "$name" || true)" done - pwd + echo "$PWD" ) } fi diff --git a/plugins/python-build/bin/python-build b/plugins/python-build/bin/python-build index 75725312..c211fea3 100755 --- a/plugins/python-build/bin/python-build +++ b/plugins/python-build/bin/python-build @@ -60,20 +60,21 @@ resolve_link() { } abs_dirname() { - local cwd="$(pwd)" local path="$1" - # Use a subshell to avoid modifying the current path + # Use a subshell to avoid changing the current path ( while [ -n "$path" ]; do - cd "${path%/*}" - local name="${path##*/}" + cd_path="${path%/*}" + if [[ "$cd_path" != "$path" ]]; then + cd "$cd_path" + fi + name="${path##*/}" path="$(resolve_link "$name" || true)" done - pwd + echo "$PWD" ) -# cd "$cwd" } capitalize() { diff --git a/test/pyenv_ext.bats b/test/pyenv_ext.bats index fdae87fc..4a3a4a8f 100644 --- a/test/pyenv_ext.bats +++ b/test/pyenv_ext.bats @@ -28,3 +28,12 @@ load test_helper PYENV_FILE_ARG="${PYENV_TEST_DIR}/dir2/symlink.py" run pyenv echo PYENV_DIR assert_output "${PYENV_TEST_DIR}/dir1" } + +@test "should handle relative symlinks for file argument (#580)" { + mkdir -p "${PYENV_TEST_DIR}" + cd "${PYENV_TEST_DIR}" + touch file.py + ln -s file.py symlink.py + PYENV_FILE_ARG="symlink.py" run pyenv echo PYENV_DIR + assert_output "${PYENV_TEST_DIR}" +}