django-pipeline
django-pipeline copied to clipboard
PipelineCachedStorage breaks app tests
I made a test app to highlight this bug: https://github.com/amarandon/trypipeline/tree/with-pipeline
I have a trivial test:
from django.test import TestCase
class TestHome(TestCase):
def test_home(self):
response = self.client.get('/')
self.assertContains(response, 'css')
As soon as I add this to my settings:
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
The test breaks with this error:
Traceback (most recent call last):
File "/home/al/projects/trypipeline/main/tests.py", line 7, in test_home
response = self.client.get('/')
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/test/client.py", line 473, in get
response = super(Client, self).get(path, data=data, **extra)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/test/client.py", line 280, in get
return self.request(**r)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/utils/six.py", line 491, in reraise
raise value
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/al/projects/trypipeline/main/views.py", line 5, in home
return render(request, 'main/home.html')
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/shortcuts/__init__.py", line 53, in render
return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/template/loader.py", line 169, in render_to_string
return t.render(context_instance)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/test/utils.py", line 85, in instrumented_test_render
return self.nodelist.render(context)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/templatetags/static.py", line 106, in render
url = self.url(context)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py", line 12, in url
return staticfiles_storage.url(path)
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/contrib/staticfiles/storage.py", line 134, in url
hashed_name = self.hashed_name(clean_name).replace('\\', '/')
File "/home/al/projects/env/dummy/lib/python3.3/site-packages/django/contrib/staticfiles/storage.py", line 91, in hashed_name
(clean_name, self))
ValueError: The file 'main/css/style.css' could not be found with <pipeline.storage.PipelineCachedStorage object at 0x7f853a37b850>.
Executing collectstatic
solves this error. This is confusing because tests usually don't require to run collectstatic
. If this hard to fix in the code, we should probably mention this issue in the documentation with possible workarounds such as executing collectstatic
before running tests or running test with settings that don't enable PipelineCachedStorage. If this is the way to go then I'll be happy to do a doc PR.
Tried against the master branch, commit 68646764b68a43e6136254c3aa5c27e2ad50f008 Django 1.6, Python 3.3.1
/sub
You might want to try to add this finder 'pipeline.finders.PipelineFinder'
to STATICFILES_FINDERS
. See the documentation.
No more luck I'm afraid. As per commit https://github.com/amarandon/trypipeline/commit/30de829b11305584319512e2a2ce015e558a0ce7 the problem remains.
I'm experiencing this issue too in django 1.7a2, but have noted that it only occurs with pipeline.storage.PipelineCachedStorage
(having included both pipeline.finders.CachedFileFinder
and pipeline.finders.PipelineFinder
in STATICFILES_FINDERS).
Setting STATICFILES_STORAGE=pipeline.storage.PipelineStorage
works.
I too am running into this issue. I've added the following to my settings.py
to workaround this issue.
if DEBUG:
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
else:
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
I use py.test, so my workaround was put this in conftest.py as a workaround:
''' import pytest from django.conf import settings
def pytest_configure(): # workaround to avoid django pipeline issue # refers to settings.STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage' '''
So I can use this option only for testing, for all tests.
I think I'm also seeing this - Failing tests even when using 'pipeline.finders.CachedFileFinder'
It appears to be partially related to if STATIC_ROOT
is empty or not. I had the same problem after I changed the default storage PipelineCachedStorage
and had to use the work-around by @jdufresne
Workaround worked for me
Perfect! @jdufresne