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' get_filename('css/one.css') -> '/full/path/to/static/css/one.css'
""" """
filename = None 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: if not settings.DEBUG:
# first try finding the file in the root
try: try:
# call path first so remote storages don't make it to exists, # call path first so remote storages don't make it to exists,
# which would cause network I/O # which would cause network I/O
@@ -129,7 +131,7 @@ class Compressor(object):
# remote storages don't implement path, access the file locally # remote storages don't implement path, access the file locally
if compressor_file_storage.exists(basename): if compressor_file_storage.exists(basename):
filename = compressor_file_storage.path(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: if not filename and self.finders:
filename = self.finders.find(url2pathname(basename)) filename = self.finders.find(url2pathname(basename))
if filename: if filename:

View File

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