import unittest
import os
import shutil
import makesite
from test import path
class PagesTest(unittest.TestCase):
def setUp(self):
self.blog_path = path.temppath('blog')
self.site_path = path.temppath('site')
os.makedirs(self.blog_path)
with open(os.path.join(self.blog_path, 'foo.txt'), 'w') as f:
f.write('Foo')
with open(os.path.join(self.blog_path, 'bar.txt'), 'w') as f:
f.write('Bar')
with open(os.path.join(self.blog_path, '2018-01-01-foo.txt'), 'w') as f:
f.write('Foo')
with open(os.path.join(self.blog_path, '2018-01-02-bar.txt'), 'w') as f:
f.write('Bar')
with open(os.path.join(self.blog_path, 'header-foo.txt'), 'w') as f:
f.write('Foo')
with open(os.path.join(self.blog_path, 'header-bar.txt'), 'w') as f:
f.write('Bar')
def tearDown(self):
shutil.rmtree(self.blog_path)
shutil.rmtree(self.site_path)
def test_pages_undated(self):
src = os.path.join(self.blog_path, '[fb]*.txt')
dst = os.path.join(self.site_path, '{{ slug }}.txt')
tpl = '
{{ content }}
'
makesite.make_pages(src, dst, tpl)
with open(os.path.join(self.site_path, 'foo.txt')) as f:
self.assertEqual(f.read(), 'Foo
')
with open(os.path.join(self.site_path, 'bar.txt')) as f:
self.assertEqual(f.read(), 'Bar
')
def test_pages_dated(self):
src = os.path.join(self.blog_path, '2*.txt')
dst = os.path.join(self.site_path, '{{ slug }}.txt')
tpl = '{{ content }}
'
makesite.make_pages(src, dst, tpl)
with open(os.path.join(self.site_path, 'foo.txt')) as f:
self.assertEqual(f.read(), 'Foo
')
with open(os.path.join(self.site_path, 'bar.txt')) as f:
self.assertEqual(f.read(), 'Bar
')
def test_pages_layout_params(self):
src = os.path.join(self.blog_path, '2*.txt')
dst = os.path.join(self.site_path, '{{ slug }}.txt')
tpl = '{{ slug }}:{{ title }}:{{ date }}:{{ content }}
'
makesite.make_pages(src, dst, tpl, title='Lorem')
with open(os.path.join(self.site_path, 'foo.txt')) as f:
self.assertEqual(f.read(), 'foo:Lorem:2018-01-01:Foo
')
with open(os.path.join(self.site_path, 'bar.txt')) as f:
self.assertEqual(f.read(), 'bar:Lorem:2018-01-02:Bar
')
def test_pages_return_value(self):
src = os.path.join(self.blog_path, '2*.txt')
dst = os.path.join(self.site_path, '{{ slug }}.txt')
tpl = '{{ content }}
'
posts = makesite.make_pages(src, dst, tpl)
self.assertEqual(len(posts), 2)
self.assertEqual(posts[0]['date'], '2018-01-02')
self.assertEqual(posts[1]['date'], '2018-01-01')
def test_content_header_params(self):
# Test that header params from one post is not used in another
# post.
src = os.path.join(self.blog_path, 'header*.txt')
dst = os.path.join(self.site_path, '{{ slug }}.txt')
tpl = '{{ title }}:{{ tag }}:{{ content }}'
makesite.make_pages(src, dst, tpl)
with open(os.path.join(self.site_path, 'header-foo.txt')) as f:
self.assertEqual(f.read(), '{{ title }}:foo:Foo')
with open(os.path.join(self.site_path, 'header-bar.txt')) as f:
self.assertEqual(f.read(), 'bar:{{ tag }}:Bar')