Parent directory

test_check_venv.zunit

6035 bytes
  1#!/usr/bin/env zunit
  2
  3@setup {
  4
  5    if [[ ! -d "$HOME/.cache/pypoetry/virtualenvs/poetry-foo" ]]; then
  6        echo "Creating poetry test virtualenv"
  7        run virtualenv "$HOME/.cache/pypoetry/virtualenvs/poetry-foo" --python=python3
  8    fi
  9    if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
 10        echo "Creating test virtualenv"
 11        run virtualenv "$HOME/.virtualenvs/foobar" --python=python3
 12    fi
 13    if [[ ! -d "$HOME/.virtualenvs/foodefault/" ]]; then
 14        echo "Creating default test virtualenv"
 15        run virtualenv "$HOME/.virtualenvs/foodefault" --python=python3
 16    fi
 17
 18    unset AUTOSWITCH_MESSAGE_FORMAT
 19    PYTHON_VERSION="$(python3 --version 2>&1)"
 20
 21    export DISABLE_AUTOSWITCH_VENV="1"
 22    load "../autoswitch_virtualenv.plugin.zsh"
 23    TARGET="$(mktemp -d)"
 24    OLDPWD="$(mktemp -d)"
 25
 26    LANG=".UTF-8"
 27}
 28
 29
 30@teardown {
 31    rm -rf "$TARGET"
 32    rm -rf "$OLDPWD"
 33    rm -rf Pipfile
 34}
 35
 36@test 'check_venv - does not display a project detection message' {
 37    PWD="$TARGET"
 38    unset AUTOSWITCH_DEFAULTENV
 39    unset VIRTUAL_ENV
 40    touch "$TARGET/requirements.txt"
 41
 42    run check_venv
 43
 44    assert $status equals 0
 45    assert "$output" is_empty
 46}
 47
 48@test 'check_venv - Security warning for weak writeable by group permissions' {
 49    PWD="$TARGET"
 50
 51    echo "foobar" > "$TARGET/.venv"
 52    chmod 600 "$TARGET/.venv"
 53    chmod g+w "$TARGET/.venv"
 54
 55    run check_venv
 56
 57    assert $status equals 0
 58    assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
 59    assert "$lines[2]" is_empty
 60    assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (620)."
 61    assert "$lines[4]" same_as "Run the following command to fix this: \e[35m\"chmod 600 $TARGET/.venv\"\e[0m"
 62}
 63
 64@test 'check_venv - Security warning for weak writeable by everyone permissions' {
 65    PWD="$TARGET"
 66
 67    echo "foobar" > "$TARGET/.venv"
 68    chmod 600 "$TARGET/.venv"
 69    chmod a+w "$TARGET/.venv"
 70
 71    run check_venv
 72
 73    assert $status equals 0
 74    assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
 75    assert "$lines[2]" is_empty
 76    assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (622)."
 77    assert "$lines[4]" same_as "Run the following command to fix this: \e[35m\"chmod 600 $TARGET/.venv\"\e[0m"
 78}
 79
 80@test 'check_venv - No security warning for readable by everyone permission' {
 81    PWD="$TARGET"
 82
 83    echo "foobar" > "$TARGET/.venv"
 84    chmod 600 "$TARGET/.venv"
 85    chmod a+r "$TARGET/.venv"
 86    AUTOSWITCH_DEFAULTENV="foodefault"
 87
 88    run check_venv
 89
 90    assert $status equals 0
 91    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
 92}
 93
 94@test 'check_venv - No security warning for readable by group permission' {
 95    PWD="$TARGET"
 96
 97    echo "foobar" > "$TARGET/.venv"
 98    chmod 600 "$TARGET/.venv"
 99    chmod g+r "$TARGET/.venv"
100    AUTOSWITCH_DEFAULTENV="foodefault"
101
102    run check_venv
103
104    assert $status equals 0
105    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
106}
107
108@test 'check_venv - No security warning for readable only by owner permission' {
109    PWD="$TARGET"
110
111    echo "foobar" > "$TARGET/.venv"
112    chmod 600 "$TARGET/.venv"
113    AUTOSWITCH_DEFAULTENV="foodefault"
114
115    run check_venv
116
117    assert $status equals 0
118    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
119}
120
121
122@test 'check_venv - go to default if .venv unavailable' {
123    PWD="$TARGET"
124    AUTOSWITCH_DEFAULTENV="foodefault"
125
126    run check_venv
127
128    assert $status equals 0
129    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoodefault\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
130}
131
132@test 'check_venv - activate if .venv unavailable but poetry available' {
133    PWD="$TARGET/sub-directory"  # Should also work in subdirectories where poetry.lock is found
134    touch "$TARGET/poetry.lock"
135
136    # Mimic "poetry env list --full-path"
137    function poetry {
138        echo "$HOME/.cache/pypoetry/virtualenvs/some-other-env\n$HOME/.cache/pypoetry/virtualenvs/poetry-foo (Activated)"
139    }
140
141    run check_venv
142
143    assert $status equals 0
144    assert "$output" same_as "Switching to \e[1mpoetry\e[0m project: \e[1m\e[35mpoetry-foo\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
145}
146
147@test 'check_venv - activate if .venv unavailable but pipenv available' {
148    PWD="$TARGET/sub-directory"  # Should also work in subdirectories where Pipfile is found
149    touch "$TARGET/Pipfile"
150
151    function pipenv {
152        echo "$HOME/.virtualenvs/foobar"
153    }
154
155    run check_venv
156
157    assert $status equals 0
158    assert "$output" same_as "Switching to \e[1mpipenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
159}
160
161
162@test 'check_venv - deactivate if neither .venv nor pipenv available' {
163    PWD="$TARGET"
164    VIRTUAL_ENV="foo"
165    unset AUTOSWITCH_DEFAULTENV
166    function deactivate {
167    }
168
169    run check_venv
170
171    assert $status equals 0
172    assert "$output" same_as "Deactivating: \e[1m\e[35mfoo\e[0m"
173}
174
175@test 'check_venv - deactivate if neither .venv nor pipenv available (previous dir is pipenv)' {
176    PWD="$TARGET"
177    touch "$OLDPWD/Pipfile"
178
179    VIRTUAL_ENV="foo-bar-defa23"
180    unset AUTOSWITCH_DEFAULTENV
181    function deactivate {
182    }
183
184    run check_venv
185
186    assert $status equals 0
187    assert "$output" same_as "Deactivating: \e[1m\e[35mfoo-bar\e[0m"
188}
189
190@test 'check_venv - works as intended with .venv file available' {
191    PWD="$TARGET"
192
193    echo "foobar" > "$TARGET/.venv"
194    chmod 600 "$TARGET/.venv"
195
196    run check_venv
197
198    assert $status equals 0
199    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
200}
201
202@test 'check_venv - works as intended with .venv directory not being virtualenv' {
203    PWD="$TARGET"
204    mkdir "$TARGET/.venv"
205
206    run check_venv
207
208    assert $status equals 0
209    assert "$output" same_as ""
210}