Commit c17fd826 by Frankie Dintino

Add unit tests for compressor implementations

parent 6d3e470d
(function(){(function(){window.concat=function(){console.log(arguments)}})();(function(){window.cat=function(){console.log("hello world")}})()}).call(this);
.concat{display:none}.concatenate{display:block}
\ No newline at end of file
.concat{display:none;}.concatenate{display:block;}
\ No newline at end of file
(function(){(function(){window.concat=function(){console.log(arguments);}}());(function(){window.cat=function(){console.log("hello world");}}());}).call(this);
\ No newline at end of file
(function(){(function(){window.concat=function(){console.log(arguments);};}());(function(){window.cat=function(){console.log("hello world");};}());}).call(this);
\ No newline at end of file
(function(){(function(){window.concat=function(){console.log(arguments)}})();(function(){window.cat=function(){console.log("hello world")}})()}).call(this);
.concat{display:none}.concatenate{display:block}
(function(){(function(){window.concat=function(){console.log(arguments)}})(),function(){window.cat=function(){console.log("hello world")}}()}).call(this);
.concat{display:none}.concatenate{display:block}
\ No newline at end of file
(function(){(function(){window.concat=function(){console.log(arguments)}}());(function(){window.cat=function(){console.log("hello world")}}())}).call(this);
\ No newline at end of file
......@@ -17,6 +17,11 @@
"less": "^2.5.3",
"livescript": "^1.4.0",
"node-sass": "^3.4.2",
"stylus": "^0.53.0"
"stylus": "^0.53.0",
"cssmin": "^0.4.3",
"google-closure-compiler": "^20151216.2.0",
"uglify-js": "^2.6.1",
"yuglify": "^0.1.4",
"yuicompressor": "^2.4.8"
}
}
import glob
import os
import distutils.spawn
......@@ -59,6 +60,8 @@ SECRET_KEY = "django-pipeline"
PIPELINE = {
'PIPELINE_ENABLED': True,
'JS_COMPRESSOR': None,
'CSS_COMPRESSOR': None,
'STYLESHEETS': {
'screen': {
'source_filenames': (
......@@ -122,7 +125,11 @@ PIPELINE = {
NODE_MODULES_PATH = local_path('node_modules')
NODE_BIN_PATH = os.path.join(NODE_MODULES_PATH, '.bin')
NODE_EXE_PATH = distutils.spawn.find_executable('node')
JAVA_EXE_PATH = distutils.spawn.find_executable('java')
CSSTIDY_EXE_PATH = distutils.spawn.find_executable('csstidy')
HAS_NODE = os.path.exists(NODE_BIN_PATH) and NODE_EXE_PATH
HAS_JAVA = bool(JAVA_EXE_PATH)
HAS_CSSTIDY = bool(CSSTIDY_EXE_PATH)
if HAS_NODE:
def node_exe_path(command):
......@@ -139,8 +146,24 @@ if HAS_NODE:
'STYLUS_BINARY': node_exe_path('stylus'),
'LIVE_SCRIPT_BINARY': node_exe_path('lsc'),
'LIVE_SCRIPT_ARGUMENTS': ['--no-header'],
'YUGLIFY_BINARY': node_exe_path('yuglify'),
'UGLIFYJS_BINARY': node_exe_path('uglifyjs'),
'CSSMIN_BINARY': node_exe_path('cssmin'),
})
if HAS_NODE and HAS_JAVA:
PIPELINE.update({
'CLOSURE_BINARY': [
JAVA_EXE_PATH, '-jar',
os.path.join(NODE_MODULES_PATH, 'google-closure-compiler', 'compiler.jar')],
'YUI_BINARY': [
JAVA_EXE_PATH, '-jar',
glob.glob(os.path.join(NODE_MODULES_PATH, 'yuicompressor', 'build', '*.jar'))[0]]
})
if HAS_CSSTIDY:
PIPELINE.update({'CSSTIDY_BINARY': CSSTIDY_EXE_PATH})
TEMPLATE_DIRS = (
local_path('templates'),
)
......
......@@ -11,18 +11,24 @@ try:
except ImportError:
from unittest.mock import patch # noqa
from unittest import skipIf
from unittest import skipIf, skipUnless
from django.conf import settings
from django.test import TestCase
from django.test.client import RequestFactory
from django.utils.encoding import smart_bytes
from pipeline.compressors import Compressor, TEMPLATE_FUNC, \
SubProcessCompressor
from pipeline.compressors import (
Compressor, TEMPLATE_FUNC, SubProcessCompressor)
from pipeline.compressors.yuglify import YuglifyCompressor
from pipeline.collector import default_collector
from tests.utils import _, pipeline_settings
@pipeline_settings(
CSS_COMPRESSOR='pipeline.compressors.yuglify.YuglifyCompressor',
JS_COMPRESSOR='pipeline.compressors.yuglify.YuglifyCompressor')
class CompressorTest(TestCase):
def setUp(self):
self.maxDiff = None
......@@ -186,3 +192,84 @@ class CompressorTest(TestCase):
def tearDown(self):
default_collector.clear()
class CompressorImplementationTest(TestCase):
maxDiff = None
def setUp(self):
self.compressor = Compressor()
default_collector.collect(RequestFactory().get('/'))
def tearDown(self):
default_collector.clear()
def _test_compressor(self, compressor_cls, compress_type, expected_file):
override_settings = {
("%s_COMPRESSOR" % compress_type.upper()): compressor_cls,
}
with pipeline_settings(**override_settings):
if compress_type == 'js':
result = self.compressor.compress_js(
[_('pipeline/js/first.js'), _('pipeline/js/second.js')])
else:
result = self.compressor.compress_css(
[_('pipeline/css/first.css'), _('pipeline/css/second.css')],
os.path.join('pipeline', 'css', os.path.basename(expected_file)))
with self.compressor.storage.open(expected_file) as f:
expected = f.read()
self.assertEqual(smart_bytes(result), expected)
def test_jsmin(self):
self._test_compressor('pipeline.compressors.jsmin.JSMinCompressor',
'js', 'pipeline/compressors/jsmin.js')
def test_slimit(self):
self._test_compressor('pipeline.compressors.slimit.SlimItCompressor',
'js', 'pipeline/compressors/slimit.js')
@skipUnless(settings.HAS_NODE, "requires node")
def test_uglifyjs(self):
self._test_compressor('pipeline.compressors.uglifyjs.UglifyJSCompressor',
'js', 'pipeline/compressors/uglifyjs.js')
@skipUnless(settings.HAS_NODE, "requires node")
def test_yuglify_js(self):
self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor',
'js', 'pipeline/compressors/yuglify.js')
@skipUnless(settings.HAS_NODE, "requires node")
def test_yuglify_css(self):
self._test_compressor('pipeline.compressors.yuglify.YuglifyCompressor',
'css', 'pipeline/compressors/yuglify.css')
@skipUnless(settings.HAS_NODE, "requires node")
def test_cssmin(self):
self._test_compressor('pipeline.compressors.cssmin.CSSMinCompressor',
'css', 'pipeline/compressors/cssmin.css')
@skipUnless(settings.HAS_NODE, "requires node")
@skipUnless(settings.HAS_JAVA, "requires java")
def test_closure(self):
self._test_compressor('pipeline.compressors.closure.ClosureCompressor',
'js', 'pipeline/compressors/closure.js')
@skipUnless(settings.HAS_NODE, "requires node")
@skipUnless(settings.HAS_JAVA, "requires java")
def test_yui_js(self):
self._test_compressor('pipeline.compressors.yui.YUICompressor',
'js', 'pipeline/compressors/yui.js')
@skipUnless(settings.HAS_NODE, "requires node")
@skipUnless(settings.HAS_JAVA, "requires java")
def test_yui_css(self):
self._test_compressor('pipeline.compressors.yui.YUICompressor',
'css', 'pipeline/compressors/yui.css')
@skipUnless(settings.HAS_CSSTIDY, "requires csstidy")
def test_csstidy(self):
self._test_compressor('pipeline.compressors.csstidy.CSSTidyCompressor',
'css', 'pipeline/compressors/csstidy.css')
......@@ -14,6 +14,9 @@ deps =
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
jinja2
jsmin==2.2.0
ply==3.4
slimit==0.8.1
setenv =
DJANGO_SETTINGS_MODULE = tests.settings
PYTHONPATH = {toxinidir}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment