Skip to content

Commit f5295de

Browse files
authored
Merge pull request #114 from Guts/quality/completing-tests
Tests: road to 80% of coverage
2 parents 15961fa + 368d562 commit f5295de

10 files changed

Lines changed: 231 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,4 @@ dmypy.json
217217

218218
**/*.xml
219219
site/
220+
tests/fixtures/docs/temp_page_not_in_git_log.md

mkdocs_rss_plugin/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def get_file_dates(
131131

132132
if source_date_update != "git" and in_page.meta.get(source_date_update):
133133
dt_updated = self.get_date_from_meta(
134-
date_metatag_value=in_page.meta.get(source_date_creation),
134+
date_metatag_value=in_page.meta.get(source_date_update),
135135
meta_datetime_format=meta_datetime_format,
136136
)
137137
if isinstance(dt_updated, str):

tests/base.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ def build_docs_setup(
8080
:return: Object with results of command
8181
:rtype: click.testing.Result
8282
"""
83+
cmd_args = [
84+
"--clean",
85+
"--config-file",
86+
f"{mkdocs_yml_filepath}",
87+
"--site-dir",
88+
f"{output_path}",
89+
"--verbose",
90+
]
91+
if strict:
92+
cmd_args.append("--strict")
93+
8394
try:
8495
runner = CliRunner()
85-
run = runner.invoke(
86-
build_command,
87-
[
88-
"--clean",
89-
"--config-file",
90-
f"{mkdocs_yml_filepath}",
91-
"--site-dir",
92-
f"{output_path}",
93-
"--verbose",
94-
],
95-
)
96+
run = runner.invoke(build_command, cmd_args)
9697
return run
9798
except Exception as err:
9899
logging.critical(err)

tests/fixtures/docs/index.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Test home page
22

3-
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
3+
Sed ut perspiciatis *unde* omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
44

5-
Feeds:
5+
Eos **aliquid incidunt non**. Possimus ipsam id reiciendis enim. Laudantium eligendi deleniti rem delectus corrupti sit doloremque quod. Dolorem est laborum enim a ut aut.
66

7-
- latest created: [feed_rss_created.xml](feed_rss_created.xml)
8-
- latest updated: [feed_rss_updated.xml](feed_rss_updated.xml)
7+
Laudantium minima cumque mollitia alias voluptatibus sit doloribus natus. Nam earum dolores est velit optio ipsa sit. Veritatis aut iusto distinctio veritatis aperiam in. Nemo aperiam ipsum dolorem ea officiis pariatur sapiente. Quibusdam est ut eligendi vitae ducimus fugiat. Harum ea necessitatibus inventore rem voluptatem est quaerat incidunt.

tests/fixtures/docs/page_with_meta.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
---
2-
title: Use the MkDocs RSS Plugin
2+
title: Page with complete meta
33
authors:
44
- Guts
55
- Tim Vink
66
- liang2kl
77
date: 2020-03-20 10:20
8+
update: 2022-03-30 10:20
89
description: First test page of mkdocs-rss-plugin test suite
10+
image: "https://svgsilh.com/png-512/97849.png"
911
tags:
1012
- test
1113
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
author: Julien Moura
3+
category: blog
4+
date: 2022-03-30
5+
description: First test page of mkdocs-rss-plugin test suite
6+
tags:
7+
- test
8+
---
9+
10+
# Test page with meta values as string
11+
12+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
author:
3+
- Julien Moura
4+
- Tim Vink
5+
tags:
6+
- blog
7+
- tests
8+
date: 2022-03-30
9+
description: Test page with meta keys as singular and values as iterable
10+
tags:
11+
- test
12+
---
13+
14+
# Test page with meta keys as singular
15+
16+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
17+
18+

tests/fixtures/mkdocs_complete.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ use_directory_urls: true
1313

1414
plugins:
1515
- rss:
16-
abstract_chars_count: 200
16+
abstract_chars_count: 160 # -1 for full content
17+
categories:
18+
- tags
1719
comments_path: "#__comments"
18-
feed_ttl: 90
19-
length: 3
20-
20+
date_from_meta:
21+
as_creation: "date"
22+
as_update: false
23+
datetime_format: "%Y-%m-%d %H:%M"
24+
enabled: true
25+
feed_ttl: 1440
26+
image: https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/128px-Feed-icon.svg.png
27+
length: 20
28+
pretty_print: false
29+
match_path: ".*"
30+
url_parameters:
31+
utm_source: "documentation"
32+
utm_medium: "RSS"
33+
utm_campaign: "feed-syndication"
2134
theme:
2235
name: readthedocs
2336

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Project information
2+
site_name: MkDocs RSS Plugin - TEST
3+
site_description: Basic setup to test against MkDocs RSS plugin
4+
site_author: Julien Moura (Guts)
5+
site_url: https://guts.github.io/mkdocs-rss-plugin
6+
copyright: 'Guts - In Geo Veritas'
7+
8+
# Repository
9+
repo_name: 'guts/mkdocs-rss-plugin'
10+
repo_url: 'https://github.com/guts/mkdocs-rss-plugin'
11+
12+
use_directory_urls: true
13+
14+
plugins:
15+
- rss:
16+
date_from_meta:
17+
as_creation: "date"
18+
as_update: "update"
19+
datetime_format: "%Y-%m-%d %H:%M"
20+
21+
theme:
22+
name: readthedocs
23+
24+
# Extensions to enhance markdown
25+
markdown_extensions:
26+
- meta

tests/test_build.py

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ def tearDown(self):
5757
@classmethod
5858
def tearDownClass(cls):
5959
"""Executed after the last test."""
60-
pass
60+
# In case of some tests failure, ensure that everything is cleaned up
61+
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
62+
# if temp_page.exists():
63+
# temp_page.unlink()
64+
65+
git_dir = Path("_git")
66+
if git_dir.exists():
67+
git_dir.replace(git_dir.with_name(".git"))
6168

6269
# -- TESTS ---------------------------------------------------------
6370
def test_simple_build(self):
@@ -111,6 +118,48 @@ def test_simple_build_minimal(self):
111118
if feed_item.title in ("Test page with meta",):
112119
self.assertTrue("author" in feed_item)
113120

121+
def test_simple_build_complete(self):
122+
with tempfile.TemporaryDirectory() as tmpdirname:
123+
cli_result = self.build_docs_setup(
124+
testproject_path="docs",
125+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
126+
output_path=tmpdirname,
127+
strict=True,
128+
)
129+
130+
if cli_result.exception is not None:
131+
e = cli_result.exception
132+
logger.debug(format_exception(type(e), e, e.__traceback__))
133+
134+
self.assertEqual(cli_result.exit_code, 0)
135+
self.assertIsNone(cli_result.exception)
136+
137+
# created items
138+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
139+
for feed_item in feed_parsed.entries:
140+
141+
# mandatory properties
142+
self.assertTrue("description" in feed_item)
143+
self.assertTrue("guid" in feed_item)
144+
self.assertTrue("link" in feed_item)
145+
self.assertTrue("published" in feed_item)
146+
self.assertTrue("source" in feed_item)
147+
self.assertTrue("title" in feed_item)
148+
149+
# optional - following should not be present in the feed by default
150+
if (
151+
"without_meta" in feed_item.title
152+
or feed_item.title == "Test home page"
153+
):
154+
self.assertTrue("category" not in feed_item)
155+
self.assertTrue("comments" in feed_item)
156+
elif "with_meta" in feed_item.title:
157+
self.assertTrue("author" in feed_item)
158+
self.assertTrue("category" in feed_item)
159+
self.assertTrue("enclosure" in feed_item)
160+
else:
161+
pass
162+
114163
def test_simple_build_disabled(self):
115164
with tempfile.TemporaryDirectory() as tmpdirname:
116165
cli_result = self.build_docs_setup(
@@ -125,6 +174,20 @@ def test_simple_build_disabled(self):
125174
self.assertEqual(cli_result.exit_code, 0)
126175
self.assertIsNone(cli_result.exception)
127176

177+
def test_simple_build_item_dates(self):
178+
with tempfile.TemporaryDirectory() as tmpdirname:
179+
cli_result = self.build_docs_setup(
180+
testproject_path="docs",
181+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_dates_overridden.yml"),
182+
output_path=tmpdirname,
183+
)
184+
if cli_result.exception is not None:
185+
e = cli_result.exception
186+
logger.debug(format_exception(type(e), e, e.__traceback__))
187+
188+
self.assertEqual(cli_result.exit_code, 0)
189+
self.assertIsNone(cli_result.exception)
190+
128191
def test_simple_build_feed_length(self):
129192
with tempfile.TemporaryDirectory() as tmpdirname:
130193
cli_result = self.build_docs_setup(
@@ -316,7 +379,7 @@ def test_rss_feed_validation(self):
316379
with tempfile.TemporaryDirectory() as tmpdirname:
317380
cli_result = self.build_docs_setup(
318381
testproject_path="docs",
319-
mkdocs_yml_filepath=Path("mkdocs.yml"),
382+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
320383
output_path=tmpdirname,
321384
)
322385

@@ -342,15 +405,85 @@ def test_bad_config(self):
342405
with tempfile.TemporaryDirectory() as tmpdirname:
343406
cli_result = self.build_docs_setup(
344407
testproject_path="docs",
345-
mkdocs_yml_filepath=Path("mkdocs_bad_config.yml"),
408+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_bad_config.yml"),
409+
output_path=tmpdirname,
410+
strict=True,
411+
)
412+
413+
# cli should returns an error code (1)
414+
self.assertEqual(cli_result.exit_code, 1)
415+
self.assertIsNotNone(cli_result.exception)
416+
417+
def test_bad_date_format(self):
418+
# add a new page without tracking it
419+
md_str = """---\ndate: 13 April 2022\n---\n\n# This page is dynamically created for test purposes\n\nHi!\n
420+
"""
421+
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
422+
if temp_page.exists():
423+
temp_page.unlink()
424+
temp_page.write_text(md_str)
425+
426+
with tempfile.TemporaryDirectory() as tmpdirname:
427+
cli_result = self.build_docs_setup(
428+
testproject_path="docs",
429+
mkdocs_yml_filepath=Path("mkdocs.yml"),
430+
output_path=tmpdirname,
431+
strict=True,
432+
)
433+
434+
# cli should returns an error code (1)
435+
self.assertEqual(cli_result.exit_code, 1)
436+
self.assertIsNotNone(cli_result.exception)
437+
438+
# rm page
439+
temp_page.unlink()
440+
441+
def test_not_in_git_log(self):
442+
# add a new page without tracking it
443+
md_str = """# This page is dynamically created for test purposes\n\nHi!\n
444+
"""
445+
temp_page = Path("tests/fixtures/docs/temp_page_not_in_git_log.md")
446+
if temp_page.exists():
447+
temp_page.unlink()
448+
temp_page.write_text(md_str)
449+
450+
with tempfile.TemporaryDirectory() as tmpdirname:
451+
cli_result = self.build_docs_setup(
452+
testproject_path="docs",
453+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_disabled.yml"),
346454
output_path=tmpdirname,
347455
strict=True,
348456
)
349457

350-
# cli should returns an error code (2)
351-
self.assertEqual(cli_result.exit_code, 2)
458+
self.assertEqual(cli_result.exit_code, 0)
459+
self.assertIsNone(cli_result.exception)
460+
461+
# rm page
462+
temp_page.unlink()
463+
464+
def test_not_git_repo(self):
465+
# temporarily rename the git folder to fake a non-git repo
466+
git_dir = Path(".git")
467+
git_dir_tmp = git_dir.with_name("_git")
468+
git_dir.replace(git_dir_tmp)
469+
470+
with tempfile.TemporaryDirectory() as tmpdirname:
471+
cli_result = self.build_docs_setup(
472+
testproject_path="docs",
473+
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_minimal.yml"),
474+
output_path=tmpdirname,
475+
strict=True,
476+
)
477+
if cli_result.exception is not None:
478+
e = cli_result.exception
479+
logger.debug(format_exception(type(e), e, e.__traceback__))
480+
481+
self.assertEqual(cli_result.exit_code, 1)
352482
self.assertIsNotNone(cli_result.exception)
353483

484+
# restore name
485+
git_dir_tmp.replace(git_dir)
486+
354487

355488
# ##############################################################################
356489
# ##### Stand alone program ########

0 commit comments

Comments
 (0)