0135d1137a1e33f67cbbca173f50f4857c5b561b

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

Message

Merge pull request #61 from MichaelAquilina/next

v1.3.1

Diff

  1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
  2index 9a78cd18ed6588e6aed95893cd75455267aabaa6..c0661ade4102a9de89d8f2edab2f88c529444121 100644
  3--- a/CHANGELOG.rst
  4+++ b/CHANGELOG.rst
  5@@ -1,6 +1,11 @@
  6 Changelog
  7 =========
  8 
  9+1.3.1
 10+-----
 11+* Remove redundancy in directory checking logic
 12+* Display help message when python project is detected
 13+
 14 1.2.1
 15 -----
 16 * Improvements to coloring of output
 17diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 18index 51f5ee49599ccd73d1f1c9d1c95afb4831ec82e0..f433ecd690d20530e78b0fc56f12bf2b52416d30 100644
 19--- a/autoswitch_virtualenv.plugin.zsh
 20+++ b/autoswitch_virtualenv.plugin.zsh
 21@@ -1,4 +1,4 @@
 22-export AUTOSWITCH_VERSION='1.2.1'
 23+export AUTOSWITCH_VERSION='1.3.1'
 24 
 25 RED="\e[31m"
 26 GREEN="\e[32m"
 27@@ -79,50 +79,46 @@ function _check_venv_path()
 28 # Automatically switch virtualenv when .venv file detected
 29 function check_venv()
 30 {
 31-    if [ "AS:$PWD" != "$MYOLDPWD" ]; then
 32-        # Prefix PWD with "AS:" to signify this belongs to this plugin
 33-        # this prevents the AUTONAMEDIRS in prezto from doing strange things
 34-        # See https://github.com/MichaelAquilina/zsh-autoswitch-virtualenv/issues/19
 35-        MYOLDPWD="AS:$PWD"
 36-
 37-        SWITCH_TO=""
 38-
 39-        # Get the .venv file, scanning parent directories
 40-        venv_path=$(_check_venv_path "$PWD")
 41-        if [[ -n "$venv_path" ]]; then
 42-
 43-            stat --version &> /dev/null
 44-            if [[ $? -eq 0 ]]; then   # Linux, or GNU stat
 45-                file_owner="$(stat -c %u "$venv_path")"
 46-                file_permissions="$(stat -c %a "$venv_path")"
 47-            else                      # macOS, or FreeBSD stat
 48-                file_owner="$(stat -f %u "$venv_path")"
 49-                file_permissions="$(stat -f %OLp "$venv_path")"
 50-            fi
 51-
 52-            if [[ "$file_owner" != "$(id -u)" ]]; then
 53-                printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 54-                printf "Reason: Found a .venv file but it is not owned by the current user\n"
 55-                printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
 56-            elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
 57-                printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 58-                printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
 59-                printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
 60-            else
 61-                SWITCH_TO="$(<"$venv_path")"
 62-            fi
 63+    SWITCH_TO=""
 64+
 65+    # Get the .venv file, scanning parent directories
 66+    venv_path=$(_check_venv_path "$PWD")
 67+    if [[ -n "$venv_path" ]]; then
 68+
 69+        stat --version &> /dev/null
 70+        if [[ $? -eq 0 ]]; then   # Linux, or GNU stat
 71+            file_owner="$(stat -c %u "$venv_path")"
 72+            file_permissions="$(stat -c %a "$venv_path")"
 73+        else                      # macOS, or FreeBSD stat
 74+            file_owner="$(stat -f %u "$venv_path")"
 75+            file_permissions="$(stat -f %OLp "$venv_path")"
 76         fi
 77 
 78-        if [[ -n "$SWITCH_TO" ]]; then
 79-            _maybeworkon "$SWITCH_TO" "virtualenv"
 80-
 81-            # check if Pipfile exists rather than invoking pipenv as it is slow
 82-        elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
 83-            venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
 84-            _maybeworkon "$(basename "$venv_path")" "pipenv"
 85+        if [[ "$file_owner" != "$(id -u)" ]]; then
 86+            printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 87+            printf "Reason: Found a .venv file but it is not owned by the current user\n"
 88+            printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
 89+        elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
 90+            printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 91+            printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
 92+            printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
 93         else
 94-            _default_venv
 95+            SWITCH_TO="$(<"$venv_path")"
 96         fi
 97+    elif [[ -f "$PWD/requirements.txt" ]]; then
 98+        printf "Python project detected. "
 99+        printf "Run ${PURPLE}mkvenv${NORMAL} to setup autoswitching\n"
100+    fi
101+
102+    if [[ -n "$SWITCH_TO" ]]; then
103+        _maybeworkon "$SWITCH_TO" "virtualenv"
104+
105+        # check if Pipfile exists rather than invoking pipenv as it is slow
106+    elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
107+        venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
108+        _maybeworkon "$(basename "$venv_path")" "pipenv"
109+    else
110+        _default_venv
111     fi
112 }
113 
114diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
115index 532a7b45438a6bfe2a353c1543a1cca5db2e4ead..cab07d2dd4be4a56c416c2677685454327ea6eaf 100644
116--- a/tests/test_check_venv.zunit
117+++ b/tests/test_check_venv.zunit
118@@ -25,6 +25,16 @@
119     rm -rf Pipfile
120 }
121 
122+@test 'check_venv - Displays message on project detection' {
123+    PWD="$TARGET"
124+    touch "$TARGET/requirements.txt"
125+
126+    run check_venv
127+
128+    assert $status equals 0
129+    assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
130+}
131+
132 
133 @test 'check_venv - Security warning for weak writeable by group permissions' {
134     PWD="$TARGET"
135@@ -64,7 +74,6 @@
136     echo "foobar" > "$TARGET/.venv"
137     chmod 600 "$TARGET/.venv"
138     chmod a+r "$TARGET/.venv"
139-    MYOLDPWD="$(dirname $TARGET)"
140     AUTOSWITCH_DEFAULTENV="foodefault"
141 
142     run check_venv
143@@ -79,7 +88,6 @@
144     echo "foobar" > "$TARGET/.venv"
145     chmod 600 "$TARGET/.venv"
146     chmod g+r "$TARGET/.venv"
147-    MYOLDPWD="$(dirname $TARGET)"
148     AUTOSWITCH_DEFAULTENV="foodefault"
149 
150     run check_venv
151@@ -93,7 +101,6 @@
152 
153     echo "foobar" > "$TARGET/.venv"
154     chmod 600 "$TARGET/.venv"
155-    MYOLDPWD="$(dirname $TARGET)"
156     AUTOSWITCH_DEFAULTENV="foodefault"
157 
158     run check_venv
159@@ -103,23 +110,8 @@
160 }
161 
162 
163-@test 'check_venv - does not change if MYOLDPWD=PWD' {
164-    PWD="$TARGET"
165-    # Must be prefixed. See code for details
166-    MYOLDPWD="AS:$TARGET"
167-
168-    echo "foobar" > "$TARGET/.venv"
169-
170-    run check_venv
171-
172-    assert $status equals 0
173-    assert "$output" is_empty
174-}
175-
176-
177 @test 'check_venv - go to default if .venv unavailable' {
178     PWD="$TARGET"
179-    MYOLDPWD="$(dirname $TARGET)"
180     AUTOSWITCH_DEFAULTENV="foodefault"
181 
182     run check_venv
183@@ -131,7 +123,6 @@
184 
185 @test 'check_venv - activate if .venv unavailable but pipenv available' {
186     PWD="$TARGET"
187-    MYOLDPWD="$(dirname $TARGET)"
188     VIRTUAL_ENV="foo"
189     touch "Pipfile"
190 
191@@ -148,7 +139,6 @@
192 
193 @test 'check_venv - deactivate if neither .venv nor pipenv available' {
194     PWD="$TARGET"
195-    MYOLDPWD="$(dirname $TARGET)"
196     VIRTUAL_ENV="foo"
197     unset AUTOSWITCH_DEFAULTENV
198     function deactivate {
199@@ -164,7 +154,6 @@
200 
201 @test 'check_venv - works as intended with .venv available' {
202     PWD="$TARGET"
203-    MYOLDPWD="(dirname $TARGET)"
204 
205     echo "foobar" > "$TARGET/.venv"
206     chmod 600 "$TARGET/.venv"