941e378c6aeccfb8fef287d37f8036a2063341a5

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

Message

Merge pull request #64 from MichaelAquilina/error_message_missing_virtualenv

Show error message if target virtualenv is not found

Diff

 1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 2index f433ecd690d20530e78b0fc56f12bf2b52416d30..87cc67788deba56dff2e998f65dacd595e141713 100644
 3--- a/autoswitch_virtualenv.plugin.zsh
 4+++ b/autoswitch_virtualenv.plugin.zsh
 5@@ -43,8 +43,17 @@ function _maybeworkon() {
 6     DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[%py_version]${NORMAL}"
 7 
 8     if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
 9+
10+        venv_dir="$(_virtual_env_dir)/$venv_name"
11+
12+        if [[ ! -d "$venv_dir" ]]; then
13+            printf "Unable to find ${PURPLE}$venv_name${NORMAL} virtualenv\n"
14+            printf "If the issue persists run ${PURPLE}rmvenv && mkvenv${NORMAL} in this directory\n"
15+            return
16+        fi
17+
18         if [ -z "$AUTOSWITCH_SILENT" ]; then
19-            py_version="$(_python_version "$(_virtual_env_dir)/$venv_name/bin/python")"
20+            py_version="$(_python_version "$venv_dir/bin/python")"
21 
22             message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
23             message="${message//\%venv_type/$venv_type}"
24diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
25index d698ef504424cad6010b74d5ba6bedfdda12f72e..d64d291d77a0a9d83adc6477a01c45eca6862bb0 100644
26--- a/tests/test_maybeworkon.zunit
27+++ b/tests/test_maybeworkon.zunit
28@@ -20,6 +20,16 @@
29     rm -rf "$TARGET"
30 }
31 
32+@test '_maybeworkon - error message if virtualenv can not be found' {
33+    VIRTUAL_ENV=""
34+
35+    run _maybeworkon idontexist virtualenv
36+
37+    assert $state equals 0
38+    assert "${lines[1]}" same_as "Unable to find \e[35midontexist\e[0m virtualenv"
39+    assert "${lines[2]}" same_as "If the issue persists run \e[35mrmvenv && mkvenv\e[0m in this directory"
40+}
41+
42 
43 @test '_maybeworkon - switches virtualenv if nothing is activated' {
44     VIRTUAL_ENV=""