sanitize settings overriding in CompressorInDebugModeTestCase

also, tweak and add comments.
This commit is contained in:
Johannes Linke
2015-09-15 18:42:57 +02:00
parent 503c3c8c99
commit 5dff028e8c
2 changed files with 21 additions and 16 deletions

View File

@@ -117,8 +117,10 @@ class Compressor(object):
get_filename('css/one.css') -> '/full/path/to/static/css/one.css'
"""
filename = None
# First try finding the file using the storage class.
# This is skipped in DEBUG mode as files might be outdated in
# compressor's final destination (COMPRESS_ROOT) during development
if not settings.DEBUG:
# first try finding the file in the root
try:
# call path first so remote storages don't make it to exists,
# which would cause network I/O
@@ -129,7 +131,7 @@ class Compressor(object):
# remote storages don't implement path, access the file locally
if compressor_file_storage.exists(basename):
filename = compressor_file_storage.path(basename)
# secondly try to find it with staticfiles (and always in debug mode)
# secondly try to find it with staticfiles
if not filename and self.finders:
filename = self.finders.find(url2pathname(basename))
if filename:

View File

@@ -345,27 +345,30 @@ class CacheTestCase(SimpleTestCase):
class CompressorInDebugModeTestCase(SimpleTestCase):
def setUp(self):
settings.COMPRESS_ENABLED = True
settings.COMPRESS_PRECOMPILERS = ()
settings.COMPRESS_DEBUG_TOGGLE = 'nocompress'
settings.DEBUG = True
self.css = '<link rel="stylesheet" href="/static/css/one.css" type="text/css" />'
self.tmpdir = mkdtemp()
copytree(settings.STATIC_ROOT, os.path.join(self.tmpdir, "static"))
self.old_compress_root = settings.COMPRESS_ROOT
settings.STATIC_ROOT = settings.COMPRESS_ROOT = os.path.join(self.tmpdir, "static")
settings.STATICFILES_DIRS = [self.old_compress_root]
new_static_root = os.path.join(self.tmpdir, "static")
copytree(settings.STATIC_ROOT, new_static_root)
self.override_settings = self.settings(
COMPRESS_ENABLED=True,
COMPRESS_PRECOMPILERS=(),
COMPRESS_DEBUG_TOGGLE='nocompress',
DEBUG=True,
STATIC_ROOT=new_static_root,
COMPRESS_ROOT=new_static_root,
STATICFILES_DIRS=[settings.COMPRESS_ROOT]
)
self.override_settings.__enter__()
def tearDown(self):
rmtree(self.tmpdir)
settings.DEBUG = False
settings.STATIC_ROOT = settings.COMPRESS_ROOT = self.old_compress_root
delattr(settings, "STATICFILES_DIRS")
self.override_settings.__exit__(None, None, None)
def test_filename_in_debug_mode(self):
# In debug mode, files should always be compressed based on the app's
# static directory, not the global static directory, where files can
# be outdated
# In debug mode, compressor should look for files using staticfiles
# finders only, and not look into the global static directory, where
# files can be outdated
css_filename = os.path.join(settings.COMPRESS_ROOT, "css", "one.css")
# Store the hash of the original file's content
css_content = open(css_filename).read()