1# -*- mode: shell-script -*-
2
3test_dir=$(cd $(dirname $0) && pwd)
4source "$test_dir/setup.sh"
5
6setUp () {
7    rm -rf "$WORKON_HOME"
8    mkdir -p "$WORKON_HOME"
9    source "$test_dir/../virtualenvwrapper.sh"
10    echo
11}
12
13tearDown () {
14    if type deactivate >/dev/null 2>&1
15    then
16        deactivate
17    fi
18    rm -rf "$WORKON_HOME"
19}
20
21test_no_arguments () {
22    assertSame "Please provide a valid virtualenv to copy." "$(cpvirtualenv)"
23}
24
25test_venv_already_exists_in_workon () {
26    mkvirtualenv "cpvenv_test" >/dev/null 2>&1
27    assertSame  "cpvenv_test virtualenv already exists." "$(cpvirtualenv 'cpvenv_test')"
28}
29
30test_bad_path () {
31    assertSame "Please provide a valid virtualenv to copy." "$(cpvirtualenv '~/cpvenv_test')"
32}
33
34test_copy_venv () {
35    # verify venvs don't exist
36    assertTrue "Virtualenv to copy already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
37    assertTrue "Copied virtualenv already exists" "[ ! -d $WORKON_HOME/copied_venv ]"
38    mkvirtualenv "cpvenv_test" >/dev/null 2>&1
39    touch "$WORKON_HOME/cpvenv_test/mytestpackage"
40
41    assertTrue "Virtualenv to copy didn't get created" "[ -d $WORKON_HOME/cpvenv_test ]"
42    assertTrue "Copied virtualenv already exists" "[ ! -d $WORKON_HOME/copied_venv ]"
43
44    cpvirtualenv "cpvenv_test" "copied_venv" >/dev/null 2>&1
45
46    # verify copied venv exist
47    assertTrue "Copied virtualenv doesn't exist" "[ -d $WORKON_HOME/copied_venv ]"
48    # verify test package exists
49    assertTrue "Test package is missing" "[ -f $WORKON_HOME/copied_venv/mytestpackage ]"
50}
51
52test_copy_venv_activate () {
53    # verify venvs don't exist
54    assertTrue "Virtualenv to copy already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
55    assertTrue "Copied virtualenv already exists" "[ ! -d $WORKON_HOME/copied_venv ]"
56    mkvirtualenv "cpvenv_test" >/dev/null 2>&1
57
58    assertTrue "Virtualenv to copy didn't get created" "[ -d $WORKON_HOME/cpvenv_test ]"
59    assertTrue "Copied virtualenv already exists" "[ ! -d $WORKON_HOME/copied_venv ]"
60
61    cpvirtualenv "cpvenv_test" "copied_venv" >/dev/null 2>&1
62
63    # verify copied venv exist
64    assertTrue "Copied virtualenv doesn't exist" "[ -d $WORKON_HOME/copied_venv ]"
65
66    assertSame "copied_venv" "$(basename $VIRTUAL_ENV)"
67    assertTrue "Virtualenv not active" virtualenvwrapper_verify_active_environment
68}
69
70test_copy_venv_is_listed () {
71    # verify venvs don't exist
72    assertTrue "Virtualenv to copy already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
73    assertTrue "Copied virtualenv already exists" "[ ! -d $WORKON_HOME/copied_venv ]"
74    mkvirtualenv "cpvenv_test" >/dev/null 2>&1
75
76    typeset listed=$(lsvirtualenv)
77    [[ "$listed" != *copied_venv* ]]
78    RC=$?
79    assertTrue "Copied virtualenv found in virtualenv list" $RC
80
81    cpvirtualenv "cpvenv_test" "copied_venv" >/dev/null 2>&1
82
83    listed=$(lsvirtualenv -b)
84    [[ "$listed" == *copied_venv* ]]
85    RC=$?
86    assertTrue "Copied virtualenv not found in virtualenv list" $RC
87}
88
89test_clone_venv () {
90    typeset tmplocation=$(dirname $WORKON_HOME)
91
92    # verify venvs don't exist
93    assertTrue "Virtualenv to clone already exists" "[ ! -d $tmplocation/cpvenv_test ]"
94    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cloned_venv ]"
95
96    $VIRTUALENVWRAPPER_VIRTUALENV "$tmplocation/cpvenv_test" >/dev/null 2>&1
97    touch "$tmplocation/cpvenv_test/mytestpackage"
98
99    assertTrue "Virtualenv to clone didn't get created" "[ -d $tmplocation/cpvenv_test ]"
100    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cloned_venv ]"
101
102    cpvirtualenv "$tmplocation/cpvenv_test" "cloned_venv" >/dev/null 2>&1
103
104    # verify cloned venv exist
105    assertTrue "Cloned virtualenv doesn't exist" "[ -d $WORKON_HOME/cloned_venv ]"
106    # verify test package exists
107    assertTrue "Test package is missing" "[ -f $WORKON_HOME/cloned_venv/mytestpackage ]"
108
109    rm -rf "$tmplocation/cpvenv_test"
110}
111
112test_clone_venv_activate () {
113    typeset tmplocation=$(dirname $WORKON_HOME)
114
115    # verify venvs don't exist
116    assertTrue "Virtualenv to clone already exists" "[ ! -d $tmplocation/cpvenv_test ]"
117    assertTrue "Virtualenv with same name as clone source already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
118    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cloned_venv ]"
119
120    $VIRTUALENVWRAPPER_VIRTUALENV "$tmplocation/cpvenv_test" >/dev/null 2>&1
121
122    assertTrue "Virtualenv to clone didn't get created" "[ -d $tmplocation/cpvenv_test ]"
123    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cloned_venv ]"
124
125    #cpvirtualenv "$tmplocation/cpvenv_test" "cloned_venv" >/dev/null 2>&1
126    cpvirtualenv "$tmplocation/cpvenv_test" "cloned_venv" >/dev/null 2/>&1
127
128    # verify cloned venv exist
129    assertTrue "Cloned virtualenv doesn't exist" "[ -d $WORKON_HOME/cloned_venv ]"
130
131    assertSame "cloned_venv" "$(basename $VIRTUAL_ENV)"
132    assertTrue "Virtualenv not active" virtualenvwrapper_verify_active_environment
133
134    rm -rf "$tmplocation/cpvenv_test"
135}
136
137test_clone_venv_is_listed (){
138    typeset tmplocation=$(dirname $WORKON_HOME)
139
140    # verify venvs don't exist
141    assertTrue "Virtualenv to clone already exists" "[ ! -d $tmplocation/cpvenv_test ]"
142    assertTrue "Virtualenv with same name as clone source already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
143    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cloned_venv ]"
144
145    typeset listed=$(lsvirtualenv)
146    [[ "$listed" != *cloned_venv* ]]
147    RC=$?
148    assertTrue "Cloned virtualenv found in virtualenv list" $RC
149
150    $VIRTUALENVWRAPPER_VIRTUALENV "$tmplocation/cpvenv_test" >/dev/null 2>&1
151    cpvirtualenv "$tmplocation/cpvenv_test" "cloned_venv" >/dev/null 2>&1
152
153    listed=$(lsvirtualenv -b)
154    [[ "$listed" == *cloned_venv* ]]
155    RC=$?
156    assertTrue "Cloned virtualenv not found in virtualenv list" $RC
157
158    rm -rf "$tmplocation/cpvenv_test"
159
160}
161
162test_clone_venv_using_same_name () {
163    typeset tmplocation=$(dirname $WORKON_HOME)
164
165    # verify venvs don't exist
166    assertTrue "Virtualenv to clone already exists" "[ ! -d $tmplocation/cpvenv_test ]"
167    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
168
169    $VIRTUALENVWRAPPER_VIRTUALENV "$tmplocation/cpvenv_test" >/dev/null 2>&1
170    touch "$tmplocation/cpvenv_test/mytestpackage"
171
172    assertTrue "Virtualenv to clone didn't get created" "[ -d $tmplocation/cpvenv_test ]"
173    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
174
175    typeset listed=$(lsvirtualenv)
176    [[ "$listed" != *cpvenv_test* ]]
177    RC=$?
178    assertTrue "Cloned virtualenv found in virtualenv list" $RC
179
180    cpvirtualenv "$tmplocation/cpvenv_test" >/dev/null 2>&1 >/dev/null 2>&1
181
182    # verify cloned venv exist
183    assertTrue "Cloned virtualenv doesn't exist" "[ -d $WORKON_HOME/cpvenv_test ]"
184    assertTrue "Test package is missing" "[ -f $WORKON_HOME/cpvenv_test/mytestpackage ]"
185
186    listed=$(lsvirtualenv -b)
187    [[ "$listed" == *cpvenv_test* ]]
188    RC=$?
189    assertTrue "Cloned virtualenv not found in virtualenv list" $RC
190
191    assertSame "cpvenv_test" "$(basename $VIRTUAL_ENV)"
192    assertTrue "Virtualenv not active" virtualenvwrapper_verify_active_environment
193
194    rm -rf "$tmplocation/cpvenv_test"
195}
196
197test_clone_venv_using_vars () {
198
199    # verify venvs don't exist
200    assertTrue "Virtualenv to clone already exists" "[ ! -d $TMPDIR/cpvenv_test ]"
201    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
202
203    $VIRTUALENVWRAPPER_VIRTUALENV "$TMPDIR/cpvenv_test" >/dev/null 2>&1
204    touch "$TMPDIR/cpvenv_test/mytestpackage"
205
206    assertTrue "Virtualenv to clone didn't get created" "[ -d $TMPDIR/cpvenv_test ]"
207    assertTrue "Cloned virtualenv already exists" "[ ! -d $WORKON_HOME/cpvenv_test ]"
208
209    typeset listed=$(lsvirtualenv)
210    [[ "$listed" != *cpvenv_test* ]]
211    RC=$?
212    assertTrue "Cloned virtualenv found in virtualenv list" $RC
213
214    cpvirtualenv '$TMPDIR/cpvenv_test' >/dev/null 2>&1 >/dev/null 2>&1
215
216    # verify cloned venv exist
217    assertTrue "Cloned virtualenv doesn't exist" "[ -d $WORKON_HOME/cpvenv_test ]"
218    assertTrue "Test package is missing" "[ -f $WORKON_HOME/cpvenv_test/mytestpackage ]"
219
220    listed=$(lsvirtualenv -b)
221    [[ "$listed" == *cpvenv_test* ]]
222    RC=$?
223    assertTrue "Cloned virtualenv not found in virtualenv list" $RC
224
225    assertSame "cpvenv_test" "$(basename $VIRTUAL_ENV)"
226    assertTrue "Virtualenv not active" virtualenvwrapper_verify_active_environment
227
228    rm -rf "$TMPDIR/cpvenv_test"
229}
230
231source "$test_dir/shunit2"
232