From c736a733a67ce8263b32070ba131bcd5eb42c3c9 Mon Sep 17 00:00:00 2001 From: Wouter Weerkamp - Zeta Alpha Vector Date: Wed, 11 Dec 2019 11:32:27 +0100 Subject: [PATCH 1/3] feature: Added support for pyproject.toml. Installs requirements, fails when not run in FULL mode (not supported for non-setuptools builds). --- autoswitch_virtualenv.plugin.zsh | 33 ++++++++++++++++++++------- tests/test_check_venv.zunit | 10 ++++++++ tests/test_get_venv_type.zunit | 9 ++++++++ tests/test_install_requirements.zunit | 31 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh index d91669a..8e9b36f 100644 --- a/autoswitch_virtualenv.plugin.zsh +++ b/autoswitch_virtualenv.plugin.zsh @@ -18,7 +18,7 @@ function _virtual_env_dir() { function _python_version() { local PYTHON_BIN="$1" - if [[ -f "$PYTHON_BIN" ]] then + if [[ -f "$PYTHON_BIN" ]]; then # For some reason python --version writes to stderr printf "%s" "$($PYTHON_BIN --version 2>&1)" else @@ -38,7 +38,7 @@ function _get_venv_type() { local venv_type="${2:-virtualenv}" if [[ -f "$venv_dir/Pipfile" ]]; then venv_type="pipenv" - elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then + elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" || -f "$venv_dir/pyproject.toml" ]]; then venv_type="virtualenv" fi printf "%s" "$venv_type" @@ -62,7 +62,7 @@ function _get_venv_name() { function _maybeworkon() { local venv_dir="$1" local venv_type="$2" - local venv_name="$(_get_venv_name $venv_dir $venv_type)" + local venv_name="$(_get_venv_name "$venv_dir" "$venv_type")" local DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[🐍%py_version]${NORMAL}" if [[ "$LANG" != *".UTF-8" ]]; then @@ -70,7 +70,7 @@ function _maybeworkon() { DEFAULT_MESSAGE_FORMAT="${DEFAULT_MESSAGE_FORMAT/🐍/}" fi - if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then + if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename "$VIRTUAL_ENV")" ]]; then if [[ ! -d "$venv_dir" ]]; then printf "Unable to find ${PURPLE}$venv_name${NORMAL} virtualenv\n" @@ -83,7 +83,7 @@ function _maybeworkon() { message="${message//\%venv_type/$venv_type}" message="${message//\%venv_name/$venv_name}" message="${message//\%py_version/$py_version}" - _autoswitch_message "${message}\n" + _autoswitch_message "${message}" # If we are using pipenv and activate its virtual environment - turn down its verbosity # to prevent users seeing " Pipenv found itself running within a virtual environment" warning @@ -103,7 +103,7 @@ function _check_venv_path() local check_dir="$1" if [[ -f "${check_dir}/$AUTOSWITCH_FILE" ]]; then - printf "${check_dir}/$AUTOSWITCH_FILE" + printf "%s/$AUTOSWITCH_FILE" "${check_dir}" return else # Abort search at file system root or HOME directory (latter is a performance optimisation). @@ -179,7 +179,7 @@ function _default_venv() _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "$venv_type" elif [[ -n "$VIRTUAL_ENV" ]]; then local venv_name="$(_get_venv_name "$VIRTUAL_ENV" "$venv_type")" - _autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$venv_name" + _autoswitch_message "Deactivating: ${BOLD}${PURPLE}$venv_name${NORMAL}" deactivate fi } @@ -228,7 +228,7 @@ function mkvenv() if [[ -f "$AUTOSWITCH_FILE" ]]; then printf "$AUTOSWITCH_FILE file already exists. If this is a mistake use the rmvenv command\n" else - local venv_name="$(basename $PWD)" + local venv_name="$(basename "$PWD")" printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name" @@ -279,6 +279,23 @@ function install_requirements() { fi fi + if [[ -f "$PWD/pyproject.toml" ]]; then + printf "Found a ${PURPLE}pyproject.toml${NORMAL} file. Install dependencies? [y/N]: " + read ans + + if [[ "$ans" = "y" || "$ans" = "Y" ]]; then + if [[ "$AUTOSWITCH_PIPINSTALL" = "FULL" ]] + then + pip install . + else + printf "Found a pyproject.toml file, but editable mode currently requires a setup.py based build.\n" + printf "Set AUTOSWITCH_PIPINSTALL to FULL to disable editable mode.\n" + printf "\n" + return + fi + fi + fi + setopt nullglob local requirements for requirements in **/*requirements.txt diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit index 7cf2092..f3299a2 100644 --- a/tests/test_check_venv.zunit +++ b/tests/test_check_venv.zunit @@ -47,6 +47,16 @@ assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching" } +@test 'check_venv - Displays message on project detection (pyproject.toml)' { + PWD="$TARGET" + touch "$TARGET/pyproject.toml" + + run check_venv + + assert $status equals 0 + assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching" +} + @test 'check_venv - Displays message on project detection (Pipfile)' { PWD="$TARGET" touch "$TARGET/Pipfile" diff --git a/tests/test_get_venv_type.zunit b/tests/test_get_venv_type.zunit index f181779..1ca3c4b 100644 --- a/tests/test_get_venv_type.zunit +++ b/tests/test_get_venv_type.zunit @@ -37,6 +37,15 @@ assert "$output" same_as "virtualenv" } +@test '_get_venv_type virtualenv (pyproject.toml)' { + touch "$TARGET/pyproject.toml" + + run _get_venv_type "$TARGET" "unknown" + + assert $state equals 0 + assert "$output" same_as "virtualenv" +} + @test '_get_venv_type virtualenv (default)' { run _get_venv_type "$TARGET" diff --git a/tests/test_install_requirements.zunit b/tests/test_install_requirements.zunit index 33be19c..fd3ee6a 100644 --- a/tests/test_install_requirements.zunit +++ b/tests/test_install_requirements.zunit @@ -103,6 +103,21 @@ assert "$lines[2]" same_as "pip install -e ." } +@test 'install_requirements - installs pyproject.toml' { + touch "pyproject.toml" + + # mock pip to make it testable + function pip { + echo "pip $@" + } + + run install_requirements + + assert $status equals 0 + assert "$lines[1]" same_as "Found a \e[35mpyproject.toml\e[0m file. Install dependencies? [y/N]: y" + assert "$lines[2]" same_as "Found a pyproject.toml file, but editable mode currently requires a setup.py based build.\n" +} + @test 'install_requirements - installs recursive *requirements.txt' { echo "django" > requirements.txt mkdir subdir @@ -142,3 +157,19 @@ assert "$lines[1]" same_as "Found a \e[35msetup.py\e[0m file. Install dependencies? [y/N]: y" assert "$lines[2]" same_as "pip install ." } + +@test 'install_requirements - installs pyproject.toml - full' { + touch "pyproject.toml" + + # mock pip to make it testable + function pip { + echo "pip $@" + } + local AUTOSWITCH_PIPINSTALL="FULL" + + run install_requirements + + assert $status equals 0 + assert "$lines[1]" same_as "Found a \e[35mpyproject.toml\e[0m file. Install dependencies? [y/N]: y" + assert "$lines[2]" same_as "pip install ." +} From 0535b019e8b1a36fa273720abc18c58930bc7a29 Mon Sep 17 00:00:00 2001 From: Wouter Weerkamp - Zeta Alpha Vector Date: Fri, 13 Dec 2019 08:25:02 +0100 Subject: [PATCH 2/3] fix: install dependencies for pyproject.toml, ignoring AUTOSWITCH_PIPINSTALL variable. --- autoswitch_virtualenv.plugin.zsh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh index 8e9b36f..85b5a7f 100644 --- a/autoswitch_virtualenv.plugin.zsh +++ b/autoswitch_virtualenv.plugin.zsh @@ -179,7 +179,7 @@ function _default_venv() _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "$venv_type" elif [[ -n "$VIRTUAL_ENV" ]]; then local venv_name="$(_get_venv_name "$VIRTUAL_ENV" "$venv_type")" - _autoswitch_message "Deactivating: ${BOLD}${PURPLE}$venv_name${NORMAL}" + _autoswitch_message "Deactivating: ${BOLD}${PURPLE}$venv_name${NORMAL}\n" deactivate fi } @@ -284,15 +284,7 @@ function install_requirements() { read ans if [[ "$ans" = "y" || "$ans" = "Y" ]]; then - if [[ "$AUTOSWITCH_PIPINSTALL" = "FULL" ]] - then - pip install . - else - printf "Found a pyproject.toml file, but editable mode currently requires a setup.py based build.\n" - printf "Set AUTOSWITCH_PIPINSTALL to FULL to disable editable mode.\n" - printf "\n" - return - fi + pip install . fi fi From 8364a146195f3e9352183bea8b18cb390b2e4c6d Mon Sep 17 00:00:00 2001 From: Wouter Weerkamp - Zeta Alpha Vector Date: Fri, 13 Dec 2019 08:25:45 +0100 Subject: [PATCH 3/3] tests: updated 'install_requirements - installs pyproject.toml' test to reflect changes in plugin --- tests/test_install_requirements.zunit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_install_requirements.zunit b/tests/test_install_requirements.zunit index fd3ee6a..d8c975b 100644 --- a/tests/test_install_requirements.zunit +++ b/tests/test_install_requirements.zunit @@ -115,7 +115,7 @@ assert $status equals 0 assert "$lines[1]" same_as "Found a \e[35mpyproject.toml\e[0m file. Install dependencies? [y/N]: y" - assert "$lines[2]" same_as "Found a pyproject.toml file, but editable mode currently requires a setup.py based build.\n" + assert "$lines[2]" same_as "pip install ." } @test 'install_requirements - installs recursive *requirements.txt' {