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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, absolute_import
# This code is so you can run the samples without installing the package,
# and should be before any import touching nikola, in any file under tests/
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import codecs
import locale
import shutil
import tempfile
import unittest
import lxml.html
from nose.plugins.skip import SkipTest
from nikola import __main__
import nikola
import nikola.plugins.command
import nikola.plugins.command.init
from .base import BaseTestCase, cd
class EmptyBuildTest(BaseTestCase):
"""Basic integration testcase."""
dataname = None
@classmethod
def setUpClass(cls):
"""Setup a demo site."""
cls.tmpdir = tempfile.mkdtemp()
cls.target_dir = os.path.join(cls.tmpdir, "target")
cls.init_command = nikola.plugins.command.init.CommandInit()
cls.fill_site()
cls.patch_site()
cls.build()
@classmethod
def fill_site(self):
"""Add any needed initial content."""
self.init_command.create_empty_site(self.target_dir)
self.init_command.create_configuration(self.target_dir)
if self.dataname:
src = os.path.join(os.path.dirname(__file__), 'data',
self.dataname)
for root, dirs, files in os.walk(src):
for src_name in files:
rel_dir = os.path.relpath(root, src)
dst_file = os.path.join(self.target_dir, rel_dir, src_name)
src_file = os.path.join(root, src_name)
shutil.copy2(src_file, dst_file)
@classmethod
def patch_site(self):
"""Make any modifications you need to the site."""
@classmethod
def build(self):
"""Build the site."""
with cd(self.target_dir):
__main__.main(["build"])
@classmethod
def tearDownClass(self):
"""Remove the demo site."""
# ignore_errors=True for windows by issue #782
shutil.rmtree(self.tmpdir, ignore_errors=(sys.platform == 'win32'))
# Fixes Issue #438
try:
del sys.modules['conf']
except KeyError:
pass
def test_build(self):
"""Ensure the build did something."""
index_path = os.path.join(
self.target_dir, "output", "archive.html")
self.assertTrue(os.path.isfile(index_path))
class DemoBuildTest(EmptyBuildTest):
"""Test that a default build of --demo works."""
@classmethod
def fill_site(self):
"""Fill the site with demo content."""
self.init_command.copy_sample_site(self.target_dir)
self.init_command.create_configuration(self.target_dir)
# File for Issue #374 (empty post text)
with codecs.open(os.path.join(self.target_dir, 'posts', 'empty.txt'), "wb+", "utf8") as outf:
outf.write(
".. title: foobar\n"
".. slug: foobar\n"
".. date: 2013-03-06 19:08:15\n"
)
def test_index_in_sitemap(self):
sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
self.assertTrue('<loc>http://getnikola.com/index.html</loc>' in sitemap_data)
def test_avoid_double_slash_in_rss(self):
rss_path = os.path.join(self.target_dir, "output", "rss.xml")
rss_data = codecs.open(rss_path, "r", "utf8").read()
self.assertFalse('http://getnikola.com//' in rss_data)
class RepeatedPostsSetting(DemoBuildTest):
"""Duplicate POSTS, should not read each post twice, which causes conflicts."""
@classmethod
def patch_site(self):
"""Set the SITE_URL to have a path"""
conf_path = os.path.join(self.target_dir, "conf.py")
with codecs.open(conf_path, "ab", "utf8") as outf:
outf.write('\nPOSTS = (("posts/*.txt", "posts", "post.tmpl"),("posts/*.txt", "posts", "post.tmpl"))\n')
class FuturePostTest(EmptyBuildTest):
"""Test a site with future posts."""
@classmethod
def fill_site(self):
import datetime
from nikola.utils import current_time
self.init_command.copy_sample_site(self.target_dir)
self.init_command.create_configuration(self.target_dir)
# Change COMMENT_SYSTEM_ID to not wait for 5 seconds
with codecs.open(os.path.join(self.target_dir, 'conf.py'), "ab+", "utf8") as outf:
outf.write('\nCOMMENT_SYSTEM_ID = "nikolatest"\n')
with codecs.open(os.path.join(self.target_dir, 'posts', 'empty1.txt'), "wb+", "utf8") as outf:
outf.write(
".. title: foo\n"
".. slug: foo\n"
".. date: %s\n" % (current_time() + datetime.timedelta(-1)).strftime('%Y-%m-%d %H:%M:%S')
)
with codecs.open(os.path.join(self.target_dir, 'posts', 'empty2.txt'), "wb+", "utf8") as outf:
outf.write(
".. title: bar\n"
".. slug: bar\n"
".. date: %s\n" % (current_time() + datetime.timedelta(1)).strftime('%Y-%m-%d %H:%M:%S')
)
def test_future_post(self):
""" Ensure that the future post is not present in the index and sitemap."""
index_path = os.path.join(self.target_dir, "output", "index.html")
sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
foo_path = os.path.join(self.target_dir, "output", "posts", "foo.html")
bar_path = os.path.join(self.target_dir, "output", "posts", "bar.html")
self.assertTrue(os.path.isfile(index_path))
self.assertTrue(os.path.isfile(foo_path))
self.assertTrue(os.path.isfile(bar_path))
index_data = codecs.open(index_path, "r", "utf8").read()
sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
self.assertTrue('foo.html' in index_data)
self.assertFalse('bar.html' in index_data)
self.assertTrue('foo.html' in sitemap_data)
self.assertFalse('bar.html' in sitemap_data)
# Run deploy command to see if future post is deleted
with cd(self.target_dir):
__main__.main(["deploy"])
self.assertTrue(os.path.isfile(index_path))
self.assertTrue(os.path.isfile(foo_path))
self.assertFalse(os.path.isfile(bar_path))
class TranslatedBuildTest(EmptyBuildTest):
"""Test a site with translated content."""
dataname = "translated_titles"
def __init__(self, *a, **kw):
super(TranslatedBuildTest, self).__init__(*a, **kw)
try:
locale.setlocale(locale.LC_ALL, ("pl_PL", "utf8"))
except:
raise SkipTest
def test_translated_titles(self):
"""Check that translated title is picked up."""
en_file = os.path.join(self.target_dir, "output", "stories", "1.html")
pl_file = os.path.join(self.target_dir, "output", "pl", "stories", "1.html")
# Files should be created
self.assertTrue(os.path.isfile(en_file))
self.assertTrue(os.path.isfile(pl_file))
# And now let's check the titles
with codecs.open(en_file, 'r', 'utf8') as inf:
doc = lxml.html.parse(inf)
self.assertEqual(doc.find('//title').text, 'Foo | Demo Site')
with codecs.open(pl_file, 'r', 'utf8') as inf:
doc = lxml.html.parse(inf)
self.assertEqual(doc.find('//title').text, 'Bar | Demo Site')
class TranslationsPatternTest1(TranslatedBuildTest):
"""Check that the path.lang.ext TRANSLATIONS_PATTERN works too"""
@classmethod
def patch_site(self):
"""Set the TRANSLATIONS_PATTERN to the new v7 default"""
os.rename(os.path.join(self.target_dir, "stories", "1.txt.pl"),
os.path.join(self.target_dir, "stories", "1.pl.txt")
)
conf_path = os.path.join(self.target_dir, "conf.py")
with codecs.open(conf_path, "rb", "utf-8") as inf:
data = inf.read()
data = data.replace('TRANSLATIONS_PATTERN = "{path}.{ext}.{lang}"',
'TRANSLATIONS_PATTERN = "{path}.{lang}.{ext}"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
class TranslationsPatternTest2(TranslatedBuildTest):
"""Check that the path_lang.ext TRANSLATIONS_PATTERN works too"""
@classmethod
def patch_site(self):
"""Set the TRANSLATIONS_PATTERN to the new v7 default"""
conf_path = os.path.join(self.target_dir, "conf.py")
os.rename(os.path.join(self.target_dir, "stories", "1.txt.pl"),
os.path.join(self.target_dir, "stories", "1_pl.txt")
)
with codecs.open(conf_path, "rb", "utf-8") as inf:
data = inf.read()
data = data.replace('TRANSLATIONS_PATTERN = "{path}.{ext}.{lang}"',
'TRANSLATIONS_PATTERN = "{path}_{lang}.{ext}"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
class RelativeLinkTest(DemoBuildTest):
"""Check that SITE_URL with a path doesn't break links."""
@classmethod
def patch_site(self):
"""Set the SITE_URL to have a path"""
conf_path = os.path.join(self.target_dir, "conf.py")
with codecs.open(conf_path, "rb", "utf-8") as inf:
data = inf.read()
data = data.replace('SITE_URL = "http://getnikola.com/"',
'SITE_URL = "http://getnikola.com/foo/bar/"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
def test_relative_links(self):
"""Check that the links in output/index.html are correct"""
test_path = os.path.join(self.target_dir, "output", "index.html")
flag = False
with open(test_path, "rb") as inf:
data = inf.read()
for _, _, url, _ in lxml.html.iterlinks(data):
# Just need to be sure this one is ok
if url.endswith("css"):
self.assertFalse(url.startswith(".."))
flag = True
# But I also need to be sure it is there!
self.assertTrue(flag)
def test_index_in_sitemap(self):
"""Test that the correct path is in sitemap, and not the wrong one."""
sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
self.assertFalse('<loc>http://getnikola.com/</loc>' in sitemap_data)
self.assertTrue('<loc>http://getnikola.com/foo/bar/index.html</loc>' in sitemap_data)
class TestCheck(DemoBuildTest):
"""The demo build should pass 'nikola check'"""
def test_check_links(self):
with cd(self.target_dir):
try:
__main__.main(['check', '-l'])
except SystemExit as e:
self.assertEqual(e.code, 0)
def test_check_files(self):
with cd(self.target_dir):
try:
__main__.main(['check', '-f'])
except SystemExit as e:
self.assertEqual(e.code, 0)
class TestCheckFailure(DemoBuildTest):
"""The demo build should pass 'nikola check'"""
def test_check_links_fail(self):
with cd(self.target_dir):
os.unlink(os.path.join("output", "archive.html"))
try:
__main__.main(['check', '-l'])
except SystemExit as e:
self.assertNotEqual(e.code, 0)
def test_check_files_fail(self):
with cd(self.target_dir):
with codecs.open(os.path.join("output", "foobar"), "wb+", "utf8") as outf:
outf.write("foo")
try:
__main__.main(['check', '-f'])
except SystemExit as e:
self.assertNotEqual(e.code, 0)
class RelativeLinkTest2(DemoBuildTest):
"""Check that dropping stories to the root doesn't break links."""
@classmethod
def patch_site(self):
"""Set the SITE_URL to have a path"""
conf_path = os.path.join(self.target_dir, "conf.py")
with codecs.open(conf_path, "rb", "utf-8") as inf:
data = inf.read()
data = data.replace('("stories/*.txt", "stories", "story.tmpl"),',
'("stories/*.txt", "", "story.tmpl"),')
data = data.replace('("stories/*.rst", "stories", "story.tmpl"),',
'("stories/*.rst", "", "story.tmpl"),')
data = data.replace('# INDEX_PATH = ""',
'INDEX_PATH = "blog"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
outf.flush()
def test_relative_links(self):
"""Check that the links in a story are correct"""
test_path = os.path.join(self.target_dir, "output", "about-nikola.html")
flag = False
with open(test_path, "rb") as inf:
data = inf.read()
for _, _, url, _ in lxml.html.iterlinks(data):
# Just need to be sure this one is ok
if url.endswith("css"):
self.assertFalse(url.startswith(".."))
flag = True
# But I also need to be sure it is there!
self.assertTrue(flag)
def test_index_in_sitemap(self):
"""Test that the correct path is in sitemap, and not the wrong one."""
sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
self.assertFalse('<loc>http://getnikola.com/</loc>' in sitemap_data)
self.assertTrue('<loc>http://getnikola.com/blog/index.html</loc>' in sitemap_data)
class MonthlyArchiveTest(DemoBuildTest):
"""Check that the monthly archives build and are correct."""
@classmethod
def patch_site(self):
"""Set the SITE_URL to have a path"""
conf_path = os.path.join(self.target_dir, "conf.py")
with codecs.open(conf_path, "rb", "utf-8") as inf:
data = inf.read()
data = data.replace('# CREATE_MONTHLY_ARCHIVE = False',
'CREATE_MONTHLY_ARCHIVE = True')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
outf.flush()
def test_monthly_archive(self):
"""See that it builds"""
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'target', 'output', '2012', '03', 'index.html')))
class SubdirRunningTest(DemoBuildTest):
"""Check that running nikola from subdir works."""
def test_subdir_run(self):
"""Check whether build works from posts/"""
with cd(os.path.join(self.target_dir, 'posts')):
result = __main__.main(['build'])
self.assertEquals(result, 0)
if __name__ == "__main__":
unittest.main()
|