Files are not always available from storage despite DEBUG=False

For example, in tests. So instead of switching on DEBUG, we first try to
find a file from the finders, and then fall back to storage if that
doesn't work.
This commit is contained in:
Gavin Wahl
2015-04-23 14:25:40 -06:00
parent a91323b135
commit 9093679d35
3 changed files with 13 additions and 9 deletions

View File

@@ -1,7 +1,6 @@
import fnmatch import fnmatch
import os import os
from django.conf import settings
from django.contrib.staticfiles import finders from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
@@ -40,8 +39,10 @@ def get_file_from_finders(filename):
def get_file_and_storage(filename): def get_file_and_storage(filename):
# TODO: the switch probably shouldn't be on DEBUG name, storage = get_file_from_finders(filename)
if settings.DEBUG: # get_file_from_finders could fail in production if code is a deployed as a
return get_file_from_finders(filename) # package without it's package_data. In that case, we'd assume that
else: # collectstatic had been run and we can get the file from storage.
return get_file_from_storage(filename) if storage is None:
name, storage = get_file_from_storage(filename)
return name, storage

View File

@@ -23,6 +23,7 @@ tests_require = [
'Pillow', 'Pillow',
'django-compressor>=1.3', 'django-compressor>=1.3',
'django-discover-runner', 'django-discover-runner',
'mock',
] ]

View File

@@ -1,8 +1,8 @@
import os import os
import re import re
import mock
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
from django.conf import settings from django.conf import settings
from scss.errors import SassImportError from scss.errors import SassImportError
@@ -99,12 +99,14 @@ class ImportTestMixin(CompilerTestMixin):
self.assertEqual(clean_css(actual), clean_css(PATH_CONFLICT_CONTENTS)) self.assertEqual(clean_css(actual), clean_css(PATH_CONFLICT_CONTENTS))
@override_settings(DEBUG=True)
class FindersImportTest(ImportTestMixin, NoCollectStaticTestCase): class FindersImportTest(ImportTestMixin, NoCollectStaticTestCase):
pass pass
@override_settings(DEBUG=False) # Emulate the condition were collectstatic was run but the source files are no
# longer available.
@mock.patch('django_pyscss.utils.get_file_from_finders',
new=mock.MagicMock(return_value=(None, None)))
class StorageImportTest(ImportTestMixin, CollectStaticTestCase): class StorageImportTest(ImportTestMixin, CollectStaticTestCase):
pass pass