diff --git a/libexec/pyenv-completions b/libexec/pyenv-completions index 9fe54879..b75ad813 100755 --- a/libexec/pyenv-completions +++ b/libexec/pyenv-completions @@ -11,7 +11,7 @@ if [ -z "$COMMAND" ]; then fi COMMAND_PATH="$(command -v "pyenv-$COMMAND" || command -v "pyenv-sh-$COMMAND")" -if grep -i "^\([#%]\|--\|//\) provide pyenv completions" "$COMMAND_PATH" >/dev/null; then +if grep -iE "^([#%]|--|//) provide pyenv completions" "$COMMAND_PATH" >/dev/null; then shift exec "$COMMAND_PATH" --complete "$@" fi diff --git a/libexec/pyenv-init b/libexec/pyenv-init index c66e33e4..d559c77f 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -22,8 +22,9 @@ done shell="$1" if [ -z "$shell" ]; then - shell="$(ps c -p $(ps -p $$ -o 'ppid=' 2>/dev/null) -o 'comm=' 2>/dev/null || true)" + shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)" shell="${shell##-}" + shell="${shell%% *}" shell="$(basename "${shell:-$SHELL}")" fi diff --git a/test/completions.bats b/test/completions.bats index 8012a98d..f18a088c 100644 --- a/test/completions.bats +++ b/test/completions.bats @@ -18,7 +18,7 @@ create_command() { @test "command with completion support" { create_command "pyenv-hello" "#!$BASH -# provide pyenv completions +# Provide pyenv completions if [[ \$1 = --complete ]]; then echo hello else diff --git a/test/init.bats b/test/init.bats index 71cbd853..f5e60c03 100644 --- a/test/init.bats +++ b/test/init.bats @@ -51,14 +51,14 @@ load test_helper } @test "adds shims to PATH" { - export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" run pyenv-init - bash assert_success assert_line 0 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"' } @test "adds shims to PATH (fish)" { - export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" run pyenv-init - fish assert_success assert_line 0 "setenv PATH '${PYENV_ROOT}/shims' \$PATH" diff --git a/test/prefix.bats b/test/prefix.bats index 1245ceeb..be9dbaed 100644 --- a/test/prefix.bats +++ b/test/prefix.bats @@ -25,15 +25,6 @@ load test_helper } @test "prefix for invalid system" { - USRBIN_ALT="${PYENV_TEST_DIR}/usr-bin-alt" - mkdir -p "$USRBIN_ALT" - for util in head readlink greadlink; do - if [ -x "/usr/bin/$util" ]; then - ln -s "/usr/bin/$util" "${USRBIN_ALT}/$util" - fi - done - PATH_WITHOUT_PYTHON="${PATH/\/usr\/bin:/$USRBIN_ALT:}" - - PATH="$PATH_WITHOUT_PYTHON" run pyenv-prefix system + PATH="$(path_without python)" run pyenv-prefix system assert_failure "pyenv: system version not found in PATH" } diff --git a/test/test_helper.bash b/test/test_helper.bash index 57f75ea3..9bd8082d 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -8,7 +8,7 @@ if [ "$PYENV_ROOT" != "${PYENV_TEST_DIR}/root" ]; then export PYENV_ROOT="${PYENV_TEST_DIR}/root" export HOME="${PYENV_TEST_DIR}/home" - PATH=/usr/bin:/bin:/usr/sbin:/sbin + PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin PATH="${PYENV_TEST_DIR}/bin:$PATH" PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH" PATH="${BATS_TEST_DIRNAME}/libexec:$PATH" @@ -93,3 +93,25 @@ assert() { flunk "failed: $@" fi } + +# Output a modified PATH that ensures that the given executable is not present, +# but in which system utils necessary for pyenv operation are still available. +path_without() { + local exe="$1" + local path="${PATH}:" + local found alt util + for found in $(which -a "$exe"); do + found="${found%/*}" + if [ "$found" != "${PYENV_ROOT}/shims" ]; then + alt="${PYENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')" + mkdir -p "$alt" + for util in bash head cut readlink greadlink; do + if [ -x "${found}/$util" ]; then + ln -s "${found}/$util" "${alt}/$util" + fi + done + path="${path/${found}:/${alt}:}" + fi + done + echo "${path%:}" +}