mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Add tests for webassets: test absolute url and Jinja configuration
This commit is contained in:
parent
4c15ec9f86
commit
7088135f5e
1 changed files with 67 additions and 18 deletions
|
|
@ -8,6 +8,11 @@ from codecs import open
|
||||||
from tempfile import mkdtemp
|
from tempfile import mkdtemp
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
|
|
||||||
|
try:
|
||||||
|
import webassets
|
||||||
|
except ImportError:
|
||||||
|
webassets = None
|
||||||
|
|
||||||
from pelican import Pelican
|
from pelican import Pelican
|
||||||
from pelican.generators import ArticlesGenerator, PagesGenerator, \
|
from pelican.generators import ArticlesGenerator, PagesGenerator, \
|
||||||
TemplatePagesGenerator
|
TemplatePagesGenerator
|
||||||
|
|
@ -243,6 +248,7 @@ class TestTemplatePagesGenerator(unittest.TestCase):
|
||||||
self.assertEquals(output_file.read(), 'foo: bar')
|
self.assertEquals(output_file.read(), 'foo: bar')
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(webassets, "webassets isn't installed")
|
||||||
@skipIfNoExecutable(['scss', '-v'])
|
@skipIfNoExecutable(['scss', '-v'])
|
||||||
@skipIfNoExecutable(['cssmin', '--version'])
|
@skipIfNoExecutable(['cssmin', '--version'])
|
||||||
class TestWebAssets(unittest.TestCase):
|
class TestWebAssets(unittest.TestCase):
|
||||||
|
|
@ -250,28 +256,52 @@ class TestWebAssets(unittest.TestCase):
|
||||||
scss style.scss style.ref.css
|
scss style.scss style.ref.css
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
@classmethod
|
||||||
self.temp_path = mkdtemp()
|
def setUpClass(cls):
|
||||||
self.theme_dir = os.path.join(CUR_DIR, 'themes', 'assets')
|
"""Run pelican with two settings (absolute and relative urls)."""
|
||||||
|
|
||||||
self.settings = read_settings(override={
|
cls.theme_dir = os.path.join(CUR_DIR, 'themes', 'assets')
|
||||||
|
|
||||||
|
cls.temp_path = mkdtemp()
|
||||||
|
cls.settings = read_settings(override={
|
||||||
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
|
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
|
||||||
'OUTPUT_PATH': self.temp_path,
|
'OUTPUT_PATH': cls.temp_path,
|
||||||
'PLUGINS': ['pelican.plugins.assets', ],
|
'PLUGINS': ['pelican.plugins.assets', ],
|
||||||
'THEME': self.theme_dir,
|
'THEME': cls.theme_dir,
|
||||||
})
|
})
|
||||||
pelican = Pelican(settings=self.settings)
|
pelican = Pelican(settings=cls.settings)
|
||||||
pelican.run()
|
pelican.run()
|
||||||
|
|
||||||
self.css_ref = open(os.path.join(self.theme_dir, 'static', 'css',
|
# run Pelican a second time with absolute urls
|
||||||
'style.min.css')).read()
|
cls.temp_path2 = mkdtemp()
|
||||||
self.version = hashlib.md5(self.css_ref).hexdigest()[0:8]
|
cls.settings2 = read_settings(override={
|
||||||
|
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
|
||||||
|
'OUTPUT_PATH': cls.temp_path2,
|
||||||
|
'PLUGINS': ['pelican.plugins.assets', ],
|
||||||
|
'THEME': cls.theme_dir,
|
||||||
|
'RELATIVE_URLS': False,
|
||||||
|
'SITEURL': 'http://localhost'
|
||||||
|
})
|
||||||
|
pelican2 = Pelican(settings=cls.settings2)
|
||||||
|
pelican2.run()
|
||||||
|
|
||||||
def tearDown(self):
|
cls.css_ref = open(os.path.join(cls.theme_dir, 'static', 'css',
|
||||||
rmtree(self.temp_path)
|
'style.min.css')).read()
|
||||||
|
cls.version = hashlib.md5(cls.css_ref).hexdigest()[0:8]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
rmtree(cls.temp_path)
|
||||||
|
rmtree(cls.temp_path2)
|
||||||
|
|
||||||
|
def test_jinja2_ext(self):
|
||||||
|
"""Test that the Jinja2 extension was correctly added."""
|
||||||
|
|
||||||
|
from webassets.ext.jinja2 import AssetsExtension
|
||||||
|
self.assertIn(AssetsExtension, self.settings['JINJA_EXTENSIONS'])
|
||||||
|
|
||||||
def test_compilation(self):
|
def test_compilation(self):
|
||||||
"Compare the compiled css with the reference"
|
"""Compare the compiled css with the reference."""
|
||||||
|
|
||||||
gen_file = os.path.join(self.temp_path, 'theme', 'gen',
|
gen_file = os.path.join(self.temp_path, 'theme', 'gen',
|
||||||
'style.{0}.min.css'.format(self.version))
|
'style.{0}.min.css'.format(self.version))
|
||||||
|
|
@ -280,10 +310,29 @@ class TestWebAssets(unittest.TestCase):
|
||||||
css_new = open(gen_file).read()
|
css_new = open(gen_file).read()
|
||||||
self.assertEqual(css_new, self.css_ref)
|
self.assertEqual(css_new, self.css_ref)
|
||||||
|
|
||||||
def test_template(self):
|
def check_link_tag(self, css_file, html_file):
|
||||||
"Look in the output index.html file for the link tag"
|
"""Check the presence of `css_file` in `html_file`."""
|
||||||
|
|
||||||
link_tag = '<link rel="stylesheet" ' + \
|
link_tag = '<link rel="stylesheet" href="{css_file}">'.\
|
||||||
'href="theme/gen/style.{0}.min.css">'.format(self.version)
|
format(css_file=css_file)
|
||||||
html = open(os.path.join(self.temp_path, 'index.html')).read()
|
html = open(html_file).read()
|
||||||
self.assertRegexpMatches(html, link_tag)
|
self.assertRegexpMatches(html, link_tag)
|
||||||
|
|
||||||
|
def test_template(self):
|
||||||
|
"""Look in the output index.html file for the link tag."""
|
||||||
|
|
||||||
|
css_file = 'theme/gen/style.{0}.min.css'.format(self.version)
|
||||||
|
html_files = ['index.html', 'archives.html',
|
||||||
|
'this-is-an-article-with-category.html']
|
||||||
|
for f in html_files:
|
||||||
|
self.check_link_tag(css_file, os.path.join(self.temp_path, f))
|
||||||
|
|
||||||
|
def test_absolute_url(self):
|
||||||
|
"""Look in the output index.html file for the link tag with abs url."""
|
||||||
|
|
||||||
|
css_file = 'http://localhost/theme/gen/style.{0}.min.css'.\
|
||||||
|
format(self.version)
|
||||||
|
html_files = ['index.html', 'archives.html',
|
||||||
|
'this-is-an-article-with-category.html']
|
||||||
|
for f in html_files:
|
||||||
|
self.check_link_tag(css_file, os.path.join(self.temp_path2, f))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue