summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2015-02-21 18:56:36 +0100
committerAymeric Augustin <aymeric.augustin@oscaro.com>2015-02-23 16:55:26 +0100
commit934400759de817471ff37d736686201d7ae34e82 (patch)
tree834ceac28e66f7d81350a79a7f23fdb71e6cb1f4 /tests
parente83aba0e2cce16cd1b32d1c172239a4e20867e95 (diff)
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/admin_scripts/tests.py3
-rw-r--r--tests/admin_views/admin.py2
-rw-r--r--tests/admin_views/models.py2
-rw-r--r--tests/file_storage/models.py3
-rw-r--r--tests/file_uploads/tests.py2
-rw-r--r--tests/files/tests.py4
-rw-r--r--tests/forms_tests/models.py4
-rw-r--r--tests/model_fields/models.py2
-rw-r--r--tests/model_forms/models.py2
-rwxr-xr-xtests/runtests.py15
-rw-r--r--tests/staticfiles_tests/tests.py2
11 files changed, 21 insertions, 20 deletions
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
index a031313cf8..e5c49f0144 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
@@ -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()
diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py
index aa5a336f4f..263008aeb5 100644
--- a/tests/admin_views/admin.py
+++ b/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')
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 e9cc9cd8b0..0a9a379247 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 bc96bf3f9b..4208454b68 100644
--- a/tests/model_fields/models.py
+++ b/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')
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 8cabb5ebae..f81fe74c3f 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 = [{
@@ -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():
diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py
index eafbdc9b9f..d8750f9e52 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)