Commits

Aron Griffis  committed 2d02918

Fix error handling in virtualenvwrapper_tempfile; the typeset builtin will
return success even if the command-substitution fails, so put them on
separate lines.

  • Participants
  • Parent commits b60d930

Comments (0)

Files changed (2)

File tests/test_tempfile.sh

     assertTrue "virtualenvwrapper-hook not in filename." "echo $filename | grep virtualenvwrapper-hook"
 }
 
+test_bad_mktemp() {
+    # All of the following bogus mktemp programs should cause
+    # virtualenvwrapper_tempfile to return non-zero status
+    mktemp_nonzero() { return 1; }
+    mktemp_empty_string() { return 0; }
+    mktemp_missing_executable() { /foo/bar/baz/qux 2>/dev/null; }   # returns status 127
+    mktemp_missing_result() { echo /foo/bar/baz/qux; }
+
+    for mktemp_func in mktemp_nonzero mktemp_empty_string \
+        mktemp_missing_executable mktemp_missing_result
+    do
+        mktemp() { $mktemp_func "$@"; }
+        filename=$(virtualenvwrapper_tempfile hook)
+        assertSame "($mktemp_func) Unexpected exit code $?" "1" "$?"
+    done
+
+    unset -f mktemp
+}
+
 test_no_such_tmpdir () {
     old_tmpdir="$TMPDIR"
     export TMPDIR="$tmplocation/does-not-exist"

File virtualenvwrapper.sh

 function virtualenvwrapper_tempfile {
     # Note: the 'X's must come last
     typeset suffix=${1:-hook}
-    typeset file="`\mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX`"
-    if [ $? -ne 0 ]
+    typeset file
+    
+    file="`\mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX`"
+    if [ $? -ne 0 ] || [ -z "$file" ] || [ ! -f "$file" ]
     then
         echo "ERROR: virtualenvwrapper could not create a temporary file name." 1>&2
         return 1
 
 # Run the hooks
 function virtualenvwrapper_run_hook {
-    typeset hook_script="$(virtualenvwrapper_tempfile ${1}-hook)"
-    if [ -z "$hook_script" ]
-    then
-        echo "ERROR: Could not create temporary file name. Make sure TMPDIR is set." 1>&2
-        return 1
-    fi
+    typeset hook_script
+    typeset result
+    
+    hook_script="$(virtualenvwrapper_tempfile ${1}-hook)" || return 1
+
     if [ -z "$VIRTUALENVWRAPPER_LOG_DIR" ]
     then
         echo "ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2