16
16
import re
17
17
import shutil
18
18
from contextlib import contextmanager
19
+ from pathlib import Path
19
20
20
21
# MkDocs
21
22
from mkdocs .__main__ import build_command
@@ -112,6 +113,8 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
112
113
)
113
114
shutil .rmtree (str (testproject_path ))
114
115
116
+ # shutil.copytree(str(Path(mkdocs_yml_path).parent), testproject_path)
117
+
115
118
# Copy correct mkdocs.yml file and our test 'docs/'
116
119
if "i18n" in mkdocs_yml_path :
117
120
shutil .copytree ("tests/fixtures/i18n/docs" , str (testproject_path / "docs" ))
@@ -120,6 +123,9 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
120
123
121
124
shutil .copyfile (mkdocs_yml_path , str (testproject_path / "mkdocs.yml" ))
122
125
126
+ if "gen-files" in mkdocs_yml_path :
127
+ shutil .copyfile (str (Path (mkdocs_yml_path ).parent / "gen_pages.py" ), str (testproject_path / "gen_pages.py" ))
128
+
123
129
return testproject_path
124
130
125
131
@@ -143,49 +149,54 @@ def setup_commit_history(testproject_path):
143
149
repo = git .Repo .init (testproject_path , bare = False )
144
150
author = "Test Person <[email protected] >"
145
151
152
+
146
153
with working_directory (testproject_path ):
147
154
148
155
# page_with_tags contains tags we replace and test
149
- repo .git .add ("docs/page_with_tag.md" )
150
- repo .git .commit (message = "add homepage" , author = author , date = "1500854705" ) # Mon Jul 24 2017 00:05:05 GMT+0000
151
-
152
- file_name = os .path .join (testproject_path , "docs/page_with_tag.md" )
153
- with open (file_name , "a" ) as the_file :
154
- the_file .write ("awa\n " )
155
- repo .git .add ("docs/page_with_tag.md" )
156
- repo .git .commit (message = "update homepage" , author = author , date = "1642911026" ) # Sun Jan 23 2022 04:10:26 GMT+0000
157
-
158
- bf_file_name = os .path .join (testproject_path , "docs/page_with_renamed.md" )
159
- af_file_name = os .path .join (testproject_path , "docs/subfolder/page_with_renamed.md" )
160
- # Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
161
- if os .path .exists (af_file_name ):
162
- os .replace (af_file_name , bf_file_name )
163
- repo .git .add ("docs/page_with_renamed.md" )
164
- repo .git .commit (message = "page_with_renamed.md before renamed" , author = author , date = "1655229469" ) # Tue Jun 14 2022 17:57:49 GMT+0000
165
- repo .git .mv ("docs/page_with_renamed.md" , "docs/subfolder/page_with_renamed.md" )
166
- repo .git .commit (message = "page_with_renamed.md after renamed" , author = author , date = "1655229515" ) # Tue Jun 14 2022 17:58:35 GMT+0000
167
-
168
- repo .git .add ("docs/first_page.md" )
169
- repo .git .commit (message = "first page" , author = author , date = "1500854705" ) # Mon Jul 24 2017 00:05:05 GMT+0000
170
- file_name = os .path .join (testproject_path , "docs/first_page.md" )
171
- with open (file_name , "w+" ) as the_file :
172
- the_file .write ("Hello\n " )
173
- repo .git .add ("docs/first_page.md" )
174
- repo .git .commit (message = "first page update 1" , author = author , date = "1519964705" ) # Fri Mar 02 2018 04:25:05 GMT+0000
175
- with open (file_name , "w" ) as the_file :
176
- the_file .write ("# First Test Page Edited\n \n Some Lorem text" )
177
- repo .git .add ("docs/first_page.md" )
178
- repo .git .commit (message = "first page update 2" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
156
+ if os .path .exists ("docs/page_with_tag.md" ):
157
+ repo .git .add ("docs/page_with_tag.md" )
158
+ repo .git .commit (message = "add homepage" , author = author , date = "1500854705" ) # Mon Jul 24 2017 00:05:05 GMT+0000
159
+
160
+ file_name = os .path .join (testproject_path , "docs/page_with_tag.md" )
161
+ with open (file_name , "a" ) as the_file :
162
+ the_file .write ("awa\n " )
163
+ repo .git .add ("docs/page_with_tag.md" )
164
+ repo .git .commit (message = "update homepage" , author = author , date = "1642911026" ) # Sun Jan 23 2022 04:10:26 GMT+0000
165
+
166
+ if os .path .exists ("docs/page_with_renamed.md" ):
167
+ bf_file_name = os .path .join (testproject_path , "docs/page_with_renamed.md" )
168
+ af_file_name = os .path .join (testproject_path , "docs/subfolder/page_with_renamed.md" )
169
+ # Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
170
+ if os .path .exists (af_file_name ):
171
+ os .replace (af_file_name , bf_file_name )
172
+ repo .git .add ("docs/page_with_renamed.md" )
173
+ repo .git .commit (message = "page_with_renamed.md before renamed" , author = author , date = "1655229469" ) # Tue Jun 14 2022 17:57:49 GMT+0000
174
+ repo .git .mv ("docs/page_with_renamed.md" , "docs/subfolder/page_with_renamed.md" )
175
+ repo .git .commit (message = "page_with_renamed.md after renamed" , author = author , date = "1655229515" ) # Tue Jun 14 2022 17:58:35 GMT+0000
176
+
177
+ if os .path .exists ("docs/first_page.md" ):
178
+ repo .git .add ("docs/first_page.md" )
179
+ repo .git .commit (message = "first page" , author = author , date = "1500854705" ) # Mon Jul 24 2017 00:05:05 GMT+0000
180
+ file_name = os .path .join (testproject_path , "docs/first_page.md" )
181
+ with open (file_name , "w+" ) as the_file :
182
+ the_file .write ("Hello\n " )
183
+ repo .git .add ("docs/first_page.md" )
184
+ repo .git .commit (message = "first page update 1" , author = author , date = "1519964705" ) # Fri Mar 02 2018 04:25:05 GMT+0000
185
+ with open (file_name , "w" ) as the_file :
186
+ the_file .write ("# First Test Page Edited\n \n Some Lorem text" )
187
+ repo .git .add ("docs/first_page.md" )
188
+ repo .git .commit (message = "first page update 2" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
179
189
180
190
repo .git .add ("mkdocs.yml" )
181
191
repo .git .commit (message = "add mkdocs" , author = author , date = "1500854705 -0700" ) # Mon Jul 24 2017 00:05:05 GMT+0000
182
- repo .git .add ("docs/second_page.md" )
183
- repo .git .commit (message = "second page" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
192
+
193
+ if os .path .exists ("docs/second_page.md" ):
194
+ repo .git .add ("docs/second_page.md" )
195
+ repo .git .commit (message = "second page" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
196
+
184
197
repo .git .add ("docs/index.md" )
185
198
repo .git .commit (message = "homepage" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
186
199
187
-
188
-
189
200
return repo
190
201
191
202
@@ -264,11 +275,12 @@ def validate_build(testproject_path, plugin_config: dict = {}):
264
275
searches = [x in contents for x in date_formats .values ()]
265
276
assert any (searches ), "No correct creation date formats output was found"
266
277
267
- commit_timestamp = repo .get_git_commit_timestamp (
268
- path = str (testproject_path / "docs/subfolder/page_with_renamed.md" ),
269
- is_first_commit = True
270
- )
271
- assert commit_timestamp == 1655229469
278
+ if os .path .exists (str (testproject_path / "docs/subfolder/page_with_renamed.md" )):
279
+ commit_timestamp = repo .get_git_commit_timestamp (
280
+ path = str (testproject_path / "docs/subfolder/page_with_renamed.md" ),
281
+ is_first_commit = True
282
+ )
283
+ assert commit_timestamp == 1655229469
272
284
273
285
def validate_mkdocs_file (temp_path : str , mkdocs_yml_file : str ):
274
286
"""
@@ -642,3 +654,21 @@ def test_low_fetch_depth(tmp_path, caplog):
642
654
assert result .exit_code == 0
643
655
assert "Running on GitHub Actions might" in caplog .text
644
656
657
+
658
+
659
+ def test_mkdocs_genfiles_plugin (tmp_path ):
660
+ """
661
+ Make sure the mkdocs-gen-files plugin works correctly.
662
+ """
663
+ testproject_path = setup_clean_mkdocs_folder (
664
+ mkdocs_yml_path = f"tests/fixtures/mkdocs-gen-files/mkdocs.yml" , output_path = tmp_path
665
+ )
666
+ setup_commit_history (testproject_path )
667
+ result = build_docs_setup (testproject_path )
668
+ assert result .exit_code == 0 , f"'mkdocs build' command failed with { result .stdout } "
669
+
670
+ # validate the build
671
+ plugin_config = get_plugin_config_from_mkdocs (str (testproject_path / "mkdocs.yml" ))
672
+ validate_build (
673
+ testproject_path , plugin_config
674
+ )
0 commit comments