Debian Codegraph @

mkdocs/tests/config/config_tests.py

raw
1 #!/usr/bin/env python
2
3 import os
4 import unittest
5
6 import mkdocs
7 from mkdocs import config
8 from mkdocs.config import config_options as c
9 from mkdocs.config import defaults
10 from mkdocs.config.base import ValidationError
11 from mkdocs.exceptions import ConfigurationError
12 from mkdocs.localization import parse_locale
13 from mkdocs.tests.base import dedent, tempdir
14
15
16 class ConfigTests(unittest.TestCase):
17 def test_missing_config_file(self):
18 with self.assertRaises(ConfigurationError):
19 config.load_config(config_file='bad_filename.yaml')
20
21 def test_missing_site_name(self):
22 conf = defaults.MkDocsConfig()
23 conf.load_dict({})
24 errors, warnings = conf.validate()
25 self.assertEqual(
26 errors, [('site_name', ValidationError("Required configuration not provided."))]
27 )
28 self.assertEqual(warnings, [])
29
30 def test_nonexistant_config(self):
31 with self.assertRaises(ConfigurationError):
32 config.load_config(config_file='/path/that/is/not/real')
33
34 @tempdir()
35 def test_invalid_config(self, temp_path):
36 file_contents = dedent(
37 """
38 - ['index.md', 'Introduction']
39 - ['index.md', 'Introduction']
40 - ['index.md', 'Introduction']
41 """
42 )
43 config_path = os.path.join(temp_path, 'foo.yml')
44 with open(config_path, 'w') as config_file:
45 config_file.write(file_contents)
46
47 with self.assertRaises(ConfigurationError):
48 config.load_config(config_file=open(config_file.name, 'rb'))
49
50 @tempdir()
51 def test_config_option(self, temp_path):
52 """
53 Users can explicitly set the config file using the '--config' option.
54 Allows users to specify a config other than the default `mkdocs.yml`.
55 """
56 expected_result = {
57 'site_name': 'Example',
58 'nav': [{'Introduction': 'index.md'}],
59 }
60 file_contents = dedent(
61 """
62 site_name: Example
63 nav:
64 - 'Introduction': 'index.md'
65 """
66 )
67 config_path = os.path.join(temp_path, 'mkdocs.yml')
68 with open(config_path, 'w') as config_file:
69 config_file.write(file_contents)
70 os.mkdir(os.path.join(temp_path, 'docs'))
71
72 result = config.load_config(config_file=config_file.name)
73 self.assertEqual(result['site_name'], expected_result['site_name'])
74 self.assertEqual(result['nav'], expected_result['nav'])
75
76 @tempdir()
77 @tempdir()
78 def test_theme(self, mytheme, custom):
79 configs = [
80 {}, # default theme
81 {"theme": "readthedocs"}, # builtin theme
82 {"theme": {'name': 'readthedocs'}}, # builtin as complex
83 {"theme": {'name': None, 'custom_dir': mytheme}}, # custom only as complex
84 {
85 "theme": {'name': 'readthedocs', 'custom_dir': custom}
86 }, # builtin and custom as complex
87 { # user defined variables
88 'theme': {
89 'name': 'mkdocs',
90 'locale': 'fr',
91 'static_templates': ['foo.html'],
92 'show_sidebar': False,
93 'some_var': 'bar',
94 }
95 },
96 ]
97
98 mkdocs_dir = os.path.abspath(os.path.dirname(mkdocs.__file__))
99 mkdocs_templates_dir = os.path.join(mkdocs_dir, 'templates')
100 theme_dir = os.path.abspath(os.path.join(mkdocs_dir, 'themes'))
101
102 results = (
103 {
104 'dirs': [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir],
105 'static_templates': ['404.html', 'sitemap.xml'],
106 'vars': {
107 'name': 'mkdocs',
108 'color_mode': 'light',
109 'user_color_mode_toggle': False,
110 'locale': parse_locale('en'),
111 'include_search_page': False,
112 'search_index_only': False,
113 'analytics': {'gtag': None},
114 'highlightjs': True,
115 'hljs_style': 'github',
116 'hljs_languages': [],
117 'hljs_style_dark': 'github-dark',
118 'navigation_depth': 2,
119 'nav_style': 'primary',
120 'shortcuts': {'help': 191, 'next': 78, 'previous': 80, 'search': 83},
121 },
122 },
123 {
124 'dirs': [os.path.join(theme_dir, 'readthedocs'), mkdocs_templates_dir],
125 'static_templates': ['404.html', 'sitemap.xml'],
126 'vars': {
127 'name': 'readthedocs',
128 'locale': parse_locale('en'),
129 'include_search_page': True,
130 'search_index_only': False,
131 'analytics': {'anonymize_ip': False, 'gtag': None},
132 'highlightjs': True,
133 'hljs_languages': [],
134 'hljs_style': 'github',
135 'include_homepage_in_sidebar': True,
136 'prev_next_buttons_location': 'bottom',
137 'navigation_depth': 4,
138 'sticky_navigation': True,
139 'logo': None,
140 'titles_only': False,
141 'collapse_navigation': True,
142 },
143 },
144 {
145 'dirs': [os.path.join(theme_dir, 'readthedocs'), mkdocs_templates_dir],
146 'static_templates': ['404.html', 'sitemap.xml'],
147 'vars': {
148 'name': 'readthedocs',
149 'locale': parse_locale('en'),
150 'include_search_page': True,
151 'search_index_only': False,
152 'analytics': {'anonymize_ip': False, 'gtag': None},
153 'highlightjs': True,
154 'hljs_languages': [],
155 'hljs_style': 'github',
156 'include_homepage_in_sidebar': True,
157 'prev_next_buttons_location': 'bottom',
158 'navigation_depth': 4,
159 'sticky_navigation': True,
160 'logo': None,
161 'titles_only': False,
162 'collapse_navigation': True,
163 },
164 },
165 {
166 'dirs': [mytheme, mkdocs_templates_dir],
167 'static_templates': ['sitemap.xml'],
168 'vars': {'name': None, 'locale': parse_locale('en')},
169 },
170 {
171 'dirs': [custom, os.path.join(theme_dir, 'readthedocs'), mkdocs_templates_dir],
172 'static_templates': ['404.html', 'sitemap.xml'],
173 'vars': {
174 'name': 'readthedocs',
175 'locale': parse_locale('en'),
176 'include_search_page': True,
177 'search_index_only': False,
178 'analytics': {'anonymize_ip': False, 'gtag': None},
179 'highlightjs': True,
180 'hljs_languages': [],
181 'hljs_style': 'github',
182 'include_homepage_in_sidebar': True,
183 'prev_next_buttons_location': 'bottom',
184 'navigation_depth': 4,
185 'sticky_navigation': True,
186 'logo': None,
187 'titles_only': False,
188 'collapse_navigation': True,
189 },
190 },
191 {
192 'dirs': [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir],
193 'static_templates': ['404.html', 'sitemap.xml', 'foo.html'],
194 'vars': {
195 'name': 'mkdocs',
196 'color_mode': 'light',
197 'user_color_mode_toggle': False,
198 'locale': parse_locale('fr'),
199 'show_sidebar': False,
200 'some_var': 'bar',
201 'include_search_page': False,
202 'search_index_only': False,
203 'analytics': {'gtag': None},
204 'highlightjs': True,
205 'hljs_style': 'github',
206 'hljs_languages': [],
207 'hljs_style_dark': 'github-dark',
208 'navigation_depth': 2,
209 'nav_style': 'primary',
210 'shortcuts': {'help': 191, 'next': 78, 'previous': 80, 'search': 83},
211 },
212 },
213 )
214
215 for config_contents, result in zip(configs, results):
216 with self.subTest(config_contents):
217 conf = config.Config(schema=(('theme', c.Theme(default='mkdocs')),))
218 conf.load_dict(config_contents)
219 errors, warnings = conf.validate()
220 self.assertEqual(errors, [])
221 self.assertEqual(warnings, [])
222 self.assertEqual(conf['theme'].dirs, result['dirs'])
223 self.assertEqual(conf['theme'].static_templates, set(result['static_templates']))
224 self.assertEqual(dict(conf['theme']), result['vars'])
225
226 def test_empty_nav(self):
227 conf = defaults.MkDocsConfig(
228 config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml')
229 )
230 conf.load_dict({'site_name': 'Example'})
231 conf.validate()
232 self.assertEqual(conf['nav'], None)
233
234 def test_error_on_pages(self):
235 conf = defaults.MkDocsConfig()
236 conf.load_dict(
237 {
238 'site_name': 'Example',
239 'pages': ['index.md', 'about.md'],
240 }
241 )
242 errors, warnings = conf.validate()
243 exp_error = "The configuration option 'pages' was removed from MkDocs. Use 'nav' instead."
244 self.assertEqual(errors, [('pages', ValidationError(exp_error))])
245 self.assertEqual(warnings, [])
246
247 def test_doc_dir_in_site_dir(self):
248 test_configs = (
249 {'docs_dir': os.path.join('site', 'docs'), 'site_dir': 'site'},
250 {'docs_dir': 'docs', 'site_dir': '.'},
251 {'docs_dir': '.', 'site_dir': '.'},
252 {'docs_dir': 'docs', 'site_dir': ''},
253 {'docs_dir': '', 'site_dir': ''},
254 {'docs_dir': 'docs', 'site_dir': 'docs'},
255 )
256
257 for test_config in test_configs:
258 with self.subTest(test_config):
259 # Same as the default schema, but don't verify the docs_dir exists.
260 conf = config.Config(
261 schema=(
262 ('docs_dir', c.Dir(default='docs')),
263 ('site_dir', c.SiteDir(default='site')),
264 ),
265 config_file_path=os.path.join(os.path.abspath('..'), 'mkdocs.yml'),
266 )
267 conf.load_dict(test_config)
268
269 errors, warnings = conf.validate()
270
271 self.assertEqual(len(errors), 1)
272 self.assertEqual(warnings, [])