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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import unittest
import shutil
import os
import makesite
from test import path
class ContentTest(unittest.TestCase):
def setUp(self):
self.blog_path = path.temppath('blog')
self.undated_path = os.path.join(self.blog_path, 'foo.txt')
self.dated_path = os.path.join(self.blog_path, '2018-01-01-foo.txt')
self.normal_post_path = os.path.join(self.blog_path, 'baz.txt')
self.md_post_path = os.path.join(self.blog_path, 'qux.md')
self.no_md_post_path = os.path.join(self.blog_path, 'qux.txt')
os.makedirs(self.blog_path)
with open(self.undated_path, 'w') as f:
f.write('hello world')
with open(self.dated_path, 'w') as f:
f.write('hello world')
with open(self.normal_post_path, 'w') as f:
f.write('<!-- a: 1 -->\n<!-- b: 2 -->\nFoo')
with open(self.md_post_path, 'w') as f:
f.write('*Foo*')
with open(self.no_md_post_path, 'w') as f:
f.write('*Foo*')
def tearDown(self):
shutil.rmtree(self.blog_path)
# Rudimentary mock because unittest.mock is unavailable in Python 2.7.
def mock(self, *args):
self.mock_args = args
def test_content_content(self):
content = makesite.read_content(self.undated_path)
self.assertEqual(content['content'], 'hello world')
def test_content_date(self):
content = makesite.read_content(self.dated_path)
self.assertEqual(content['date'], '2018-01-01')
def test_content_date_missing(self):
content = makesite.read_content(self.undated_path)
self.assertEqual(content['date'], '1970-01-01')
def test_content_slug_dated(self):
content = makesite.read_content(self.dated_path)
self.assertEqual(content['slug'], 'foo')
def test_content_slug_undated(self):
content = makesite.read_content(self.undated_path)
self.assertEqual(content['slug'], 'foo')
def test_content_headers(self):
content = makesite.read_content(self.normal_post_path)
self.assertEqual(content['a'], '1')
self.assertEqual(content['b'], '2')
self.assertEqual(content['content'], 'Foo')
def test_markdown_rendering(self):
content = makesite.read_content(self.md_post_path)
self.assertEqual(content['content'], '<p><em>Foo</em></p>\n')
def test_markdown_import_error(self):
makesite._test = 'ImportError'
original_log = makesite.log
makesite.log = self.mock
self.mock_args = None
content = makesite.read_content(self.md_post_path)
makesite._test = None
makesite.log = original_log
self.assertEqual(content['content'], '*Foo*')
self.assertEqual(self.mock_args,
('WARNING: Cannot render Markdown in {}: {}',
self.md_post_path, 'Error forced by test'))
def test_no_markdown_rendering(self):
content = makesite.read_content(self.no_md_post_path)
self.assertEqual(content['content'], '*Foo*')
def test_no_markdown_import_error(self):
makesite._test = 'ImportError'
original_log = makesite.log
makesite.log = self.mock
self.mock_args = None
content = makesite.read_content(self.no_md_post_path)
makesite._test = None
makesite.log = original_log
self.assertEqual(content['content'], '*Foo*')
self.assertIsNone(self.mock_args)
|