99>>> os.mkdir(tmp_path)
1010"""
1111
12+ # #############################################################################
13+ # ########## Libraries #############
14+ # ##################################
15+
1216# standard lib
1317import logging
1418import os
1519import re
1620import shutil
1721
18- # 3rd partu
22+ # MkDocs
23+ from mkdocs .__main__ import build_command
24+ from mkdocs .config import load_config
25+
26+ # other 3rd party
1927import git
2028import pytest
21- import yaml
2229from click .testing import CliRunner
23- from mkdocs .__main__ import build_command
2430
2531# package module
2632from mkdocs_git_revision_date_localized_plugin .util import Util
2733
34+ # #############################################################################
35+ # ######## Globals #################
36+ # ##################################
37+
38+ PLUGIN_NAME = "git-revision-date-localized"
2839
29- def load_config (mkdocs_path ):
30- return yaml .load (open (mkdocs_path , "rb" ), Loader = yaml .Loader )
40+ # custom log level to get plugin info messages
41+ logging .basicConfig (level = logging .INFO )
42+
43+
44+ # #############################################################################
45+ # ########## Helpers ###############
46+ # ##################################
47+ def get_plugin_config_from_mkdocs (mkdocs_path ) -> dict :
48+ # instanciate plugin
49+ cfg_mkdocs = load_config (mkdocs_path )
50+
51+ plugins = cfg_mkdocs .get ("plugins" )
52+ plugin_loaded = plugins .get (PLUGIN_NAME )
53+
54+ cfg = plugin_loaded .on_config (cfg_mkdocs )
55+ logging .info ("Fixture configuration loaded: " + str (cfg ))
56+
57+ assert (
58+ plugin_loaded .config .get ("locale" ) is not None
59+ ), "Locale should never be None after plugin is loaded"
60+
61+ logging .info (
62+ "Locale '%s' determined from %s"
63+ % (plugin_loaded .config .get ("locale" ), mkdocs_path )
64+ )
65+ return plugin_loaded .config
3166
3267
3368def setup_clean_mkdocs_folder (mkdocs_yml_path , output_path ):
@@ -141,7 +176,7 @@ def build_docs_setup(testproject_path):
141176 raise
142177
143178
144- def validate_build (testproject_path ):
179+ def validate_build (testproject_path , plugin_config : dict ):
145180 """
146181 Validates a build from a testproject
147182
@@ -157,19 +192,21 @@ def validate_build(testproject_path):
157192 # Make sure with markdown tag has valid
158193 # git revision date tag
159194 page_with_tag = testproject_path / "site/page_with_tag/index.html"
160- contents = page_with_tag .read_text ()
195+ contents = page_with_tag .read_text (encoding = "utf8" )
161196 assert re .search (r"Markdown tag\:\s[<span>|\w].+" , contents )
162197
163198 repo = Util (testproject_path )
164199 date_formats = repo .get_revision_date_for_file (
165- testproject_path / "docs/page_with_tag.md"
200+ path = testproject_path / "docs/page_with_tag.md" ,
201+ locale = plugin_config .get ("locale" ),
202+ fallback_to_build_date = plugin_config .get ("fallback_to_build_date" ),
166203 )
167204
168205 searches = [re .search (x , contents ) for x in date_formats .values ()]
169206 assert any (searches ), "No correct date formats output was found"
170207
171208
172- def validate_mkdocs_file (temp_path , mkdocs_yml_file ):
209+ def validate_mkdocs_file (temp_path : str , mkdocs_yml_file : str ):
173210 """
174211 Creates a clean mkdocs project
175212 for a mkdocs YML file, builds and validates it.
@@ -184,14 +221,18 @@ def validate_mkdocs_file(temp_path, mkdocs_yml_file):
184221 setup_commit_history (testproject_path )
185222 result = build_docs_setup (testproject_path )
186223 assert result .exit_code == 0 , "'mkdocs build' command failed"
187- validate_build (testproject_path )
188-
189- return testproject_path
190224
225+ # validate build with locale retrieved from mkdocs config file
226+ validate_build (
227+ testproject_path , plugin_config = get_plugin_config_from_mkdocs (mkdocs_yml_file )
228+ )
191229
192- #### Tests ####
230+ return testproject_path
193231
194232
233+ # #############################################################################
234+ # ########### Tests ################
235+ # ##################################
195236def test_date_formats ():
196237 u = Util ()
197238 assert u ._date_formats (1582397529 ) == {
@@ -217,19 +258,6 @@ def test_missing_git_repo(tmp_path):
217258 ), "'mkdocs build' command succeeded while there is no GIT repo"
218259
219260
220- def test_missing_git_repo_ignored (tmp_path ):
221- """
222- When there is no GIT repo, the git error should be ignored.
223- """
224- testproject_path = setup_clean_mkdocs_folder (
225- mkdocs_yml_path = "tests/basic_setup/mkdocs_fallback_to_build_date.yml" ,
226- output_path = tmp_path ,
227- )
228-
229- result = build_docs_setup (testproject_path )
230- assert result .exit_code == 0
231-
232-
233261def test_build_no_options (tmp_path ):
234262 # Enable plugin with no extra options set
235263 validate_mkdocs_file (tmp_path , "tests/basic_setup/mkdocs.yml" )
@@ -241,7 +269,7 @@ def test_build_locale_plugin(tmp_path):
241269
242270
243271def test_build_locale_mkdocs (tmp_path ):
244- # Enable plugin with mkdocs locale set to 'nl '
272+ # Enable plugin with mkdocs locale set to 'fr '
245273 validate_mkdocs_file (tmp_path , "tests/basic_setup/mkdocs_locale.yml" )
246274
247275
@@ -257,10 +285,25 @@ def test_material_theme(tmp_path):
257285 # In mkdocs-material, a 'last update' should appear
258286 # in German because locale is set to 'de'
259287 index_file = testproject_path / "site/index.html"
260- contents = index_file .read_text ()
288+ contents = index_file .read_text (encoding = "utf8" )
261289 assert re .search (r"Letztes Update\:\s[\w].+" , contents )
262290
263291
292+ def test_material_theme_no_locale (tmp_path ):
293+ """
294+ When using mkdocs-material theme, test correct working
295+ """
296+ # theme set to 'material' with 'language' set to 'de'
297+ testproject_path = validate_mkdocs_file (
298+ tmp_path , "tests/basic_setup/mkdocs_theme_no_locale.yml"
299+ )
300+
301+ # In mkdocs-material, a 'last update' should appear
302+ # in German because locale is set to 'de'
303+ index_file = testproject_path / "site/index.html"
304+ contents = index_file .read_text (encoding = "utf8" )
305+ assert re .search (r"Last update\:\s[\w].+" , contents )
306+
264307def test_type_timeago (tmp_path ):
265308 # type: 'timeago'
266309 testproject_path = validate_mkdocs_file (
0 commit comments