1616import re
1717import shutil
1818from contextlib import contextmanager
19+ from pathlib import Path
1920
2021# MkDocs
2122from mkdocs .__main__ import build_command
@@ -112,6 +113,8 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
112113 )
113114 shutil .rmtree (str (testproject_path ))
114115
116+ # shutil.copytree(str(Path(mkdocs_yml_path).parent), testproject_path)
117+
115118 # Copy correct mkdocs.yml file and our test 'docs/'
116119 if "i18n" in mkdocs_yml_path :
117120 shutil .copytree ("tests/fixtures/i18n/docs" , str (testproject_path / "docs" ))
@@ -120,6 +123,9 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
120123
121124 shutil .copyfile (mkdocs_yml_path , str (testproject_path / "mkdocs.yml" ))
122125
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+
123129 return testproject_path
124130
125131
@@ -143,49 +149,54 @@ def setup_commit_history(testproject_path):
143149 repo = git .Repo .init (testproject_path , bare = False )
144150 author = "Test Person <testtest@gmail.com>"
145151
152+
146153 with working_directory (testproject_path ):
147154
148155 # 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
179189
180190 repo .git .add ("mkdocs.yml" )
181191 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+
184197 repo .git .add ("docs/index.md" )
185198 repo .git .commit (message = "homepage" , author = author , date = "1643911026" ) # Thu Feb 03 2022 17:57:06 GMT+0000
186199
187-
188-
189200 return repo
190201
191202
@@ -264,11 +275,12 @@ def validate_build(testproject_path, plugin_config: dict = {}):
264275 searches = [x in contents for x in date_formats .values ()]
265276 assert any (searches ), "No correct creation date formats output was found"
266277
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
272284
273285def validate_mkdocs_file (temp_path : str , mkdocs_yml_file : str ):
274286 """
@@ -642,3 +654,21 @@ def test_low_fetch_depth(tmp_path, caplog):
642654 assert result .exit_code == 0
643655 assert "Running on GitHub Actions might" in caplog .text
644656
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