From 85d8c5aabf1f7e742232de5e14f973ede7cc27a2 Mon Sep 17 00:00:00 2001 From: Marcin Konowalczyk Date: Mon, 10 Apr 2023 19:42:47 +0100 Subject: [PATCH] More helpful error message when env's base version is not installed (#454) --- bin/pyenv-virtualenv | 15 +++++++++++++-- test/python.bats | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bin/pyenv-virtualenv b/bin/pyenv-virtualenv index 10caa1a..0bdfda5 100755 --- a/bin/pyenv-virtualenv +++ b/bin/pyenv-virtualenv @@ -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 @@ -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 diff --git a/test/python.bats b/test/python.bats index abd0e71..0947299 100644 --- a/test/python.bats +++ b/test/python.bats @@ -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() { @@ -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"/* } @@ -96,6 +98,7 @@ OUT assert_output <