From 0e389d7afb5b64d8d809707c6888ca7556622c47 Mon Sep 17 00:00:00 2001 From: Jervis Whitley Date: Thu, 24 Nov 2011 16:35:34 +1100 Subject: [PATCH] both precompiled and standard files rendered without compression when COMPRESS_ENABLED==False and COMPRESS_PRECOMPILERS != [] previously only precompiled files (those with a mimetype in the COMPRESS_PRECOMPILERS setting) were rendered when COMPRESS_ENABLED == False. --- compressor/base.py | 8 +++- docs/settings.txt | 29 +++++++++++-- tests/media/js/one.coffee | 1 + tests/tests/__init__.py | 2 +- tests/tests/templatetags.py | 84 ++++++++++++++++++++++++++++++++++++- 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 tests/media/js/one.coffee diff --git a/compressor/base.py b/compressor/base.py index 4c36359..beef25e 100644 --- a/compressor/base.py +++ b/compressor/base.py @@ -215,13 +215,17 @@ class Compressor(object): if not verbatim_content and not rendered_content: return '' + + rendered_output = '\n'.join(c.encode(self.charset) + for c in rendered_content) if settings.COMPRESS_ENABLED or forced: - filtered_content = self.filtered_output( - '\n'.join((c.encode(self.charset) for c in rendered_content))) + filtered_content = self.filtered_output(rendered_output) finished_content = self.handle_output(mode, filtered_content, forced) verbatim_content.append(finished_content) if verbatim_content: + if not settings.COMPRESS_ENABLED and rendered_output: + verbatim_content.append(rendered_output) return '\n'.join(verbatim_content) return self.content diff --git a/docs/settings.txt b/docs/settings.txt index 19b9949..ea92cc8 100644 --- a/docs/settings.txt +++ b/docs/settings.txt @@ -13,9 +13,32 @@ COMPRESS_ENABLED :Default: the opposite of ``DEBUG`` -Boolean that decides if compression will happen. In order to test compression -when ``DEBUG`` is enabled COMPRESS_ENABLED_ needs to explicitly be set to -``True``. +Boolean that decides if compression will happen. To test compression +when ``DEBUG`` is ``True`` COMPRESS_ENABLED_ must also be set to ``True``. + +When COMPRESS_ENABLED_ is ``False`` the input will be rendered without any +compression except for code with a mimetype matching one listed in the +COMPRESS_PRECOMPILERS_ setting. These matching files are still passed to the +precompiler before rendering. + +An example for some javascript and coffeescript. + +.. code-block:: django + + {% load compress %} + + {% compress js %} + + + + .. _compress_url: diff --git a/tests/media/js/one.coffee b/tests/media/js/one.coffee new file mode 100644 index 0000000..57bf896 --- /dev/null +++ b/tests/media/js/one.coffee @@ -0,0 +1 @@ +# this is a comment. diff --git a/tests/tests/__init__.py b/tests/tests/__init__.py index d800c50..7212692 100644 --- a/tests/tests/__init__.py +++ b/tests/tests/__init__.py @@ -5,4 +5,4 @@ from .offline import OfflineGenerationTestCase from .parsers import LxmlParserTests, Html5LibParserTests, BeautifulSoupParserTests, HtmlParserTests from .signals import PostCompressSignalTestCase from .storages import StorageTestCase -from .templatetags import TemplatetagTestCase +from .templatetags import TemplatetagTestCase, PrecompilerTemplatetagTestCase diff --git a/tests/tests/templatetags.py b/tests/tests/templatetags.py index 0f627e1..549bc11 100644 --- a/tests/tests/templatetags.py +++ b/tests/tests/templatetags.py @@ -1,5 +1,8 @@ from __future__ import with_statement +import os +import sys + from mock import Mock from django.template import Template, Context, TemplateSyntaxError @@ -8,7 +11,7 @@ from django.test import TestCase from compressor.conf import settings from compressor.signals import post_compress -from .base import css_tag +from .base import css_tag, test_dir def render(template_string, context_dict=None): @@ -122,3 +125,82 @@ class TemplatetagTestCase(TestCase): args, kwargs = callback.call_args context = kwargs['context'] self.assertEqual('foo', context['name']) + +class PrecompilerTemplatetagTestCase(TestCase): + def setUp(self): + self.old_enabled = settings.COMPRESS_ENABLED + self.old_precompil = settings.COMPRESS_PRECOMPILERS + + precompiler = os.path.join(test_dir, 'precompiler.py') + python = sys.executable + + settings.COMPRESS_ENABLED = True + settings.COMPRESS_PRECOMPILERS = ( + ('text/coffeescript', '%s %s' % (python, precompiler)),) + + self.context = {'MEDIA_URL': settings.COMPRESS_URL} + + def tearDown(self): + settings.COMPRESS_ENABLED = self.old_enabled + settings.COMPRESS_PRECOMPILERS = self.old_precompil + + def test_compress_coffeescript_tag(self): + template = u"""{% load compress %}{% compress js %} + + {% endcompress %}""" + out = script(src="/media/CACHE/js/e920d58f166d.js") + self.assertEqual(out, render(template, self.context)) + + def test_compress_coffeescript_tag_and_javascript_tag(self): + template = u"""{% load compress %}{% compress js %} + + + {% endcompress %}""" + out = script(src="/media/CACHE/js/ef6b32a54575.js") + self.assertEqual(out, render(template, self.context)) + + def test_coffeescript_and_js_tag_with_compress_enabled_equals_false(self): + settings.COMPRESS_ENABLED = False + + template = u"""{% load compress %}{% compress js %} + + + {% endcompress %}""" + out = (script('# this is a comment.\n') + '\n' + + script('# this too is a comment.')) + self.assertEqual(out, render(template, self.context)) + + def test_compress_coffeescript_tag_compress_enabled_is_false(self): + settings.COMPRESS_ENABLED = False + + template = u"""{% load compress %}{% compress js %} + + {% endcompress %}""" + out = script("# this is a comment.\n") + self.assertEqual(out, render(template, self.context)) + + def test_compress_coffeescript_file_tag_compress_enabled_is_false(self): + settings.COMPRESS_ENABLED = False + + template = u""" + {% load compress %}{% compress js %} + + {% endcompress %}""" + out = script(src="/media/CACHE/js/95cfb869eead.js") + self.assertEqual(out, render(template, self.context)) + +def script(content="", src="", scripttype="text/javascript"): + """ + returns a unicode text html script element. + + >>> script('#this is a comment', scripttype="text/applescript") + '' + """ + out_script = u'' % content +