Skip to content

Commit da01a8c

Browse files
committed
Add -c/-n options to mktmpenv
Add options to control whether to cd to the virtualenv when creating a temporary env. Closes #220 Change-Id: I2a1fdfb9705ab30f13a6a6c3af9582e413655652
1 parent 5b61cb5 commit da01a8c

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

docs/source/command_ref.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,18 @@ Create a new virtualenv in the ``WORKON_HOME`` directory.
6868

6969
Syntax::
7070

71-
mktmpenv [VIRTUALENV_OPTIONS]
71+
mktmpenv [(-c|--cd)|(-n|--no-cd)] [VIRTUALENV_OPTIONS]
7272

7373
A unique virtualenv name is generated.
7474

75+
If ``-c`` or ``--cd`` is specified the working directory is changed to
76+
the virtualenv directory during the post-activate phase, regardless of
77+
the value of ``VIRTUALENVWRAPPER_WORKON_CD``.
78+
79+
If ``-n`` or ``--no-cd`` is specified the working directory is **not**
80+
changed to the virtualenv directory during the post-activate phase,
81+
regardless of the value of ``VIRTUALENVWRAPPER_WORKON_CD``.
82+
7583
::
7684

7785
$ mktmpenv

tests/test_mktmpenv.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ test_mktmpenv_name() {
3232
assertTrue "Error was not detected" "[ $RC -ne 0 ]"
3333
}
3434

35+
test_mktmpenv_n() {
36+
mktmpenv -n >/dev/null 2>&1
37+
assertNotSame "$VIRTUAL_ENV" "$(pwd)"
38+
}
39+
40+
test_mktmpenv_no_cd() {
41+
mktmpenv --no-cd >/dev/null 2>&1
42+
assertNotSame "$VIRTUAL_ENV" "$(pwd)"
43+
}
44+
3545
test_mktmpenv_virtualenv_args() {
3646
mktmpenv --no-site-packages >/dev/null 2>&1
3747
ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"

virtualenvwrapper.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,40 @@ function cdproject {
11951195
function mktmpenv {
11961196
typeset tmpenvname
11971197
typeset RC
1198+
typeset -a in_args
1199+
typeset -a out_args
1200+
1201+
in_args=( "$@" )
1202+
1203+
if [ -n "$ZSH_VERSION" ]
1204+
then
1205+
i=1
1206+
tst="-le"
1207+
else
1208+
i=0
1209+
tst="-lt"
1210+
fi
1211+
typeset cd_after_activate=$VIRTUALENVWRAPPER_WORKON_CD
1212+
while [ $i $tst $# ]
1213+
do
1214+
a="${in_args[$i]}"
1215+
case "$a" in
1216+
-n|--no-cd)
1217+
cd_after_activate=0;;
1218+
-c|--cd)
1219+
cd_after_activate=1;;
1220+
*)
1221+
if [ ${#out_args} -gt 0 ]
1222+
then
1223+
out_args=( "${out_args[@]-}" "$a" )
1224+
else
1225+
out_args=( "$a" )
1226+
fi;;
1227+
esac
1228+
i=$(( $i + 1 ))
1229+
done
1230+
1231+
set -- "${out_args[@]}"
11981232

11991233
# Generate a unique temporary name
12001234
tmpenvname=$("$VIRTUALENVWRAPPER_PYTHON" -c 'import uuid,sys; sys.stdout.write(uuid.uuid4()+"\n")' 2>/dev/null)
@@ -1214,7 +1248,7 @@ function mktmpenv {
12141248
fi
12151249

12161250
# Change working directory
1217-
cdvirtualenv
1251+
[ "$cd_after_activate" = "1" ] && cdvirtualenv
12181252

12191253
# Create the tmpenv marker file
12201254
echo "This is a temporary environment. It will be deleted when you run 'deactivate'." | tee "$VIRTUAL_ENV/README.tmpenv"

0 commit comments

Comments
 (0)