a2b60b8c528a9655a390b204fcfb02562657f6b5

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

Merge pull request #80 from MichaelAquilina/fix_pipenv

Fix pipenv integration

Diff

  1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
  2index dce513633e2203d6594f3ba043107b2f3dd9c0a3..8cab2a6ac8ebeb268b2f6b56fff8686f12589f3c 100644
  3--- a/CHANGELOG.rst
  4+++ b/CHANGELOG.rst
  5@@ -1,6 +1,10 @@
  6 Changelog
  7 =========
  8 
  9+1.8.1
 10+-----
 11+* Fixes a regression in Pipenv integration
 12+
 13 1.8.0
 14 -----
 15 * Prompt to install requirements.txt even if setup dependencies installed
 16diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 17index f7df59ef3bcda5f22a41614aea631b35ba5ea903..4aabc5d40c7683d2f5f3b46ff879779e2e6438f6 100644
 18--- a/autoswitch_virtualenv.plugin.zsh
 19+++ b/autoswitch_virtualenv.plugin.zsh
 20@@ -1,4 +1,4 @@
 21-export AUTOSWITCH_VERSION='1.8.0'
 22+export AUTOSWITCH_VERSION='1.8.1'
 23 
 24 RED="\e[31m"
 25 GREEN="\e[32m"
 26@@ -19,9 +19,10 @@ fi
 27 
 28 
 29 function _virtual_env_dir() {
 30+    local venv_name="$1"
 31     local VIRTUAL_ENV_DIR="${AUTOSWITCH_VIRTUAL_ENV_DIR:-$HOME/.virtualenvs}"
 32     mkdir -p "$VIRTUAL_ENV_DIR"
 33-    printf "%s" "$VIRTUAL_ENV_DIR"
 34+    printf "%s/%s" "$VIRTUAL_ENV_DIR" "$venv_name"
 35 }
 36 
 37 
 38@@ -37,8 +38,9 @@ function _python_version() {
 39 
 40 
 41 function _maybeworkon() {
 42-    venv_name="$1"
 43+    venv_dir="$1"
 44     venv_type="$2"
 45+    venv_name="$(basename $venv_dir)"
 46 
 47     DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[๐Ÿ%py_version]${NORMAL}"
 48     if [[ "$LANG" != *".UTF-8" ]]; then
 49@@ -48,8 +50,6 @@ function _maybeworkon() {
 50 
 51     if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
 52 
 53-        venv_dir="$(_virtual_env_dir)/$venv_name"
 54-
 55         if [[ ! -d "$venv_dir" ]]; then
 56             printf "Unable to find ${PURPLE}$venv_name${NORMAL} virtualenv\n"
 57             printf "If the issue persists run ${PURPLE}rmvenv && mkvenv${NORMAL} in this directory\n"
 58@@ -67,7 +67,7 @@ function _maybeworkon() {
 59         fi
 60 
 61         # Much faster to source the activate file directly rather than use the `workon` command
 62-        source "$(_virtual_env_dir)/$venv_name/bin/activate"
 63+        source "$venv_dir/bin/activate"
 64     fi
 65 }
 66 
 67@@ -124,12 +124,12 @@ function check_venv()
 68     fi
 69 
 70     if [[ -n "$SWITCH_TO" ]]; then
 71-        _maybeworkon "$SWITCH_TO" "virtualenv"
 72+        _maybeworkon "$(_virtual_env_dir "$SWITCH_TO")" "virtualenv"
 73 
 74         # check if Pipfile exists rather than invoking pipenv as it is slow
 75     elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
 76         venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
 77-        _maybeworkon "$(basename "$venv_path")" "pipenv"
 78+        _maybeworkon "$venv_path" "pipenv"
 79     else
 80         _default_venv
 81     fi
 82@@ -139,7 +139,7 @@ function check_venv()
 83 function _default_venv()
 84 {
 85     if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
 86-        _maybeworkon "$AUTOSWITCH_DEFAULTENV" "virtualenv"
 87+        _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "virtualenv"
 88     elif [[ -n "$VIRTUAL_ENV" ]]; then
 89         deactivate
 90     fi
 91@@ -162,7 +162,7 @@ function rmvenv()
 92         fi
 93 
 94         printf "Removing ${PURPLE}%s${NORMAL}...\n" "$venv_name"
 95-        rm -rf "$(_virtual_env_dir)/$venv_name"
 96+        rm -rf "$(_virtual_env_dir "$venv_name")"
 97         rm ".venv"
 98     else
 99         printf "No .venv file in the current directory!\n"
100@@ -188,15 +188,15 @@ function mkvenv()
101         fi
102 
103         if [[ ${params[(I)--verbose]} -eq 0 ]]; then
104-            virtualenv $params "$(_virtual_env_dir)/$venv_name"
105+            virtualenv $params "$(_virtual_env_dir "$venv_name")"
106         else
107-            virtualenv $params "$(_virtual_env_dir)/$venv_name" > /dev/null
108+            virtualenv $params "$(_virtual_env_dir "$venv_name")" > /dev/null
109         fi
110 
111         printf "$venv_name\n" > ".venv"
112         chmod 600 .venv
113 
114-        _maybeworkon "$venv_name"
115+        _maybeworkon "$(_virtual_env_dir "$venv_name")" "virtualenv"
116 
117         install_requirements
118     fi
119diff --git a/tests/test_install_requirements.txt b/tests/test_install_requirements.txt
120index 9513251da44328e995813219a3794a5b23e34d87..5ad700495bb58597646086c83d80a8b32b5d3d9e 100644
121--- a/tests/test_install_requirements.txt
122+++ b/tests/test_install_requirements.txt
123@@ -3,6 +3,7 @@
124 @setup {
125     export DISABLE_AUTOSWITCH_VENV="1"
126     load "../autoswitch_virtualenv.plugin.zsh"
127+    unset AUTOSWITCH_DEFAULT_REQUIREMENTS
128 
129     function read {
130         eval $1=\"y\"
131diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
132index 7ffcfa845cd07526859855ebe5d04c06779408ae..ee9d7d08281b34b2c7d3c202efbd1980f244b6b9 100644
133--- a/tests/test_maybeworkon.zunit
134+++ b/tests/test_maybeworkon.zunit
135@@ -2,10 +2,11 @@
136 
137 
138 @setup {
139+    TEST_VIRTUALENV="$HOME/.virtualenvs/foobar/"
140 
141-    if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
142+    if [[ ! -d "$TEST_VIRTUALENV" ]]; then
143         echo "Creating test virtualenv"
144-        run python -m virtualenv "$HOME/.virtualenvs/foobar" --python=python3
145+        run python -m virtualenv "$TEST_VIRTUALENV" --python=python3
146     fi
147 
148     unset AUTOSWITCH_MESSAGE_FORMAT
149@@ -23,7 +24,7 @@
150 @test '_maybeworkon - error message if virtualenv can not be found' {
151     VIRTUAL_ENV=""
152 
153-    run _maybeworkon idontexist virtualenv
154+    run _maybeworkon $HOME/.virtualenvs/idontexist virtualenv
155 
156     assert $state equals 0
157     assert "${lines[1]}" same_as "Unable to find \e[35midontexist\e[0m virtualenv"
158@@ -34,7 +35,7 @@
159 @test '_maybeworkon - switches virtualenv if nothing is activated' {
160     VIRTUAL_ENV=""
161 
162-    run _maybeworkon foobar virtualenv
163+    run _maybeworkon "$TEST_VIRTUALENV" virtualenv
164 
165     assert $state equals 0
166     assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[๐Ÿ$PYTHON_VERSION]\e[0m"
167@@ -44,7 +45,7 @@
168     VIRTUAL_ENV=""
169     AUTOSWITCH_MESSAGE_FORMAT="Hello %venv_type - this is %venv_name (%py_version)"
170 
171-    run _maybeworkon foobar cheese
172+    run _maybeworkon "$TEST_VIRTUALENV" cheese
173 
174     assert $state equals 0
175     assert "$output" same_as "Hello cheese - this is foobar ($PYTHON_VERSION)"
176@@ -54,7 +55,7 @@
177     VIRTUAL_ENV=""
178     LANG="ASCII"
179 
180-    run _maybeworkon foobar virtualenv
181+    run _maybeworkon "$TEST_VIRTUALENV" virtualenv
182 
183     assert $state equals 0
184     assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
185@@ -63,7 +64,7 @@
186 @test '_maybeworkon - switches virtualenv if current virtualenv is different' {
187     VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
188 
189-    run _maybeworkon foobar virtualfoo
190+    run _maybeworkon "$TEST_VIRTUALENV" virtualfoo
191 
192     assert $state equals 0
193     assert "$output" contains "Switching virtualfoo: \e[1m\e[35mfoobar\e[0m \e[32m[๐Ÿ$PYTHON_VERSION]\e[0m"
194@@ -73,7 +74,7 @@
195     VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
196     AUTOSWITCH_SILENT="1"
197 
198-    run _maybeworkon foobar virtualenv
199+    run _maybeworkon "$TEST_VIRTUALENV" virtualenv
200 
201     assert $state equals 0
202     assert "$output" is_empty
203diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
204index 6a5854c1791ab21b517d9315f7756062eb1bdfb8..530319a2a186ea26a7b54c2bede4edadb9f4fa5d 100644
205--- a/tests/test_mkvenv.zunit
206+++ b/tests/test_mkvenv.zunit
207@@ -4,6 +4,7 @@
208 
209     export DISABLE_AUTOSWITCH_VENV="1"
210     load "../autoswitch_virtualenv.plugin.zsh"
211+    unset AUTOSWITCH_DEFAULT_REQUIREMENTS
212     TARGET="$(mktemp -d)"
213     cd "$TARGET"
214 }
215diff --git a/tests/test_rmvenv.zunit b/tests/test_rmvenv.zunit
216index 9ef479905b18eedbd79371017b6ac79df7376081..fdce5fa59c46cc445f442c98687d22ebfde9815f 100644
217--- a/tests/test_rmvenv.zunit
218+++ b/tests/test_rmvenv.zunit
219@@ -4,6 +4,7 @@
220 
221     export DISABLE_AUTOSWITCH_VENV="1"
222     load "../autoswitch_virtualenv.plugin.zsh"
223+    unset AUTOSWITCH_DEFAULT_REQUIREMENTS
224     TARGET="$(mktemp -d)"
225     cd "$TARGET"
226 }