Skip to content

Commit 8138948

Browse files
committed
Add a test for plugin module testing
1 parent 673d2bc commit 8138948

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

tests/test_plugin.py

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#! python3 # noqa E265
2+
3+
"""
4+
Test for the plugin class (subclass of mkdocs.BasePlugin).
5+
6+
Usage from the repo root folder:
7+
8+
# all tests
9+
python -m unittest tests.test_plugin
10+
11+
# specific test
12+
python -m unittest tests.test_plugin.TestMkdocsPlugin.
13+
"""
14+
15+
# #############################################################################
16+
# ########## Libraries #############
17+
# ##################################
18+
19+
# Standard library
20+
from pathlib import Path
21+
import logging
22+
import unittest
23+
24+
# MkDocs
25+
from mkdocs.config import load_config
26+
27+
# package
28+
from mkdocs_git_revision_date_localized_plugin.plugin import (
29+
GitRevisionDateLocalizedPlugin,
30+
)
31+
32+
33+
# #############################################################################
34+
# ######## Globals #################
35+
# ##################################
36+
37+
# make this test module easily reusable
38+
PLUGIN_NAME = "git-revision-date-localized"
39+
40+
# custom log level to get plugin info messages
41+
logging.basicConfig(level=logging.INFO)
42+
43+
# #############################################################################
44+
# ########## Helpers ###############
45+
# ##################################
46+
47+
48+
# #############################################################################
49+
# ########## Classes ###############
50+
# ##################################
51+
52+
53+
class TestMkdocsPlugin(unittest.TestCase):
54+
"""MkDocs plugin module."""
55+
56+
# -- Standard methods --------------------------------------------------------
57+
@classmethod
58+
def setUpClass(cls):
59+
"""Executed when module is loaded before any test."""
60+
cls.fixtures_mkdocs_config_files = sorted(
61+
Path("tests/basic_setup").glob("*.yml")
62+
)
63+
64+
cls.fixtures_config_cases_ok = {
65+
"default_explicit": {
66+
"type": "date",
67+
"locale": "en",
68+
"fallback_to_build_date": False,
69+
},
70+
# locale variations
71+
"default_no_locale": {"type": "date", "fallback_to_build_date": False},
72+
"custom_locale": {"locale": "fr"},
73+
# type variations
74+
"type_datetime": {"type": "datetime"},
75+
"type_iso_date": {"type": "iso_date"},
76+
"type_iso_datetime": {"type": "iso_datetime"},
77+
"type_timeago": {"type": "timeago"},
78+
# falbback variations
79+
"fallback_true": {"fallback_to_build_date": True},
80+
}
81+
82+
cls.fixtures_config_cases_bad = {
83+
"invalid_option_name": {"language": "en",},
84+
# "invalid_value": {"type": "calendar", "locale": "nl"},
85+
"invalid_value_type": {"type": 1, "locale": "de"},
86+
}
87+
88+
def setUp(self):
89+
"""Executed before each test."""
90+
pass
91+
92+
def tearDown(self):
93+
"""Executed after each test."""
94+
pass
95+
96+
@classmethod
97+
def tearDownClass(cls):
98+
"""Executed after the last test."""
99+
pass
100+
101+
# -- TESTS ---------------------------------------------------------
102+
103+
# -- GET --
104+
def test_plugin_instanciation(self):
105+
"""Simple test plugin instanciation"""
106+
# instanciate
107+
plg = GitRevisionDateLocalizedPlugin()
108+
109+
# default values
110+
self.assertIsInstance(plg.config, dict)
111+
self.assertEqual(plg.config, {})
112+
113+
def test_plugin_load_configs_ok(self):
114+
"""Test inherited plugin load_config method on good configurations"""
115+
# instanciate
116+
plg = GitRevisionDateLocalizedPlugin()
117+
118+
# parse fixtures configurations alone
119+
for i in self.fixtures_config_cases_ok:
120+
cfg = self.fixtures_config_cases_ok.get(i)
121+
out_cfg = plg.load_config(options=cfg)
122+
123+
# check if config loader returned no errors
124+
self.assertIsInstance(out_cfg, tuple)
125+
[self.assertListEqual(v, []) for v in out_cfg]
126+
self.assertEqual(all([len(i) == 0 for i in out_cfg]), True)
127+
128+
# try associating mkdocs configuration
129+
for i in self.fixtures_mkdocs_config_files:
130+
out_cfg_mkdocs = plg.load_config(
131+
options=cfg, config_file_path=str(i.resolve())
132+
)
133+
134+
# check if config loader returned no errors
135+
self.assertIsInstance(out_cfg_mkdocs, tuple)
136+
[self.assertListEqual(v, []) for v in out_cfg_mkdocs]
137+
self.assertEqual(all([len(i) == 0 for i in out_cfg_mkdocs]), True)
138+
139+
def test_plugin_load_configs_bad(self):
140+
"""Test inherited plugin load_config method on bad configurations"""
141+
# instanciate
142+
plg = GitRevisionDateLocalizedPlugin()
143+
144+
# simulate a complete configuration
145+
for i in self.fixtures_config_cases_bad:
146+
cfg = self.fixtures_config_cases_bad.get(i)
147+
out_cfg = plg.load_config(options=cfg)
148+
149+
# check if config loader returned no errors
150+
self.assertIsInstance(out_cfg, tuple)
151+
self.assertEqual(all([len(i) == 0 for i in out_cfg]), False)
152+
153+
# try associating mkdocs configuration
154+
for i in self.fixtures_mkdocs_config_files:
155+
out_cfg_mkdocs = plg.load_config(
156+
options=cfg, config_file_path=str(i.resolve())
157+
)
158+
159+
# check if config loader returned no errors
160+
self.assertIsInstance(out_cfg_mkdocs, tuple)
161+
self.assertEqual(all([len(i) == 0 for i in out_cfg_mkdocs]), False)
162+
163+
def test_plugin_on_config(self):
164+
"""Test inherited plugin on_config method"""
165+
# load try associating mkdocs configuration
166+
for i in self.fixtures_mkdocs_config_files:
167+
# logging.info("Using Mkdocs configuration: %s " % i.resolve())
168+
cfg_mkdocs = load_config(str(i))
169+
170+
# get mkdocs locale config - expected as future feature
171+
mkdocs_locale = cfg_mkdocs.get("locale", None)
172+
173+
# get our plugin config and copy it
174+
plugin_loaded_from_mkdocs = cfg_mkdocs.get("plugins").get(PLUGIN_NAME)
175+
cfg_before_on_config = plugin_loaded_from_mkdocs.config.copy()
176+
177+
# get theme configuration
178+
theme = cfg_mkdocs.get("theme") # -> Theme
179+
180+
# look for the theme locale/language
181+
if "locale" in theme._vars:
182+
theme_locale = theme._vars.get("locale")
183+
elif "language" in theme._vars:
184+
theme_locale = theme._vars.get("language")
185+
else:
186+
theme_locale = None
187+
188+
# execute on_config with global mkdocs loaded configuration and save config
189+
plugin_loaded_from_mkdocs.on_config(cfg_mkdocs)
190+
cfg_after_on_config = plugin_loaded_from_mkdocs.config.copy()
191+
192+
# -- CASES for LOCALE ---------------------------------------
193+
result_locale = cfg_after_on_config.get("locale")
194+
# if locale set in plugin configuration = it the one!
195+
if cfg_before_on_config.get("locale"):
196+
self.assertEqual(result_locale, cfg_before_on_config.get("locale"))
197+
# if locale set in theme: it should be used
198+
elif theme_locale and not cfg_before_on_config.get("locale"):
199+
self.assertEqual(result_locale, theme_locale)
200+
# if locale not set in plugin nor in theme but in mkdocs = mkdocs
201+
elif (
202+
mkdocs_locale
203+
and not cfg_before_on_config.get("locale")
204+
and not theme_locale
205+
):
206+
self.assertEqual(result_locale, mkdocs_locale)
207+
# if locale is not set at all = default = "en"
208+
else:
209+
self.assertEqual(result_locale, "en")
210+
211+
212+
# ##############################################################################
213+
# ##### Stand alone program ########
214+
# ##################################
215+
if __name__ == "__main__":
216+
unittest.main()

0 commit comments

Comments
 (0)