|
15 | 15 | import os
|
16 | 16 | import re
|
17 | 17 | import shutil
|
| 18 | +from contextlib import contextmanager |
18 | 19 |
|
19 | 20 | # MkDocs
|
20 | 21 | from mkdocs.__main__ import build_command
|
|
40 | 41 | # ########## Helpers ###############
|
41 | 42 | # ##################################
|
42 | 43 |
|
| 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 | + |
43 | 65 |
|
44 | 66 | def get_plugin_config_from_mkdocs(mkdocs_path) -> dict:
|
45 | 67 | # instanciate plugin
|
@@ -116,43 +138,38 @@ def setup_commit_history(testproject_path):
|
116 | 138 | repo = git.Repo.init(testproject_path, bare=False)
|
117 | 139 | author = "Test Person <[email protected]>"
|
118 | 140 |
|
119 |
| - # Change the working directory |
120 |
| - cwd = os.getcwd() |
121 |
| - os.chdir(testproject_path) |
| 141 | + with working_directory(testproject_path): |
122 | 142 |
|
123 |
| - try: |
| 143 | + # page_with_tags contains tags we replace and test |
124 | 144 | 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 |
126 | 146 |
|
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 |
129 | 152 |
|
130 | 153 | 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 |
132 | 155 | file_name = os.path.join(testproject_path, "docs/first_page.md")
|
133 | 156 | with open(file_name, "w+") as the_file:
|
134 | 157 | the_file.write("Hello\n")
|
135 | 158 | 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 |
137 | 160 | with open(file_name, "w") as the_file:
|
138 | 161 | the_file.write("# First Test Page Edited\n\nSome Lorem text")
|
139 | 162 | 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 |
141 | 164 |
|
| 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 |
142 | 167 | 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 |
144 | 169 | 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 | + |
146 | 172 |
|
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 |
156 | 173 |
|
157 | 174 | return repo
|
158 | 175 |
|
@@ -269,6 +286,35 @@ def test_date_formats():
|
269 | 286 | }
|
270 | 287 |
|
271 | 288 |
|
| 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 | + |
272 | 318 | def test_git_not_available(tmp_path, recwarn):
|
273 | 319 | """
|
274 | 320 | When there is no GIT repo, this should fail
|
|
0 commit comments