Diff
1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
2index 51f5ee49599ccd73d1f1c9d1c95afb4831ec82e0..dbb9e389d1428adbd298f30b31ffb79086db5bf1 100644
3--- a/autoswitch_virtualenv.plugin.zsh
4+++ b/autoswitch_virtualenv.plugin.zsh
5@@ -79,51 +79,44 @@ function _check_venv_path()
6 # Automatically switch virtualenv when .venv file detected
7 function check_venv()
8 {
9- if [ "AS:$PWD" != "$MYOLDPWD" ]; then
10- # Prefix PWD with "AS:" to signify this belongs to this plugin
11- # this prevents the AUTONAMEDIRS in prezto from doing strange things
12- # See https://github.com/MichaelAquilina/zsh-autoswitch-virtualenv/issues/19
13- MYOLDPWD="AS:$PWD"
14-
15- SWITCH_TO=""
16-
17- # Get the .venv file, scanning parent directories
18- venv_path=$(_check_venv_path "$PWD")
19- if [[ -n "$venv_path" ]]; then
20-
21- stat --version &> /dev/null
22- if [[ $? -eq 0 ]]; then # Linux, or GNU stat
23- file_owner="$(stat -c %u "$venv_path")"
24- file_permissions="$(stat -c %a "$venv_path")"
25- else # macOS, or FreeBSD stat
26- file_owner="$(stat -f %u "$venv_path")"
27- file_permissions="$(stat -f %OLp "$venv_path")"
28- fi
29-
30- if [[ "$file_owner" != "$(id -u)" ]]; then
31- printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
32- printf "Reason: Found a .venv file but it is not owned by the current user\n"
33- printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
34- elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
35- printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
36- printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
37- printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
38- else
39- SWITCH_TO="$(<"$venv_path")"
40- fi
41+ SWITCH_TO=""
42+
43+ # Get the .venv file, scanning parent directories
44+ venv_path=$(_check_venv_path "$PWD")
45+ if [[ -n "$venv_path" ]]; then
46+
47+ stat --version &> /dev/null
48+ if [[ $? -eq 0 ]]; then # Linux, or GNU stat
49+ file_owner="$(stat -c %u "$venv_path")"
50+ file_permissions="$(stat -c %a "$venv_path")"
51+ else # macOS, or FreeBSD stat
52+ file_owner="$(stat -f %u "$venv_path")"
53+ file_permissions="$(stat -f %OLp "$venv_path")"
54 fi
55
56- if [[ -n "$SWITCH_TO" ]]; then
57- _maybeworkon "$SWITCH_TO" "virtualenv"
58-
59- # check if Pipfile exists rather than invoking pipenv as it is slow
60- elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
61- venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
62- _maybeworkon "$(basename "$venv_path")" "pipenv"
63+ if [[ "$file_owner" != "$(id -u)" ]]; then
64+ printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
65+ printf "Reason: Found a .venv file but it is not owned by the current user\n"
66+ printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
67+ elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
68+ printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
69+ printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
70+ printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
71 else
72- _default_venv
73+ SWITCH_TO="$(<"$venv_path")"
74 fi
75 fi
76+
77+ if [[ -n "$SWITCH_TO" ]]; then
78+ _maybeworkon "$SWITCH_TO" "virtualenv"
79+
80+ # check if Pipfile exists rather than invoking pipenv as it is slow
81+ elif [[ -a "Pipfile" ]] && type "pipenv" > /dev/null; then
82+ venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv)"
83+ _maybeworkon "$(basename "$venv_path")" "pipenv"
84+ else
85+ _default_venv
86+ fi
87 }
88
89 # Switch to the default virtual environment
90diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
91index 532a7b45438a6bfe2a353c1543a1cca5db2e4ead..79a858695882a6742fe28a3afecf1c41731ababa 100644
92--- a/tests/test_check_venv.zunit
93+++ b/tests/test_check_venv.zunit
94@@ -64,7 +64,6 @@
95 echo "foobar" > "$TARGET/.venv"
96 chmod 600 "$TARGET/.venv"
97 chmod a+r "$TARGET/.venv"
98- MYOLDPWD="$(dirname $TARGET)"
99 AUTOSWITCH_DEFAULTENV="foodefault"
100
101 run check_venv
102@@ -79,7 +78,6 @@
103 echo "foobar" > "$TARGET/.venv"
104 chmod 600 "$TARGET/.venv"
105 chmod g+r "$TARGET/.venv"
106- MYOLDPWD="$(dirname $TARGET)"
107 AUTOSWITCH_DEFAULTENV="foodefault"
108
109 run check_venv
110@@ -93,7 +91,6 @@
111
112 echo "foobar" > "$TARGET/.venv"
113 chmod 600 "$TARGET/.venv"
114- MYOLDPWD="$(dirname $TARGET)"
115 AUTOSWITCH_DEFAULTENV="foodefault"
116
117 run check_venv
118@@ -103,23 +100,8 @@
119 }
120
121
122-@test 'check_venv - does not change if MYOLDPWD=PWD' {
123- PWD="$TARGET"
124- # Must be prefixed. See code for details
125- MYOLDPWD="AS:$TARGET"
126-
127- echo "foobar" > "$TARGET/.venv"
128-
129- run check_venv
130-
131- assert $status equals 0
132- assert "$output" is_empty
133-}
134-
135-
136 @test 'check_venv - go to default if .venv unavailable' {
137 PWD="$TARGET"
138- MYOLDPWD="$(dirname $TARGET)"
139 AUTOSWITCH_DEFAULTENV="foodefault"
140
141 run check_venv
142@@ -131,7 +113,6 @@
143
144 @test 'check_venv - activate if .venv unavailable but pipenv available' {
145 PWD="$TARGET"
146- MYOLDPWD="$(dirname $TARGET)"
147 VIRTUAL_ENV="foo"
148 touch "Pipfile"
149
150@@ -148,7 +129,6 @@
151
152 @test 'check_venv - deactivate if neither .venv nor pipenv available' {
153 PWD="$TARGET"
154- MYOLDPWD="$(dirname $TARGET)"
155 VIRTUAL_ENV="foo"
156 unset AUTOSWITCH_DEFAULTENV
157 function deactivate {
158@@ -164,7 +144,6 @@
159
160 @test 'check_venv - works as intended with .venv available' {
161 PWD="$TARGET"
162- MYOLDPWD="(dirname $TARGET)"
163
164 echo "foobar" > "$TARGET/.venv"
165 chmod 600 "$TARGET/.venv"