-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtest_build.py
510 lines (419 loc) · 19.5 KB
/
test_build.py
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#! python3 # noqa E265
"""Usage from the repo root folder:
.. code-block:: python
# for whole test
python -m unittest tests.test_build
"""
# #############################################################################
# ########## Libraries #############
# ##################################
# Standard library
import tempfile
import unittest
# logging
from logging import DEBUG, getLogger
from pathlib import Path
from traceback import format_exception
# 3rd party
import feedparser
# test suite
from tests.base import BaseTest
logger = getLogger(__name__)
logger.setLevel(DEBUG)
# #############################################################################
# ########## Classes ###############
# ##################################
class TestBuildRss(BaseTest):
"""Test MkDocs build with RSS plugin."""
# -- Standard methods --------------------------------------------------------
@classmethod
def setUpClass(cls):
"""Executed when module is loaded before any test."""
cls.config_files = sorted(Path("tests/fixtures/").glob("**/*.yml"))
cls.feed_image = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/128px-Feed-icon.svg.png"
def setUp(self):
"""Executed before each test."""
pass
def tearDown(self):
"""Executed after each test."""
pass
@classmethod
def tearDownClass(cls):
"""Executed after the last test."""
# In case of some tests failure, ensure that everything is cleaned up
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
# if temp_page.exists():
# temp_page.unlink()
git_dir = Path("_git")
if git_dir.exists():
git_dir.replace(git_dir.with_name(".git"))
# -- TESTS ---------------------------------------------------------
def test_simple_build(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("mkdocs.yml"),
output_path=tmpdirname,
strict=False,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
def test_simple_build_minimal(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_minimal.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
for feed_item in feed_parsed.entries:
# mandatory properties
self.assertTrue("description" in feed_item)
self.assertTrue("guid" in feed_item)
self.assertTrue("link" in feed_item)
self.assertTrue("published" in feed_item)
self.assertTrue("source" in feed_item)
self.assertTrue("title" in feed_item)
# optional - following should not be present in the feed by default
self.assertTrue("category" not in feed_item)
self.assertTrue("comments" not in feed_item)
self.assertTrue("enclosure" not in feed_item)
if feed_item.title in ("Test page with meta",):
self.assertTrue("author" in feed_item)
def test_simple_build_complete(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
for feed_item in feed_parsed.entries:
# mandatory properties
self.assertTrue("description" in feed_item)
self.assertTrue("guid" in feed_item)
self.assertTrue("link" in feed_item)
self.assertTrue("published" in feed_item)
self.assertTrue("source" in feed_item)
self.assertTrue("title" in feed_item)
# optional - following should not be present in the feed by default
if (
"without_meta" in feed_item.title
or feed_item.title == "Test home page"
):
self.assertTrue("category" not in feed_item)
self.assertTrue("comments" in feed_item)
elif "with_meta" in feed_item.title:
self.assertTrue("author" in feed_item)
self.assertTrue("category" in feed_item)
self.assertTrue("enclosure" in feed_item)
else:
pass
def test_simple_build_disabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_disabled.yml"),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
def test_simple_build_item_dates(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_dates_overridden.yml"),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
def test_simple_build_feed_length(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_feed_length_custom.yml"
),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(len(feed_parsed.entries), 3)
# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertEqual(len(feed_parsed.entries), 3)
def test_simple_build_feed_ttl(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_feed_ttl_custom.yml"),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertNotEqual(feed_parsed.feed.ttl, "1440")
self.assertEqual(feed_parsed.feed.ttl, "90")
# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertNotEqual(feed_parsed.feed.ttl, "1440")
self.assertEqual(feed_parsed.feed.ttl, "90")
def test_simple_build_item_categories_enabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_item_categories.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
for feed_item in feed_parsed.entries:
if feed_item.title in ("Test page with meta",):
self.assertTrue("category" in feed_item)
def test_simple_build_item_comments_enabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_item_comments.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.bozo, 0)
for feed_item in feed_parsed.entries:
self.assertTrue("comments" in feed_item)
def test_simple_build_item_comments_disabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_item_no_comments.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.bozo, 0)
for feed_item in feed_parsed.entries:
self.assertTrue("comments" not in feed_item)
def test_simple_build_item_length_unlimited(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_item_length_unlimited.yml"
),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.bozo, 0)
for feed_item in feed_parsed.entries:
if feed_item.title not in (
"Page without meta with short text",
"Blog sample",
):
self.assertGreaterEqual(
len(feed_item.description), 150, feed_item.title
)
def test_simple_build_pretty_print_enabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_pretty_print_enabled.yml"
),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
with Path(Path(tmpdirname) / "feed_rss_created.xml").open("r") as f:
self.assertGreater(len(f.readlines()), 0)
# updated items
with Path(Path(tmpdirname) / "feed_rss_updated.xml").open("r") as f:
self.assertGreater(len(f.readlines()), 0)
def test_simple_build_pretty_print_disabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_pretty_print_disabled.yml"
),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
with Path(Path(tmpdirname) / "feed_rss_created.xml").open("r") as f:
self.assertEqual(len(f.readlines()), 1)
# updated items
with Path(Path(tmpdirname) / "feed_rss_updated.xml").open("r") as f:
self.assertEqual(len(f.readlines()), 1)
def test_rss_feed_validation(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.bozo, 0)
# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertEqual(feed_parsed.bozo, 0)
# some feed characteristics
self.assertEqual(feed_parsed.version, "rss20")
def test_config_no_site_url(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_minimal_no_site_url.yml"
),
output_path=tmpdirname,
strict=True,
)
# cli should returns an error code (1)
self.assertEqual(cli_result.exit_code, 1)
self.assertIsNotNone(cli_result.exception)
def test_bad_config(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_bad_config.yml"),
output_path=tmpdirname,
strict=True,
)
# cli should returns an error code (1)
self.assertEqual(cli_result.exit_code, 1)
self.assertIsNotNone(cli_result.exception)
def test_bad_date_format(self):
# add a new page without tracking it
md_str = """---\ndate: 13 April 2022\n---\n\n# This page is dynamically created for test purposes\n\nHi!\n
"""
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
if temp_page.exists():
temp_page.unlink()
temp_page.write_text(md_str)
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("mkdocs.yml"),
output_path=tmpdirname,
strict=True,
)
# cli should returns an error code (1)
self.assertEqual(cli_result.exit_code, 1)
self.assertIsNotNone(cli_result.exception)
# rm page
temp_page.unlink()
def test_not_in_git_log(self):
# add a new page without tracking it
md_str = """# This page is dynamically created for test purposes\n\nHi!\n
"""
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
if temp_page.exists():
temp_page.unlink()
temp_page.write_text(md_str)
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
output_path=tmpdirname,
strict=True,
)
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)
# rm page
temp_page.unlink()
def test_not_git_repo(self):
# temporarily rename the git folder to fake a non-git repo
git_dir = Path(".git")
git_dir_tmp = git_dir.with_name("_git")
git_dir.replace(git_dir_tmp)
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_minimal.yml"),
output_path=tmpdirname,
strict=True,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))
self.assertEqual(cli_result.exit_code, 1)
self.assertIsNotNone(cli_result.exception)
# restore name
git_dir_tmp.replace(git_dir)
# ##############################################################################
# ##### Stand alone program ########
# ##################################
if __name__ == "__main__":
unittest.main()