diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 90f6cc077f..ed0958af3f 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -13,6 +13,7 @@ import shutil import socket import subprocess import sys +import tempfile import unittest import django @@ -31,7 +32,7 @@ from django.utils.deprecation import RemovedInDjango19Warning 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() diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py index 0ed31ce0c5..d7d4be86bc 100644 --- a/tests/admin_views/admin.py +++ b/tests/admin_views/admin.py @@ -310,7 +310,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') diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index c4d247d56c..07f6864cf5 100644 --- a/tests/admin_views/models.py +++ b/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') diff --git a/tests/file_storage/models.py b/tests/file_storage/models.py index 45a3d9f0e7..c7523bdea4 100644 --- a/tests/file_storage/models.py +++ b/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) diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index 39cc2b4df0..1338add474 100644 --- a/tests/file_uploads/tests.py +++ b/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') diff --git a/tests/files/tests.py b/tests/files/tests.py index c72d948392..2401f26c8f 100644 --- a/tests/files/tests.py +++ b/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)) diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py index 936bf8b41f..830927aeba 100644 --- a/tests/forms_tests/models.py +++ b/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): diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index c988fce246..620fa9ec5d 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -237,7 +237,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') diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index ac3c6062a2..2cfe523175 100644 --- a/tests/model_forms/models.py +++ b/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 = ( diff --git a/tests/runtests.py b/tests/runtests.py index a1f918c726..9cc14cfb8d 100755 --- a/tests/runtests.py +++ b/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 = [{ @@ -215,13 +218,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(): diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py index 72f8f2fd79..45d29106d1 100644 --- a/tests/staticfiles_tests/tests.py +++ b/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)