Fixed #27057 -- Made setup_test_environment() store saved settings in one place.

This commit is contained in:
Chris Jerdonek 2016-08-13 07:56:57 -07:00 committed by Tim Graham
parent df92f6f2e3
commit 48ede58971
1 changed files with 26 additions and 25 deletions

View File

@ -15,7 +15,6 @@ from django.core import mail
from django.core.signals import request_started
from django.db import reset_queries
from django.db.models.options import Options
from django.http import request
from django.template import Template
from django.test.signals import setting_changed, template_rendered
from django.urls import get_script_prefix, set_script_prefix
@ -24,6 +23,12 @@ from django.utils.decorators import available_attrs
from django.utils.encoding import force_str
from django.utils.translation import deactivate
if six.PY3:
from types import SimpleNamespace
else:
class SimpleNamespace(object):
pass
try:
import jinja2
except ImportError:
@ -94,7 +99,7 @@ def instrumented_test_render(self, context):
return self.nodelist.render(context)
class _SavedSettings(object):
class _TestState(object):
pass
@ -103,7 +108,7 @@ def setup_test_environment(debug=None):
Perform global pre-test setup, such as installing the instrumented template
renderer and setting the email backend to the locmem email backend.
"""
if hasattr(_SavedSettings, 'debug'):
if hasattr(_TestState, 'saved_data'):
# Executing this function twice would overwrite the saved values.
raise RuntimeError(
"setup_test_environment() was already called and can't be called "
@ -113,22 +118,22 @@ def setup_test_environment(debug=None):
if debug is None:
debug = settings.DEBUG
_SavedSettings.debug = settings.DEBUG
settings.DEBUG = debug
saved_data = SimpleNamespace()
_TestState.saved_data = saved_data
Template._original_render = Template._render
Template._render = instrumented_test_render
# Storing previous values in the settings module itself is problematic.
# Store them in arbitrary (but related) modules instead. See #20636.
mail._original_email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
request._original_allowed_hosts = settings.ALLOWED_HOSTS
saved_data.allowed_hosts = settings.ALLOWED_HOSTS
# Add the default host of the test client.
settings.ALLOWED_HOSTS = settings.ALLOWED_HOSTS + ['testserver']
saved_data.debug = settings.DEBUG
settings.DEBUG = debug
saved_data.email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
saved_data.template_render = Template._render
Template._render = instrumented_test_render
mail.outbox = []
deactivate()
@ -139,18 +144,14 @@ def teardown_test_environment():
Perform any global post-test teardown, such as restoring the original
template renderer and restoring the email sending functions.
"""
settings.DEBUG = _SavedSettings.debug
del _SavedSettings.debug
saved_data = _TestState.saved_data
Template._render = Template._original_render
del Template._original_render
settings.EMAIL_BACKEND = mail._original_email_backend
del mail._original_email_backend
settings.ALLOWED_HOSTS = request._original_allowed_hosts
del request._original_allowed_hosts
settings.ALLOWED_HOSTS = saved_data.allowed_hosts
settings.DEBUG = saved_data.debug
settings.EMAIL_BACKEND = saved_data.email_backend
Template._render = saved_data.template_render
del _TestState.saved_data
del mail.outbox