Skip to content

Commit 94e3d32

Browse files
committed
Use "command" to avoid aliases or functions that mask common utilities. fixes #119
1 parent 04eb339 commit 94e3d32

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

tests/test_tempfile.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ test_bad_mktemp() {
4343
for mktemp_func in mktemp_nonzero mktemp_empty_string \
4444
mktemp_missing_executable mktemp_missing_result
4545
do
46-
mktemp() { $mktemp_func "$@"; }
46+
__virtualenvwrapper_mktemp() { $mktemp_func "$@"; }
4747
filename=$(virtualenvwrapper_tempfile hook 2>/dev/null)
4848
assertSame "($mktemp_func) Unexpected exit code $?" "1" "$?"
4949
done
5050

51-
unset -f mktemp
51+
# Restore the "real" definition of the replaceable function
52+
__virtualenvwrapper_mktemp() { command mktemp "$@"; }
5253
}
5354

5455
test_no_such_tmpdir () {

virtualenvwrapper.sh

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
# Locate the global Python where virtualenvwrapper is installed.
4848
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ]
4949
then
50-
VIRTUALENVWRAPPER_PYTHON="$(\which python)"
50+
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
5151
fi
5252

5353
# Set the name of the virtualenv app to use.
@@ -78,6 +78,29 @@ then
7878
export VIRTUALENVWRAPPER_PROJECT_FILENAME=".project"
7979
fi
8080

81+
# Portable shell scripting is hard, let's go shopping.
82+
#
83+
# People insist on aliasing commands like 'cd', either with a real
84+
# alias or even a shell function. Under bash and zsh, "builtin" forces
85+
# the use of a command that is part of the shell itself instead of an
86+
# alias, function, or external command, while "command" does something
87+
# similar but allows external commands. Under ksh "builtin" registers
88+
# a new command from a shared library, but "command" will pick up
89+
# existing builtin commands. We need to use a builtin for cd because
90+
# we are trying to change the state of the current shell, so we use
91+
# "builtin" for bash and zsh but "command" under ksh.
92+
function __virtualenvwrapper_cd {
93+
if [ -n "$BASH" ]
94+
then
95+
builtin \cd "$@"
96+
elif [ -n "$ZSH_VERSION" ]
97+
then
98+
builtin \cd "$@"
99+
else
100+
command \cd "$@"
101+
fi
102+
}
103+
81104
function virtualenvwrapper_expandpath {
82105
if [ "$1" = "" ]; then
83106
return 1
@@ -99,7 +122,7 @@ function virtualenvwrapper_derive_workon_home {
99122

100123
# If the path is relative, prefix it with $HOME
101124
# (note: for compatibility)
102-
if echo "$workon_home_dir" | (unset GREP_OPTIONS; \grep '^[^/~]' > /dev/null)
125+
if echo "$workon_home_dir" | (unset GREP_OPTIONS; command \grep '^[^/~]' > /dev/null)
103126
then
104127
workon_home_dir="$HOME/$WORKON_HOME"
105128
fi
@@ -108,7 +131,7 @@ function virtualenvwrapper_derive_workon_home {
108131
# path might contain stuff to expand.
109132
# (it might be possible to do this in shell, but I don't know a
110133
# cross-shell-safe way of doing it -wolever)
111-
if echo "$workon_home_dir" | (unset GREP_OPTIONS; \egrep '([\$~]|//)' >/dev/null)
134+
if echo "$workon_home_dir" | (unset GREP_OPTIONS; command \egrep '([\$~]|//)' >/dev/null)
112135
then
113136
# This will normalize the path by:
114137
# - Removing extra slashes (e.g., when TMPDIR ends in a slash)
@@ -141,13 +164,19 @@ function virtualenvwrapper_verify_workon_home {
141164

142165
#HOOK_VERBOSE_OPTION="-q"
143166

167+
# Function to wrap mktemp so tests can replace it for error condition
168+
# testing.
169+
function __virtualenvwrapper_mktemp {
170+
command \mktemp "$@"
171+
}
172+
144173
# Expects 1 argument, the suffix for the new file.
145174
function virtualenvwrapper_tempfile {
146175
# Note: the 'X's must come last
147176
typeset suffix=${1:-hook}
148177
typeset file
149178

150-
file="`\mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX`"
179+
file="$(__virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX)"
151180
if [ $? -ne 0 ] || [ -z "$file" ] || [ ! -f "$file" ]
152181
then
153182
echo "ERROR: virtualenvwrapper could not create a temporary file name." 1>&2
@@ -167,7 +196,7 @@ function virtualenvwrapper_run_hook {
167196
if [ -z "$VIRTUALENVWRAPPER_LOG_DIR" ]
168197
then
169198
echo "ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2
170-
\rm -f "$hook_script"
199+
command \rm -f "$hook_script"
171200
return 1
172201
fi
173202
"$VIRTUALENVWRAPPER_PYTHON" -c 'from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script "$hook_script" "$@"
@@ -178,7 +207,7 @@ function virtualenvwrapper_run_hook {
178207
if [ ! -f "$hook_script" ]
179208
then
180209
echo "ERROR: virtualenvwrapper_run_hook could not find temporary file $hook_script" 1>&2
181-
\rm -f "$hook_script"
210+
command \rm -f "$hook_script"
182211
return 2
183212
fi
184213
# cat "$hook_script"
@@ -194,7 +223,7 @@ VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON and that PATH is
194223
set properly.
195224
EOF
196225
fi
197-
\rm -f "$hook_script"
226+
command \rm -f "$hook_script"
198227
return $result
199228
}
200229

@@ -263,7 +292,7 @@ function virtualenvwrapper_initialize {
263292

264293
# Verify that the passed resource is in path and exists
265294
function virtualenvwrapper_verify_resource {
266-
typeset exe_path=$(\which "$1" | (unset GREP_OPTIONS; \grep -v "not found"))
295+
typeset exe_path=$(command \which "$1" | (unset GREP_OPTIONS; command \grep -v "not found"))
267296
if [ "$exe_path" = "" ]
268297
then
269298
echo "ERROR: virtualenvwrapper could not find $1 in your path" >&2
@@ -395,7 +424,7 @@ function mkvirtualenv {
395424
virtualenvwrapper_verify_virtualenv || return 1
396425
(
397426
[ -n "$ZSH_VERSION" ] && setopt SH_WORD_SPLIT
398-
\cd "$WORKON_HOME" &&
427+
__virtualenvwrapper_cd "$WORKON_HOME" &&
399428
"$VIRTUALENVWRAPPER_VIRTUALENV" $VIRTUALENVWRAPPER_VIRTUALENV_ARGS "$@" &&
400429
[ -d "$WORKON_HOME/$envname" ] && \
401430
virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
@@ -456,16 +485,16 @@ function rmvirtualenv {
456485
# Move out of the current directory to one known to be
457486
# safe, in case we are inside the environment somewhere.
458487
typeset prior_dir="$(pwd)"
459-
\cd "$WORKON_HOME"
488+
__virtualenvwrapper_cd "$WORKON_HOME"
460489

461490
virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
462-
\rm -rf "$env_dir"
491+
command \rm -rf "$env_dir"
463492
virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
464493

465494
# If the directory we used to be in still exists, move back to it.
466495
if [ -d "$prior_dir" ]
467496
then
468-
\cd "$prior_dir"
497+
__virtualenvwrapper_cd "$prior_dir"
469498
fi
470499
done
471500
}
@@ -476,9 +505,7 @@ function virtualenvwrapper_show_workon_options {
476505
# NOTE: DO NOT use ls here because colorized versions spew control characters
477506
# into the output list.
478507
# echo seems a little faster than find, even with -depth 3.
479-
(\cd "$WORKON_HOME"; for f in */$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate; do echo $f; done) 2>/dev/null | \sed 's|^\./||' | \sed "s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate||" | \sort | (unset GREP_OPTIONS; \egrep -v '^\*$')
480-
481-
# (\cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
508+
(__virtualenvwrapper_cd "$WORKON_HOME"; for f in */$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate; do echo $f; done) 2>/dev/null | command \sed 's|^\./||' | command \sed "s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate||" | command \sort | (unset GREP_OPTIONS; command \egrep -v '^\*$')
482509
}
483510

484511
function _lsvirtualenv_usage {
@@ -725,14 +752,14 @@ function cdsitepackages {
725752
virtualenvwrapper_verify_workon_home || return 1
726753
virtualenvwrapper_verify_active_environment || return 1
727754
typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
728-
\cd "$site_packages"/$1
755+
__virtualenvwrapper_cd "$site_packages"/$1
729756
}
730757

731758
# Does a ``cd`` to the root of the currently-active virtualenv.
732759
function cdvirtualenv {
733760
virtualenvwrapper_verify_workon_home || return 1
734761
virtualenvwrapper_verify_active_environment || return 1
735-
\cd $VIRTUAL_ENV/$1
762+
__virtualenvwrapper_cd $VIRTUAL_ENV/$1
736763
}
737764

738765
# Shows the content of the site-packages directory of the currently-active
@@ -820,7 +847,7 @@ function cpvirtualenv {
820847
echo "Copying $src_name as $trg_name..."
821848
(
822849
[ -n "$ZSH_VERSION" ] && setopt SH_WORD_SPLIT
823-
\cd "$WORKON_HOME" &&
850+
__virtualenvwrapper_cd "$WORKON_HOME" &&
824851
"$VIRTUALENVWRAPPER_VIRTUALENV_CLONE" "$src" "$trg"
825852
[ -d "$trg" ] &&
826853
virtualenvwrapper_run_hook "pre_cpvirtualenv" "$src" "$trg_name" &&

0 commit comments

Comments
 (0)