2d6243c576a70defe7b7702ed8c28dadf4ef9bcf

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

Merge pull request #18 from MichaelAquilina/development

Add unit test support

Diff

  1diff --git a/.travis.yml b/.travis.yml
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..20862b42b42daa5188c789e0a69180cb5a206571
  4--- /dev/null
  5+++ b/.travis.yml
  6@@ -0,0 +1,38 @@
  7+dist: trusty
  8+sudo: required
  9+
 10+language: python
 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+
 18+addons:
 19+  apt:
 20+    packages:
 21+    - build-essential
 22+
 23+before_install:
 24+    - export LOCAL="$(mktemp --directory --tmpdir=${TMPDIR:/tmp} local.bin.XXXXXX)"
 25+    - wget $URL
 26+    - tar -xf zsh-$ZSH_VERSION.tar.*
 27+    - cd zsh-$ZSH_VERSION
 28+    - ./configure --prefix=$LOCAL
 29+    - make
 30+    - make install
 31+    - cd -
 32+    - export PATH="$LOCAL/bin:$PATH"
 33+
 34+install:
 35+    - pip install virtualenvwrapper
 36+
 37+before_script:
 38+    - mkdir ~/bin
 39+    - curl -L https://raw.githubusercontent.com/molovo/revolver/master/revolver > ~/bin/revolver
 40+    - curl -L https://github.com/molovo/zunit/releases/download/v0.8.1/zunit > ~/bin/zunit
 41+    - chmod u+x ~/bin/{revolver,zunit}
 42+
 43+script:
 44+    - zunit --verbose
 45diff --git a/README.md b/README.md
 46index f33d22a5472c36198dfb3312146d363e8ca4858b..814c59a12efbc55e10672bc2aabbf54de0afc697 100644
 47--- a/README.md
 48+++ b/README.md
 49@@ -1,6 +1,8 @@
 50 Autoswitch Python Virtualenv
 51 ============================
 52 
 53+![Travis CI](https://travis-ci.org/MichaelAquilina/zsh-autoswitch-virtualenv#)
 54+
 55 *zsh-autoswitch-virtualenv* is a simple ZSH plugin that switches python virtualenvs automatically as
 56 you move between directories.
 57 
 58@@ -32,31 +34,16 @@ the Version Control you are using).
 59 Requirements
 60 ------------
 61 
 62-`virtualenvwrapper` must be installed for this plugin to work correctly. You can install it as
 63-follows based on what operating system you are using:
 64-
 65-#### Ubuntu
 66-```
 67-sudo apt-get install virtualenvwrapper
 68-```
 69-
 70-#### OpenSUSE
 71-```
 72-sudo zypper install python-virtualenvwrapper
 73-```
 74+`virtualenvwrapper` must be installed for this plugin to work correctly. You should install it with pip:
 75 
 76-#### Mac OSX
 77 ```
 78-brew install pyenv-virtualenvwrapper
 79+pip install virtualenvwrapper
 80 ```
 81 
 82-You need to source this file in your `.zshrc` file as part of your setup. In Ubuntu for example:
 83+You need to source this file in your `.zshrc` file as part of your setup. This should work:
 84 
 85 ```
 86-VIRTUALENVWRAPPER="/usr/share/virtualenvwrapper/virtualenvwrapper.sh"
 87-if [[ -f "$VIRTUALENVWRAPPER" ]]; then
 88-  source "$VIRTUALENVWRAPPER"
 89-fi
 90+source =virtualenvwrapper.sh
 91 ```
 92 
 93 Installing with ZSH Package Managers
 94diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 95index b0ea24a400c6ddf4febd03fdd4fa0f09988aca68..50ce0ece9b6ccf9cb94be5b8fba82720e6e4ea79 100644
 96--- a/autoswitch_virtualenv.plugin.zsh
 97+++ b/autoswitch_virtualenv.plugin.zsh
 98@@ -130,4 +130,6 @@ function mkvenv()
 99   fi
100 }
101 
102+autoload -Uz add-zsh-hook
103+add-zsh-hook -D precmd _check_venv
104 add-zsh-hook precmd _check_venv
105diff --git a/tests/test_check_venv_path.zunit b/tests/test_check_venv_path.zunit
106new file mode 100644
107index 0000000000000000000000000000000000000000..e8bf5e98bd8608d8f76602120df143a8b8067436
108--- /dev/null
109+++ b/tests/test_check_venv_path.zunit
110@@ -0,0 +1,49 @@
111+#!/usr/bin/env zunit
112+
113+@setup {
114+    load "../autoswitch_virtualenv.plugin.zsh"
115+    target="$(mktemp -d)"
116+    assert "$target" is_dir
117+}
118+
119+@teardown {
120+    rm -rf "$target"
121+}
122+
123+@test 'precmd functions are loaded' {
124+    assert '_check_venv' in $precmd_functions
125+}
126+
127+@test '_check_venv_path with non-existent path' {
128+    run _check_venv_path '/i/dont/exist'
129+
130+    assert "$output" is_empty
131+    assert $state equals 0
132+}
133+
134+@test '_check_venv_path with no venv' {
135+    run _check_venv_path "$target"
136+
137+    assert "$output" is_empty
138+    assert $state equals 0
139+}
140+
141+@test '_check_venv_path with existing venv' {
142+    touch "$target/.venv"
143+    assert "$target/.venv" is_file
144+
145+    run _check_venv_path "$target"
146+
147+    assert "$output" same_as "$target/.venv"
148+    assert $state equals 0
149+}
150+
151+@test '_check_venv_path in subdirectory with parent venv' {
152+    touch "$target/.venv"
153+    assert "$target/.venv" is_file
154+
155+    run _check_venv_path "$target/src/examples"
156+
157+    assert "$output" same_as "$target/.venv"
158+    assert $state equals 0
159+}
160diff --git a/tests/test_rmvenv.zunit b/tests/test_rmvenv.zunit
161new file mode 100644
162index 0000000000000000000000000000000000000000..c9566fe361ba83cc7c101b63577ead91ceb7148f
163--- /dev/null
164+++ b/tests/test_rmvenv.zunit
165@@ -0,0 +1,46 @@
166+#!/usr/bin/env zunit
167+
168+@setup {
169+    load "../autoswitch_virtualenv.plugin.zsh"
170+    source =virtualenvwrapper.sh
171+    target="$(mktemp -d)"
172+    assert "$target" is_dir
173+}
174+
175+@teardown {
176+    rm -rf "$teardown"
177+}
178+
179+@test 'rmvenv with no .venv' {
180+    cd "$target"
181+
182+    run rmvenv
183+
184+    assert "$output" same_as "No .venv file in the current directory!"
185+    assert $state equals 0
186+}
187+
188+@test 'rmvenv with existing .venv' {
189+    run mkvirtualenv "oranges"
190+    run deactivate
191+
192+    echo "oranges" > "$target/.venv"
193+    assert "$target/.venv" is_file
194+
195+    cd "$target"
196+    run rmvenv
197+
198+    assert "$output" same_as "Removing oranges..."
199+    assert $state equals 0
200+}
201+
202+@test 'rmvenv with existing .venv with deleted virtualenv' {
203+    echo "idontexist" > "$target/.venv"
204+    assert "$target/.venv" is_file
205+
206+    cd "$target"
207+    run rmvenv
208+
209+    assert "$lines[1]" same_as "Removing idontexist..."
210+    assert "$lines[2]" same_as "Did not find environment $HOME/.virtualenvs/idontexist to remove."
211+}