Skip to content

Commit ca2e625

Browse files
committed
Add test for replacing markdown tags correctly
1 parent 3b19f0e commit ca2e625

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

tests/fixtures/basic_project/docs/page_with_tag.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
Tag <mark>\{\{ git_revision_date_localized \}\}</mark> renders as: {{ git_revision_date_localized }}
44

55
Tag <mark>\{\{ git_creation_date_localized \}\}</mark> renders as: {{ git_creation_date_localized }}
6+
7+
Tag <mark>\{\{ git_site_revision_date_localized \}\}</mark> renders as: {{ git_site_revision_date_localized }}
8+

tests/test_builds.py

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import re
1717
import shutil
18+
from contextlib import contextmanager
1819

1920
# MkDocs
2021
from mkdocs.__main__ import build_command
@@ -40,6 +41,27 @@
4041
# ########## Helpers ###############
4142
# ##################################
4243

44+
@contextmanager
45+
def working_directory(path):
46+
"""
47+
Temporarily change working directory.
48+
A context manager which changes the working directory to the given
49+
path, and then changes it back to its previous value on exit.
50+
Usage:
51+
```python
52+
# Do something in original directory
53+
with working_directory('/my/new/path'):
54+
# Do something in new directory
55+
# Back to old directory
56+
```
57+
"""
58+
prev_cwd = os.getcwd()
59+
os.chdir(path)
60+
try:
61+
yield
62+
finally:
63+
os.chdir(prev_cwd)
64+
4365

4466
def get_plugin_config_from_mkdocs(mkdocs_path) -> dict:
4567
# instanciate plugin
@@ -116,43 +138,38 @@ def setup_commit_history(testproject_path):
116138
repo = git.Repo.init(testproject_path, bare=False)
117139
author = "Test Person <[email protected]>"
118140

119-
# Change the working directory
120-
cwd = os.getcwd()
121-
os.chdir(testproject_path)
141+
with working_directory(testproject_path):
122142

123-
try:
143+
# page_with_tags contains tags we replace and test
124144
repo.git.add("docs/page_with_tag.md")
125-
repo.git.commit(message="add homepage", author=author, date="1500854705 -0700")
145+
repo.git.commit(message="add homepage", author=author, date="1500854705 -0700") # Mon Jul 24 2017 00:05:05 GMT+0000
126146

127-
repo.git.add("mkdocs.yml")
128-
repo.git.commit(message="add mkdocs", author=author)
147+
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
148+
with open(file_name, "a") as the_file:
149+
the_file.write("awa\n")
150+
repo.git.add("docs/page_with_tag.md")
151+
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
129152

130153
repo.git.add("docs/first_page.md")
131-
repo.git.commit(message="first page", author=author)
154+
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
132155
file_name = os.path.join(testproject_path, "docs/first_page.md")
133156
with open(file_name, "w+") as the_file:
134157
the_file.write("Hello\n")
135158
repo.git.add("docs/first_page.md")
136-
repo.git.commit(message="first page update 1", author=author)
159+
repo.git.commit(message="first page update 1", author=author, date="1519964705") # Fri Mar 02 2018 04:25:05 GMT+0000
137160
with open(file_name, "w") as the_file:
138161
the_file.write("# First Test Page Edited\n\nSome Lorem text")
139162
repo.git.add("docs/first_page.md")
140-
repo.git.commit(message="first page update 2", author=author)
163+
repo.git.commit(message="first page update 2", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
141164

165+
repo.git.add("mkdocs.yml")
166+
repo.git.commit(message="add mkdocs", author=author, date="1500854705 -0700") # Mon Jul 24 2017 00:05:05 GMT+0000
142167
repo.git.add("docs/second_page.md")
143-
repo.git.commit(message="second page", author=author)
168+
repo.git.commit(message="second page", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
144169
repo.git.add("docs/index.md")
145-
repo.git.commit(message="homepage", author=author)
170+
repo.git.commit(message="homepage", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
171+
146172

147-
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
148-
with open(file_name, "a") as the_file:
149-
the_file.write("awa\n")
150-
repo.git.add("docs/page_with_tag.md")
151-
repo.git.commit(message="update homepage", author=author)
152-
os.chdir(cwd)
153-
except:
154-
os.chdir(cwd)
155-
raise
156173

157174
return repo
158175

@@ -269,6 +286,35 @@ def test_date_formats():
269286
}
270287

271288

289+
290+
291+
@pytest.mark.parametrize("mkdocs_file", [
292+
"basic_project/mkdocs.yml",
293+
"basic_project/mkdocs_creation_date.yml"])
294+
def test_tags_are_replaced(tmp_path, mkdocs_file):
295+
"""
296+
Make sure the {{ }} tags are replaced properly.
297+
"""
298+
testproject_path = setup_clean_mkdocs_folder(
299+
mkdocs_yml_path=f"tests/fixtures/{mkdocs_file}", output_path=tmp_path
300+
)
301+
setup_commit_history(testproject_path)
302+
result = build_docs_setup(testproject_path)
303+
assert result.exit_code == 0, "'mkdocs build' command failed"
304+
305+
tags_file = testproject_path / "site/page_with_tag/index.html"
306+
contents = tags_file.read_text(encoding="utf8")
307+
# Assert {{ git_revision_date_localized }} is replaced
308+
assert re.search(r"January 23, 2022\<\/span.+", contents)
309+
310+
# Assert {{ git_site_revision_date_localized }} is replaced
311+
assert re.search(r"February 3, 2022\<\/span.+", contents)
312+
313+
# Note {{ git_creation_date_localized }} is only replaced when configured in the config
314+
if mkdocs_file == "basic_project/mkdocs_creation_date.yml":
315+
assert re.search(r"July 24, 2017\<\/span.+", contents)
316+
317+
272318
def test_git_not_available(tmp_path, recwarn):
273319
"""
274320
When there is no GIT repo, this should fail

0 commit comments

Comments
 (0)