Skip to content

Commit 673d2bc

Browse files
committed
Using mkdocs config loader to improve tests
1 parent 82b6a8c commit 673d2bc

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

tests/test_basic.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,63 @@
99
>>> os.mkdir(tmp_path)
1010
"""
1111

12+
# #############################################################################
13+
# ########## Libraries #############
14+
# ##################################
15+
1216
# standard lib
1317
import logging
1418
import os
1519
import re
1620
import 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
1927
import git
2028
import pytest
2129
import yaml
2230
from click.testing import CliRunner
23-
from mkdocs.__main__ import build_command
2431

2532
# package module
2633
from mkdocs_git_revision_date_localized_plugin.plugin import (
2734
GitRevisionDateLocalizedPlugin,
2835
)
2936
from mkdocs_git_revision_date_localized_plugin.util import Util
3037

31-
# GLOBALS
38+
# #############################################################################
39+
# ######## Globals #################
40+
# ##################################
3241

33-
plugin_name = "git-revision-date-localized"
42+
PLUGIN_NAME = "git-revision-date-localized"
3443

3544
# custom log level to get plugin info messages
3645
logging.basicConfig(level=logging.INFO)
3746

38-
# HELPERS
47+
# #############################################################################
48+
# ########## Helpers ###############
49+
# ##################################
3950
def get_locale_from_config(mkdocs_path):
4051
# instanciate plugin
41-
plg = GitRevisionDateLocalizedPlugin()
42-
43-
# return yaml.load(open(mkdocs_path, "rb"), Loader=yaml.Loader)
44-
cfg = yaml.load(open(mkdocs_path, "rb"), Loader=yaml.Loader)
45-
logging.info("Fixture configuration loaded: " + str(cfg))
52+
cfg_mkdocs = load_config(mkdocs_path)
4653

47-
# get plugin config
48-
if "plugins" in cfg and plugin_name not in cfg.get("plugins"):
49-
t = [i for i in cfg.get("plugins") if isinstance(i, dict) and plugin_name in i]
50-
plg.config = t[0].get(plugin_name, {})
51-
elif "plugins" in cfg and plugin_name in cfg.get("plugins"):
52-
plg.config = cfg.get(plugin_name, {})
53-
else:
54-
pass
54+
plugins = cfg_mkdocs.get("plugins")
55+
plugin_loaded = plugins.get(PLUGIN_NAME)
5556

56-
# use plugin config loader
57-
plg.on_config(cfg)
57+
cfg = plugin_loaded.on_config(cfg_mkdocs)
58+
logging.info("Fixture configuration loaded: " + str(cfg))
5859

59-
assert plg.locale is not None, "Locale should never be None after plugin is loaded"
60+
assert (
61+
plugin_loaded.config.get("locale") is not None
62+
), "Locale should never be None after plugin is loaded"
6063

61-
return plg.locale
64+
logging.info(
65+
"Locale '%s' determined from %s"
66+
% (plugin_loaded.config.get("locale"), mkdocs_path)
67+
)
68+
return plugin_loaded.config.get("locale")
6269

6370

6471
def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
@@ -193,8 +200,7 @@ def validate_build(testproject_path, project_locale: str):
193200

194201
repo = Util(testproject_path)
195202
date_formats = repo.get_revision_date_for_file(
196-
path=testproject_path / "docs/page_with_tag.md",
197-
locale=project_locale
203+
path=testproject_path / "docs/page_with_tag.md", locale=project_locale
198204
)
199205

200206
searches = [re.search(x, contents) for x in date_formats.values()]
@@ -225,9 +231,9 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
225231
return testproject_path
226232

227233

228-
#### Tests ####
229-
230-
234+
# #############################################################################
235+
# ########### Tests ################
236+
# ##################################
231237
def test_date_formats():
232238
u = Util()
233239
assert u._date_formats(1582397529) == {

0 commit comments

Comments
 (0)