9
9
>>> os.mkdir(tmp_path)
10
10
"""
11
11
12
+ # #############################################################################
13
+ # ########## Libraries #############
14
+ # ##################################
15
+
12
16
# standard lib
13
17
import logging
14
18
import os
15
19
import re
16
20
import shutil
17
21
18
- # 3rd partu
22
+ # MkDocs
23
+ from mkdocs .__main__ import build_command
24
+ from mkdocs .config import load_config
25
+
26
+ # other 3rd party
19
27
import git
20
28
import pytest
21
- import yaml
22
29
from click .testing import CliRunner
23
- from mkdocs .__main__ import build_command
24
30
25
31
# package module
26
32
from mkdocs_git_revision_date_localized_plugin .util import Util
27
33
34
+ # #############################################################################
35
+ # ######## Globals #################
36
+ # ##################################
37
+
38
+ PLUGIN_NAME = "git-revision-date-localized"
28
39
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
31
66
32
67
33
68
def setup_clean_mkdocs_folder (mkdocs_yml_path , output_path ):
@@ -141,7 +176,7 @@ def build_docs_setup(testproject_path):
141
176
raise
142
177
143
178
144
- def validate_build (testproject_path ):
179
+ def validate_build (testproject_path , plugin_config : dict ):
145
180
"""
146
181
Validates a build from a testproject
147
182
@@ -157,19 +192,21 @@ def validate_build(testproject_path):
157
192
# Make sure with markdown tag has valid
158
193
# git revision date tag
159
194
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" )
161
196
assert re .search (r"Markdown tag\:\s[<span>|\w].+" , contents )
162
197
163
198
repo = Util (testproject_path )
164
199
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" ),
166
203
)
167
204
168
205
searches = [re .search (x , contents ) for x in date_formats .values ()]
169
206
assert any (searches ), "No correct date formats output was found"
170
207
171
208
172
- def validate_mkdocs_file (temp_path , mkdocs_yml_file ):
209
+ def validate_mkdocs_file (temp_path : str , mkdocs_yml_file : str ):
173
210
"""
174
211
Creates a clean mkdocs project
175
212
for a mkdocs YML file, builds and validates it.
@@ -184,14 +221,18 @@ def validate_mkdocs_file(temp_path, mkdocs_yml_file):
184
221
setup_commit_history (testproject_path )
185
222
result = build_docs_setup (testproject_path )
186
223
assert result .exit_code == 0 , "'mkdocs build' command failed"
187
- validate_build (testproject_path )
188
-
189
- return testproject_path
190
224
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
+ )
191
229
192
- #### Tests ####
230
+ return testproject_path
193
231
194
232
233
+ # #############################################################################
234
+ # ########### Tests ################
235
+ # ##################################
195
236
def test_date_formats ():
196
237
u = Util ()
197
238
assert u ._date_formats (1582397529 ) == {
@@ -217,19 +258,6 @@ def test_missing_git_repo(tmp_path):
217
258
), "'mkdocs build' command succeeded while there is no GIT repo"
218
259
219
260
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
-
233
261
def test_build_no_options (tmp_path ):
234
262
# Enable plugin with no extra options set
235
263
validate_mkdocs_file (tmp_path , "tests/basic_setup/mkdocs.yml" )
@@ -241,7 +269,7 @@ def test_build_locale_plugin(tmp_path):
241
269
242
270
243
271
def test_build_locale_mkdocs (tmp_path ):
244
- # Enable plugin with mkdocs locale set to 'nl '
272
+ # Enable plugin with mkdocs locale set to 'fr '
245
273
validate_mkdocs_file (tmp_path , "tests/basic_setup/mkdocs_locale.yml" )
246
274
247
275
@@ -257,10 +285,25 @@ def test_material_theme(tmp_path):
257
285
# In mkdocs-material, a 'last update' should appear
258
286
# in German because locale is set to 'de'
259
287
index_file = testproject_path / "site/index.html"
260
- contents = index_file .read_text ()
288
+ contents = index_file .read_text (encoding = "utf8" )
261
289
assert re .search (r"Letztes Update\:\s[\w].+" , contents )
262
290
263
291
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
+
264
307
def test_type_timeago (tmp_path ):
265
308
# type: 'timeago'
266
309
testproject_path = validate_mkdocs_file (
0 commit comments