1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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:
Aymeric Augustin
2015-02-21 18:56:36 +01:00
committed by Aymeric Augustin
parent e83aba0e2c
commit 934400759d
11 changed files with 21 additions and 20 deletions

View File

@@ -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():