Diff
1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
2index 5bc2cd3936eb33f3b57b7b983929409ea400644a..f0768dd9f7ba0e75896e61419495774c555ecc02 100644
3--- a/CHANGELOG.rst
4+++ b/CHANGELOG.rst
5@@ -1,6 +1,10 @@
6 Changelog
7 =========
8
9+1.13.0
10+------
11+* Clean up pipenv names when displaying them
12+
13 1.12.0
14 ------
15 * Remove virtualenv requirement. This is now only needed for running mkvenv
16diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
17index 1ec1483be13cf33d7d56ed90c3406a47536d3e46..807f0796a52abb09d2763b4ad65e0ba9d45eb91c 100644
18--- a/autoswitch_virtualenv.plugin.zsh
19+++ b/autoswitch_virtualenv.plugin.zsh
20@@ -1,4 +1,4 @@
21-export AUTOSWITCH_VERSION='1.12.0'
22+export AUTOSWITCH_VERSION='1.13.0'
23
24 RED="\e[31m"
25 GREEN="\e[32m"
26@@ -31,11 +31,36 @@ function _autoswitch_message() {
27 fi
28 }
29
30+function _get_venv_type() {
31+ local venv_dir="$1"
32+ local venv_type="${2:-virtualenv}"
33+ if [[ -f "$venv_dir/Pipfile" ]]; then
34+ venv_type="pipenv"
35+ elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
36+ venv_type="virtualenv"
37+ fi
38+ printf "%s" "$venv_type"
39+}
40+
41+
42+function _get_venv_name() {
43+ local venv_dir="$1"
44+ local venv_type="$2"
45+ local venv_name="$(basename "$venv_dir")"
46+
47+ # clear pipenv from the extra identifiers at the end
48+ if [[ "$venv_type" == "pipenv" ]]; then
49+ venv_name="${venv_name%-*}"
50+ fi
51+
52+ printf "%s" "$venv_name"
53+}
54+
55
56 function _maybeworkon() {
57 local venv_dir="$1"
58 local venv_type="$2"
59- local venv_name="$(basename $venv_dir)"
60+ local venv_name="$(_get_venv_name $venv_dir $venv_type)"
61
62 local DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[๐%py_version]${NORMAL}"
63 if [[ "$LANG" != *".UTF-8" ]]; then
64@@ -90,6 +115,7 @@ function check_venv()
65
66 # Get the .venv file, scanning parent directories
67 local venv_path=$(_check_venv_path "$PWD")
68+
69 if [[ -n "$venv_path" ]]; then
70
71 stat --version &> /dev/null
72@@ -117,18 +143,20 @@ function check_venv()
73 fi
74
75 # check if Pipfile exists rather than invoking pipenv as it is slow
76- if [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
77+ if [[ -f "Pipfile" ]] && type "pipenv" > /dev/null; then
78 if venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv 2>/dev/null)"; then
79 _maybeworkon "$venv_path" "pipenv"
80 return
81 fi
82 fi
83
84+ local venv_type="$(_get_venv_type "$PWD" "unknown")"
85+
86 # If we still haven't got anywhere, fallback to defaults
87- if [[ -f "$PWD/Pipfile" ]]; then
88+ if [[ "$venv_type" == "pipenv" ]]; then
89 printf "Python project detected. "
90 printf "Run ${PURPLE}pipenv install${NORMAL} to setup autoswitching\n"
91- elif [[ -f "$PWD/requirements.txt" || -f "$PWD/setup.py" ]]; then
92+ elif [[ "$venv_type" == "virtualenv" ]]; then
93 printf "Python project detected. "
94 printf "Run ${PURPLE}mkvenv${NORMAL} to setup autoswitching\n"
95 fi
96@@ -138,10 +166,12 @@ function check_venv()
97 # Switch to the default virtual environment
98 function _default_venv()
99 {
100+ local venv_type="$(_get_venv_type "$OLDPWD")"
101 if [[ -n "$AUTOSWITCH_DEFAULTENV" ]]; then
102- _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "virtualenv"
103+ _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "$venv_type"
104 elif [[ -n "$VIRTUAL_ENV" ]]; then
105- _autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$(basename "$VIRTUAL_ENV")"
106+ local venv_name="$(_get_venv_name "$VIRTUAL_ENV" "$venv_type")"
107+ _autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$venv_name"
108 deactivate
109 fi
110 }
111diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
112index ae0754202c076d31872fd7a1ec3d3e8760a88fc6..7cf209224ba7d661abdd3c329f5807453db8788e 100644
113--- a/tests/test_check_venv.zunit
114+++ b/tests/test_check_venv.zunit
115@@ -17,11 +17,13 @@
116 export DISABLE_AUTOSWITCH_VENV="1"
117 load "../autoswitch_virtualenv.plugin.zsh"
118 TARGET="$(mktemp -d)"
119+ OLDPWD="$(mktemp -d)"
120 }
121
122
123 @teardown {
124 rm -rf "$TARGET"
125+ rm -rf "$OLDPWD"
126 rm -rf Pipfile
127 }
128
129@@ -180,6 +182,20 @@
130 assert "$output" same_as "Deactivating: \e[1m\e[35mfoo\e[0m"
131 }
132
133+@test 'check_venv - deactivate if neither .venv nor pipenv available (previous dir is pipenv)' {
134+ PWD="$TARGET"
135+ touch "$OLDPWD/Pipfile"
136+
137+ VIRTUAL_ENV="foo-bar-defa23"
138+ unset AUTOSWITCH_DEFAULTENV
139+ function deactivate {
140+ }
141+
142+ run check_venv
143+
144+ assert $status equals 0
145+ assert "$output" same_as "Deactivating: \e[1m\e[35mfoo-bar\e[0m"
146+}
147
148 @test 'check_venv - works as intended with .venv available' {
149 PWD="$TARGET"
150diff --git a/tests/test_get_venv_type.zunit b/tests/test_get_venv_type.zunit
151new file mode 100644
152index 0000000000000000000000000000000000000000..f1817798740486c763095db36ba48da49ff9864a
153--- /dev/null
154+++ b/tests/test_get_venv_type.zunit
155@@ -0,0 +1,52 @@
156+#!/usr/bin/env zunit
157+
158+@setup {
159+ load "../autoswitch_virtualenv.plugin.zsh"
160+ TARGET="$(mktemp -d)"
161+}
162+
163+@teardown {
164+ rm -rf "$TARGET"
165+}
166+
167+@test '_get_venv_type pipenv' {
168+ touch "$TARGET/Pipfile"
169+
170+ run _get_venv_type "$TARGET"
171+
172+ assert $state equals 0
173+ assert "$output" same_as "pipenv"
174+}
175+
176+
177+@test '_get_venv_type virtualenv (requirements.txt)' {
178+ touch "$TARGET/requirements.txt"
179+
180+ run _get_venv_type "$TARGET" "unknown"
181+
182+ assert $state equals 0
183+ assert "$output" same_as "virtualenv"
184+}
185+
186+@test '_get_venv_type virtualenv (setup.py)' {
187+ touch "$TARGET/setup.py"
188+
189+ run _get_venv_type "$TARGET" "unknown"
190+
191+ assert $state equals 0
192+ assert "$output" same_as "virtualenv"
193+}
194+
195+@test '_get_venv_type virtualenv (default)' {
196+ run _get_venv_type "$TARGET"
197+
198+ assert $state equals 0
199+ assert "$output" same_as "virtualenv"
200+}
201+
202+@test '_get_venv_type virtualenv (default specified)' {
203+ run _get_venv_type "$TARGET" "foobar"
204+
205+ assert $state equals 0
206+ assert "$output" same_as "foobar"
207+}
208diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
209index ee9d7d08281b34b2c7d3c202efbd1980f244b6b9..82df610c938436f364ae216d8aa24626e9ab4106 100644
210--- a/tests/test_maybeworkon.zunit
211+++ b/tests/test_maybeworkon.zunit
212@@ -88,3 +88,10 @@
213 assert $state equals 0
214 assert "$output" is_empty
215 }
216+
217+@test '_maybeworkon - pipenv name cleaned' [
218+ run _maybeworkon hello-world-foo-de31f pipenv
219+
220+ assert $state equals 0
221+ assert "$output" same_as "Switching pipenv: \e[1m\e[35mhello-world-foo\e[0m \e[32m[๐$PYTHON_VERSION]\e[0m"
222+}