Merge 0e200e714c9d870f1551e9fa28220e6a3afb867f into b9ede4ae0e66b29f07401de2db097984ca84034a

This commit is contained in:
Christian Fredrik Johnsen 2025-06-04 20:31:17 +03:00 committed by GitHub
commit 67bdd73fb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 176 additions and 10 deletions

View File

@ -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

132
test/pip-rehash.bats Executable file
View File

@ -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
}

View File

@ -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 -