test_assets.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # from __future__ import unicode_literals
  3. import hashlib
  4. import locale
  5. import os
  6. from codecs import open
  7. from tempfile import mkdtemp
  8. from shutil import rmtree
  9. import unittest
  10. import subprocess
  11. from pelican import Pelican
  12. from pelican.settings import read_settings
  13. from pelican.tests.support import mute, skipIfNoExecutable, module_exists
  14. CUR_DIR = os.path.dirname(__file__)
  15. THEME_DIR = os.path.join(CUR_DIR, 'test_data')
  16. CSS_REF = open(os.path.join(THEME_DIR, 'static', 'css',
  17. 'style.min.css')).read()
  18. CSS_HASH = hashlib.md5(CSS_REF).hexdigest()[0:8]
  19. @unittest.skipUnless(module_exists('webassets'), "webassets isn't installed")
  20. @skipIfNoExecutable(['scss', '-v'])
  21. @skipIfNoExecutable(['cssmin', '--version'])
  22. class TestWebAssets(unittest.TestCase):
  23. """Base class for testing webassets."""
  24. def setUp(self, override=None):
  25. import assets
  26. self.temp_path = mkdtemp(prefix='pelicantests.')
  27. settings = {
  28. 'PATH': os.path.join(os.path.dirname(CUR_DIR), 'test_data', 'content'),
  29. 'OUTPUT_PATH': self.temp_path,
  30. 'PLUGINS': [assets],
  31. 'THEME': THEME_DIR,
  32. 'LOCALE': locale.normalize('en_US'),
  33. 'CACHE_CONTENT': False
  34. }
  35. if override:
  36. settings.update(override)
  37. self.settings = read_settings(override=settings)
  38. pelican = Pelican(settings=self.settings)
  39. mute(True)(pelican.run)()
  40. def tearDown(self):
  41. rmtree(self.temp_path)
  42. def check_link_tag(self, css_file, html_file):
  43. """Check the presence of `css_file` in `html_file`."""
  44. link_tag = ('<link rel="stylesheet" href="{css_file}">'
  45. .format(css_file=css_file))
  46. html = open(html_file).read()
  47. self.assertRegexpMatches(html, link_tag)
  48. class TestWebAssetsRelativeURLS(TestWebAssets):
  49. """Test pelican with relative urls."""
  50. def setUp(self):
  51. TestWebAssets.setUp(self, override={'RELATIVE_URLS': True})
  52. def test_jinja2_ext(self):
  53. # Test that the Jinja2 extension was correctly added.
  54. from webassets.ext.jinja2 import AssetsExtension
  55. self.assertIn(AssetsExtension, self.settings['JINJA_ENVIRONMENT']['extensions'])
  56. def test_compilation(self):
  57. # Compare the compiled css with the reference.
  58. gen_file = os.path.join(self.temp_path, 'theme', 'gen',
  59. 'style.{0}.min.css'.format(CSS_HASH))
  60. self.assertTrue(os.path.isfile(gen_file))
  61. css_new = open(gen_file).read()
  62. self.assertEqual(css_new, CSS_REF)
  63. def test_template(self):
  64. # Look in the output files for the link tag.
  65. css_file = './theme/gen/style.{0}.min.css'.format(CSS_HASH)
  66. html_files = ['index.html', 'archives.html',
  67. 'this-is-a-super-article.html']
  68. for f in html_files:
  69. self.check_link_tag(css_file, os.path.join(self.temp_path, f))
  70. self.check_link_tag(
  71. '../theme/gen/style.{0}.min.css'.format(CSS_HASH),
  72. os.path.join(self.temp_path, 'category/yeah.html'))
  73. class TestWebAssetsAbsoluteURLS(TestWebAssets):
  74. """Test pelican with absolute urls."""
  75. def setUp(self):
  76. TestWebAssets.setUp(self, override={'RELATIVE_URLS': False,
  77. 'SITEURL': 'http://localhost'})
  78. def test_absolute_url(self):
  79. # Look in the output files for the link tag with absolute url.
  80. css_file = ('http://localhost/theme/gen/style.{0}.min.css'
  81. .format(CSS_HASH))
  82. html_files = ['index.html', 'archives.html',
  83. 'this-is-a-super-article.html']
  84. for f in html_files:
  85. self.check_link_tag(css_file, os.path.join(self.temp_path, f))