diff --git a/pyenv.d/exec/pip-rehash.bash b/pyenv.d/exec/pip-rehash.bash index 7080f45d..3b077852 100644 --- a/pyenv.d/exec/pip-rehash.bash +++ b/pyenv.d/exec/pip-rehash.bash @@ -2,8 +2,16 @@ PYENV_PIP_REHASH_ROOT="${BASH_SOURCE[0]%/*}/pip-rehash" PYENV_REHASH_COMMAND="${PYENV_COMMAND##*/}" # Remove any version information, from e.g. "pip2" or "pip3.4". -if [[ $PYENV_REHASH_COMMAND =~ ^(pip|easy_install)[23](\.\d)?$ ]]; then +if [[ $PYENV_REHASH_COMMAND =~ ^(pip|easy_install)[23](\.[0-9]+)?$ ]]; then PYENV_REHASH_COMMAND="${BASH_REMATCH[1]}" + +# Check for `uv pip ` in arguments +elif [[ "$*" =~ uv[[:space:]]pip[[:space:]] ]]; then + PYENV_REHASH_COMMAND="pip" + +# Check for ` -m pip ` in arguments +elif [[ "$*" =~ [[:space:]]-m[[:space:]]pip[[:space:]] ]]; then + PYENV_REHASH_COMMAND="pip" fi if [ -x "${PYENV_PIP_REHASH_ROOT}/${PYENV_REHASH_COMMAND}" ]; then diff --git a/test/pip-rehash.bats b/test/pip-rehash.bats new file mode 100755 index 00000000..b617289e --- /dev/null +++ b/test/pip-rehash.bats @@ -0,0 +1,132 @@ +#!/usr/bin/env bats + +# Test the automatic rehashing after doing a pip install. + +# Tell test_helper.bash to create an isolated environment. +export ISOLATED_ENVIRONMENT=1 +load test_helper + +# Run once before all tests. +# Sets up a fresh environment for testing. +setup_file() { + eval "$(pyenv init -)" + assert_success + + run pyenv install 3.12.8 + assert_success + + pyenv global 3.12.8 + assert_success + + # Add a dummy executable in case the computer running + # the tests has black installed in system python. + echo -e "#!/bin/bash\nexit 1" > "${PYENV_TEST_DIR}/bin/black" + chmod +x "${PYENV_TEST_DIR}/bin/black" +} + +@test "auto rehash on pip install" { + # 1) Confirm that black is not found yet + run black --version + assert_failure + + # 2) Install black using pip + run pip install black + assert_success + + # 3) Confirm that black is found after install (i.e. rehash happened) + run black --version + assert_success + + # 4) Uninstall black using pip + run pip uninstall black -y + assert_success + + # 5) Confirm that black is not found after uninstall + run black --version + assert_failure +} + +@test "auto rehash on pip3 install" { + run black --version + assert_failure + + run pip3 install black + assert_success + + run black --version + assert_success + + run pip3 uninstall black -y + assert_success + + run black --version + assert_failure +} + +@test "auto rehash on pip3.12 install" { + run black --version + assert_failure + + run pip3.12 install black + assert_success + + run black --version + assert_success + + run pip3.12 uninstall black -y + assert_success + + run black --version + assert_failure +} + +@test "auto rehash on python -m pip install" { + run black --version + assert_failure + + run python -m pip install black + assert_success + + run black --version + assert_success + + run python -m pip uninstall black -y + assert_success + + run black --version + assert_failure +} + +@test "auto rehash on python3 -m pip install" { + run black --version + assert_failure + + run python3 -m pip install black + assert_success + + run black --version + assert_success + + run python3 -m pip uninstall black -y + assert_success + + run black --version + assert_failure +} + +@test "auto rehash on python3.12 -m pip install" { + run black --version + assert_failure + + run python3.12 -m pip install black + assert_success + + run black --version + assert_success + + run python3.12 -m pip uninstall black -y + assert_success + + run black --version + assert_failure +} diff --git a/test/test_helper.bash b/test/test_helper.bash index 51dcd83d..f4f53995 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -18,21 +18,47 @@ if [ -z "$PYENV_TEST_DIR" ]; then export PYENV_ROOT="${PYENV_TEST_DIR}/root" export HOME="${PYENV_TEST_DIR}/home" export PYENV_HOOK_PATH="${PYENV_ROOT}/pyenv.d" + PYENV_LIBEXEC="${BATS_TEST_DIRNAME%/*}/libexec" - 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" - PATH="${PYENV_ROOT}/shims:$PATH" - export PATH + if [ -n "$ISOLATED_ENVIRONMENT" ]; then + mkdir -p "$PYENV_ROOT" "$HOME" "$PYENV_HOOK_PATH" \ + "$PYENV_TEST_DIR"/bin "$PYENV_TEST_DIR"/plugins/python-build + + cp -r "$PYENV_LIBEXEC" "$PYENV_TEST_DIR" + cp -r "${BATS_TEST_DIRNAME%/*}/plugins/python-build" "$PYENV_TEST_DIR"/plugins/ + ln -s "$PYENV_TEST_DIR/libexec/pyenv" "$PYENV_TEST_DIR/bin/pyenv" + # cp -rL "${BATS_TEST_DIRNAME%/*}/bin" "$PYENV_TEST_DIR" + cp -r "${BATS_TEST_DIRNAME%/*}/completions" "$PYENV_TEST_DIR" + cp -r "${BATS_TEST_DIRNAME%/*}/pyenv.d" "$PYENV_TEST_DIR" + PATH="${PYENV_TEST_DIR}/bin:/usr/bin" + export PATH + + else + PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + PATH="${PYENV_TEST_DIR}/bin:$PATH" + PATH="$PYENV_LIBEXEC:$PATH" + PATH="${BATS_TEST_DIRNAME}/libexec:$PATH" + PATH="${PYENV_ROOT}/shims:$PATH" + export PATH + fi for xdg_var in `env 2>/dev/null | grep ^XDG_ | cut -d= -f1`; do unset "$xdg_var"; done unset xdg_var fi -teardown() { - rm -rf "$PYENV_TEST_DIR" -} +# We don't want to remove the test directory between +# tests if we are running in an isolated environment. +# We want to set up the isolated environment once and +# delete it at after all tests are run. +if [ -n "$ISOLATED_ENVIRONMENT" ]; then + teardown_file() { + rm -rf "$PYENV_TEST_DIR" + } +else + teardown() { + rm -rf "$PYENV_TEST_DIR" + } +fi flunk() { { if [ "$#" -eq 0 ]; then cat -