Diff
1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
2index c4a42062bb158a8d44d73aee40d5aca64b55e0b2..1274c08e62c9dce62cbfa7b2e7259436f79010d9 100644
3--- a/autoswitch_virtualenv.plugin.zsh
4+++ b/autoswitch_virtualenv.plugin.zsh
5@@ -10,6 +10,8 @@ PURPLE="\e[35m"
6 BOLD="\e[1m"
7 NORMAL="\e[0m"
8
9+DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[%py_version]${NORMAL}"
10+
11
12 if ! type "virtualenv" > /dev/null; then
13 export DISABLE_AUTOSWITCH_VENV="1"
14@@ -22,45 +24,34 @@ if ! type "virtualenv" > /dev/null; then
15 fi
16
17
18-function _print_python_version() {
19- # For some reason python --version writes to stderr
20- if type python > /dev/null; then
21- printf "${GREEN}[%s]${NORMAL}\n" "$(python --version 2>&1)"
22- elif type python3 > /dev/null; then
23- printf "${GREEN}[%s]${NORMAL}\n" "$(python3 --version 2>&1)"
24+function _python_version() {
25+ PYTHON_BIN="$1"
26+ if [[ -f "$PYTHON_BIN" ]] then
27+ # For some reason python --version writes to stderr
28+ printf "%s" "$($PYTHON_BIN --version 2>&1)"
29 else
30- printf "Unable to find python installed on this machine"
31- fi
32+ printf "unknown"
33+ fi
34 }
35
36
37 function _maybeworkon() {
38- if [[ -z "$VIRTUAL_ENV" || "$1" != "$(basename $VIRTUAL_ENV)" ]]; then
39- if [ -z "$AUTOSWITCH_SILENT" ]; then
40- printf "Switching virtualenv: ${BOLD}${PURPLE}%s${NORMAL} " $1
41- fi
42-
43- # Much faster to source the activate file directly rather than use the `workon` command
44- source "$VIRTUAL_ENV_DIR/$1/bin/activate"
45+ venv_name="$1"
46+ venv_type="$2"
47
48+ if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
49 if [ -z "$AUTOSWITCH_SILENT" ]; then
50- _print_python_version
51- fi
52- fi
53-}
54-
55+ py_version="$(_python_version "$VIRTUAL_ENV_DIR/$venv_name/bin/python")"
56
57-function _maybepipenv() {
58- if [[ -z "$VIRTUAL_ENV" || "$1" != "$VIRTUAL_ENV" ]]; then
59- if [ -z "$AUTOSWITCH_SILENT" ]; then
60- printf "Switching pipenv: ${BOLD}${PURPLE}%s${NORMAL} " "$(basename "$1")"
61+ message="$DEFAULT_MESSAGE_FORMAT"
62+ message="${message//\%venv_type/$venv_type}"
63+ message="${message//\%venv_name/$venv_name}"
64+ message="${message//\%py_version/$py_version}"
65+ printf "${message}\n"
66 fi
67
68- source "$1/bin/activate"
69-
70- if [ -z "$AUTOSWITCH_SILENT" ]; then
71- _print_python_version
72- fi
73+ # Much faster to source the activate file directly rather than use the `workon` command
74+ source "$VIRTUAL_ENV_DIR/$venv_name/bin/activate"
75 fi
76 }
77
78@@ -120,11 +111,12 @@ function check_venv()
79 fi
80
81 if [[ -n "$SWITCH_TO" ]]; then
82- _maybeworkon "$SWITCH_TO"
83+ _maybeworkon "$SWITCH_TO" "virtualenv"
84
85 # check if Pipfile exists rather than invoking pipenv as it is slow
86 elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
87- _maybepipenv "$(pipenv --venv)"
88+ venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
89+ _maybeworkon "$(basename "$venv_path")" "pipenv"
90 else
91 _default_venv
92 fi
93@@ -135,7 +127,7 @@ function check_venv()
94 function _default_venv()
95 {
96 if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
97- _maybeworkon "$AUTOSWITCH_DEFAULTENV"
98+ _maybeworkon "$AUTOSWITCH_DEFAULTENV" "virtualenv"
99 elif [[ -n "$VIRTUAL_ENV" ]]; then
100 deactivate
101 fi
102diff --git a/tests/test_maybepipenv.zunit b/tests/test_maybepipenv.zunit
103deleted file mode 100644
104index ed03b9712a79f98a7b5ce317bc6a223120029850..0000000000000000000000000000000000000000
105--- a/tests/test_maybepipenv.zunit
106+++ /dev/null
107@@ -1,62 +0,0 @@
108-#!/usr/bin/env zunit
109-
110-@setup {
111-
112- if ! type "pipenv" > /dev/null; then
113- skip "You need to install pipenv to run these tests"
114- fi
115-
116- echo "Creating test pipenv"
117- run pipenv --python 3
118-
119- PYTHON_VERSION="$(python3 --version 2>&1)"
120-
121- export DISABLE_AUTOSWITCH_VENV="1"
122- load "../autoswitch_virtualenv.plugin.zsh"
123- TARGET="$(mktemp -d)"
124-}
125-
126-@teardown {
127- rm -rf "$TARGET"
128- run deactivate 2>/dev/null
129- run pipenv --rm
130- rm -rf Pipfile
131-}
132-
133-
134-@test '_maybepipenv - switches virtualenv if nothing is activated' {
135- VIRTUAL_ENV=""
136-
137- run _maybepipenv "$(pipenv --venv)"
138-
139- assert $state equals 0
140- assert "$output" contains "Switching pipenv: \e[1m\e[35m$(basename "$PWD")"
141-}
142-
143-@test '_maybepipenv - switches virtualenv if current virtualenv is different' {
144- VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
145-
146- run _maybepipenv "$(pipenv --venv)"
147-
148- assert $state equals 0
149- assert "$output" contains "Switching pipenv: \e[1m\e[35m$(basename "$PWD")"
150-}
151-
152-@test '_maybepipenv - switches virtualenv if current virtualenv is different (silent)' {
153- VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
154- AUTOSWITCH_SILENT="1"
155-
156- run _maybepipenv "$(pipenv --venv)"
157-
158- assert $state equals 0
159- assert "$output" is_empty
160-}
161-
162-@test '_maybepipenv - does not switch to already activated virtualenv' {
163- VIRTUAL_ENV="$(pipenv --venv)"
164-
165- run _maybepipenv "$(pipenv --venv)"
166-
167- assert $state equals 0
168- assert "$output" is_empty
169-}
170diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
171index 7af6531af8768942f08a4eeeebb438c96e9c6065..60a92d0321806802f804c7d13f4a95ceec3274bf 100644
172--- a/tests/test_maybeworkon.zunit
173+++ b/tests/test_maybeworkon.zunit
174@@ -23,7 +23,7 @@
175 @test '_maybeworkon - switches virtualenv if nothing is activated' {
176 VIRTUAL_ENV=""
177
178- run _maybeworkon foobar
179+ run _maybeworkon foobar virtualenv
180
181 assert $state equals 0
182 assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
183@@ -32,17 +32,17 @@
184 @test '_maybeworkon - switches virtualenv if current virtualenv is different' {
185 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
186
187- run _maybeworkon foobar
188+ run _maybeworkon foobar virtualfoo
189
190 assert $state equals 0
191- assert "$output" contains "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
192+ assert "$output" contains "Switching virtualfoo: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
193 }
194
195 @test '_maybeworkon - switches virtualenv if current virtualenv is different (silent)' {
196 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
197 AUTOSWITCH_SILENT="1"
198
199- run _maybeworkon foobar
200+ run _maybeworkon foobar virtualenv
201
202 assert $state equals 0
203 assert "$output" is_empty
204@@ -51,7 +51,7 @@
205 @test '_maybeworkon - does not switch to already activated virtualenv' {
206 VIRTUAL_ENV="$HOME/.virtualenvs/bwaaah"
207
208- run _maybeworkon bwaaah
209+ run _maybeworkon bwaaah virtualenv
210
211 assert $state equals 0
212 assert "$output" is_empty