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