Diff
1diff --git a/.travis.yml b/.travis.yml
2index 6e22f7b1b49679fc6c6dd1c4c7f434b07be5a600..dbad1915144709b9a5868032f2e608641e06457b 100644
3--- a/.travis.yml
4+++ b/.travis.yml
5@@ -2,12 +2,15 @@ dist: trusty
6 sudo: required
7
8 language: python
9+python:
10+ - "3.6"
11
12 env:
13- - ZSH_VERSION=5.3.1 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.3.1/zsh-5.3.1.tar.xz
14- - ZSH_VERSION=5.2 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.2/zsh-5.2.tar.xz
15- - ZSH_VERSION=5.1.1 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.1.1/zsh-5.1.1.tar.xz
16- - ZSH_VERSION=5.0.8 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.0.8/zsh-5.0.8.tar.gz
17+ - ZSH_VERSION=5.4.2 URL="https://downloads.sourceforge.net/project/zsh/zsh/5.4.2/zsh-5.4.2.tar.xz"
18+ - ZSH_VERSION=5.3.1 URL="https://downloads.sourceforge.net/project/zsh/zsh/5.3.1/zsh-5.3.1.tar.xz"
19+ - ZSH_VERSION=5.2 URL="https://downloads.sourceforge.net/project/zsh/zsh/5.2/zsh-5.2.tar.xz"
20+ - ZSH_VERSION=5.1.1 URL="https://downloads.sourceforge.net/project/zsh/zsh/5.1.1/zsh-5.1.1.tar.xz"
21+ - ZSH_VERSION=5.0.8 URL="https://downloads.sourceforge.net/project/zsh/zsh/5.0.8/zsh-5.0.8.tar.gz"
22
23 addons:
24 apt:
25@@ -17,22 +20,23 @@ addons:
26 before_install:
27 - export LOCAL="$(mktemp --directory --tmpdir=${TMPDIR:/tmp} local.bin.XXXXXX)"
28 - wget $URL
29- - tar -xf zsh-$ZSH_VERSION.tar.*
30- - cd zsh-$ZSH_VERSION
31- - ./configure --prefix=$LOCAL
32+ - tar -xf "zsh-$ZSH_VERSION.tar."*
33+ - cd "zsh-$ZSH_VERSION"
34+ - ./configure --prefix="$LOCAL"
35 - make
36 - make install
37 - cd -
38 - export PATH="$LOCAL/bin:$PATH"
39
40 install:
41- - pip install virtualenvwrapper
42+ - pip install virtualenvwrapper restructuredtext_lint
43
44 before_script:
45 - mkdir -p ~/bin
46- - curl -L https://raw.githubusercontent.com/molovo/revolver/master/revolver > ~/bin/revolver
47- - curl -L https://github.com/molovo/zunit/releases/download/v0.8.1/zunit > ~/bin/zunit
48+ - curl -L "https://raw.githubusercontent.com/molovo/revolver/master/revolver" > ~/bin/revolver
49+ - curl -L "https://github.com/molovo/zunit/releases/download/v0.8.1/zunit" > ~/bin/zunit
50 - chmod u+x ~/bin/{revolver,zunit}
51
52 script:
53+ - rst-lint *.rst
54 - zunit --verbose
55diff --git a/CHANGELOG.rst b/CHANGELOG.rst
56new file mode 100644
57index 0000000000000000000000000000000000000000..9b56462b4786b354ff59735a7fc9c048b685e2d4
58--- /dev/null
59+++ b/CHANGELOG.rst
60@@ -0,0 +1,11 @@
61+Changelog
62+=========
63+
64+0.2.0
65+-----
66+* Introduce Changelog
67+* Fix tests and CI process
68+* use printf instead of echo for better system portability
69+* Add ability to disable loading hooks and running initial check_venv using DISABLE_AUTOSWITCH_VENV
70+* Introduce restructuredtext linter to CI
71+* Test ZSH 5.4.2 in CI
72diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
73index 6482d4a96ea9a5a6d493ff5cf2af134627515ba7..a80a43c61a7a9572ba0d77dab088716112e94c05 100644
74--- a/autoswitch_virtualenv.plugin.zsh
75+++ b/autoswitch_virtualenv.plugin.zsh
76@@ -20,7 +20,7 @@ function _check_venv_path()
77 local check_dir=$1
78
79 if [[ -f "${check_dir}/.venv" ]]; then
80- echo "${check_dir}/.venv"
81+ printf "${check_dir}/.venv"
82 return
83 else
84 if [ "$check_dir" = "/" ]; then
85@@ -53,15 +53,13 @@ function check_venv()
86 fi
87
88 if [[ "$file_owner" != "$(id -u)" ]]; then
89- echo "AUTOSWITCH WARNING: Virtualenv will not be activated"
90- echo ""
91- echo "Reason: Found a .venv file but it is not owned by the current user"
92- echo "Change ownership of $venv_path to '$USER' to fix this"
93+ printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
94+ printf "Reason: Found a .venv file but it is not owned by the current user\n"
95+ printf "Change ownership of $venv_path to '$USER' to fix this\n"
96 elif [[ "$file_permissions" != "600" ]]; then
97- echo "AUTOSWITCH WARNING: Virtualenv will not be activated"
98- echo ""
99- echo "Reason: Found a .venv file with weak permission settings ($file_permissions)."
100- echo "Run the following command to fix this: \"chmod 600 $venv_path\""
101+ printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
102+ printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
103+ printf "Run the following command to fix this: \"chmod 600 $venv_path\"\n"
104 else
105 SWITCH_TO="$(<"$venv_path")"
106 fi
107@@ -98,7 +96,7 @@ function rmvenv()
108 rmvirtualenv "$venv_name"
109 rm ".venv"
110 else
111- echo "No .venv file in the current directory!"
112+ printf "No .venv file in the current directory!\n"
113 fi
114 }
115
116@@ -107,7 +105,7 @@ function rmvenv()
117 function mkvenv()
118 {
119 if [[ -f ".venv" ]]; then
120- echo ".venv file already exists. If this is a mistake use the rmvenv command"
121+ printf ".venv file already exists. If this is a mistake use the rmvenv command\n"
122 else
123 venv_name="$(basename $PWD)"
124 mkvirtualenv "$venv_name" $@
125@@ -122,15 +120,16 @@ function mkvenv()
126 pip install -r "$requirements"
127 fi
128 done
129- echo "$venv_name" > ".venv"
130+ printf "$venv_name\n" > ".venv"
131 chmod 600 .venv
132 AUTOSWITCH_PROJECT="$PWD"
133 fi
134 }
135
136-autoload -Uz add-zsh-hook
137-add-zsh-hook -D chpwd check_venv
138-add-zsh-hook chpwd check_venv
139+if [[ -z "$DISABLE_AUTOSWITCH_VENV" ]]; then
140+ autoload -Uz add-zsh-hook
141+ add-zsh-hook -D chpwd check_venv
142+ add-zsh-hook chpwd check_venv
143
144-# auto-detect virtualenv on zsh startup
145-[[ -o interactive ]] && check_venv
146+ check_venv
147+fi
148diff --git a/tests/test_autoswitching.zunit b/tests/test_autoswitching.zunit
149deleted file mode 100644
150index fb8719f4a89b8d12a1debff0669079f079ed23bb..0000000000000000000000000000000000000000
151--- a/tests/test_autoswitching.zunit
152+++ /dev/null
153@@ -1,35 +0,0 @@
154-#!/usr/bin/env zunit
155-
156-@setup {
157- source =virtualenvwrapper.sh
158- load "../autoswitch_virtualenv.plugin.zsh"
159- target="$(mktemp -d)"
160- assert "$target" is_dir
161-
162- venv="$(pwgen 8 1)"
163- run mkvirtualenv "$venv"
164-}
165-
166-@teardown {
167- run rmvirtualenv "$venv"
168- rm -rf "$target"
169-}
170-
171-@test 'changing to a directory without .venv defaults' {
172- AUTOSWITCH_DEFAULTENV="mydefaultenv"
173-
174- run cd "$target"
175-
176- assert "$output" contains "Switching virtualenv: mydefaultenv"
177- assert $state equals 0
178-}
179-
180-@test 'changing to a directory with .venv autoswitches' {
181- echo "$venv" > "$target/.venv"
182- chmod 600 "$target/.venv"
183-
184- run cd "$target"
185-
186- assert "$output" contains "Switching virtualenv: $venv"
187- assert $state equals 0
188-}
189diff --git a/tests/test_check_venv_path.zunit b/tests/test_check_venv_path.zunit
190index 1665d3850ae8c581cddd588fa0c7b9b8eff6002a..c844f40caa06de10a203ab1ff7c053bc0be6cf17 100644
191--- a/tests/test_check_venv_path.zunit
192+++ b/tests/test_check_venv_path.zunit
193@@ -1,50 +1,45 @@
194 #!/usr/bin/env zunit
195
196+
197 @setup {
198+ export DISABLE_AUTOSWITCH_VENV="1"
199 load "../autoswitch_virtualenv.plugin.zsh"
200- source =virtualenvwrapper.sh
201- target="$(mktemp -d)"
202- assert "$target" is_dir
203+ TARGET="$(mktemp -d)"
204 }
205
206 @teardown {
207- rm -rf "$target"
208+ rm -rf "$TARGET"
209 }
210
211-@test 'chpwd functions are loaded' {
212- assert 'check_venv' in $chpwd_functions
213-}
214+@test '_check_venv_path - test finds in base directory' {
215+ touch "$TARGET/.venv"
216
217-@test '_check_venv_path with non-existent path' {
218- run _check_venv_path '/i/dont/exist'
219+ run _check_venv_path "$TARGET"
220
221- assert "$output" is_empty
222 assert $state equals 0
223+ assert "$output" same_as "$TARGET/.venv"
224 }
225
226-@test '_check_venv_path with no venv' {
227- run _check_venv_path "$target"
228+@test '_check_venv_path - returns nothing if not found' {
229+ run _check_venv_path "$TARGET"
230
231- assert "$output" is_empty
232- assert $state equals 0
233+ assert $state equals 0
234+ assert "$output" is_empty
235 }
236
237-@test '_check_venv_path with existing venv' {
238- touch "$target/.venv"
239- assert "$target/.venv" is_file
240+@test '_check_venv_path - finds .venv in parent directories' {
241+ touch "$TARGET/.venv"
242+ mkdir "$TARGET/foo"
243
244- run _check_venv_path "$target"
245+ run _check_venv_path "$TARGET/foo"
246
247- assert "$output" same_as "$target/.venv"
248 assert $state equals 0
249+ assert "$output" same_as "$TARGET/.venv"
250 }
251
252-@test '_check_venv_path in subdirectory with parent venv' {
253- touch "$target/.venv"
254- assert "$target/.venv" is_file
255-
256- run _check_venv_path "$target/src/examples"
257+@test '_check_venv_path - returns nothing with root path' {
258+ run _check_venv_path '/'
259
260- assert "$output" same_as "$target/.venv"
261 assert $state equals 0
262+ assert "$output" is_empty
263 }
264diff --git a/tests/test_maybeworkon.zunit b/tests/test_maybeworkon.zunit
265new file mode 100644
266index 0000000000000000000000000000000000000000..bac535b930523447b6b8d60430a46a94f1f678c9
267--- /dev/null
268+++ b/tests/test_maybeworkon.zunit
269@@ -0,0 +1,59 @@
270+#!/usr/bin/env zunit
271+
272+
273+@setup {
274+ source =virtualenvwrapper.sh
275+
276+ if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
277+ echo "Creating test virtualenv"
278+ run mkvirtualenv foobar --python=python3
279+ fi
280+
281+ PYTHON_VERSION="$(python3 --version 2>&1)"
282+
283+ export DISABLE_AUTOSWITCH_VENV="1"
284+ load "../autoswitch_virtualenv.plugin.zsh"
285+ TARGET="$(mktemp -d)"
286+}
287+
288+@teardown {
289+ rm -rf "$TARGET"
290+}
291+
292+
293+@test '_maybeworkon - switches virtualenv if nothing is activated' {
294+ VIRTUAL_ENV=""
295+
296+ run _maybeworkon foobar
297+
298+ assert $state equals 0
299+ assert "$output" same_as "Switching virtualenv: foobar [$PYTHON_VERSION]"
300+}
301+
302+@test '_maybeworkon - switches virtualenv if current virtualenv is different' {
303+ VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
304+
305+ run _maybeworkon foobar
306+
307+ assert $state equals 0
308+ assert "$output" contains "Switching virtualenv: foobar [$PYTHON_VERSION]"
309+}
310+
311+@test '_maybeworkon - switches virtualenv if current virtualenv is different (silent)' {
312+ VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
313+ AUTOSWITCH_SILENT="1"
314+
315+ run _maybeworkon foobar
316+
317+ assert $state equals 0
318+ assert "$output" is_empty
319+}
320+
321+@test '_maybeworkon - does not switch to already activated virtualenv' {
322+ VIRTUAL_ENV="$HOME/.virtualenvs/bwaaah"
323+
324+ run _maybeworkon bwaaah
325+
326+ assert $state equals 0
327+ assert "$output" is_empty
328+}
329diff --git a/tests/test_rmvenv.zunit b/tests/test_rmvenv.zunit
330index b7fd700fe44e22ce90a7082908340f768e54cecf..3be698bb120cbba4a129c6627c6ab8c726eb0208 100644
331--- a/tests/test_rmvenv.zunit
332+++ b/tests/test_rmvenv.zunit
333@@ -1,48 +1,49 @@
334 #!/usr/bin/env zunit
335
336 @setup {
337- load "../autoswitch_virtualenv.plugin.zsh"
338 source =virtualenvwrapper.sh
339- target="$(mktemp -d)"
340- assert "$target" is_dir
341+
342+ export DISABLE_AUTOSWITCH_VENV="1"
343+ load "../autoswitch_virtualenv.plugin.zsh"
344+ TARGET="$(mktemp -d)"
345+ cd "$TARGET"
346 }
347
348 @teardown {
349- rm -rf "$teardown"
350+ rm -rf "$TARGET"
351 }
352
353-@test 'rmvenv with no .venv' {
354- cd "$target"
355
356+@test 'rmvenv - shows warning if no .venv present' {
357 run rmvenv
358
359- assert "$output" same_as "No .venv file in the current directory!"
360 assert $state equals 0
361+ assert "$output" same_as "No .venv file in the current directory!"
362 }
363
364-@test 'rmvenv with existing .venv' {
365- run mkvirtualenv "oranges"
366- run deactivate
367
368- cd "$target"
369- echo "oranges" > "$target/.venv"
370- chmod 600 "$target/.venv"
371- assert "$target/.venv" is_file
372+@test 'rmvenv - removes .venv if present' {
373+ # lightweight simulation of creating a virtualenv
374+ mkdir -p "$HOME/.virtualenvs/12345"
375+
376+ echo "12345" > .venv
377
378 run rmvenv
379
380- assert "$output" same_as "Removing oranges..."
381 assert $state equals 0
382+ assert "$output" same_as "Removing 12345..."
383+ test ! -f .venv
384+ test ! -d "$HOME/.virtualenvs/12345"
385 }
386
387-@test 'rmvenv with existing .venv with deleted virtualenv' {
388- echo "idontexist" > "$target/.venv"
389- chmod 600 "$target/.venv"
390- assert "$target/.venv" is_file
391+@test 'rmvenv - removes .venv and deactivates if currently active' {
392+ test_venv="$(basename $PWD)"
393+ run mkvenv
394
395- cd "$target"
396 run rmvenv
397
398- assert "$lines[1]" same_as "Removing idontexist..."
399- assert "$lines[2]" same_as "Did not find environment $HOME/.virtualenvs/idontexist to remove."
400+ assert $state equals 0
401+ assert "$output" same_as "Removing $test_venv..."
402+ test ! -f .venv
403+ test ! -d "$HOME/.virtualenvs/$test_venv"
404 }