8430ddb1b0e4f964e7256c022363c69afe04a155

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

Message

Fix pipenv integration
Fixes issue #79

Diff

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