From 7088135f5e31202cac4894cf6d7f3514c4a86d61 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 6 Nov 2012 01:00:28 +0100 Subject: [PATCH] Add tests for webassets: test absolute url and Jinja configuration --- tests/test_generators.py | 85 +++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/tests/test_generators.py b/tests/test_generators.py index 6b714082..8a839ba1 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -8,6 +8,11 @@ from codecs import open from tempfile import mkdtemp from shutil import rmtree +try: + import webassets +except ImportError: + webassets = None + from pelican import Pelican from pelican.generators import ArticlesGenerator, PagesGenerator, \ TemplatePagesGenerator @@ -243,6 +248,7 @@ class TestTemplatePagesGenerator(unittest.TestCase): self.assertEquals(output_file.read(), 'foo: bar') +@unittest.skipUnless(webassets, "webassets isn't installed") @skipIfNoExecutable(['scss', '-v']) @skipIfNoExecutable(['cssmin', '--version']) class TestWebAssets(unittest.TestCase): @@ -250,28 +256,52 @@ class TestWebAssets(unittest.TestCase): scss style.scss style.ref.css """ - def setUp(self): - self.temp_path = mkdtemp() - self.theme_dir = os.path.join(CUR_DIR, 'themes', 'assets') + @classmethod + def setUpClass(cls): + """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'), - 'OUTPUT_PATH': self.temp_path, + 'OUTPUT_PATH': cls.temp_path, 'PLUGINS': ['pelican.plugins.assets', ], - 'THEME': self.theme_dir, + 'THEME': cls.theme_dir, }) - pelican = Pelican(settings=self.settings) + pelican = Pelican(settings=cls.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] + # run Pelican a second time with absolute urls + cls.temp_path2 = mkdtemp() + 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): - rmtree(self.temp_path) + cls.css_ref = open(os.path.join(cls.theme_dir, 'static', 'css', + '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): - "Compare the compiled css with the reference" + """Compare the compiled css with the reference.""" gen_file = os.path.join(self.temp_path, 'theme', 'gen', 'style.{0}.min.css'.format(self.version)) @@ -280,10 +310,29 @@ class TestWebAssets(unittest.TestCase): css_new = open(gen_file).read() self.assertEqual(css_new, self.css_ref) - def test_template(self): - "Look in the output index.html file for the link tag" + def check_link_tag(self, css_file, html_file): + """Check the presence of `css_file` in `html_file`.""" - link_tag = ''.format(self.version) - html = open(os.path.join(self.temp_path, 'index.html')).read() + link_tag = ''.\ + format(css_file=css_file) + html = open(html_file).read() 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))