Skip to content

Commit 2d02918

Browse files
committed
Fix error handling in virtualenvwrapper_tempfile; the typeset builtin will
return success even if the command-substitution fails, so put them on separate lines.
1 parent b60d930 commit 2d02918

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

tests/test_tempfile.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ test_tempfile () {
3232
assertTrue "virtualenvwrapper-hook not in filename." "echo $filename | grep virtualenvwrapper-hook"
3333
}
3434

35+
test_bad_mktemp() {
36+
# All of the following bogus mktemp programs should cause
37+
# virtualenvwrapper_tempfile to return non-zero status
38+
mktemp_nonzero() { return 1; }
39+
mktemp_empty_string() { return 0; }
40+
mktemp_missing_executable() { /foo/bar/baz/qux 2>/dev/null; } # returns status 127
41+
mktemp_missing_result() { echo /foo/bar/baz/qux; }
42+
43+
for mktemp_func in mktemp_nonzero mktemp_empty_string \
44+
mktemp_missing_executable mktemp_missing_result
45+
do
46+
mktemp() { $mktemp_func "$@"; }
47+
filename=$(virtualenvwrapper_tempfile hook)
48+
assertSame "($mktemp_func) Unexpected exit code $?" "1" "$?"
49+
done
50+
51+
unset -f mktemp
52+
}
53+
3554
test_no_such_tmpdir () {
3655
old_tmpdir="$TMPDIR"
3756
export TMPDIR="$tmplocation/does-not-exist"

virtualenvwrapper.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ function virtualenvwrapper_verify_workon_home {
130130
function virtualenvwrapper_tempfile {
131131
# Note: the 'X's must come last
132132
typeset suffix=${1:-hook}
133-
typeset file="`\mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX`"
134-
if [ $? -ne 0 ]
133+
typeset file
134+
135+
file="`\mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX`"
136+
if [ $? -ne 0 ] || [ -z "$file" ] || [ ! -f "$file" ]
135137
then
136138
echo "ERROR: virtualenvwrapper could not create a temporary file name." 1>&2
137139
return 1
@@ -142,12 +144,11 @@ function virtualenvwrapper_tempfile {
142144

143145
# Run the hooks
144146
function virtualenvwrapper_run_hook {
145-
typeset hook_script="$(virtualenvwrapper_tempfile ${1}-hook)"
146-
if [ -z "$hook_script" ]
147-
then
148-
echo "ERROR: Could not create temporary file name. Make sure TMPDIR is set." 1>&2
149-
return 1
150-
fi
147+
typeset hook_script
148+
typeset result
149+
150+
hook_script="$(virtualenvwrapper_tempfile ${1}-hook)" || return 1
151+
151152
if [ -z "$VIRTUALENVWRAPPER_LOG_DIR" ]
152153
then
153154
echo "ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2

0 commit comments

Comments
 (0)