From a24ffa52d02998f5082fbc5a461ccdc8004db4a6 Mon Sep 17 00:00:00 2001
From: Claude Paroz <claude@2xlibre.net>
Date: Sat, 27 Oct 2012 22:38:15 +0200
Subject: [PATCH] [1.5.x] Fixed #17744 -- Reset default file storage with
 setting_changed signal

Backport of 9a0285134 from master.
---
 django/test/signals.py                        |  7 +++++++
 docs/topics/testing.txt                       | 19 ++++++++++---------
 .../staticfiles_tests/tests.py                |  9 ++-------
 3 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/django/test/signals.py b/django/test/signals.py
index d140304f1d..a96bdff3b3 100644
--- a/django/test/signals.py
+++ b/django/test/signals.py
@@ -5,6 +5,7 @@ from django.conf import settings
 from django.db import connections
 from django.dispatch import receiver, Signal
 from django.utils import timezone
+from django.utils.functional import empty
 
 template_rendered = Signal(providing_args=["template", "context"])
 
@@ -72,3 +73,9 @@ def language_changed(**kwargs):
         trans_real._default = None
         if kwargs['setting'] == 'LOCALE_PATHS':
             trans_real._translations = {}
+
+@receiver(setting_changed)
+def file_storage_changed(**kwargs):
+    if kwargs['setting'] in ('MEDIA_ROOT', 'DEFAULT_FILE_STORAGE'):
+        from django.core.files.storage import default_storage
+        default_storage._wrapped = empty
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index 7c25a8b3ff..f5fd4fe3e6 100644
--- a/docs/topics/testing.txt
+++ b/docs/topics/testing.txt
@@ -1594,15 +1594,16 @@ callbacks to clean up and otherwise reset state when settings are changed.
 
 Django itself uses this signal to reset various data:
 
-=========================== ========================
-Overriden settings          Data reset
-=========================== ========================
-USE_TZ, TIME_ZONE           Databases timezone
-TEMPLATE_CONTEXT_PROCESSORS Context processors cache
-TEMPLATE_LOADERS            Template loaders cache
-SERIALIZATION_MODULES       Serializers cache
-LOCALE_PATHS, LANGUAGE_CODE Default translation and loaded translations
-=========================== ========================
+================================ ========================
+Overriden settings               Data reset
+================================ ========================
+USE_TZ, TIME_ZONE                Databases timezone
+TEMPLATE_CONTEXT_PROCESSORS      Context processors cache
+TEMPLATE_LOADERS                 Template loaders cache
+SERIALIZATION_MODULES            Serializers cache
+LOCALE_PATHS, LANGUAGE_CODE      Default translation and loaded translations
+MEDIA_ROOT, DEFAULT_FILE_STORAGE Default file storage
+================================ ========================
 
 Emptying the test outbox
 ~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
index 7ecbccc448..69e30613a8 100644
--- a/tests/regressiontests/staticfiles_tests/tests.py
+++ b/tests/regressiontests/staticfiles_tests/tests.py
@@ -12,7 +12,6 @@ from django.template import loader, Context
 from django.conf import settings
 from django.core.cache.backends.base import BaseCache
 from django.core.exceptions import ImproperlyConfigured
-from django.core.files.storage import default_storage
 from django.core.management import call_command
 from django.test import TestCase
 from django.test.utils import override_settings
@@ -48,10 +47,9 @@ class BaseStaticFilesTestCase(object):
     Test case with a couple utility assertions.
     """
     def setUp(self):
-        # Clear the cached default_storage out, this is because when it first
-        # gets accessed (by some other test), it evaluates settings.MEDIA_ROOT,
+        # Clear the cached staticfiles_storage out, this is because when it first
+        # gets accessed (by some other test), it evaluates settings.STATIC_ROOT,
         # since we're planning on changing that we need to clear out the cache.
-        default_storage._wrapped = empty
         storage.staticfiles_storage._wrapped = empty
         # Clear the cached staticfile finders, so they are reinitialized every
         # run and pick up changes in settings.STATICFILES_DIRS.
@@ -709,9 +707,6 @@ class TestMiscFinder(TestCase):
     """
     A few misc finder tests.
     """
-    def setUp(self):
-        default_storage._wrapped = empty
-
     def test_get_finder(self):
         self.assertIsInstance(finders.get_finder(
             'django.contrib.staticfiles.finders.FileSystemFinder'),