7a76b88035d854d419d8edcc9612e7053da906c5

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

Message

Merge pull request #168 from afeblot/master

If AUTOSWITCH_FILE is not a file but a virtualenv, activate it

Diff

  1diff --git a/README.rst b/README.rst
  2index 3182727b15dbbc9f1a5d1c8b58099b22bbf41f14..85280c0f4b85cb2a9e3ccfcb1fe2ad61af483b4b 100644
  3--- a/README.rst
  4+++ b/README.rst
  5@@ -56,6 +56,9 @@ same name as the current directory but can be edited if needed). There
  6 is then a precommand hook that looks for a ``.venv`` file and switches
  7 to the name specified if one is found.
  8 
  9+Autoswitch virtualenv also works automatically with projects which contains
 10+a ``.venv`` virtualenv directly created by the ``python -m venv`` command.
 11+
 12 For the case of pipenv projects, the plugin will look for a ``Pipfile``
 13 and activates pipenv if it detects an existing virtual environment for it.
 14 
 15@@ -264,8 +267,8 @@ the mkvenv command.
 16 
 17 **Autoswitch file name**
 18 
 19-By default, the `.venv` file is searched for in each directory in order to tell if
 20-a virtualenv should be automatically activated.
 21+By default, the `.venv` file (or virtualenv directory) is searched for in each
 22+directory in order to tell if a virtualenv should be automatically activated.
 23 
 24 If this needs to be changed (e.g. it conflicts with something else) then it may be
 25 changed by setting the value of ``AUTOSWITCH_FILE``. For example:
 26diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 27index bbd2308552eb8ba61004d3e87ae6692200d44e89..ef55eba8533b60ff305fb1e2ee5aa3cbe674574b 100644
 28--- a/autoswitch_virtualenv.plugin.zsh
 29+++ b/autoswitch_virtualenv.plugin.zsh
 30@@ -121,7 +121,7 @@ function _check_path()
 31 {
 32     local check_dir="$1"
 33 
 34-    if [[ -f "${check_dir}/${AUTOSWITCH_FILE}" ]]; then
 35+    if [[ -e "${check_dir}/${AUTOSWITCH_FILE}" ]]; then
 36         printf "${check_dir}/${AUTOSWITCH_FILE}"
 37         return
 38     elif [[ -f "${check_dir}/poetry.lock" ]]; then
 39@@ -180,11 +180,11 @@ function check_venv()
 40             file_permissions="$(/usr/bin/stat -f %OLp "$venv_path")"
 41         fi
 42 
 43-        if [[ "$file_owner" != "$(id -u)" ]]; then
 44+        if [[ -f "$venv_path" ]] && [[ "$file_owner" != "$(id -u)" ]]; then
 45             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 46             printf "Reason: Found a $AUTOSWITCH_FILE file but it is not owned by the current user\n"
 47             printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
 48-        elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
 49+        elif [[ -f "$venv_path" ]] && ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
 50             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 51             printf "Reason: Found a $AUTOSWITCH_FILE file with weak permission settings ($file_permissions).\n"
 52             printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
 53@@ -197,10 +197,15 @@ function check_venv()
 54                 if type "poetry" > /dev/null && _activate_poetry; then
 55                     return
 56                 fi
 57-            else
 58+            # standard use case: $venv_path is a file containing a virtualenv name
 59+            elif [[ -f "$venv_path" ]]; then
 60                 local switch_to="$(<"$venv_path")"
 61                 _maybeworkon "$(_virtual_env_dir "$switch_to")" "virtualenv"
 62                 return
 63+            # $venv_path actually is itself a virtualenv
 64+            elif [[ -d "$venv_path" ]] && [[ -f "$venv_path/bin/activate" ]]; then
 65+                _maybeworkon "$venv_path" "virtualenv"
 66+                return
 67             fi
 68         fi
 69     fi
 70diff --git a/tests/test_check_path.zunit b/tests/test_check_path.zunit
 71index 5fde6998bff10a62a05f9c3aeb8a8ddeef8d16b4..f144d871984552ac57616aeee681624c53a83632 100644
 72--- a/tests/test_check_path.zunit
 73+++ b/tests/test_check_path.zunit
 74@@ -11,7 +11,7 @@
 75     rm -rf "$TARGET"
 76 }
 77 
 78-@test '_check_path - finds .venv in base directory' {
 79+@test '_check_path - finds .venv file in base directory' {
 80     touch "$TARGET/.venv"
 81 
 82     run _check_path "$TARGET"
 83@@ -20,6 +20,15 @@
 84     assert "$output" same_as "$TARGET/.venv"
 85 }
 86 
 87+@test '_check_path - finds .venv directory in base directory' {
 88+    mkdir "$TARGET/.venv"
 89+
 90+    run _check_path "$TARGET"
 91+
 92+    assert $state equals 0
 93+    assert "$output" same_as "$TARGET/.venv"
 94+}
 95+
 96 @test '_check_path - returns nothing if not found' {
 97    run _check_path "$TARGET"
 98 
 99diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
100index 7ee686a82b5cbca6f4143bc5aaafb51efe205888..06a8583e24f8ce735ad9058fef4629e076b32dfd 100644
101--- a/tests/test_check_venv.zunit
102+++ b/tests/test_check_venv.zunit
103@@ -230,7 +230,7 @@
104     assert "$output" same_as "Deactivating: \e[1m\e[35mfoo-bar\e[0m"
105 }
106 
107-@test 'check_venv - works as intended with .venv available' {
108+@test 'check_venv - works as intended with .venv file available' {
109     PWD="$TARGET"
110 
111     echo "foobar" > "$TARGET/.venv"
112@@ -241,3 +241,23 @@
113     assert $status equals 0
114     assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[๐Ÿ$PYTHON_VERSION]\e[0m"
115 }
116+
117+@test 'check_venv - works as intended with .venv directory available' {
118+    PWD="$TARGET"
119+    run virtualenv "$TARGET/.venv" --python=python3
120+
121+    run check_venv
122+
123+    assert $status equals 0
124+    assert "$output" same_as "Switching virtualenv: \e[1m\e[35m.venv\e[0m \e[32m[๐Ÿ$PYTHON_VERSION]\e[0m"
125+}
126+
127+@test 'check_venv - works as intended with .venv directory not being virtualenv' {
128+    PWD="$TARGET"
129+    mkdir "$TARGET/.venv"
130+
131+    run check_venv
132+
133+    assert $status equals 0
134+    assert "$output" same_as ""
135+}