Browse Source

Guaranteed removal of temporary files during tests.

Dropped the DJANGO_TEST_TEMP_DIR environment variable.

Before this change, proper removal depended on the developer passing
dir=os.environ['DJANGO_TEST_TMP_DIR'] to tempfile functions.
Aymeric Augustin 10 years ago
parent
commit
934400759d

+ 2 - 1
tests/admin_scripts/tests.py

@@ -13,6 +13,7 @@ import shutil
 import socket
 import subprocess
 import sys
+import tempfile
 import unittest
 
 import django
@@ -27,7 +28,7 @@ from django.utils._os import npath, upath
 from django.utils.encoding import force_text
 from django.utils.six import StringIO
 
-test_dir = os.path.realpath(os.path.join(os.environ['DJANGO_TEST_TEMP_DIR'], 'test_project'))
+test_dir = os.path.realpath(os.path.join(tempfile.gettempdir(), 'test_project'))
 if not os.path.exists(test_dir):
     os.mkdir(test_dir)
     open(os.path.join(test_dir, '__init__.py'), 'w').close()

+ 1 - 1
tests/admin_views/admin.py

@@ -309,7 +309,7 @@ class OldSubscriberAdmin(admin.ModelAdmin):
     actions = None
 
 
-temp_storage = FileSystemStorage(tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR']))
+temp_storage = FileSystemStorage(tempfile.mkdtemp())
 UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
 
 

+ 1 - 1
tests/admin_views/models.py

@@ -316,7 +316,7 @@ class EmptyModel(models.Model):
         return "Primary key = %s" % self.id
 
 
-temp_storage = FileSystemStorage(tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR']))
+temp_storage = FileSystemStorage(tempfile.mkdtemp())
 UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
 
 

+ 1 - 2
tests/file_storage/models.py

@@ -5,7 +5,6 @@ Storing files according to a custom storage system
 and where files should be stored.
 """
 
-import os
 import random
 import tempfile
 
@@ -26,7 +25,7 @@ class OldStyleFSStorage(FileSystemStorage):
         return super(OldStyleFSStorage, self).save(name, content)
 
 
-temp_storage_location = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+temp_storage_location = tempfile.mkdtemp()
 temp_storage = FileSystemStorage(location=temp_storage_location)
 
 

+ 1 - 1
tests/file_uploads/tests.py

@@ -22,7 +22,7 @@ from . import uploadhandler
 from .models import FileModel
 
 UNICODE_FILENAME = 'test-0123456789_中文_Orléans.jpg'
-MEDIA_ROOT = sys_tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+MEDIA_ROOT = sys_tempfile.mkdtemp()
 UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
 
 

+ 2 - 2
tests/files/tests.py

@@ -241,8 +241,8 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase):
 
 class FileMoveSafeTests(unittest.TestCase):
     def test_file_move_overwrite(self):
-        handle_a, self.file_a = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
-        handle_b, self.file_b = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+        handle_a, self.file_a = tempfile.mkstemp()
+        handle_b, self.file_b = tempfile.mkstemp()
 
         # file_move_safe should raise an IOError exception if destination file exists and allow_overwrite is False
         self.assertRaises(IOError, lambda: file_move_safe(self.file_a, self.file_b, allow_overwrite=False))

+ 1 - 3
tests/forms_tests/models.py

@@ -3,7 +3,6 @@ from __future__ import unicode_literals
 
 import datetime
 import itertools
-import os
 import tempfile
 
 from django.core.files.storage import FileSystemStorage
@@ -14,8 +13,7 @@ callable_default_counter = itertools.count()
 callable_default = lambda: next(callable_default_counter)
 
 
-temp_storage_location = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
-temp_storage = FileSystemStorage(location=temp_storage_location)
+temp_storage = FileSystemStorage(location=tempfile.mkdtemp())
 
 
 class BoundaryModel(models.Model):

+ 1 - 1
tests/model_fields/models.py

@@ -236,7 +236,7 @@ if Image:
         attr_class = TestImageFieldFile
 
     # Set up a temp directory for file storage.
-    temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+    temp_storage_dir = tempfile.mkdtemp()
     temp_storage = FileSystemStorage(temp_storage_dir)
     temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
 

+ 1 - 1
tests/model_forms/models.py

@@ -21,7 +21,7 @@ from django.utils._os import upath
 from django.utils.encoding import python_2_unicode_compatible
 from django.utils.six.moves import range
 
-temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+temp_storage_dir = tempfile.mkdtemp()
 temp_storage = FileSystemStorage(temp_storage_dir)
 
 ARTICLE_STATUS = (

+ 9 - 6
tests/runtests.py

@@ -27,8 +27,11 @@ RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__)))
 
 TEMPLATE_DIR = os.path.join(RUNTESTS_DIR, 'templates')
 
-TEMP_DIR = tempfile.mkdtemp(prefix='django_')
-os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR
+# Create a specific subdirectory for the duration of the test suite.
+TMPDIR = tempfile.mkdtemp(prefix='django_')
+# Set the TMPDIR environment variable in addition to tempfile.tempdir
+# so that children processes inherit it.
+tempfile.tempdir = os.environ['TMPDIR'] = TMPDIR
 
 SUBDIRS_TO_SKIP = [
     'data',
@@ -121,7 +124,7 @@ def setup(verbosity, test_labels):
     settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
     settings.ROOT_URLCONF = 'urls'
     settings.STATIC_URL = '/static/'
-    settings.STATIC_ROOT = os.path.join(TEMP_DIR, 'static')
+    settings.STATIC_ROOT = os.path.join(TMPDIR, 'static')
     # Remove the following line in Django 2.0.
     settings.TEMPLATE_DIRS = [TEMPLATE_DIR]
     settings.TEMPLATES = [{
@@ -213,13 +216,13 @@ def setup(verbosity, test_labels):
 
 def teardown(state):
     try:
-        # Removing the temporary TEMP_DIR. Ensure we pass in unicode
+        # Removing the temporary TMPDIR. Ensure we pass in unicode
         # so that it will successfully remove temp trees containing
         # non-ASCII filenames on Windows. (We're assuming the temp dir
         # name itself does not contain non-ASCII characters.)
-        shutil.rmtree(six.text_type(TEMP_DIR))
+        shutil.rmtree(six.text_type(TMPDIR))
     except OSError:
-        print('Failed to remove temp directory: %s' % TEMP_DIR)
+        print('Failed to remove temp directory: %s' % TMPDIR)
 
     # Restore the old settings.
     for key, value in state.items():

+ 1 - 1
tests/staticfiles_tests/tests.py

@@ -100,7 +100,7 @@ class BaseCollectionTestCase(BaseStaticFilesTestCase):
     """
     def setUp(self):
         super(BaseCollectionTestCase, self).setUp()
-        temp_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
+        temp_dir = tempfile.mkdtemp()
         # Override the STATIC_ROOT for all tests from setUp to tearDown
         # rather than as a context manager
         self.patched_settings = self.settings(STATIC_ROOT=temp_dir)