5b7e8328f93a5902705c6885c7d5e5e5de6443a1
- Author
- Michael Aquilina <michaelaquilina@gmail.com>
- Committer
- GitHub <noreply@github.com>
- Date
Message
Merge pull request #30 from MichaelAquilina/more_tests
Add tests for check_venv
Diff
1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
2index 9b56462b4786b354ff59735a7fc9c048b685e2d4..90145377bb8914f8aafbea2876ac0b30311fc109 100644
3--- a/CHANGELOG.rst
4+++ b/CHANGELOG.rst
5@@ -1,6 +1,11 @@
6 Changelog
7 =========
8
9+0.2.1
10+-----
11+* Add tests for mkvenv and check_venv
12+
13+
14 0.2.0
15 -----
16 * Introduce Changelog
17diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
18new file mode 100644
19index 0000000000000000000000000000000000000000..6d434f6332598fabe06721a24b64281b9687cb92
20--- /dev/null
21+++ b/tests/test_check_venv.zunit
22@@ -0,0 +1,97 @@
23+#!/usr/bin/env zunit
24+
25+@setup {
26+ source =virtualenvwrapper.sh
27+
28+ if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
29+ echo "Creating test virtualenv"
30+ run mkvirtualenv foobar --python=python3
31+ fi
32+ if [[ ! -d "$HOME/.virtualenvs/foodefault/" ]]; then
33+ echo "Creating default test virtualenv"
34+ run mkvirtualenv foodefault --python=python3
35+ fi
36+
37+ PYTHON_VERSION="$(python3 --version 2>&1)"
38+
39+ export DISABLE_AUTOSWITCH_VENV="1"
40+ load "../autoswitch_virtualenv.plugin.zsh"
41+ TARGET="$(mktemp -d)"
42+}
43+
44+
45+@teardown {
46+ rm -rf "$TARGET"
47+}
48+
49+
50+@test 'check_venv - Security warning for weak permissions' {
51+ PWD="$TARGET"
52+
53+ echo "foobar" > "$TARGET/.venv"
54+ chmod 600 "$TARGET/.venv"
55+ chmod g+w "$TARGET/.venv"
56+
57+ run check_venv
58+
59+ assert $status equals 0
60+ assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
61+ assert "$lines[2]" is_empty
62+ assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (620)."
63+ assert "$lines[4]" same_as "Run the following command to fix this: \"chmod 600 $TARGET/.venv\""
64+}
65+
66+
67+@test 'check_venv - does not change if MYOLDPWD=PWD' {
68+ PWD="$TARGET"
69+ MYOLDPWD="$TARGET"
70+
71+ echo "foobar" > "$TARGET/.venv"
72+
73+ run check_venv
74+
75+ assert $status equals 0
76+ assert "$output" is_empty
77+}
78+
79+
80+@test 'check_venv - go to default if .venv available' {
81+ PWD="$TARGET"
82+ MYOLDPWD="$(dirname $TARGET)"
83+ AUTOSWITCH_DEFAULTENV="foodefault"
84+
85+ run check_venv
86+
87+ assert $status equals 0
88+ assert "$output" same_as "Switching virtualenv: foodefault [$PYTHON_VERSION]"
89+}
90+
91+
92+@test 'check_venv - deactivate if no .venv available' {
93+ PWD="$TARGET"
94+ MYOLDPWD="$(dirname $TARGET)"
95+ unset AUTOSWITCH_DEFAULTENV
96+ echo world
97+ function deactivate {
98+ echo deactivating
99+ }
100+
101+ run check_venv
102+
103+ assert $status equals 0
104+ assert "$output" same_as "deactivating"
105+}
106+
107+
108+@test 'check_venv - works as intended with .venv available' {
109+ PWD="$TARGET"
110+ MYOLDPWD="(dirname $TARGET)"
111+
112+ echo "foobar" > "$TARGET/.venv"
113+ chmod 600 "$TARGET/.venv"
114+
115+ run check_venv
116+
117+ assert $status equals 0
118+ assert "$output" same_as "Switching virtualenv: foobar [$PYTHON_VERSION]"
119+}
120diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
121new file mode 100644
122index 0000000000000000000000000000000000000000..3e3beb25e2d73d0f6694a37a26f32140fbb1c43e
123--- /dev/null
124+++ b/tests/test_mkvenv.zunit
125@@ -0,0 +1,40 @@
126+#!/usr/bin/env zunit
127+
128+@setup {
129+ source =virtualenvwrapper.sh
130+
131+ export DISABLE_AUTOSWITCH_VENV="1"
132+ load "../autoswitch_virtualenv.plugin.zsh"
133+ TARGET="$(mktemp -d)"
134+ cd "$TARGET"
135+}
136+
137+@teardown {
138+ rm -rf "$TARGET"
139+}
140+
141+@test 'mkvenv - does not create .venv if one exists' {
142+ touch .venv
143+
144+ run mkvenv
145+
146+ assert $status equals 0
147+ assert "$output" same_as ".venv file already exists. If this is a mistake use the rmvenv command"
148+}
149+
150+
151+@test 'mkvenv - creates .venv' {
152+ mkdir myproject
153+ cd myproject
154+
155+ run mkvenv
156+
157+ assert $status equals 0
158+ assert "$output" contains "New python executable"
159+ assert "$TARGET/myproject/.venv" exists
160+
161+ run cat "$TARGET/myproject/.venv"
162+
163+ assert $status equals 0
164+ assert "$output" same_as "myproject"
165+}