Unit tests for webassets

remove the unit tests for the lesscss generator (TestLessCSSGenerator class) and
replace it with tests for webassets:

- add a sample scss file
- run pelican with example files
- compare the generated css file with a reference one
- look in the html code for the correct link tag
This commit is contained in:
Simon 2012-10-24 22:51:45 +02:00
commit b66a37ccb1
4 changed files with 68 additions and 44 deletions

View file

@ -1,15 +1,18 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from mock import MagicMock from mock import MagicMock
import hashlib
import os import os
import re
from codecs import open
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
from pelican.generators import ArticlesGenerator, LessCSSGenerator, \ from pelican import Pelican
PagesGenerator, TemplatePagesGenerator from pelican.generators import ArticlesGenerator, PagesGenerator, \
TemplatePagesGenerator
from pelican.writers import Writer from pelican.writers import Writer
from pelican.settings import _DEFAULT_CONFIG from pelican.settings import _DEFAULT_CONFIG, read_settings
from .support import unittest, skipIfNoExecutable from .support import unittest, skipIfNoExecutable
CUR_DIR = os.path.dirname(__file__) CUR_DIR = os.path.dirname(__file__)
@ -240,53 +243,47 @@ class TestTemplatePagesGenerator(unittest.TestCase):
self.assertEquals(output_file.read(), 'foo: bar') self.assertEquals(output_file.read(), 'foo: bar')
class TestLessCSSGenerator(unittest.TestCase): @skipIfNoExecutable(['scss', '-v'])
@skipIfNoExecutable(['cssmin', '--version'])
LESS_CONTENT = """ class TestWebAssets(unittest.TestCase):
@color: #4D926F; """
scss style.scss style.ref.css
#header {
color: @color;
}
h2 {
color: @color;
}
""" """
def setUp(self): def setUp(self):
self.temp_content = mkdtemp() self.temp_path = mkdtemp()
self.temp_output = mkdtemp() self.theme_dir = os.path.join(CUR_DIR, 'themes', 'assets')
self.settings = read_settings(override={
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
'OUTPUT_PATH': self.temp_path,
'WEBASSETS': True,
'THEME': self.theme_dir,
})
pelican = Pelican(settings=self.settings)
pelican.run()
self.css_ref = open(os.path.join(self.theme_dir, 'static', 'css',
'style.min.css')).read()
self.version = hashlib.md5(self.css_ref).hexdigest()[0:8]
def tearDown(self): def tearDown(self):
rmtree(self.temp_content) rmtree(self.temp_path)
rmtree(self.temp_output)
@skipIfNoExecutable('lessc') def test_compilation(self):
def test_less_compiler(self): "Compare the compiled css with the reference"
settings = _DEFAULT_CONFIG.copy() gen_file = os.path.join(self.temp_path, 'theme', 'gen',
settings['STATIC_PATHS'] = ['static'] 'style.{0}.min.css'.format(self.version))
settings['LESS_GENERATOR'] = True
generator = LessCSSGenerator(None, settings, self.temp_content, self.assertTrue(os.path.isfile(gen_file))
_DEFAULT_CONFIG['THEME'], self.temp_output, None) css_new = open(gen_file).read()
self.assertEqual(css_new, self.css_ref)
# create a dummy less file def test_template(self):
less_dir = os.path.join(self.temp_content, 'static', 'css') "Look in the output index.html file for the link tag"
less_filename = os.path.join(less_dir, 'test.less')
less_output = os.path.join(self.temp_output, 'static', 'css', link_tag = '<link rel="stylesheet" ' + \
'test.css') 'href="theme/gen/style.{0}.min.css">'.format(self.version)
html = open(os.path.join(self.temp_path, 'index.html')).read()
os.makedirs(less_dir) self.assertRegexpMatches(html, link_tag)
with open(less_filename, 'w') as less_file:
less_file.write(self.LESS_CONTENT)
generator.generate_output()
# we have the file ?
self.assertTrue(os.path.exists(less_output))
# was it compiled ?
self.assertIsNotNone(re.search(r'^\s+color:\s*#4D926F;$',
open(less_output).read(), re.MULTILINE | re.IGNORECASE))

View file

@ -0,0 +1 @@
body{font:14px/1.5 "Droid Sans",sans-serif;background-color:#e4e4e4;color:#242424}a{color:red}a:hover{color:orange}

View file

@ -0,0 +1,19 @@
/* -*- scss-compile-at-save: nil -*- */
$baseFontFamily : "Droid Sans", sans-serif;
$textColor : #242424;
$bodyBackground : #e4e4e4;
body {
font: 14px/1.5 $baseFontFamily;
background-color: $bodyBackground;
color: $textColor;
}
a {
color: red;
&:hover {
color: orange;
}
}

View file

@ -0,0 +1,7 @@
{% extends "!simple/base.html" %}
{% block head %}
{% assets filters="scss,cssmin", output="gen/style.%(version)s.min.css", "css/style.scss" %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
{% endblock %}