Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #20290 -- Allow override_settings to be nested

Refactored override_settings to store the underlying settings._wrapped
value seen at runtime, not instantiation time.
  • Loading branch information...
commit 552a90b44456f24de228a6c7cd916c040787cf22 1 parent 7314007
@obeattie obeattie authored claudep committed
View
1  AUTHORS
@@ -97,6 +97,7 @@ answer newbie questions, and generally made Django that much better:
Ned Batchelder <http://www.nedbatchelder.com/>
batiste@dosimple.ch
Batman
+ Oliver Beattie <oliver@obeattie.com>
Brian Beck <http://blog.brianbeck.com/>
Shannon -jj Behrens <http://jjinux.blogspot.com/>
Esdras Beleza <linux@esdrasbeleza.com>
View
3  django/test/utils.py
@@ -207,7 +207,6 @@ class override_settings(object):
"""
def __init__(self, **kwargs):
self.options = kwargs
- self.wrapped = settings._wrapped
def __enter__(self):
self.enable()
@@ -246,6 +245,7 @@ def enable(self):
override = UserSettingsHolder(settings._wrapped)
for key, new_value in self.options.items():
setattr(override, key, new_value)
+ self.wrapped = settings._wrapped
settings._wrapped = override
for key, new_value in self.options.items():
setting_changed.send(sender=settings._wrapped.__class__,
@@ -253,6 +253,7 @@ def enable(self):
def disable(self):
settings._wrapped = self.wrapped
+ del self.wrapped
for key in self.options:
new_value = getattr(settings, key, None)
setting_changed.send(sender=settings._wrapped.__class__,
View
5 tests/servers/tests.py
@@ -96,6 +96,11 @@ def setUpClass(cls):
del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']
@classmethod
+ def tearDownClass(cls):
+ # skip it, as setUpClass doesn't call its parent either
+ pass
+
+ @classmethod
def raises_exception(cls, address, exception):
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = address
try:
View
27 tests/settings_tests/tests.py
@@ -9,17 +9,19 @@
from django.utils import unittest, six
-@override_settings(TEST='override')
+@override_settings(TEST='override', TEST_OUTER='outer')
class FullyDecoratedTranTestCase(TransactionTestCase):
available_apps = []
def test_override(self):
self.assertEqual(settings.TEST, 'override')
+ self.assertEqual(settings.TEST_OUTER, 'outer')
@override_settings(TEST='override2')
def test_method_override(self):
self.assertEqual(settings.TEST, 'override2')
+ self.assertEqual(settings.TEST_OUTER, 'outer')
def test_decorated_testcase_name(self):
self.assertEqual(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase')
@@ -168,6 +170,29 @@ def test_override_settings_delete(self):
self.assertRaises(AttributeError, getattr, settings, 'USE_I18N')
self.assertEqual(settings.USE_I18N, previous_i18n)
+ def test_override_settings_nested(self):
+ """
+ Test that override_settings uses the actual _wrapped attribute at
+ runtime, not when it was instantiated.
+ """
+
+ self.assertRaises(AttributeError, getattr, settings, 'TEST')
+ self.assertRaises(AttributeError, getattr, settings, 'TEST2')
+
+ inner = override_settings(TEST2='override')
+ with override_settings(TEST='override'):
+ self.assertEqual('override', settings.TEST)
+ with inner:
+ self.assertEqual('override', settings.TEST)
+ self.assertEqual('override', settings.TEST2)
+ # inner's __exit__ should have restored the settings of the outer
+ # context manager, not those when the class was instantiated
+ self.assertEqual('override', settings.TEST)
+ self.assertRaises(AttributeError, getattr, settings, 'TEST2')
+
+ self.assertRaises(AttributeError, getattr, settings, 'TEST')
+ self.assertRaises(AttributeError, getattr, settings, 'TEST2')
+
def test_allowed_include_roots_string(self):
"""
ALLOWED_INCLUDE_ROOTS is not allowed to be incorrectly set to a string
Please sign in to comment.
Something went wrong with that request. Please try again.