Skip to content

Commit 4a6aab0

Browse files
committed
make cd after workon optional
Provide an environment variable and command line option to control whether the project plugin changes the working directory during the post-activate phase. Addresses #246 Change-Id: If710959339e17a6960ae12f9023088460f546daa
1 parent 057ea9f commit 4a6aab0

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

docs/source/command_ref.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,19 @@ List or change working virtual environments
238238

239239
Syntax::
240240

241-
workon [environment_name]
241+
workon [(-c|--cd)|(-n|--no-cd)] [environment_name]
242242

243243
If no ``environment_name`` is given the list of available environments
244244
is printed to stdout.
245245

246+
If ``-c`` or ``--cd`` is specified the working directory is changed to
247+
the project directory during the post-activate phase, regardless of
248+
the value of ``VIRTUALENVWRAPPER_WORKON_CD``.
249+
250+
If ``-n`` or ``--no-cd`` is specified the working directory is **not**
251+
changed to the project directory during the post-activate phase,
252+
regardless of the value of ``VIRTUALENVWRAPPER_WORKON_CD``.
253+
246254
::
247255

248256
$ workon
@@ -276,6 +284,7 @@ is printed to stdout.
276284
* :ref:`scripts-postdeactivate`
277285
* :ref:`scripts-preactivate`
278286
* :ref:`scripts-postactivate`
287+
* :ref:`variable-VIRTUALENVWRAPPER_WORKON_CD`
279288

280289
.. _command-deactivate:
281290

docs/source/install.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ project working directory. The default is ``.project``.
207207

208208
* :ref:`project-management`
209209

210+
.. _variable-VIRTUALENVWRAPPER_WORKON_CD:
211+
212+
Enable Project Directory Switching
213+
----------------------------------
214+
215+
The variable ``VIRTUALENVWRAPPER_WORKON_CD`` controls whether the
216+
working directory is changed during the post activate phase. The
217+
default is ``1``, to enable changing directories. Set the value to
218+
``0`` to disable this behavior for all invocations of ``workon``.
219+
220+
.. seealso::
221+
222+
* :ref:`command-workon`
223+
210224
.. _variable-VIRTUALENVWRAPPER_HOOK_DIR:
211225

212226
Location of Hook Scripts

docs/source/projects.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ site, combine the :ref:`templates-bitbucket` and
3636
* :ref:`extensions-templates`
3737
* :ref:`variable-PROJECT_HOME`
3838
* :ref:`variable-VIRTUALENVWRAPPER_PROJECT_FILENAME`
39+
* :ref:`variable-VIRTUALENVWRAPPER_WORKON_CD`

tests/test_project_activate.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ oneTimeSetUp() {
1818

1919
setUp () {
2020
echo
21+
# In case the user has the value set, we need to force it to the default
22+
VIRTUALENVWRAPPER_WORKON_CD=1
23+
}
24+
25+
tearDown () {
26+
# In case a test has changed the value, we need to force it to the default
27+
VIRTUALENVWRAPPER_WORKON_CD=1
2128
}
2229

2330
test_activate () {
@@ -31,6 +38,43 @@ test_activate () {
3138
deactivate
3239
}
3340

41+
test_activate_n () {
42+
mkproject myproject_n >/dev/null 2>&1
43+
assertTrue $?
44+
deactivate
45+
cd $TMPDIR
46+
assertSame "" "$VIRTUAL_ENV"
47+
workon -n myproject_n
48+
assertSame "myproject_n" "$(basename $VIRTUAL_ENV)"
49+
assertSame "$TMPDIR" "$(pwd)"
50+
deactivate
51+
}
52+
53+
test_activate_no_cd () {
54+
mkproject myproject_no_cd >/dev/null 2>&1
55+
assertTrue $?
56+
deactivate
57+
cd $TMPDIR
58+
assertSame "" "$VIRTUAL_ENV"
59+
workon --no-cd myproject_no_cd
60+
assertSame "myproject_no_cd" "$(basename $VIRTUAL_ENV)"
61+
assertSame "$TMPDIR" "$(pwd)"
62+
deactivate
63+
}
64+
65+
test_activate_workon_cd_disabled () {
66+
export VIRTUALENVWRAPPER_WORKON_CD=0
67+
mkproject myproject_cd_disabled >/dev/null 2>&1
68+
assertTrue $?
69+
deactivate
70+
cd $TMPDIR
71+
assertSame "" "$VIRTUAL_ENV"
72+
workon myproject_cd_disabled
73+
assertSame "myproject_cd_disabled" "$(basename $VIRTUAL_ENV)"
74+
assertSame "$TMPDIR" "$(pwd)"
75+
deactivate
76+
}
77+
3478
test_space_in_path () {
3579
old_project_home="$PROJECT_HOME"
3680
PROJECT_HOME="$PROJECT_HOME/with spaces"

virtualenvwrapper.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ then
7878
export VIRTUALENVWRAPPER_PROJECT_FILENAME=".project"
7979
fi
8080

81+
# Let the user tell us they never want to cd to projects
82+
# automatically.
83+
if [ "$VIRTUALENVWRAPPER_WORKON_CD" = "" ]
84+
then
85+
export VIRTUALENVWRAPPER_WORKON_CD=1
86+
fi
87+
8188
# Remember where we are running from.
8289
if [ -z "$VIRTUALENVWRAPPER_SCRIPT" ]
8390
then
@@ -658,10 +665,23 @@ function virtualenvwrapper_workon_help {
658665
echo ""
659666
echo " Show this help message."
660667
echo ""
668+
echo " workon (-c|--cd) envname"
669+
echo ""
670+
echo " After activating the environment, cd to the associated"
671+
echo " project directory if it is set."
672+
echo ""
673+
echo " workon (-n|--no-cd) envname"
674+
echo ""
675+
echo " After activating the environment, do not cd to the"
676+
echo " associated project directory."
677+
echo ""
661678
}
662679

663680
#:help:workon: list or change working virtualenvs
664681
function workon {
682+
typeset -a in_args
683+
typeset -a out_args
684+
665685
in_args=( "$@" )
666686

667687
if [ -n "$ZSH_VERSION" ]
@@ -672,17 +692,31 @@ function workon {
672692
i=0
673693
tst="-lt"
674694
fi
695+
typeset cd_after_activate=$VIRTUALENVWRAPPER_WORKON_CD
675696
while [ $i $tst $# ]
676697
do
677698
a="${in_args[$i]}"
678699
case "$a" in
679700
-h|--help)
680701
virtualenvwrapper_workon_help;
681702
return 0;;
703+
-n|--no-cd)
704+
cd_after_activate=0;;
705+
-c|--cd)
706+
cd_after_activate=1;;
707+
*)
708+
if [ ${#out_args} -gt 0 ]
709+
then
710+
out_args=( "${out_args[@]-}" "$a" )
711+
else
712+
out_args=( "$a" )
713+
fi;;
682714
esac
683715
i=$(( $i + 1 ))
684716
done
685717

718+
set -- "${out_args[@]}"
719+
686720
typeset env_name="$1"
687721
if [ "$env_name" = "" ]
688722
then
@@ -745,7 +779,7 @@ function workon {
745779
746780
}'
747781

748-
virtualenvwrapper_run_hook "post_activate"
782+
VIRTUALENVWRAPPER_PROJECT_CD=$cd_after_activate virtualenvwrapper_run_hook "post_activate"
749783

750784
return 0
751785
}

virtualenvwrapper/project.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def post_mkproject_source(args):
5252
def post_activate_source(args):
5353
return """
5454
#
55-
# Change to the project directory
55+
# Change to the project directory, as long as we haven't been told not to.
5656
#
57-
[ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ] && \
57+
[ -f "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" -a "$VIRTUALENVWRAPPER_PROJECT_CD" = 1 ] && \
5858
virtualenvwrapper_cd \
5959
"$(cat \"$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME\")"
6060
"""

0 commit comments

Comments
 (0)