Skip to content

Commit

Permalink
More helpful error message when env's base version is not installed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinKonowalczyk committed Apr 10, 2023
1 parent fca1241 commit 85d8c5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 13 additions & 2 deletions bin/pyenv-virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,20 @@ fi
# Set VERSION_NAME as default version in this script
export PYENV_VERSION="${VERSION_NAME}"

not_installed_message() {
local is_available=$(python-build --definitions | grep -F -x "$1")
echo "pyenv-virtualenv: \`${1}' is not installed in pyenv." 1>&2
if [[ $is_available ]]; then
echo "Run \`pyenv install ${1}' to install it." 1>&2
else
echo "It does not look like a valid Python version. See \`pyenv install --list' for available versions." 1>&2
fi
}

# Source version must exist before creating virtualenv.
PREFIX="$(pyenv-prefix 2>/dev/null || true)"
if [ ! -d "${PREFIX}" ]; then
echo "pyenv-virtualenv: \`${PYENV_VERSION}' is not installed in pyenv." 1>&2
not_installed_message "${PYENV_VERSION}"
exit 1
fi

Expand Down Expand Up @@ -441,7 +451,8 @@ else
if [ -x "${python}" ]; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${python}"
else
echo "pyenv-virtualenv: \`${VIRTUALENV_PYTHON##*/}' is not installed in pyenv." 1>&2
# echo "pyenv-virtualenv: \`${VIRTUALENV_PYTHON##*/}' is not installed in pyenv." 1>&2
not_installed_message "${VIRTUALENV_PYTHON##*/}"
exit 1
fi
fi
Expand Down
13 changes: 13 additions & 0 deletions test/python.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ setup() {
stub pyenv-rehash " : true"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub curl true
stub python-build "echo python2.7"
}

teardown() {
Expand All @@ -22,6 +23,7 @@ teardown() {
unstub pyenv-prefix
unstub pyenv-hooks
unstub pyenv-rehash
unstub python-build
teardown_version "2.7.8"
rm -fr "$TMP"/*
}
Expand Down Expand Up @@ -96,6 +98,7 @@ OUT

assert_output <<OUT
pyenv-virtualenv: \`python2.7' is not installed in pyenv.
Run \`pyenv install python2.7' to install it.
OUT
assert_failure

Expand All @@ -106,3 +109,13 @@ OUT
remove_executable "2.7.8" "python2.7"
remove_executable "2.7.9" "python2.7"
}

@test "invalid python name" {
run pyenv-virtualenv --verbose --python=99.99.99 venv

assert_output <<OUT
pyenv-virtualenv: \`99.99.99' is not installed in pyenv.
It does not look like a valid Python version. See \`pyenv install --list' for available versions.
OUT
assert_failure
}

0 comments on commit 85d8c5a

Please sign in to comment.