Skip to content

Commit ff481de

Browse files
bradcyberdelia
authored andcommitted
Allow to work with storages not implementing the path method.
1 parent 1f78607 commit ff481de

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

pipeline/compilers/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
except ImportError:
88
from pipes import quote
99

10+
from django.contrib.staticfiles import finders
1011
from django.contrib.staticfiles.storage import staticfiles_storage
1112
from django.core.files.base import ContentFile
1213
from django.utils.encoding import smart_str, smart_bytes
@@ -33,7 +34,10 @@ def _compile(input_path):
3334
compiler = compiler(verbose=self.verbose, storage=self.storage)
3435
if compiler.match_file(input_path):
3536
output_path = self.output_path(input_path, compiler.output_extension)
36-
infile = self.storage.path(input_path)
37+
try:
38+
infile = self.storage.path(input_path)
39+
except NotImplementedError:
40+
infile = finders.find(input_path)
3741
outfile = self.output_path(infile, compiler.output_extension)
3842
outdated = compiler.is_outdated(input_path, output_path)
3943
try:

tests/tests/test_storage.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
from __future__ import unicode_literals
22

3+
from django.contrib.staticfiles.storage import staticfiles_storage
4+
from django.core.management import call_command
35
from django.test import TestCase
6+
from django.test.utils import override_settings
47

58
from pipeline.storage import PipelineStorage
69

10+
from tests.tests.test_compiler import DummyCompiler
711
from tests.utils import pipeline_settings
812

13+
try:
14+
from io import StringIO
15+
except ImportError:
16+
from StringIO import StringIO
17+
18+
19+
class PipelineNoPathStorage(PipelineStorage):
20+
"""Storage without an implemented path method"""
21+
def path(self, *args):
22+
raise NotImplementedError()
23+
24+
def delete(self, *args):
25+
return
26+
27+
def exists(self, *args):
28+
return True
29+
30+
def save(self, *args):
31+
return
32+
33+
def open(self, *args):
34+
return StringIO()
35+
36+
37+
class DummyCSSCompiler(DummyCompiler):
38+
""" Handles css files """
39+
output_extension = 'css'
40+
41+
def match_file(self, path):
42+
return path.endswith('.css')
43+
944

1045
class StorageTest(TestCase):
46+
def tearDown(self):
47+
staticfiles_storage._setup()
48+
1149
def test_post_process_dry_run(self):
1250
with pipeline_settings(PIPELINE_JS_COMPRESSOR=None, PIPELINE_CSS_COMPRESSOR=None):
1351
processed_files = PipelineStorage().post_process({}, True)
@@ -19,3 +57,15 @@ def test_post_process(self):
1957
processed_files = storage.post_process({})
2058
self.assertTrue(('screen.css', 'screen.css', True) in processed_files)
2159
self.assertTrue(('scripts.js', 'scripts.js', True) in processed_files)
60+
61+
def test_post_process_no_path(self):
62+
"""
63+
Test post_process with a storage that doesn't implement the path method.
64+
"""
65+
with override_settings(STATICFILES_STORAGE='tests.tests.test_storage.PipelineNoPathStorage', PIPELINE_COMPILERS=['tests.tests.test_storage.DummyCSSCompiler']):
66+
with pipeline_settings(PIPELINE_JS_COMPRESSOR=None, PIPELINE_CSS_COMPRESSOR=None):
67+
staticfiles_storage._setup()
68+
try:
69+
call_command('collectstatic', verbosity=0, interactive=False)
70+
except NotImplementedError:
71+
self.fail('Received an error running collectstatic')

0 commit comments

Comments
 (0)