Commits

Doug Hellmann committed 03d2cf5 Merge

Merged in agriffis/virtualenvwrapper (pull request #20)

Comments (0)

Files changed (2)

tests/test_tempfile.sh

 test_tempfile () {
     filename=$(virtualenvwrapper_tempfile hook)
     assertTrue "Filename is empty" "[ ! -z \"$filename\" ]"
+    assertTrue "File doesn't exist" "[ -f \"$filename\" ]"
     rm -f $filename
     comparable_tmpdir=$(echo $tmplocation | sed 's|/$||')
     comparable_dirname=$(dirname $filename | sed 's|/$||')
     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"

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
     fi
-    trap "\rm -f '$file' >/dev/null 2>&1" EXIT
     echo $file
     return 0
 }
 
 # 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
+	\rm -f "$hook_script"
         return 1
     fi
     "$VIRTUALENVWRAPPER_PYTHON" -c 'from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script "$hook_script" "$@"
         if [ ! -f "$hook_script" ]
         then
             echo "ERROR: virtualenvwrapper_run_hook could not find temporary file $hook_script" 1>&2
+	    \rm -f "$hook_script"
             return 2
         fi
         # cat "$hook_script"
         source "$hook_script"
     fi
-    \rm -f "$hook_script" >/dev/null 2>&1
+    \rm -f "$hook_script"
     return $result
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.