9e5756dedd3b5ec685512133c50b3bafee3a3b63
- Author
- Michael Aquilina <michaelaquilina@gmail.com>
- Committer
- Michael Aquilina <michaelaquilina@gmail.com>
- Date
Message
Add tests for check_venv
Diff
1diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
2new file mode 100644
3index 0000000000000000000000000000000000000000..6d434f6332598fabe06721a24b64281b9687cb92
4--- /dev/null
5+++ b/tests/test_check_venv.zunit
6@@ -0,0 +1,97 @@
7+#!/usr/bin/env zunit
8+
9+@setup {
10+ source =virtualenvwrapper.sh
11+
12+ if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
13+ echo "Creating test virtualenv"
14+ run mkvirtualenv foobar --python=python3
15+ fi
16+ if [[ ! -d "$HOME/.virtualenvs/foodefault/" ]]; then
17+ echo "Creating default test virtualenv"
18+ run mkvirtualenv foodefault --python=python3
19+ fi
20+
21+ PYTHON_VERSION="$(python3 --version 2>&1)"
22+
23+ export DISABLE_AUTOSWITCH_VENV="1"
24+ load "../autoswitch_virtualenv.plugin.zsh"
25+ TARGET="$(mktemp -d)"
26+}
27+
28+
29+@teardown {
30+ rm -rf "$TARGET"
31+}
32+
33+
34+@test 'check_venv - Security warning for weak permissions' {
35+ PWD="$TARGET"
36+
37+ echo "foobar" > "$TARGET/.venv"
38+ chmod 600 "$TARGET/.venv"
39+ chmod g+w "$TARGET/.venv"
40+
41+ run check_venv
42+
43+ assert $status equals 0
44+ assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
45+ assert "$lines[2]" is_empty
46+ assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (620)."
47+ assert "$lines[4]" same_as "Run the following command to fix this: \"chmod 600 $TARGET/.venv\""
48+}
49+
50+
51+@test 'check_venv - does not change if MYOLDPWD=PWD' {
52+ PWD="$TARGET"
53+ MYOLDPWD="$TARGET"
54+
55+ echo "foobar" > "$TARGET/.venv"
56+
57+ run check_venv
58+
59+ assert $status equals 0
60+ assert "$output" is_empty
61+}
62+
63+
64+@test 'check_venv - go to default if .venv available' {
65+ PWD="$TARGET"
66+ MYOLDPWD="$(dirname $TARGET)"
67+ AUTOSWITCH_DEFAULTENV="foodefault"
68+
69+ run check_venv
70+
71+ assert $status equals 0
72+ assert "$output" same_as "Switching virtualenv: foodefault [$PYTHON_VERSION]"
73+}
74+
75+
76+@test 'check_venv - deactivate if no .venv available' {
77+ PWD="$TARGET"
78+ MYOLDPWD="$(dirname $TARGET)"
79+ unset AUTOSWITCH_DEFAULTENV
80+ echo world
81+ function deactivate {
82+ echo deactivating
83+ }
84+
85+ run check_venv
86+
87+ assert $status equals 0
88+ assert "$output" same_as "deactivating"
89+}
90+
91+
92+@test 'check_venv - works as intended with .venv available' {
93+ PWD="$TARGET"
94+ MYOLDPWD="(dirname $TARGET)"
95+
96+ echo "foobar" > "$TARGET/.venv"
97+ chmod 600 "$TARGET/.venv"
98+
99+ run check_venv
100+
101+ assert $status equals 0
102+ assert "$output" same_as "Switching virtualenv: foobar [$PYTHON_VERSION]"
103+}