Skip to content

Commit bb623bd

Browse files
committed
Prevent removing symlinks bound to different version (#106)
1 parent 3152718 commit bb623bd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

bin/pyenv-virtualenv-delete

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# See `pyenv virtualenvs` for a complete list of installed versions.
1212
#
1313
set -e
14+
[ -n "$PYENV_DEBUG" ] && set -x
1415

1516
# Provide pyenv completions
1617
if [ "$1" = "--complete" ]; then
@@ -54,11 +55,16 @@ COMPAT_PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}"
5455

5556
if [[ "${DEFINITION}" != "${DEFINITION%/envs/*}" ]]; then
5657
PREFIX="${PYENV_ROOT}/versions/${DEFINITION}"
58+
if [ -L "${COMPAT_PREFIX}" ]; then
59+
if [[ "${PREFIX}" != "$(resolve_link "${COMPAT_PREFIX}" 2>/dev/null || true)" ]]; then
60+
unset COMPAT_PREFIX
61+
fi
62+
fi
5763
else
5864
if [ -L "${COMPAT_PREFIX}" ]; then
5965
PREFIX="$(resolve_link "${COMPAT_PREFIX}" 2>/dev/null || true)"
6066
if [[ "${PREFIX}" == "${PREFIX%/envs/*}" ]]; then
61-
echo "pyenv-virtualenv: \`${PREFIX}' is a symlink for unknown location." 1>&2
67+
echo "pyenv-virtualenv: \`${COMPAT_PREFIX}' is a symlink for unknown location." 1>&2
6268
exit 1
6369
fi
6470
else

test/delete.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ setup() {
5454
[ ! -L "${PYENV_ROOT}/versions/venv27" ]
5555
}
5656

57+
@test "not delete virtualenv with different symlink" {
58+
mkdir -p "${PYENV_ROOT}/versions/2.7.8/envs/venv27"
59+
mkdir -p "${PYENV_ROOT}/versions/2.7.10/envs/venv27"
60+
ln -fs "${PYENV_ROOT}/versions/2.7.8/envs/venv27" "${PYENV_ROOT}/versions/venv27"
61+
62+
stub pyenv-rehash "true"
63+
64+
run pyenv-virtualenv-delete -f "2.7.10/envs/venv27"
65+
66+
assert_success
67+
68+
unstub pyenv-rehash
69+
70+
[ ! -d "${PYENV_ROOT}/versions/2.7.10/envs/venv27" ]
71+
[ -L "${PYENV_ROOT}/versions/venv27" ]
72+
}
73+
5774
@test "not delete virtualenv with same name" {
5875
mkdir -p "${PYENV_ROOT}/versions/2.7.10/envs/venv27"
5976
mkdir -p "${PYENV_ROOT}/versions/venv27"

0 commit comments

Comments
 (0)