2012-03-10 21:18:01 +09:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-18 21:08:43 +00:00
|
|
|
|
|
|
|
|
from mock import MagicMock
|
2012-03-24 13:52:03 +09:00
|
|
|
import os
|
2012-04-28 03:03:53 +03:00
|
|
|
import re
|
2012-03-10 21:18:01 +09:00
|
|
|
|
2012-04-28 03:03:53 +03:00
|
|
|
from pelican.generators import ArticlesGenerator, LessCSSGenerator
|
2012-03-10 21:18:01 +09:00
|
|
|
from pelican.settings import _DEFAULT_CONFIG
|
2012-04-28 03:03:53 +03:00
|
|
|
from .support import unittest, temporary_folder, skipIfNoExecutable
|
2012-03-10 21:18:01 +09:00
|
|
|
|
2012-03-24 13:52:03 +09:00
|
|
|
CUR_DIR = os.path.dirname(__file__)
|
2012-03-10 21:18:01 +09:00
|
|
|
|
2012-05-06 02:43:13 +02:00
|
|
|
|
2012-03-11 11:25:30 +01:00
|
|
|
class TestArticlesGenerator(unittest.TestCase):
|
2012-03-10 21:18:01 +09:00
|
|
|
|
2012-03-11 11:25:30 +01:00
|
|
|
def test_generate_feeds(self):
|
2012-03-10 21:18:01 +09:00
|
|
|
|
|
|
|
|
generator = ArticlesGenerator(None, {'FEED': _DEFAULT_CONFIG['FEED']},
|
|
|
|
|
None, _DEFAULT_CONFIG['THEME'], None,
|
2012-03-24 13:52:03 +09:00
|
|
|
_DEFAULT_CONFIG['MARKUP'])
|
2012-03-11 11:25:30 +01:00
|
|
|
writer = MagicMock()
|
2012-03-10 21:18:01 +09:00
|
|
|
generator.generate_feeds(writer)
|
2012-03-11 11:25:30 +01:00
|
|
|
writer.write_feed.assert_called_with([], None, 'feeds/all.atom.xml')
|
2012-03-10 21:18:01 +09:00
|
|
|
|
|
|
|
|
generator = ArticlesGenerator(None, {'FEED': None}, None,
|
|
|
|
|
_DEFAULT_CONFIG['THEME'], None, None)
|
2012-03-11 11:25:30 +01:00
|
|
|
writer = MagicMock()
|
2012-03-10 21:18:01 +09:00
|
|
|
generator.generate_feeds(writer)
|
2012-03-11 11:25:30 +01:00
|
|
|
self.assertFalse(writer.write_feed.called)
|
2012-03-24 13:52:03 +09:00
|
|
|
|
|
|
|
|
def test_generate_context(self):
|
|
|
|
|
|
2012-03-30 13:33:07 +02:00
|
|
|
settings = _DEFAULT_CONFIG.copy()
|
2012-03-24 13:52:03 +09:00
|
|
|
settings['ARTICLE_DIR'] = 'content'
|
|
|
|
|
settings['DEFAULT_CATEGORY'] = 'Default'
|
|
|
|
|
generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
|
|
|
|
|
_DEFAULT_CONFIG['THEME'], None,
|
|
|
|
|
_DEFAULT_CONFIG['MARKUP'])
|
|
|
|
|
generator.generate_context()
|
|
|
|
|
for article in generator.articles:
|
|
|
|
|
relfilepath = os.path.relpath(article.filename, CUR_DIR)
|
|
|
|
|
if relfilepath == os.path.join("TestCategory",
|
|
|
|
|
"article_with_category.rst"):
|
|
|
|
|
self.assertEquals(article.category.name, 'yeah')
|
|
|
|
|
elif relfilepath == os.path.join("TestCategory",
|
|
|
|
|
"article_without_category.rst"):
|
|
|
|
|
self.assertEquals(article.category.name, 'TestCategory')
|
|
|
|
|
elif relfilepath == "article_without_category.rst":
|
|
|
|
|
self.assertEquals(article.category.name, 'Default')
|
|
|
|
|
|
2012-05-06 02:43:13 +02:00
|
|
|
categories = [cat.name for cat, _ in generator.categories]
|
|
|
|
|
# assert that the categories are ordered as expected
|
|
|
|
|
self.assertEquals(
|
|
|
|
|
categories, ['Default', 'TestCategory', 'Yeah', 'test',
|
|
|
|
|
'yeah'])
|
|
|
|
|
|
2012-04-29 13:45:14 +01:00
|
|
|
def test_direct_templates_save_as_default(self):
|
|
|
|
|
|
|
|
|
|
settings = _DEFAULT_CONFIG.copy()
|
|
|
|
|
settings['DIRECT_TEMPLATES'] = ['archives']
|
|
|
|
|
generator = ArticlesGenerator(settings.copy(), settings, None,
|
|
|
|
|
_DEFAULT_CONFIG['THEME'], None,
|
|
|
|
|
_DEFAULT_CONFIG['MARKUP'])
|
2012-05-02 09:26:33 +01:00
|
|
|
write = MagicMock()
|
|
|
|
|
generator.generate_direct_templates(write)
|
|
|
|
|
write.assert_called_with("archives.html",
|
|
|
|
|
generator.get_template("archives"), settings,
|
2012-04-29 13:45:14 +01:00
|
|
|
blog=True, paginated={}, page_name='archives')
|
|
|
|
|
|
|
|
|
|
def test_direct_templates_save_as_modified(self):
|
|
|
|
|
|
|
|
|
|
settings = _DEFAULT_CONFIG.copy()
|
|
|
|
|
settings['DIRECT_TEMPLATES'] = ['archives']
|
|
|
|
|
settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
|
|
|
|
|
generator = ArticlesGenerator(settings, settings, None,
|
|
|
|
|
_DEFAULT_CONFIG['THEME'], None,
|
|
|
|
|
_DEFAULT_CONFIG['MARKUP'])
|
2012-05-02 09:26:33 +01:00
|
|
|
write = MagicMock()
|
|
|
|
|
generator.generate_direct_templates(write)
|
|
|
|
|
write.assert_called_with("archives/index.html",
|
|
|
|
|
generator.get_template("archives"), settings,
|
2012-04-29 13:45:14 +01:00
|
|
|
blog=True, paginated={}, page_name='archives')
|
|
|
|
|
|
|
|
|
|
def test_direct_templates_save_as_false(self):
|
|
|
|
|
|
|
|
|
|
settings = _DEFAULT_CONFIG.copy()
|
|
|
|
|
settings['DIRECT_TEMPLATES'] = ['archives']
|
|
|
|
|
settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
|
|
|
|
|
generator = ArticlesGenerator(settings, settings, None,
|
|
|
|
|
_DEFAULT_CONFIG['THEME'], None,
|
|
|
|
|
_DEFAULT_CONFIG['MARKUP'])
|
2012-05-02 09:26:33 +01:00
|
|
|
write = MagicMock()
|
|
|
|
|
generator.generate_direct_templates(write)
|
|
|
|
|
write.assert_called_count == 0
|
2012-05-11 16:17:47 +02:00
|
|
|
|
2012-04-28 03:03:53 +03:00
|
|
|
|
|
|
|
|
class TestLessCSSGenerator(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
LESS_CONTENT = """
|
|
|
|
|
@color: #4D926F;
|
|
|
|
|
|
|
|
|
|
#header {
|
|
|
|
|
color: @color;
|
|
|
|
|
}
|
|
|
|
|
h2 {
|
|
|
|
|
color: @color;
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@skipIfNoExecutable('lessc')
|
|
|
|
|
def test_less_compiler(self):
|
|
|
|
|
|
|
|
|
|
settings = _DEFAULT_CONFIG.copy()
|
|
|
|
|
settings['STATIC_PATHS'] = ['static']
|
|
|
|
|
settings['LESS_GENERATOR'] = True
|
|
|
|
|
|
|
|
|
|
# we'll nest here for py < 2.7 compat
|
|
|
|
|
with temporary_folder() as temp_content:
|
|
|
|
|
with temporary_folder() as temp_output:
|
|
|
|
|
generator = LessCSSGenerator(None, settings, temp_content,
|
2012-05-11 16:17:47 +02:00
|
|
|
_DEFAULT_CONFIG['THEME'], temp_output, None)
|
2012-04-28 03:03:53 +03:00
|
|
|
|
|
|
|
|
# create a dummy less file
|
|
|
|
|
less_dir = os.path.join(temp_content, 'static', 'css')
|
|
|
|
|
less_filename = os.path.join(less_dir, 'test.less')
|
|
|
|
|
|
|
|
|
|
less_output = os.path.join(temp_output, 'static', 'css',
|
|
|
|
|
'test.css')
|
|
|
|
|
|
|
|
|
|
os.makedirs(less_dir)
|
|
|
|
|
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))
|