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.
This commit is contained in:
parent
e83aba0e2c
commit
934400759d
|
@ -13,6 +13,7 @@ import shutil
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import django
|
import django
|
||||||
|
@ -27,7 +28,7 @@ from django.utils._os import npath, upath
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.six import StringIO
|
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):
|
if not os.path.exists(test_dir):
|
||||||
os.mkdir(test_dir)
|
os.mkdir(test_dir)
|
||||||
open(os.path.join(test_dir, '__init__.py'), 'w').close()
|
open(os.path.join(test_dir, '__init__.py'), 'w').close()
|
||||||
|
|
|
@ -309,7 +309,7 @@ class OldSubscriberAdmin(admin.ModelAdmin):
|
||||||
actions = None
|
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')
|
UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -316,7 +316,7 @@ class EmptyModel(models.Model):
|
||||||
return "Primary key = %s" % self.id
|
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')
|
UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ Storing files according to a custom storage system
|
||||||
and where files should be stored.
|
and where files should be stored.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import random
|
import random
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@ class OldStyleFSStorage(FileSystemStorage):
|
||||||
return super(OldStyleFSStorage, self).save(name, content)
|
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)
|
temp_storage = FileSystemStorage(location=temp_storage_location)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ from . import uploadhandler
|
||||||
from .models import FileModel
|
from .models import FileModel
|
||||||
|
|
||||||
UNICODE_FILENAME = 'test-0123456789_中文_Orléans.jpg'
|
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')
|
UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -241,8 +241,8 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
||||||
|
|
||||||
class FileMoveSafeTests(unittest.TestCase):
|
class FileMoveSafeTests(unittest.TestCase):
|
||||||
def test_file_move_overwrite(self):
|
def test_file_move_overwrite(self):
|
||||||
handle_a, self.file_a = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
handle_a, self.file_a = tempfile.mkstemp()
|
||||||
handle_b, self.file_b = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
handle_b, self.file_b = tempfile.mkstemp()
|
||||||
|
|
||||||
# file_move_safe should raise an IOError exception if destination file exists and allow_overwrite is False
|
# 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))
|
self.assertRaises(IOError, lambda: file_move_safe(self.file_a, self.file_b, allow_overwrite=False))
|
||||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
|
@ -14,8 +13,7 @@ callable_default_counter = itertools.count()
|
||||||
callable_default = lambda: next(callable_default_counter)
|
callable_default = lambda: next(callable_default_counter)
|
||||||
|
|
||||||
|
|
||||||
temp_storage_location = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
temp_storage = FileSystemStorage(location=tempfile.mkdtemp())
|
||||||
temp_storage = FileSystemStorage(location=temp_storage_location)
|
|
||||||
|
|
||||||
|
|
||||||
class BoundaryModel(models.Model):
|
class BoundaryModel(models.Model):
|
||||||
|
|
|
@ -236,7 +236,7 @@ if Image:
|
||||||
attr_class = TestImageFieldFile
|
attr_class = TestImageFieldFile
|
||||||
|
|
||||||
# Set up a temp directory for file storage.
|
# 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_storage = FileSystemStorage(temp_storage_dir)
|
||||||
temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
|
temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ from django.utils._os import upath
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
from django.utils.six.moves import range
|
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)
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
||||||
|
|
||||||
ARTICLE_STATUS = (
|
ARTICLE_STATUS = (
|
||||||
|
|
|
@ -27,8 +27,11 @@ RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__)))
|
||||||
|
|
||||||
TEMPLATE_DIR = os.path.join(RUNTESTS_DIR, 'templates')
|
TEMPLATE_DIR = os.path.join(RUNTESTS_DIR, 'templates')
|
||||||
|
|
||||||
TEMP_DIR = tempfile.mkdtemp(prefix='django_')
|
# Create a specific subdirectory for the duration of the test suite.
|
||||||
os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR
|
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 = [
|
SUBDIRS_TO_SKIP = [
|
||||||
'data',
|
'data',
|
||||||
|
@ -121,7 +124,7 @@ def setup(verbosity, test_labels):
|
||||||
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
|
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
|
||||||
settings.ROOT_URLCONF = 'urls'
|
settings.ROOT_URLCONF = 'urls'
|
||||||
settings.STATIC_URL = '/static/'
|
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.
|
# Remove the following line in Django 2.0.
|
||||||
settings.TEMPLATE_DIRS = [TEMPLATE_DIR]
|
settings.TEMPLATE_DIRS = [TEMPLATE_DIR]
|
||||||
settings.TEMPLATES = [{
|
settings.TEMPLATES = [{
|
||||||
|
@ -213,13 +216,13 @@ def setup(verbosity, test_labels):
|
||||||
|
|
||||||
def teardown(state):
|
def teardown(state):
|
||||||
try:
|
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
|
# so that it will successfully remove temp trees containing
|
||||||
# non-ASCII filenames on Windows. (We're assuming the temp dir
|
# non-ASCII filenames on Windows. (We're assuming the temp dir
|
||||||
# name itself does not contain non-ASCII characters.)
|
# name itself does not contain non-ASCII characters.)
|
||||||
shutil.rmtree(six.text_type(TEMP_DIR))
|
shutil.rmtree(six.text_type(TMPDIR))
|
||||||
except OSError:
|
except OSError:
|
||||||
print('Failed to remove temp directory: %s' % TEMP_DIR)
|
print('Failed to remove temp directory: %s' % TMPDIR)
|
||||||
|
|
||||||
# Restore the old settings.
|
# Restore the old settings.
|
||||||
for key, value in state.items():
|
for key, value in state.items():
|
||||||
|
|
|
@ -100,7 +100,7 @@ class BaseCollectionTestCase(BaseStaticFilesTestCase):
|
||||||
"""
|
"""
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(BaseCollectionTestCase, self).setUp()
|
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
|
# Override the STATIC_ROOT for all tests from setUp to tearDown
|
||||||
# rather than as a context manager
|
# rather than as a context manager
|
||||||
self.patched_settings = self.settings(STATIC_ROOT=temp_dir)
|
self.patched_settings = self.settings(STATIC_ROOT=temp_dir)
|
||||||
|
|
Loading…
Reference in New Issue