summaryrefslogtreecommitdiff
path: root/test/test_pages.py
blob: 9f52fb7f04e6c17ce9f2c87a2cf35719192f9b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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('<!-- tag: foo -->Foo')
        with open(os.path.join(self.blog_path, 'header-bar.txt'), 'w') as f:
            f.write('<!-- title: bar -->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 = '<div>{{ content }}</div>'
        makesite.make_pages(src, dst, tpl)
        with open(os.path.join(self.site_path, 'foo.txt')) as f:
            self.assertEqual(f.read(), '<div>Foo</div>')
        with open(os.path.join(self.site_path, 'bar.txt')) as f:
            self.assertEqual(f.read(), '<div>Bar</div>')

    def test_pages_dated(self):
        src = os.path.join(self.blog_path, '2*.txt')
        dst = os.path.join(self.site_path, '{{ slug }}.txt')
        tpl = '<div>{{ content }}</div>'
        makesite.make_pages(src, dst, tpl)
        with open(os.path.join(self.site_path, 'foo.txt')) as f:
            self.assertEqual(f.read(), '<div>Foo</div>')
        with open(os.path.join(self.site_path, 'bar.txt')) as f:
            self.assertEqual(f.read(), '<div>Bar</div>')

    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 = '<div>{{ slug }}:{{ title }}:{{ date }}:{{ content }}</div>'
        makesite.make_pages(src, dst, tpl, title='Lorem')
        with open(os.path.join(self.site_path, 'foo.txt')) as f:
            self.assertEqual(f.read(), '<div>foo:Lorem:2018-01-01:Foo</div>')
        with open(os.path.join(self.site_path, 'bar.txt')) as f:
            self.assertEqual(f.read(), '<div>bar:Lorem:2018-01-02:Bar</div>')

    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 = '<div>{{ content }}</div>'
        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')