Skip to content

Commit 4f5c84c

Browse files
committed
Update cpvirtualenv utilizing virtualenv-clone and allowing for external virutalenvs to be added to WORKON_HOME.
1 parent 30c3663 commit 4f5c84c

File tree

7 files changed

+392
-69
lines changed

7 files changed

+392
-69
lines changed

docs/en/command_ref.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,17 @@ environment.
153153
cpvirtualenv
154154
------------
155155

156-
Duplicate an environment, in the WORKON_HOME.
156+
In the WORKON_HOME, duplicate an environment or clone an external environment.
157157

158158
Syntax::
159159

160-
cpvirtualenv ENVNAME TARGETENVNAME
160+
cpvirtualenv ENVNAME [TARGETENVNAME]
161+
162+
161163

162164
.. note::
163165

164-
The environment created by the copy operation is made `relocatable
165-
<http://virtualenv.openplans.org/#making-environments-relocatable>`__.
166+
Target environment name is required for WORKON_HOME duplications. However, target environment name can be ommited for external copies, causing the new environment to have the name of the original within the WORKON_HOME.
166167

167168
::
168169

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def find_package_data(
147147
'virtualenvwrapper.user_scripts',
148148
'virtualenvwrapper.project',
149149
],
150-
install_requires=['virtualenv'],
150+
install_requires=['virtualenv',
151+
'virtualenv-clone',
152+
],
151153

152154
namespace_packages = [ 'virtualenvwrapper' ],
153155
packages = find_packages(),

tests/test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,18 @@ test_python_interpreter_set_incorrectly() {
102102
deactivate
103103
}
104104

105+
test_virtualenvwrapper_verify_virtualenv(){
106+
assertTrue "Verified unable to verify virtualenv" virtualenvwrapper_verify_virtualenv
107+
108+
VIRTUALENVWRAPPER_VIRTUALENV="thiscannotpossiblyexist123"
109+
assertFalse "Incorrectly verified virtualenv" virtualenvwrapper_verify_virtualenv
110+
}
111+
112+
test_virtualenvwrapper_verify_virtualenv_clone(){
113+
assertTrue "Verified unable to verify virtualenv_clone" virtualenvwrapper_verify_virtualenv_clone
114+
115+
VIRTUALENVWRAPPER_VIRTUALENV_CLONE="thiscannotpossiblyexist123"
116+
assertFalse "Incorrectly verified virtualenv_clone" virtualenvwrapper_verify_virtualenv_clone
117+
}
118+
105119
. "$test_dir/shunit2"

tests/test_cp.sh

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ tearDown() {
2020
}
2121

2222
test_new_env_activated () {
23-
mkvirtualenv "source"
23+
mkvirtualenv "source" >/dev/null 2>&1
2424
(cd tests/testpackage && python setup.py install) >/dev/null 2>&1
25-
cpvirtualenv "source" "destination"
26-
rmvirtualenv "source"
25+
cpvirtualenv "source" "destination" >/dev/null 2>&1
26+
rmvirtualenv "source" >/dev/null 2>&1
2727
testscript="$(which testscript.py)"
2828
assertTrue "Environment test script not found in path" "[ $WORKON_HOME/destination/bin/testscript.py -ef $testscript ]"
2929
testscriptcontent="$(cat $testscript)"
@@ -32,44 +32,78 @@ test_new_env_activated () {
3232
}
3333

3434
test_virtual_env_variable () {
35-
mkvirtualenv "source"
36-
cpvirtualenv "source" "destination"
35+
mkvirtualenv "source" >/dev/null 2>&1
36+
cpvirtualenv "source" "destination" >/dev/null 2>&1
3737
assertSame "Wrong virtualenv name" "destination" $(basename "$VIRTUAL_ENV")
3838
assertTrue "$WORKON_HOME not in $VIRTUAL_ENV" "echo $VIRTUAL_ENV | grep -q $WORKON_HOME"
3939
}
4040

41-
fake_virtualenv () {
41+
fake_venv () {
42+
###
43+
# create a silly file to ensure copy happens
44+
###
4245
typeset envname="$1"
43-
touch "$envname/fake_virtualenv_was_here"
4446
virtualenv $@
47+
touch "$WORKON_HOME/$envname/fake_virtualenv_was_here"
48+
}
49+
50+
fake_venv_clone () {
51+
###
52+
# create a silly file to ensure copy happens
53+
###
54+
typeset src_path="$1"
55+
touch "$src_path/fake_virtualenv_clone_was_here"
56+
virtualenv-clone $@
4557
}
4658

4759
test_virtualenvwrapper_virtualenv_variable () {
48-
mkvirtualenv "source"
49-
export VIRTUALENVWRAPPER_VIRTUALENV=fake_virtualenv
50-
cpvirtualenv "source" "destination"
60+
61+
eval 'virtualenvwrapper_verify_virtualenv () {
62+
return 0
63+
}'
64+
65+
VIRTUALENVWRAPPER_VIRTUALENV=fake_venv
66+
assertSame "VIRTUALENVWRAPPER_VIRTUALENV is not set correctly" "$VIRTUALENVWRAPPER_VIRTUALENV" "fake_venv"
67+
68+
mkvirtualenv "source" >/dev/null 2>&1
69+
assertTrue "Fake file not made in fake_venv" "[ -f "$VIRTUAL_ENV/fake_virtualenv_was_here" ]"
70+
cpvirtualenv "source" "destination" >/dev/null 2>&1
5171
unset VIRTUALENVWRAPPER_VIRTUALENV
52-
assertTrue "wrapper was not run" "[ -f $VIRTUAL_ENV/fake_virtualenv_was_here ]"
72+
assertTrue "VIRTUALENVWRAPPER_CLONE did not clone fake file" "[ -f $WORKON_HOME/destination/fake_virtualenv_was_here ]"
73+
}
74+
75+
test_virtualenvwrapper_virtualenv_clone_variable () {
76+
77+
eval 'virtualenvwrapper_verify_virtualenv_clone () {
78+
return 0
79+
}'
80+
81+
VIRTUALENVWRAPPER_VIRTUALENV_CLONE=fake_venv_clone
82+
assertSame "VIRTUALENVWRAPPER_VIRTUALENV_CLONE is not set correctly" "$VIRTUALENVWRAPPER_VIRTUALENV_CLONE" "fake_venv_clone"
83+
84+
mkvirtualenv "source" >/dev/null 2>&1
85+
cpvirtualenv "source" "destination" >/dev/null 2>&1
86+
unset VIRTUALENVWRAPPER_VIRTUALENV_CLONE
87+
assertTrue "VIRTUALENVWRAPPER_CLONE did not clone fake file" "[ -f $WORKON_HOME/destination/fake_virtualenv_clone_was_here ]"
5388
}
5489

5590
test_source_relocatable () {
56-
mkvirtualenv "source"
91+
mkvirtualenv "source" >/dev/null 2>&1
5792
(cd tests/testpackage && python setup.py install) >/dev/null 2>&1
5893
assertTrue "virtualenv --relocatable \"$WORKON_HOME/source\""
59-
cpvirtualenv "source" "destination"
94+
cpvirtualenv "source" "destination" >/dev/null 2>&1
6095
testscript="$(which testscript.py)"
6196
assertTrue "Environment test script not the same as copy" "[ $WORKON_HOME/destination/bin/testscript.py -ef $testscript ]"
6297
assertTrue virtualenvwrapper_verify_active_environment
6398
assertSame "Wrong virtualenv name" "destination" $(basename "$VIRTUAL_ENV")
6499
}
65100

66101
test_source_does_not_exist () {
67-
out="$(cpvirtualenv virtualenvthatdoesntexist foo)"
68-
assertSame "$out" "virtualenvthatdoesntexist virtualenv doesn't exist"
102+
assertSame "Please provide a valid virtualenv to copy." "$(cpvirtualenv virtualenvthatdoesntexist foo)"
69103
}
70104

71105
test_hooks () {
72-
mkvirtualenv "source"
106+
mkvirtualenv "source" >/dev/null 2>&1
73107

74108
export pre_test_dir=$(cd "$test_dir"; pwd)
75109

@@ -89,12 +123,12 @@ test_hooks () {
89123
echo "#!/bin/sh" > "$WORKON_HOME/postcpvirtualenv"
90124
echo "echo GLOBAL postcpvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postcpvirtualenv"
91125

92-
cpvirtualenv "source" "destination"
126+
cpvirtualenv "source" "destination" >/dev/null 2>&1
93127

94128
output=$(cat "$test_dir/catch_output")
95129
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
96130

97-
expected="GLOBAL precpvirtualenv $workon_home_as_pwd source destination
131+
expected="GLOBAL precpvirtualenv $workon_home_as_pwd $workon_home_as_pwd/source destination
98132
GLOBAL premkvirtualenv $workon_home_as_pwd destination
99133
GLOBAL postmkvirtualenv
100134
GLOBAL postcpvirtualenv"
@@ -108,7 +142,7 @@ GLOBAL postcpvirtualenv"
108142
test_no_site_packages () {
109143
# See issue #102
110144
mkvirtualenv "source" --no-site-packages >/dev/null 2>&1
111-
cpvirtualenv "source" "destination"
145+
cpvirtualenv "source" "destination" >/dev/null 2>&1
112146
ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"
113147
assertTrue "$ngsp_file does not exist in copied env" "[ -f \"$ngsp_file\" ]"
114148
}
@@ -118,7 +152,7 @@ test_no_site_packages_default_args () {
118152
VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--no-site-packages"
119153
# With the argument, verify that they are not copied.
120154
mkvirtualenv "source" >/dev/null 2>&1
121-
cpvirtualenv "source" "destination"
155+
cpvirtualenv "source" "destination" >/dev/null 2>&1
122156
ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"
123157
assertTrue "$ngsp_file does not exist" "[ -f \"$ngsp_file\" ]"
124158
unset VIRTUALENVWRAPPER_VIRTUALENV_ARGS
@@ -128,7 +162,7 @@ test_no_site_packages_default_behavior () {
128162
# See issue #102
129163
# virtualenv 1.7 changed to make --no-site-packages the default
130164
mkvirtualenv "source" >/dev/null 2>&1
131-
cpvirtualenv "source" "destination"
165+
cpvirtualenv "source" "destination" >/dev/null 2>&1
132166
ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"
133167
assertTrue "$ngsp_file does not exist in copied env" "[ -f \"$ngsp_file\" ]"
134168
}

0 commit comments

Comments
 (0)