From 7ff0d0e6867abbc0f35afb4cc7404ec4da17bc84 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2012 01:00:44 +0100 Subject: [PATCH] Move webassets tests in their own file. --- tests/support.py | 20 ++++++-- tests/test_generators.py | 101 +-------------------------------------- tests/test_webassets.py | 98 +++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 103 deletions(-) create mode 100644 tests/test_webassets.py diff --git a/tests/support.py b/tests/support.py index 13bbfb15..52a410ff 100644 --- a/tests/support.py +++ b/tests/support.py @@ -3,11 +3,12 @@ __all__ = [ 'unittest', ] -import os -import subprocess -import re -import sys import cStringIO +import importlib +import os +import re +import subprocess +import sys from functools import wraps from contextlib import contextmanager @@ -138,3 +139,14 @@ def skipIfNoExecutable(executable): return unittest.skip('{0} executable not found'.format(executable)) return lambda func: func + + +def module_exists(module_name): + """Test if a module is importable.""" + + try: + importlib.import_module(module_name) + except ImportError: + return False + else: + return True diff --git a/tests/test_generators.py b/tests/test_generators.py index 8a839ba1..95a55cdb 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -1,24 +1,17 @@ # -*- coding: utf-8 -*- from mock import MagicMock -import hashlib import os 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 from pelican.writers import Writer -from pelican.settings import _DEFAULT_CONFIG, read_settings -from .support import unittest, skipIfNoExecutable +from pelican.settings import _DEFAULT_CONFIG +from .support import unittest CUR_DIR = os.path.dirname(__file__) @@ -246,93 +239,3 @@ class TestTemplatePagesGenerator(unittest.TestCase): # output content is correct with open(output_filename, 'r') as output_file: self.assertEquals(output_file.read(), 'foo: bar') - - -@unittest.skipUnless(webassets, "webassets isn't installed") -@skipIfNoExecutable(['scss', '-v']) -@skipIfNoExecutable(['cssmin', '--version']) -class TestWebAssets(unittest.TestCase): - """ - scss style.scss style.ref.css - """ - - @classmethod - def setUpClass(cls): - """Run pelican with two settings (absolute and relative urls).""" - - 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': cls.temp_path, - 'PLUGINS': ['pelican.plugins.assets', ], - 'THEME': cls.theme_dir, - }) - pelican = Pelican(settings=cls.settings) - pelican.run() - - # 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() - - 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.""" - - gen_file = os.path.join(self.temp_path, 'theme', 'gen', - 'style.{0}.min.css'.format(self.version)) - - self.assertTrue(os.path.isfile(gen_file)) - css_new = open(gen_file).read() - self.assertEqual(css_new, self.css_ref) - - def check_link_tag(self, css_file, html_file): - """Check the presence of `css_file` in `html_file`.""" - - 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)) diff --git a/tests/test_webassets.py b/tests/test_webassets.py new file mode 100644 index 00000000..0f7a59ce --- /dev/null +++ b/tests/test_webassets.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- + +import hashlib +import os +from codecs import open +from tempfile import mkdtemp +from shutil import rmtree + +from pelican import Pelican +from pelican.settings import read_settings +from .support import unittest, skipIfNoExecutable, module_exists + +CUR_DIR = os.path.dirname(__file__) + + +@unittest.skipUnless(module_exists('webassets'), "webassets isn't installed") +@skipIfNoExecutable(['scss', '-v']) +@skipIfNoExecutable(['cssmin', '--version']) +class TestWebAssets(unittest.TestCase): + + def setUp(self): + """Run pelican with two settings (absolute and relative urls).""" + + self.theme_dir = os.path.join(CUR_DIR, 'themes', 'assets') + + self.temp_path = mkdtemp() + self.settings = read_settings(override={ + 'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'), + 'OUTPUT_PATH': self.temp_path, + 'PLUGINS': ['pelican.plugins.assets', ], + 'THEME': self.theme_dir, + }) + pelican = Pelican(settings=self.settings) + pelican.run() + + # run Pelican a second time with absolute urls + self.temp_path2 = mkdtemp() + self.settings2 = read_settings(override={ + 'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'), + 'OUTPUT_PATH': self.temp_path2, + 'PLUGINS': ['pelican.plugins.assets', ], + 'THEME': self.theme_dir, + 'RELATIVE_URLS': False, + 'SITEURL': 'http://localhost' + }) + pelican2 = Pelican(settings=self.settings2) + pelican2.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): + rmtree(self.temp_path) + rmtree(self.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.""" + + gen_file = os.path.join(self.temp_path, 'theme', 'gen', + 'style.{0}.min.css'.format(self.version)) + + self.assertTrue(os.path.isfile(gen_file)) + css_new = open(gen_file).read() + self.assertEqual(css_new, self.css_ref) + + def check_link_tag(self, css_file, html_file): + """Check the presence of `css_file` in `html_file`.""" + + 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))