Skip to content

Commit bae9862

Browse files
committed
make it possible to remove a virtualenv while inside it; fixes #83
1 parent 3885460 commit bae9862

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2011-04-13 Doug Hellmann <[email protected]>
2+
3+
* virtualenvwrapper.sh (rmvirtualenv): Move to a safe directory
4+
before removing the virtual environment, and only move back if the
5+
previously occupied directory still exists.
6+
17
2011-04-10 Doug Hellmann <[email protected]>
28

39
* virtualenvwrapper.sh: Initialize VIRTUALENVWRAPPER_LOG_DIR and

docs/en/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dev
2929
locations of hooks and logs.
3030
- Enabled tab completion for :ref:`command-showvirtualenv`
3131
(:bbissue:`78`).
32+
- Fixed a problem with running :ref:`command-rmvirtualenv` from
33+
within the environment being removed (:bbissue:`83`).
3234

3335
2.6.3
3436

tests/test_rmvirtualenv.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,26 @@ setUp () {
2222
}
2323

2424
test_remove () {
25-
mkvirtualenv "deleteme"
25+
mkvirtualenv "deleteme" >/dev/null 2>&1
2626
assertTrue "[ -d $WORKON_HOME/deleteme ]"
2727
deactivate
2828
rmvirtualenv "deleteme"
2929
assertFalse "[ -d $WORKON_HOME/deleteme ]"
3030
}
3131

32+
test_within_virtualenv () {
33+
mkvirtualenv "deleteme" >/dev/null 2>&1
34+
assertTrue "[ -d $WORKON_HOME/deleteme ]"
35+
cdvirtualenv
36+
assertSame "$VIRTUAL_ENV" "$(pwd)"
37+
deactivate
38+
rmvirtualenv "deleteme"
39+
assertSame "$WORKON_HOME" "$(pwd)"
40+
assertFalse "[ -d $WORKON_HOME/deleteme ]"
41+
}
42+
3243
test_rm_aliased () {
33-
mkvirtualenv "deleteme"
44+
mkvirtualenv "deleteme" >/dev/null 2>&1
3445
deactivate
3546
alias rm='rm -i'
3647
rmvirtualenv "deleteme"

virtualenvwrapper.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,21 @@ rmvirtualenv () {
252252
echo "Either switch to another environment, or run 'deactivate'." >&2
253253
return 1
254254
fi
255+
256+
# Move out of the current directory to one known to be
257+
# safe, in case we are inside the environment somewhere.
258+
typeset prior_dir="$(pwd)"
259+
cd "$WORKON_HOME"
260+
255261
virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
256262
\rm -rf "$env_dir"
257263
virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
264+
265+
# If the directory we used to be in still exists, move back to it.
266+
if [ -d "$prior_dir" ]
267+
then
268+
cd "$prior_dir"
269+
fi
258270
}
259271

260272
# List the available environments.

0 commit comments

Comments
 (0)