| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import copy |
| 5 |
import io |
| 6 |
import os |
| 7 |
import re |
| 8 |
import sys |
| 9 |
import textwrap |
| 10 |
import unittest |
| 11 |
from typing import Any |
| 12 |
from unittest import mock |
| 13 |
|
| 14 |
import mkdocs |
| 15 |
from mkdocs.config import base |
| 16 |
from mkdocs.config import config_options as c |
| 17 |
from mkdocs.tests.base import tempdir |
| 18 |
from mkdocs.utils import write_file, yaml_load |
| 19 |
|
| 20 |
|
| 21 |
class UnexpectedError(Exception): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
class TestCase(unittest.TestCase): |
| 26 |
@contextlib.contextmanager |
| 27 |
def expect_error(self, **kwargs): |
| 28 |
[(key, msg)] = kwargs.items() |
| 29 |
with self.assertRaises(UnexpectedError) as cm: |
| 30 |
yield |
| 31 |
if isinstance(msg, re.Pattern): |
| 32 |
self.assertRegex(str(cm.exception), f'^{key}="{msg.pattern}"$') |
| 33 |
else: |
| 34 |
self.assertEqual(f'{key}="{msg}"', str(cm.exception)) |
| 35 |
|
| 36 |
def get_config( |
| 37 |
self, |
| 38 |
schema: type, |
| 39 |
cfg: dict[str, Any], |
| 40 |
warnings: dict[str, str] = {}, |
| 41 |
config_file_path=None, |
| 42 |
): |
| 43 |
config = base.LegacyConfig(base.get_schema(schema), config_file_path=config_file_path) |
| 44 |
config.load_dict(cfg) |
| 45 |
actual_errors, actual_warnings = config.validate() |
| 46 |
if actual_errors: |
| 47 |
raise UnexpectedError(', '.join(f'{key}="{msg}"' for key, msg in actual_errors)) |
| 48 |
self.assertEqual(warnings, dict(actual_warnings)) |
| 49 |
return config |
| 50 |
|
| 51 |
|
| 52 |
class OptionallyRequiredTest(TestCase): |
| 53 |
def test_empty(self): |
| 54 |
class Schema: |
| 55 |
option = c.OptionallyRequired() |
| 56 |
|
| 57 |
conf = self.get_config(Schema, {'option': None}) |
| 58 |
self.assertEqual(conf['option'], None) |
| 59 |
|
| 60 |
self.assertEqual(Schema.option.required, False) |
| 61 |
|
| 62 |
def test_required(self): |
| 63 |
class Schema: |
| 64 |
option = c.OptionallyRequired(required=True) |
| 65 |
|
| 66 |
with self.expect_error(option="Required configuration not provided."): |
| 67 |
self.get_config(Schema, {'option': None}) |
| 68 |
|
| 69 |
self.assertEqual(Schema.option.required, True) |
| 70 |
|
| 71 |
def test_required_no_default(self): |
| 72 |
class Schema: |
| 73 |
option = c.OptionallyRequired(required=True) |
| 74 |
|
| 75 |
conf = self.get_config(Schema, {'option': 2}) |
| 76 |
self.assertEqual(conf['option'], 2) |
| 77 |
|
| 78 |
def test_default(self): |
| 79 |
class Schema: |
| 80 |
option = c.OptionallyRequired(default=1) |
| 81 |
|
| 82 |
conf = self.get_config(Schema, {'option': None}) |
| 83 |
self.assertEqual(conf['option'], 1) |
| 84 |
|
| 85 |
def test_replace_default(self): |
| 86 |
class Schema: |
| 87 |
option = c.OptionallyRequired(default=1) |
| 88 |
|
| 89 |
conf = self.get_config(Schema, {'option': 2}) |
| 90 |
self.assertEqual(conf['option'], 2) |
| 91 |
|
| 92 |
|
| 93 |
class TypeTest(TestCase): |
| 94 |
def test_single_type(self): |
| 95 |
class Schema: |
| 96 |
option = c.Type(str) |
| 97 |
|
| 98 |
conf = self.get_config(Schema, {'option': "Testing"}) |
| 99 |
self.assertEqual(conf['option'], "Testing") |
| 100 |
|
| 101 |
def test_multiple_types(self): |
| 102 |
class Schema: |
| 103 |
option = c.Type((list, tuple)) |
| 104 |
|
| 105 |
conf = self.get_config(Schema, {'option': [1, 2, 3]}) |
| 106 |
self.assertEqual(conf['option'], [1, 2, 3]) |
| 107 |
|
| 108 |
conf = self.get_config(Schema, {'option': (1, 2, 3)}) |
| 109 |
self.assertEqual(conf['option'], (1, 2, 3)) |
| 110 |
|
| 111 |
with self.expect_error( |
| 112 |
option="Expected type: (<class 'list'>, <class 'tuple'>) but received: <class 'dict'>" |
| 113 |
): |
| 114 |
self.get_config(Schema, {'option': {'a': 1}}) |
| 115 |
|
| 116 |
def test_length(self): |
| 117 |
class Schema: |
| 118 |
option = c.Type(str, length=7) |
| 119 |
|
| 120 |
conf = self.get_config(Schema, {'option': "Testing"}) |
| 121 |
self.assertEqual(conf['option'], "Testing") |
| 122 |
|
| 123 |
with self.expect_error( |
| 124 |
option="Expected type: <class 'str'> with length 7 but received: 'Testing Long' with length 12" |
| 125 |
): |
| 126 |
self.get_config(Schema, {'option': "Testing Long"}) |
| 127 |
|
| 128 |
|
| 129 |
class ChoiceTest(TestCase): |
| 130 |
def test_required(self): |
| 131 |
class Schema: |
| 132 |
option = c.Choice(('python', 'node'), required=True) |
| 133 |
|
| 134 |
conf = self.get_config(Schema, {'option': 'python'}) |
| 135 |
self.assertEqual(conf['option'], 'python') |
| 136 |
|
| 137 |
def test_optional(self): |
| 138 |
class Schema: |
| 139 |
option = c.Choice(('python', 'node')) |
| 140 |
|
| 141 |
conf = self.get_config(Schema, {'option': 'python'}) |
| 142 |
self.assertEqual(conf['option'], 'python') |
| 143 |
|
| 144 |
conf = self.get_config(Schema, {}) |
| 145 |
self.assertEqual(conf['option'], None) |
| 146 |
|
| 147 |
conf = self.get_config(Schema, {'option': None}) |
| 148 |
self.assertEqual(conf['option'], None) |
| 149 |
|
| 150 |
def test_default(self): |
| 151 |
class Schema: |
| 152 |
option = c.Choice(('a', 'b', 'c'), default='b') |
| 153 |
|
| 154 |
conf = self.get_config(Schema, {}) |
| 155 |
self.assertEqual(conf['option'], 'b') |
| 156 |
|
| 157 |
conf = self.get_config(Schema, {'option': None}) |
| 158 |
self.assertEqual(conf['option'], 'b') |
| 159 |
|
| 160 |
with self.expect_error(option="Expected one of: ('a', 'b', 'c') but received: 'go'"): |
| 161 |
self.get_config(Schema, {'option': 'go'}) |
| 162 |
|
| 163 |
def test_invalid_default(self): |
| 164 |
with self.assertRaises(ValueError): |
| 165 |
c.Choice(('a', 'b'), default='c') |
| 166 |
with self.assertRaises(ValueError): |
| 167 |
c.Choice(('a', 'b'), default='c', required=True) |
| 168 |
|
| 169 |
def test_invalid_choice(self): |
| 170 |
class Schema: |
| 171 |
option = c.Choice(('python', 'node')) |
| 172 |
|
| 173 |
with self.expect_error(option="Expected one of: ('python', 'node') but received: 'go'"): |
| 174 |
self.get_config(Schema, {'option': 'go'}) |
| 175 |
|
| 176 |
def test_invalid_choices(self): |
| 177 |
with self.assertRaises(ValueError): |
| 178 |
c.Choice('') |
| 179 |
with self.assertRaises(ValueError): |
| 180 |
c.Choice([]) |
| 181 |
with self.assertRaises(ValueError): |
| 182 |
c.Choice(5) |
| 183 |
|
| 184 |
|
| 185 |
class DeprecatedTest(TestCase): |
| 186 |
def test_deprecated_option_simple(self): |
| 187 |
class Schema: |
| 188 |
d = c.Deprecated() |
| 189 |
|
| 190 |
self.get_config( |
| 191 |
Schema, |
| 192 |
{'d': 'value'}, |
| 193 |
warnings=dict( |
| 194 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 195 |
"future release." |
| 196 |
), |
| 197 |
) |
| 198 |
|
| 199 |
def test_deprecated_option_message(self): |
| 200 |
class Schema: |
| 201 |
d = c.Deprecated(message='custom message for {} key') |
| 202 |
|
| 203 |
self.get_config(Schema, {'d': 'value'}, warnings={'d': 'custom message for d key'}) |
| 204 |
|
| 205 |
def test_deprecated_option_with_type(self): |
| 206 |
class Schema: |
| 207 |
d = c.Deprecated(option_type=c.Type(str)) |
| 208 |
|
| 209 |
self.get_config( |
| 210 |
Schema, |
| 211 |
{'d': 'value'}, |
| 212 |
warnings=dict( |
| 213 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 214 |
"future release." |
| 215 |
), |
| 216 |
) |
| 217 |
|
| 218 |
def test_deprecated_option_with_invalid_type(self): |
| 219 |
class Schema: |
| 220 |
d = c.Deprecated(option_type=c.Type(list)) |
| 221 |
|
| 222 |
with self.expect_error(d="Expected type: <class 'list'> but received: <class 'str'>"): |
| 223 |
self.get_config( |
| 224 |
Schema, |
| 225 |
{'d': 'value'}, |
| 226 |
warnings=dict( |
| 227 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 228 |
"future release." |
| 229 |
), |
| 230 |
) |
| 231 |
|
| 232 |
def test_removed_option(self): |
| 233 |
class Schema: |
| 234 |
d = c.Deprecated(removed=True, moved_to='foo') |
| 235 |
|
| 236 |
with self.expect_error( |
| 237 |
d="The configuration option 'd' was removed from MkDocs. Use 'foo' instead.", |
| 238 |
): |
| 239 |
self.get_config(Schema, {'d': 'value'}) |
| 240 |
|
| 241 |
def test_deprecated_option_with_type_undefined(self): |
| 242 |
class Schema: |
| 243 |
option = c.Deprecated(option_type=c.Type(str)) |
| 244 |
|
| 245 |
self.get_config(Schema, {'option': None}) |
| 246 |
|
| 247 |
def test_deprecated_option_move(self): |
| 248 |
class Schema: |
| 249 |
new = c.Type(str) |
| 250 |
old = c.Deprecated(moved_to='new') |
| 251 |
|
| 252 |
conf = self.get_config( |
| 253 |
Schema, |
| 254 |
{'old': 'value'}, |
| 255 |
warnings=dict( |
| 256 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 257 |
"future release. Use 'new' instead." |
| 258 |
), |
| 259 |
) |
| 260 |
self.assertEqual(conf, {'new': 'value', 'old': None}) |
| 261 |
|
| 262 |
def test_deprecated_option_move_complex(self): |
| 263 |
class Schema: |
| 264 |
foo = c.Type(dict) |
| 265 |
old = c.Deprecated(moved_to='foo.bar') |
| 266 |
|
| 267 |
conf = self.get_config( |
| 268 |
Schema, |
| 269 |
{'old': 'value'}, |
| 270 |
warnings=dict( |
| 271 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 272 |
"future release. Use 'foo.bar' instead." |
| 273 |
), |
| 274 |
) |
| 275 |
self.assertEqual(conf, {'foo': {'bar': 'value'}, 'old': None}) |
| 276 |
|
| 277 |
def test_deprecated_option_move_existing(self): |
| 278 |
class Schema: |
| 279 |
foo = c.Type(dict) |
| 280 |
old = c.Deprecated(moved_to='foo.bar') |
| 281 |
|
| 282 |
conf = self.get_config( |
| 283 |
Schema, |
| 284 |
{'old': 'value', 'foo': {'existing': 'existing'}}, |
| 285 |
warnings=dict( |
| 286 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 287 |
"future release. Use 'foo.bar' instead." |
| 288 |
), |
| 289 |
) |
| 290 |
self.assertEqual(conf, {'foo': {'existing': 'existing', 'bar': 'value'}, 'old': None}) |
| 291 |
|
| 292 |
def test_deprecated_option_move_invalid(self): |
| 293 |
class Schema: |
| 294 |
foo = c.Type(dict) |
| 295 |
old = c.Deprecated(moved_to='foo.bar') |
| 296 |
|
| 297 |
with self.expect_error(foo="Expected type: <class 'dict'> but received: <class 'str'>"): |
| 298 |
self.get_config( |
| 299 |
Schema, |
| 300 |
{'old': 'value', 'foo': 'wrong type'}, |
| 301 |
warnings=dict( |
| 302 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 303 |
"future release. Use 'foo.bar' instead." |
| 304 |
), |
| 305 |
) |
| 306 |
|
| 307 |
|
| 308 |
class IpAddressTest(TestCase): |
| 309 |
class Schema: |
| 310 |
option = c.IpAddress() |
| 311 |
|
| 312 |
def test_valid_address(self): |
| 313 |
addr = '127.0.0.1:8000' |
| 314 |
|
| 315 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 316 |
self.assertEqual(str(conf['option']), addr) |
| 317 |
self.assertEqual(conf['option'].host, '127.0.0.1') |
| 318 |
self.assertEqual(conf['option'].port, 8000) |
| 319 |
|
| 320 |
def test_valid_IPv6_address(self): |
| 321 |
addr = '::1:8000' |
| 322 |
|
| 323 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 324 |
self.assertEqual(str(conf['option']), addr) |
| 325 |
self.assertEqual(conf['option'].host, '::1') |
| 326 |
self.assertEqual(conf['option'].port, 8000) |
| 327 |
|
| 328 |
def test_valid_full_IPv6_address(self): |
| 329 |
addr = '[2001:db8:85a3::8a2e:370:7334]:123' |
| 330 |
|
| 331 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 332 |
self.assertEqual(conf['option'].host, '2001:db8:85a3::8a2e:370:7334') |
| 333 |
self.assertEqual(conf['option'].port, 123) |
| 334 |
|
| 335 |
def test_named_address(self): |
| 336 |
addr = 'localhost:8000' |
| 337 |
|
| 338 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 339 |
self.assertEqual(str(conf['option']), addr) |
| 340 |
self.assertEqual(conf['option'].host, 'localhost') |
| 341 |
self.assertEqual(conf['option'].port, 8000) |
| 342 |
|
| 343 |
def test_default_address(self): |
| 344 |
addr = '127.0.0.1:8000' |
| 345 |
|
| 346 |
class Schema: |
| 347 |
option = c.IpAddress(default=addr) |
| 348 |
|
| 349 |
conf = self.get_config(Schema, {'option': None}) |
| 350 |
self.assertEqual(str(conf['option']), addr) |
| 351 |
self.assertEqual(conf['option'].host, '127.0.0.1') |
| 352 |
self.assertEqual(conf['option'].port, 8000) |
| 353 |
|
| 354 |
def test_bind_all_IPv4_address(self): |
| 355 |
addr = '0.0.0.0:8000' |
| 356 |
|
| 357 |
class Schema: |
| 358 |
option = c.IpAddress(default=addr) |
| 359 |
|
| 360 |
conf = self.get_config(Schema, {'option': None}) |
| 361 |
self.assertEqual(str(conf['option']), addr) |
| 362 |
self.assertEqual(conf['option'].host, '0.0.0.0') |
| 363 |
self.assertEqual(conf['option'].port, 8000) |
| 364 |
|
| 365 |
def test_bind_all_IPv6_address(self): |
| 366 |
addr = ':::8000' |
| 367 |
|
| 368 |
class Schema: |
| 369 |
option = c.IpAddress(default=addr) |
| 370 |
|
| 371 |
conf = self.get_config(Schema, {'option': None}) |
| 372 |
self.assertEqual(str(conf['option']), addr) |
| 373 |
self.assertEqual(conf['option'].host, '::') |
| 374 |
self.assertEqual(conf['option'].port, 8000) |
| 375 |
|
| 376 |
@unittest.skipIf( |
| 377 |
sys.version_info < (3, 9, 5), |
| 378 |
"Leading zeros allowed in IP addresses before Python3.9.5", |
| 379 |
) |
| 380 |
def test_invalid_leading_zeros(self): |
| 381 |
with self.expect_error( |
| 382 |
option="'127.000.000.001' does not appear to be an IPv4 or IPv6 address" |
| 383 |
): |
| 384 |
self.get_config(self.Schema, {'option': '127.000.000.001:8000'}) |
| 385 |
|
| 386 |
def test_invalid_address_range(self): |
| 387 |
with self.expect_error(option="'277.0.0.1' does not appear to be an IPv4 or IPv6 address"): |
| 388 |
self.get_config(self.Schema, {'option': '277.0.0.1:8000'}) |
| 389 |
|
| 390 |
def test_invalid_address_format(self): |
| 391 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 392 |
self.get_config(self.Schema, {'option': '127.0.0.18000'}) |
| 393 |
|
| 394 |
def test_invalid_address_type(self): |
| 395 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 396 |
self.get_config(self.Schema, {'option': 123}) |
| 397 |
|
| 398 |
def test_invalid_address_port(self): |
| 399 |
with self.expect_error(option="'foo' is not a valid port"): |
| 400 |
self.get_config(self.Schema, {'option': '127.0.0.1:foo'}) |
| 401 |
|
| 402 |
def test_invalid_address_missing_port(self): |
| 403 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 404 |
self.get_config(self.Schema, {'option': '127.0.0.1'}) |
| 405 |
|
| 406 |
|
| 407 |
class URLTest(TestCase): |
| 408 |
def test_valid_url(self): |
| 409 |
class Schema: |
| 410 |
option = c.URL() |
| 411 |
|
| 412 |
conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) |
| 413 |
self.assertEqual(conf['option'], "https://mkdocs.org") |
| 414 |
|
| 415 |
conf = self.get_config(Schema, {'option': ""}) |
| 416 |
self.assertEqual(conf['option'], "") |
| 417 |
|
| 418 |
def test_valid_url_is_dir(self): |
| 419 |
class Schema: |
| 420 |
option = c.URL(is_dir=True) |
| 421 |
|
| 422 |
conf = self.get_config(Schema, {'option': "http://mkdocs.org/"}) |
| 423 |
self.assertEqual(conf['option'], "http://mkdocs.org/") |
| 424 |
|
| 425 |
conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) |
| 426 |
self.assertEqual(conf['option'], "https://mkdocs.org/") |
| 427 |
|
| 428 |
def test_optional(self): |
| 429 |
class Schema: |
| 430 |
option = c.URL(is_dir=True) |
| 431 |
|
| 432 |
conf = self.get_config(Schema, {'option': ''}) |
| 433 |
self.assertEqual(conf['option'], '') |
| 434 |
|
| 435 |
conf = self.get_config(Schema, {'option': None}) |
| 436 |
self.assertEqual(conf['option'], None) |
| 437 |
|
| 438 |
def test_invalid_url(self): |
| 439 |
class Schema: |
| 440 |
option = c.URL(required=True) |
| 441 |
|
| 442 |
with self.expect_error(option="Required configuration not provided."): |
| 443 |
self.get_config(Schema, {'option': None}) |
| 444 |
|
| 445 |
for url in "www.mkdocs.org", "//mkdocs.org/test", "http:/mkdocs.org/", "/hello/": |
| 446 |
with self.subTest(url=url): |
| 447 |
with self.expect_error( |
| 448 |
option="The URL isn't valid, it should include the http:// (scheme)" |
| 449 |
): |
| 450 |
self.get_config(Schema, {'option': url}) |
| 451 |
|
| 452 |
def test_invalid_type(self): |
| 453 |
class Schema: |
| 454 |
option = c.URL() |
| 455 |
|
| 456 |
with self.expect_error(option="Expected a string, got <class 'int'>"): |
| 457 |
self.get_config(Schema, {'option': 1}) |
| 458 |
|
| 459 |
|
| 460 |
class EditURITest(TestCase): |
| 461 |
class Schema: |
| 462 |
repo_url = c.URL() |
| 463 |
repo_name = c.RepoName('repo_url') |
| 464 |
edit_uri_template = c.EditURITemplate('edit_uri') |
| 465 |
edit_uri = c.EditURI('repo_url') |
| 466 |
|
| 467 |
def test_repo_name_github(self): |
| 468 |
conf = self.get_config( |
| 469 |
self.Schema, |
| 470 |
{'repo_url': "https://github.com/mkdocs/mkdocs"}, |
| 471 |
) |
| 472 |
self.assertEqual(conf['repo_name'], "GitHub") |
| 473 |
|
| 474 |
def test_repo_name_bitbucket(self): |
| 475 |
conf = self.get_config( |
| 476 |
self.Schema, |
| 477 |
{'repo_url': "https://bitbucket.org/gutworth/six/"}, |
| 478 |
) |
| 479 |
self.assertEqual(conf['repo_name'], "Bitbucket") |
| 480 |
|
| 481 |
def test_repo_name_gitlab(self): |
| 482 |
conf = self.get_config( |
| 483 |
self.Schema, |
| 484 |
{'repo_url': "https://gitlab.com/gitlab-org/gitlab-ce/"}, |
| 485 |
) |
| 486 |
self.assertEqual(conf['repo_name'], "GitLab") |
| 487 |
|
| 488 |
def test_repo_name_custom(self): |
| 489 |
conf = self.get_config( |
| 490 |
self.Schema, |
| 491 |
{'repo_url': "https://launchpad.net/python-tuskarclient"}, |
| 492 |
) |
| 493 |
self.assertEqual(conf['repo_name'], "Launchpad") |
| 494 |
|
| 495 |
def test_edit_uri_github(self): |
| 496 |
conf = self.get_config( |
| 497 |
self.Schema, |
| 498 |
{'repo_url': "https://github.com/mkdocs/mkdocs"}, |
| 499 |
) |
| 500 |
self.assertEqual(conf['edit_uri'], 'edit/master/docs/') |
| 501 |
self.assertEqual(conf['repo_url'], "https://github.com/mkdocs/mkdocs") |
| 502 |
|
| 503 |
def test_edit_uri_bitbucket(self): |
| 504 |
conf = self.get_config( |
| 505 |
self.Schema, |
| 506 |
{'repo_url': "https://bitbucket.org/gutworth/six/"}, |
| 507 |
) |
| 508 |
self.assertEqual(conf['edit_uri'], 'src/default/docs/') |
| 509 |
self.assertEqual(conf['repo_url'], "https://bitbucket.org/gutworth/six/") |
| 510 |
|
| 511 |
def test_edit_uri_gitlab(self): |
| 512 |
conf = self.get_config( |
| 513 |
self.Schema, |
| 514 |
{'repo_url': "https://gitlab.com/gitlab-org/gitlab-ce/"}, |
| 515 |
) |
| 516 |
self.assertEqual(conf['edit_uri'], 'edit/master/docs/') |
| 517 |
|
| 518 |
def test_edit_uri_custom(self): |
| 519 |
conf = self.get_config( |
| 520 |
self.Schema, |
| 521 |
{'repo_url': "https://launchpad.net/python-tuskarclient"}, |
| 522 |
) |
| 523 |
self.assertEqual(conf['edit_uri'], None) |
| 524 |
self.assertEqual(conf['repo_url'], "https://launchpad.net/python-tuskarclient") |
| 525 |
|
| 526 |
def test_repo_name_custom_and_empty_edit_uri(self): |
| 527 |
conf = self.get_config( |
| 528 |
self.Schema, |
| 529 |
{'repo_url': "https://github.com/mkdocs/mkdocs", 'repo_name': 'mkdocs'}, |
| 530 |
) |
| 531 |
self.assertEqual(conf['edit_uri'], 'edit/master/docs/') |
| 532 |
|
| 533 |
def test_edit_uri_template_ok(self): |
| 534 |
conf = self.get_config( |
| 535 |
self.Schema, |
| 536 |
{ |
| 537 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 538 |
'edit_uri_template': 'edit/foo/docs/{path}', |
| 539 |
}, |
| 540 |
) |
| 541 |
self.assertEqual(conf['edit_uri_template'], 'edit/foo/docs/{path}') |
| 542 |
|
| 543 |
def test_edit_uri_template_errors(self): |
| 544 |
with self.expect_error( |
| 545 |
edit_uri_template=re.compile(r'.*[{}].*') # Complains about unclosed '{' or missing '}' |
| 546 |
): |
| 547 |
self.get_config( |
| 548 |
self.Schema, |
| 549 |
{ |
| 550 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 551 |
'edit_uri_template': 'edit/master/{path', |
| 552 |
}, |
| 553 |
) |
| 554 |
|
| 555 |
with self.expect_error(edit_uri_template=re.compile(r'.*\bz\b.*')): |
| 556 |
self.get_config( |
| 557 |
self.Schema, |
| 558 |
{ |
| 559 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 560 |
'edit_uri_template': 'edit/master/{path!z}', |
| 561 |
}, |
| 562 |
) |
| 563 |
|
| 564 |
with self.expect_error(edit_uri_template="Unknown template substitute: 'foo'"): |
| 565 |
self.get_config( |
| 566 |
self.Schema, |
| 567 |
{ |
| 568 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 569 |
'edit_uri_template': 'edit/master/{foo}', |
| 570 |
}, |
| 571 |
) |
| 572 |
|
| 573 |
def test_edit_uri_template_warning(self): |
| 574 |
conf = self.get_config( |
| 575 |
self.Schema, |
| 576 |
{ |
| 577 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 578 |
'edit_uri': 'edit', |
| 579 |
'edit_uri_template': 'edit/master/{path}', |
| 580 |
}, |
| 581 |
warnings=dict( |
| 582 |
edit_uri_template="The option 'edit_uri' has no effect when 'edit_uri_template' is set." |
| 583 |
), |
| 584 |
) |
| 585 |
self.assertEqual(conf['edit_uri_template'], 'edit/master/{path}') |
| 586 |
|
| 587 |
|
| 588 |
class ListOfItemsTest(TestCase): |
| 589 |
def test_int_type(self): |
| 590 |
class Schema: |
| 591 |
option = c.ListOfItems(c.Type(int)) |
| 592 |
|
| 593 |
conf = self.get_config(Schema, {'option': [1, 2, 3]}) |
| 594 |
self.assertEqual(conf['option'], [1, 2, 3]) |
| 595 |
|
| 596 |
with self.expect_error( |
| 597 |
option="Expected type: <class 'int'> but received: <class 'NoneType'>" |
| 598 |
): |
| 599 |
conf = self.get_config(Schema, {'option': [1, None, 3]}) |
| 600 |
|
| 601 |
def test_combined_float_type(self): |
| 602 |
class Schema: |
| 603 |
option = c.ListOfItems(c.Type((int, float))) |
| 604 |
|
| 605 |
conf = self.get_config(Schema, {'option': [1.4, 2, 3]}) |
| 606 |
self.assertEqual(conf['option'], [1.4, 2, 3]) |
| 607 |
|
| 608 |
with self.expect_error( |
| 609 |
option="Expected type: (<class 'int'>, <class 'float'>) but received: <class 'str'>" |
| 610 |
): |
| 611 |
self.get_config(Schema, {'option': ['a']}) |
| 612 |
|
| 613 |
def test_list_default(self): |
| 614 |
class Schema: |
| 615 |
option = c.ListOfItems(c.Type(int), default=[]) |
| 616 |
|
| 617 |
conf = self.get_config(Schema, {}) |
| 618 |
self.assertEqual(conf['option'], []) |
| 619 |
|
| 620 |
conf = self.get_config(Schema, {'option': None}) |
| 621 |
self.assertEqual(conf['option'], []) |
| 622 |
|
| 623 |
def test_none_without_default(self): |
| 624 |
class Schema: |
| 625 |
option = c.ListOfItems(c.Type(str)) |
| 626 |
|
| 627 |
with self.expect_error(option="Required configuration not provided."): |
| 628 |
conf = self.get_config(Schema, {}) |
| 629 |
|
| 630 |
with self.expect_error(option="Required configuration not provided."): |
| 631 |
conf = self.get_config(Schema, {'option': None}) |
| 632 |
|
| 633 |
conf = self.get_config(Schema, {'option': ['foo']}) |
| 634 |
self.assertEqual(conf['option'], ['foo']) |
| 635 |
|
| 636 |
def test_string_not_a_list_of_strings(self): |
| 637 |
class Schema: |
| 638 |
option = c.ListOfItems(c.Type(str)) |
| 639 |
|
| 640 |
with self.expect_error(option="Expected a list of items, but a <class 'str'> was given."): |
| 641 |
self.get_config(Schema, {'option': 'foo'}) |
| 642 |
|
| 643 |
def test_post_validation_error(self): |
| 644 |
class Schema: |
| 645 |
option = c.ListOfItems(c.IpAddress()) |
| 646 |
|
| 647 |
with self.expect_error(option="'asdf' is not a valid port"): |
| 648 |
self.get_config(Schema, {'option': ["localhost:8000", "1.2.3.4:asdf"]}) |
| 649 |
|
| 650 |
|
| 651 |
class FilesystemObjectTest(TestCase): |
| 652 |
def test_valid_dir(self): |
| 653 |
for cls in c.Dir, c.FilesystemObject: |
| 654 |
with self.subTest(cls): |
| 655 |
d = os.path.dirname(__file__) |
| 656 |
|
| 657 |
class Schema: |
| 658 |
option = cls(exists=True) |
| 659 |
|
| 660 |
conf = self.get_config(Schema, {'option': d}) |
| 661 |
self.assertEqual(conf['option'], d) |
| 662 |
|
| 663 |
def test_valid_file(self): |
| 664 |
for cls in c.File, c.FilesystemObject: |
| 665 |
with self.subTest(cls): |
| 666 |
f = __file__ |
| 667 |
|
| 668 |
class Schema: |
| 669 |
option = cls(exists=True) |
| 670 |
|
| 671 |
conf = self.get_config(Schema, {'option': f}) |
| 672 |
self.assertEqual(conf['option'], f) |
| 673 |
|
| 674 |
def test_missing_without_exists(self): |
| 675 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 676 |
with self.subTest(cls): |
| 677 |
d = os.path.join("not", "a", "real", "path", "I", "hope") |
| 678 |
|
| 679 |
class Schema: |
| 680 |
option = cls() |
| 681 |
|
| 682 |
conf = self.get_config(Schema, {'option': d}) |
| 683 |
self.assertEqual(conf['option'], os.path.abspath(d)) |
| 684 |
|
| 685 |
def test_missing_but_required(self): |
| 686 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 687 |
with self.subTest(cls): |
| 688 |
d = os.path.join("not", "a", "real", "path", "I", "hope") |
| 689 |
|
| 690 |
class Schema: |
| 691 |
option = cls(exists=True) |
| 692 |
|
| 693 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing .+")): |
| 694 |
self.get_config(Schema, {'option': d}) |
| 695 |
|
| 696 |
def test_not_a_dir(self): |
| 697 |
d = __file__ |
| 698 |
|
| 699 |
class Schema: |
| 700 |
option = c.Dir(exists=True) |
| 701 |
|
| 702 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing directory.")): |
| 703 |
self.get_config(Schema, {'option': d}) |
| 704 |
|
| 705 |
def test_not_a_file(self): |
| 706 |
d = os.path.dirname(__file__) |
| 707 |
|
| 708 |
class Schema: |
| 709 |
option = c.File(exists=True) |
| 710 |
|
| 711 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing file.")): |
| 712 |
self.get_config(Schema, {'option': d}) |
| 713 |
|
| 714 |
def test_incorrect_type_error(self): |
| 715 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 716 |
with self.subTest(cls): |
| 717 |
|
| 718 |
class Schema: |
| 719 |
option = cls() |
| 720 |
|
| 721 |
with self.expect_error( |
| 722 |
option="Expected type: <class 'str'> but received: <class 'int'>" |
| 723 |
): |
| 724 |
self.get_config(Schema, {'option': 1}) |
| 725 |
with self.expect_error( |
| 726 |
option="Expected type: <class 'str'> but received: <class 'list'>" |
| 727 |
): |
| 728 |
self.get_config(Schema, {'option': []}) |
| 729 |
|
| 730 |
def test_with_unicode(self): |
| 731 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 732 |
with self.subTest(cls): |
| 733 |
|
| 734 |
class Schema: |
| 735 |
dir = cls() |
| 736 |
|
| 737 |
conf = self.get_config(Schema, {'dir': 'юникод'}) |
| 738 |
self.assertIsInstance(conf['dir'], str) |
| 739 |
|
| 740 |
def test_dir_bytes(self): |
| 741 |
class Schema: |
| 742 |
dir = c.Dir() |
| 743 |
|
| 744 |
with self.expect_error(dir="Expected type: <class 'str'> but received: <class 'bytes'>"): |
| 745 |
self.get_config(Schema, {'dir': b'foo'}) |
| 746 |
|
| 747 |
def test_config_dir_prepended(self): |
| 748 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 749 |
with self.subTest(cls): |
| 750 |
base_path = os.path.dirname(os.path.abspath(__file__)) |
| 751 |
|
| 752 |
class Schema: |
| 753 |
dir = cls() |
| 754 |
|
| 755 |
conf = self.get_config( |
| 756 |
Schema, |
| 757 |
{'dir': 'foo'}, |
| 758 |
config_file_path=os.path.join(base_path, 'mkdocs.yml'), |
| 759 |
) |
| 760 |
self.assertEqual(conf['dir'], os.path.join(base_path, 'foo')) |
| 761 |
|
| 762 |
def test_site_dir_is_config_dir_fails(self): |
| 763 |
class Schema: |
| 764 |
dir = c.DocsDir() |
| 765 |
|
| 766 |
with self.expect_error( |
| 767 |
dir="The 'dir' should not be the parent directory of the config file. " |
| 768 |
"Use a child directory instead so that the 'dir' is a sibling of the config file." |
| 769 |
): |
| 770 |
self.get_config( |
| 771 |
Schema, |
| 772 |
{'dir': '.'}, |
| 773 |
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'), |
| 774 |
) |
| 775 |
|
| 776 |
|
| 777 |
class ListOfPathsTest(TestCase): |
| 778 |
def test_valid_path(self): |
| 779 |
paths = [os.path.dirname(__file__)] |
| 780 |
|
| 781 |
class Schema: |
| 782 |
option = c.ListOfPaths() |
| 783 |
|
| 784 |
self.get_config(Schema, {'option': paths}) |
| 785 |
|
| 786 |
def test_missing_path(self): |
| 787 |
paths = [os.path.join("does", "not", "exist", "i", "hope")] |
| 788 |
|
| 789 |
class Schema: |
| 790 |
option = c.ListOfPaths() |
| 791 |
|
| 792 |
with self.expect_error( |
| 793 |
option=f"The path '{paths[0]}' isn't an existing file or directory." |
| 794 |
): |
| 795 |
self.get_config(Schema, {'option': paths}) |
| 796 |
|
| 797 |
def test_non_path(self): |
| 798 |
paths = [os.path.dirname(__file__), None] |
| 799 |
|
| 800 |
class Schema: |
| 801 |
option = c.ListOfPaths() |
| 802 |
|
| 803 |
with self.expect_error( |
| 804 |
option="Expected type: <class 'str'> but received: <class 'NoneType'>" |
| 805 |
): |
| 806 |
self.get_config(Schema, {'option': paths}) |
| 807 |
|
| 808 |
def test_empty_list(self): |
| 809 |
class Schema: |
| 810 |
option = c.ListOfPaths() |
| 811 |
|
| 812 |
conf = self.get_config(Schema, {'option': []}) |
| 813 |
self.assertEqual(conf['option'], []) |
| 814 |
|
| 815 |
def test_non_list(self): |
| 816 |
paths = os.path.dirname(__file__) |
| 817 |
|
| 818 |
class Schema: |
| 819 |
option = c.ListOfPaths() |
| 820 |
|
| 821 |
with self.expect_error(option="Expected a list of items, but a <class 'str'> was given."): |
| 822 |
self.get_config(Schema, {'option': paths}) |
| 823 |
|
| 824 |
def test_file(self): |
| 825 |
paths = [__file__] |
| 826 |
|
| 827 |
class Schema: |
| 828 |
option = c.ListOfPaths() |
| 829 |
|
| 830 |
self.get_config(Schema, {'option': paths}) |
| 831 |
|
| 832 |
@tempdir() |
| 833 |
def test_paths_localized_to_config(self, base_path): |
| 834 |
with open(os.path.join(base_path, 'foo'), 'w') as f: |
| 835 |
f.write('hi') |
| 836 |
|
| 837 |
class Schema: |
| 838 |
watch = c.ListOfPaths() |
| 839 |
|
| 840 |
conf = self.get_config( |
| 841 |
Schema, |
| 842 |
{'watch': ['foo']}, |
| 843 |
config_file_path=os.path.join(base_path, 'mkdocs.yml'), |
| 844 |
) |
| 845 |
|
| 846 |
self.assertEqual(conf['watch'], [os.path.join(base_path, 'foo')]) |
| 847 |
|
| 848 |
|
| 849 |
class SiteDirTest(TestCase): |
| 850 |
class Schema: |
| 851 |
site_dir = c.SiteDir() |
| 852 |
docs_dir = c.Dir() |
| 853 |
|
| 854 |
def test_doc_dir_in_site_dir(self): |
| 855 |
j = os.path.join |
| 856 |
# The parent dir is not the same on every system, so use the actual dir name |
| 857 |
parent_dir = mkdocs.__file__.split(os.sep)[-3] |
| 858 |
|
| 859 |
test_configs = ( |
| 860 |
{'docs_dir': j('site', 'docs'), 'site_dir': 'site'}, |
| 861 |
{'docs_dir': 'docs', 'site_dir': '.'}, |
| 862 |
{'docs_dir': '.', 'site_dir': '.'}, |
| 863 |
{'docs_dir': 'docs', 'site_dir': ''}, |
| 864 |
{'docs_dir': '', 'site_dir': ''}, |
| 865 |
{'docs_dir': j('..', parent_dir, 'docs'), 'site_dir': 'docs'}, |
| 866 |
{'docs_dir': 'docs', 'site_dir': '/'}, |
| 867 |
) |
| 868 |
|
| 869 |
for test_config in test_configs: |
| 870 |
with self.subTest(test_config): |
| 871 |
with self.expect_error( |
| 872 |
site_dir=re.compile(r"The 'docs_dir' should not be within the 'site_dir'.*") |
| 873 |
): |
| 874 |
self.get_config(self.Schema, test_config) |
| 875 |
|
| 876 |
def test_site_dir_in_docs_dir(self): |
| 877 |
j = os.path.join |
| 878 |
|
| 879 |
test_configs = ( |
| 880 |
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')}, |
| 881 |
{'docs_dir': '.', 'site_dir': 'site'}, |
| 882 |
{'docs_dir': '', 'site_dir': 'site'}, |
| 883 |
{'docs_dir': '/', 'site_dir': 'site'}, |
| 884 |
) |
| 885 |
|
| 886 |
for test_config in test_configs: |
| 887 |
with self.subTest(test_config): |
| 888 |
with self.expect_error( |
| 889 |
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*") |
| 890 |
): |
| 891 |
self.get_config(self.Schema, test_config) |
| 892 |
|
| 893 |
def test_common_prefix(self): |
| 894 |
"""Legitimate settings with common prefixes should not fail validation.""" |
| 895 |
test_configs = ( |
| 896 |
{'docs_dir': 'docs', 'site_dir': 'docs-site'}, |
| 897 |
{'docs_dir': 'site-docs', 'site_dir': 'site'}, |
| 898 |
) |
| 899 |
|
| 900 |
for test_config in test_configs: |
| 901 |
with self.subTest(test_config): |
| 902 |
self.get_config(self.Schema, test_config) |
| 903 |
|
| 904 |
|
| 905 |
class ThemeTest(TestCase): |
| 906 |
def test_theme_as_string(self): |
| 907 |
class Schema: |
| 908 |
option = c.Theme() |
| 909 |
|
| 910 |
conf = self.get_config(Schema, {'option': "mkdocs"}) |
| 911 |
self.assertEqual(conf['option'].name, 'mkdocs') |
| 912 |
|
| 913 |
def test_uninstalled_theme_as_string(self): |
| 914 |
class Schema: |
| 915 |
option = c.Theme() |
| 916 |
|
| 917 |
with self.expect_error( |
| 918 |
option=re.compile( |
| 919 |
r"Unrecognised theme name: 'mkdocs2'. The available installed themes are: .+" |
| 920 |
) |
| 921 |
): |
| 922 |
self.get_config(Schema, {'option': "mkdocs2"}) |
| 923 |
|
| 924 |
def test_theme_default(self): |
| 925 |
class Schema: |
| 926 |
option = c.Theme(default='mkdocs') |
| 927 |
|
| 928 |
conf = self.get_config(Schema, {'option': None}) |
| 929 |
self.assertEqual(conf['option'].name, 'mkdocs') |
| 930 |
|
| 931 |
def test_theme_as_simple_config(self): |
| 932 |
config = { |
| 933 |
'name': 'mkdocs', |
| 934 |
} |
| 935 |
|
| 936 |
class Schema: |
| 937 |
option = c.Theme() |
| 938 |
|
| 939 |
conf = self.get_config(Schema, {'option': config}) |
| 940 |
self.assertEqual(conf['option'].name, 'mkdocs') |
| 941 |
|
| 942 |
@tempdir() |
| 943 |
def test_theme_as_complex_config(self, custom_dir): |
| 944 |
config = { |
| 945 |
'name': 'mkdocs', |
| 946 |
'custom_dir': custom_dir, |
| 947 |
'static_templates': ['sitemap.html'], |
| 948 |
'show_sidebar': False, |
| 949 |
} |
| 950 |
|
| 951 |
class Schema: |
| 952 |
option = c.Theme() |
| 953 |
|
| 954 |
conf = self.get_config(Schema, {'option': config}) |
| 955 |
self.assertEqual(conf['option'].name, 'mkdocs') |
| 956 |
self.assertIn(custom_dir, conf['option'].dirs) |
| 957 |
self.assertEqual( |
| 958 |
conf['option'].static_templates, |
| 959 |
{'404.html', 'sitemap.xml', 'sitemap.html'}, |
| 960 |
) |
| 961 |
self.assertEqual(conf['option']['show_sidebar'], False) |
| 962 |
|
| 963 |
def test_theme_name_is_none(self): |
| 964 |
config = { |
| 965 |
'name': None, |
| 966 |
} |
| 967 |
|
| 968 |
class Schema: |
| 969 |
option = c.Theme() |
| 970 |
|
| 971 |
with self.expect_error(option="At least one of 'name' or 'custom_dir' must be defined."): |
| 972 |
self.get_config(Schema, {'option': config}) |
| 973 |
|
| 974 |
def test_theme_config_missing_name(self): |
| 975 |
config = { |
| 976 |
'custom_dir': 'custom', |
| 977 |
} |
| 978 |
|
| 979 |
class Schema: |
| 980 |
option = c.Theme() |
| 981 |
|
| 982 |
with self.expect_error(option="No theme name set."): |
| 983 |
self.get_config(Schema, {'option': config}) |
| 984 |
|
| 985 |
def test_uninstalled_theme_as_config(self): |
| 986 |
config = { |
| 987 |
'name': 'mkdocs2', |
| 988 |
} |
| 989 |
|
| 990 |
class Schema: |
| 991 |
option = c.Theme() |
| 992 |
|
| 993 |
with self.expect_error( |
| 994 |
option=re.compile( |
| 995 |
r"Unrecognised theme name: 'mkdocs2'. The available installed themes are: .+" |
| 996 |
) |
| 997 |
): |
| 998 |
self.get_config(Schema, {'option': config}) |
| 999 |
|
| 1000 |
def test_theme_invalid_type(self): |
| 1001 |
config = ['mkdocs2'] |
| 1002 |
|
| 1003 |
class Schema: |
| 1004 |
option = c.Theme() |
| 1005 |
|
| 1006 |
with self.expect_error( |
| 1007 |
option="Invalid type <class 'list'>. Expected a string or key/value pairs." |
| 1008 |
): |
| 1009 |
self.get_config(Schema, {'option': config}) |
| 1010 |
|
| 1011 |
def test_post_validation_none_theme_name_and_missing_custom_dir(self): |
| 1012 |
config = { |
| 1013 |
'theme': { |
| 1014 |
'name': None, |
| 1015 |
}, |
| 1016 |
} |
| 1017 |
|
| 1018 |
class Schema: |
| 1019 |
theme = c.Theme() |
| 1020 |
|
| 1021 |
with self.expect_error(theme="At least one of 'name' or 'custom_dir' must be defined."): |
| 1022 |
self.get_config(Schema, config) |
| 1023 |
|
| 1024 |
@tempdir() |
| 1025 |
def test_post_validation_inexisting_custom_dir(self, abs_base_path): |
| 1026 |
path = os.path.join(abs_base_path, 'inexisting_custom_dir') |
| 1027 |
config = { |
| 1028 |
'theme': { |
| 1029 |
'name': None, |
| 1030 |
'custom_dir': path, |
| 1031 |
}, |
| 1032 |
} |
| 1033 |
|
| 1034 |
class Schema: |
| 1035 |
theme = c.Theme() |
| 1036 |
|
| 1037 |
with self.expect_error(theme=f"The path set in custom_dir ('{path}') does not exist."): |
| 1038 |
self.get_config(Schema, config) |
| 1039 |
|
| 1040 |
def test_post_validation_locale_none(self): |
| 1041 |
config = { |
| 1042 |
'theme': { |
| 1043 |
'name': 'mkdocs', |
| 1044 |
'locale': None, |
| 1045 |
}, |
| 1046 |
} |
| 1047 |
|
| 1048 |
class Schema: |
| 1049 |
theme = c.Theme() |
| 1050 |
|
| 1051 |
with self.expect_error(theme="'locale' must be a string."): |
| 1052 |
self.get_config(Schema, config) |
| 1053 |
|
| 1054 |
def test_post_validation_locale_invalid_type(self): |
| 1055 |
config = { |
| 1056 |
'theme': { |
| 1057 |
'name': 'mkdocs', |
| 1058 |
'locale': 0, |
| 1059 |
}, |
| 1060 |
} |
| 1061 |
|
| 1062 |
class Schema: |
| 1063 |
theme = c.Theme() |
| 1064 |
|
| 1065 |
with self.expect_error(theme="'locale' must be a string."): |
| 1066 |
self.get_config(Schema, config) |
| 1067 |
|
| 1068 |
def test_post_validation_locale(self): |
| 1069 |
config = { |
| 1070 |
'theme': { |
| 1071 |
'name': 'mkdocs', |
| 1072 |
'locale': 'fr', |
| 1073 |
}, |
| 1074 |
} |
| 1075 |
|
| 1076 |
class Schema: |
| 1077 |
theme = c.Theme() |
| 1078 |
|
| 1079 |
conf = self.get_config(Schema, config) |
| 1080 |
self.assertEqual(conf['theme']['locale'].language, 'fr') |
| 1081 |
|
| 1082 |
|
| 1083 |
class NavTest(TestCase): |
| 1084 |
class Schema: |
| 1085 |
option = c.Nav() |
| 1086 |
|
| 1087 |
def test_old_format(self): |
| 1088 |
with self.expect_error( |
| 1089 |
option="Expected nav item to be a string or dict, got a list: ['index.md']" |
| 1090 |
): |
| 1091 |
self.get_config(self.Schema, {'option': [['index.md']]}) |
| 1092 |
|
| 1093 |
def test_provided_dict(self): |
| 1094 |
conf = self.get_config(self.Schema, {'option': ['index.md', {"Page": "page.md"}]}) |
| 1095 |
self.assertEqual(conf['option'], ['index.md', {'Page': 'page.md'}]) |
| 1096 |
|
| 1097 |
def test_provided_empty(self): |
| 1098 |
conf = self.get_config(self.Schema, {'option': []}) |
| 1099 |
self.assertEqual(conf['option'], None) |
| 1100 |
|
| 1101 |
def test_normal_nav(self): |
| 1102 |
nav_yaml = textwrap.dedent( |
| 1103 |
'''\ |
| 1104 |
- Home: index.md |
| 1105 |
- getting-started.md |
| 1106 |
- User Guide: |
| 1107 |
- Overview: user-guide/index.md |
| 1108 |
- Installation: user-guide/installation.md |
| 1109 |
''' |
| 1110 |
) |
| 1111 |
nav = yaml_load(io.StringIO(nav_yaml)) |
| 1112 |
|
| 1113 |
conf = self.get_config(self.Schema, {'option': nav}) |
| 1114 |
self.assertEqual(conf['option'], nav) |
| 1115 |
|
| 1116 |
def test_invalid_type_dict(self): |
| 1117 |
with self.expect_error(option="Expected nav to be a list, got a dict: {}"): |
| 1118 |
self.get_config(self.Schema, {'option': {}}) |
| 1119 |
|
| 1120 |
def test_invalid_type_int(self): |
| 1121 |
with self.expect_error(option="Expected nav to be a list, got a int: 5"): |
| 1122 |
self.get_config(self.Schema, {'option': 5}) |
| 1123 |
|
| 1124 |
def test_invalid_item_int(self): |
| 1125 |
with self.expect_error(option="Expected nav item to be a string or dict, got a int: 1"): |
| 1126 |
self.get_config(self.Schema, {'option': [1]}) |
| 1127 |
|
| 1128 |
def test_invalid_item_none(self): |
| 1129 |
with self.expect_error(option="Expected nav item to be a string or dict, got None"): |
| 1130 |
self.get_config(self.Schema, {'option': [None]}) |
| 1131 |
|
| 1132 |
def test_invalid_children_config_int(self): |
| 1133 |
with self.expect_error(option="Expected nav to be a list, got a int: 1"): |
| 1134 |
self.get_config(self.Schema, {'option': [{"foo.md": [{"bar.md": 1}]}]}) |
| 1135 |
|
| 1136 |
def test_invalid_children_config_none(self): |
| 1137 |
with self.expect_error(option="Expected nav to be a list, got None"): |
| 1138 |
self.get_config(self.Schema, {'option': [{"foo.md": None}]}) |
| 1139 |
|
| 1140 |
def test_invalid_children_empty_dict(self): |
| 1141 |
nav = ['foo', {}] |
| 1142 |
with self.expect_error(option="Expected nav item to be a dict of size 1, got a dict: {}"): |
| 1143 |
self.get_config(self.Schema, {'option': nav}) |
| 1144 |
|
| 1145 |
def test_invalid_nested_list(self): |
| 1146 |
nav = [{'aaa': [[{"bbb": "user-guide/index.md"}]]}] |
| 1147 |
with self.expect_error( |
| 1148 |
option="Expected nav item to be a string or dict, got a list: [{'bbb': 'user-guide/index.md'}]" |
| 1149 |
): |
| 1150 |
self.get_config(self.Schema, {'option': nav}) |
| 1151 |
|
| 1152 |
def test_invalid_children_oversized_dict(self): |
| 1153 |
nav = [{"aaa": [{"bbb": "user-guide/index.md", "ccc": "user-guide/installation.md"}]}] |
| 1154 |
with self.expect_error( |
| 1155 |
option="Expected nav item to be a dict of size 1, got dict with keys ('bbb', 'ccc')" |
| 1156 |
): |
| 1157 |
self.get_config(self.Schema, {'option': nav}) |
| 1158 |
|
| 1159 |
def test_warns_for_dict(self): |
| 1160 |
self.get_config( |
| 1161 |
self.Schema, |
| 1162 |
{'option': [{"a": {"b": "c.md", "d": "e.md"}}]}, |
| 1163 |
warnings=dict(option="Expected nav to be a list, got dict with keys ('b', 'd')"), |
| 1164 |
) |
| 1165 |
|
| 1166 |
|
| 1167 |
class PrivateTest(TestCase): |
| 1168 |
def test_defined(self): |
| 1169 |
class Schema: |
| 1170 |
option = c.Private() |
| 1171 |
|
| 1172 |
with self.expect_error(option="For internal use only."): |
| 1173 |
self.get_config(Schema, {'option': 'somevalue'}) |
| 1174 |
|
| 1175 |
|
| 1176 |
class SubConfigTest(TestCase): |
| 1177 |
def test_subconfig_wrong_type(self): |
| 1178 |
# Test that an error is raised if subconfig does not receive a dict |
| 1179 |
class Schema: |
| 1180 |
option = c.SubConfig() |
| 1181 |
|
| 1182 |
for val in "not_a_dict", ("not_a_dict",), ["not_a_dict"]: |
| 1183 |
with self.subTest(val): |
| 1184 |
with self.expect_error( |
| 1185 |
option=re.compile( |
| 1186 |
r"The configuration is invalid. Expected a key-value mapping " |
| 1187 |
r"\(dict\) but received: .+" |
| 1188 |
) |
| 1189 |
): |
| 1190 |
self.get_config(Schema, {'option': val}) |
| 1191 |
|
| 1192 |
def test_subconfig_ignored(self): |
| 1193 |
"""Default behaviour of subconfig: validation is ignored.""" |
| 1194 |
|
| 1195 |
# Nominal |
| 1196 |
class Schema1: |
| 1197 |
option = c.SubConfig(('cc', c.Choice(('foo', 'bar')))) |
| 1198 |
|
| 1199 |
conf = self.get_config(Schema1, {'option': {'cc': 'foo'}}) |
| 1200 |
self.assertEqual(conf, {'option': {'cc': 'foo'}}) |
| 1201 |
|
| 1202 |
# Invalid option: No error |
| 1203 |
class Schema2: |
| 1204 |
option = c.SubConfig(('cc', c.Choice(('foo', 'bar')))) |
| 1205 |
|
| 1206 |
conf = self.get_config(Schema2, {'option': {'cc': True}}) |
| 1207 |
self.assertEqual(conf, {'option': {'cc': True}}) |
| 1208 |
|
| 1209 |
# Missing option: Will be considered optional with default None |
| 1210 |
class Schema3: |
| 1211 |
option = c.SubConfig(('cc', c.Choice(('foo', 'bar')))) |
| 1212 |
|
| 1213 |
conf = self.get_config(Schema3, {'option': {}}) |
| 1214 |
self.assertEqual(conf, {'option': {'cc': None}}) |
| 1215 |
|
| 1216 |
# Unknown option: No warning |
| 1217 |
class Schema4: |
| 1218 |
option = c.SubConfig(('cc', c.Choice(('foo', 'bar')))) |
| 1219 |
|
| 1220 |
conf = self.get_config(Schema4, {'option': {'unknown_key_is_ok': 0}}) |
| 1221 |
self.assertEqual(conf, {'option': {'cc': None, 'unknown_key_is_ok': 0}}) |
| 1222 |
|
| 1223 |
def test_subconfig_unknown_option(self): |
| 1224 |
class Schema: |
| 1225 |
option = c.SubConfig(validate=True) |
| 1226 |
|
| 1227 |
conf = self.get_config( |
| 1228 |
Schema, |
| 1229 |
{'option': {'unknown': 0}}, |
| 1230 |
warnings=dict(option="Sub-option 'unknown': Unrecognised configuration name: unknown"), |
| 1231 |
) |
| 1232 |
self.assertEqual(conf['option'], {"unknown": 0}) |
| 1233 |
|
| 1234 |
def test_subconfig_invalid_option(self): |
| 1235 |
class Schema: |
| 1236 |
option = c.SubConfig( |
| 1237 |
('cc', c.Choice(('foo', 'bar'))), |
| 1238 |
validate=True, |
| 1239 |
) |
| 1240 |
|
| 1241 |
with self.expect_error( |
| 1242 |
option="Sub-option 'cc': Expected one of: ('foo', 'bar') but received: True" |
| 1243 |
): |
| 1244 |
self.get_config(Schema, {'option': {'cc': True}}) |
| 1245 |
|
| 1246 |
def test_subconfig_normal(self): |
| 1247 |
class Schema: |
| 1248 |
option = c.SubConfig( |
| 1249 |
('cc', c.Choice(('foo', 'bar'))), |
| 1250 |
) |
| 1251 |
|
| 1252 |
conf = self.get_config(Schema, {'option': {'cc': 'foo'}}) |
| 1253 |
self.assertEqual(conf['option'], {'cc': 'foo'}) |
| 1254 |
|
| 1255 |
def test_config_file_path_pass_through(self): |
| 1256 |
"""Necessary to ensure FilesystemObject validates the correct path.""" |
| 1257 |
passed_config_path = None |
| 1258 |
|
| 1259 |
class SubType(c.BaseConfigOption): |
| 1260 |
def pre_validation(self, config, key_name): |
| 1261 |
nonlocal passed_config_path |
| 1262 |
passed_config_path = config.config_file_path |
| 1263 |
|
| 1264 |
class Schema: |
| 1265 |
sub = c.SubConfig(('opt', SubType())) |
| 1266 |
|
| 1267 |
config_path = "foo/mkdocs.yaml" |
| 1268 |
self.get_config(Schema, {"sub": {"opt": "bar"}}, config_file_path=config_path) |
| 1269 |
self.assertEqual(passed_config_path, config_path) |
| 1270 |
|
| 1271 |
|
| 1272 |
class ConfigItemsTest(TestCase): |
| 1273 |
def test_subconfig_with_multiple_items(self): |
| 1274 |
# This had a bug where subsequent items would get merged into the same dict. |
| 1275 |
class Schema: |
| 1276 |
the_items = c.ConfigItems( |
| 1277 |
("value", c.Type(str)), |
| 1278 |
) |
| 1279 |
|
| 1280 |
conf = self.get_config( |
| 1281 |
Schema, |
| 1282 |
{ |
| 1283 |
'the_items': [ |
| 1284 |
{'value': 'a'}, |
| 1285 |
{'value': 'b'}, |
| 1286 |
] |
| 1287 |
}, |
| 1288 |
) |
| 1289 |
self.assertEqual(conf['the_items'], [{'value': 'a'}, {'value': 'b'}]) |
| 1290 |
|
| 1291 |
def test_optional(self): |
| 1292 |
class Schema: |
| 1293 |
sub = c.ListOfItems( |
| 1294 |
c.SubConfig( |
| 1295 |
('opt', c.Type(int)), |
| 1296 |
validate=True, |
| 1297 |
), |
| 1298 |
default=[], |
| 1299 |
) |
| 1300 |
|
| 1301 |
conf = self.get_config(Schema, {}) |
| 1302 |
self.assertEqual(conf['sub'], []) |
| 1303 |
|
| 1304 |
conf = self.get_config(Schema, {'sub': None}) |
| 1305 |
self.assertEqual(conf['sub'], []) |
| 1306 |
|
| 1307 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {}]}) |
| 1308 |
self.assertEqual(conf['sub'], [{'opt': 1}, {'opt': None}]) |
| 1309 |
|
| 1310 |
conf = self.get_config(Schema, {'sub': []}) |
| 1311 |
|
| 1312 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {'opt': 2}]}) |
| 1313 |
self.assertEqual(conf['sub'], [{'opt': 1}, {'opt': 2}]) |
| 1314 |
|
| 1315 |
def test_required(self): |
| 1316 |
class Schema: |
| 1317 |
sub = c.ListOfItems( |
| 1318 |
c.SubConfig( |
| 1319 |
('opt', c.Type(int, required=True)), |
| 1320 |
validate=True, |
| 1321 |
) |
| 1322 |
) |
| 1323 |
|
| 1324 |
with self.expect_error(sub="Required configuration not provided."): |
| 1325 |
conf = self.get_config(Schema, {}) |
| 1326 |
|
| 1327 |
with self.expect_error(sub="Required configuration not provided."): |
| 1328 |
conf = self.get_config(Schema, {'sub': None}) |
| 1329 |
|
| 1330 |
with self.expect_error( |
| 1331 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1332 |
): |
| 1333 |
conf = self.get_config(Schema, {'sub': [{'opt': 'asdf'}, {}]}) |
| 1334 |
|
| 1335 |
conf = self.get_config(Schema, {'sub': []}) |
| 1336 |
|
| 1337 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {'opt': 2}]}) |
| 1338 |
self.assertEqual(conf['sub'], [{'opt': 1}, {'opt': 2}]) |
| 1339 |
|
| 1340 |
with self.expect_error( |
| 1341 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1342 |
): |
| 1343 |
self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]}) |
| 1344 |
|
| 1345 |
with self.expect_error( |
| 1346 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1347 |
): |
| 1348 |
conf = self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]}) |
| 1349 |
|
| 1350 |
with self.expect_error( |
| 1351 |
sub="The configuration is invalid. Expected a key-value mapping " |
| 1352 |
"(dict) but received: <class 'int'>" |
| 1353 |
): |
| 1354 |
conf = self.get_config(Schema, {'sub': [1, 2]}) |
| 1355 |
|
| 1356 |
|
| 1357 |
class MarkdownExtensionsTest(TestCase): |
| 1358 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1359 |
def test_simple_list(self): |
| 1360 |
class Schema: |
| 1361 |
markdown_extensions = c.MarkdownExtensions() |
| 1362 |
mdx_configs = c.Private() |
| 1363 |
|
| 1364 |
config = { |
| 1365 |
'markdown_extensions': ['foo', 'bar'], |
| 1366 |
} |
| 1367 |
conf = self.get_config(Schema, config) |
| 1368 |
self.assertEqual(conf['markdown_extensions'], ['foo', 'bar']) |
| 1369 |
self.assertEqual(conf['mdx_configs'], {}) |
| 1370 |
|
| 1371 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1372 |
def test_list_dicts(self): |
| 1373 |
class Schema: |
| 1374 |
markdown_extensions = c.MarkdownExtensions() |
| 1375 |
mdx_configs = c.Private() |
| 1376 |
|
| 1377 |
config = { |
| 1378 |
'markdown_extensions': [ |
| 1379 |
{'foo': {'foo_option': 'foo value'}}, |
| 1380 |
{'bar': {'bar_option': 'bar value'}}, |
| 1381 |
{'baz': None}, |
| 1382 |
] |
| 1383 |
} |
| 1384 |
conf = self.get_config(Schema, config) |
| 1385 |
self.assertEqual(conf['markdown_extensions'], ['foo', 'bar', 'baz']) |
| 1386 |
self.assertEqual( |
| 1387 |
conf['mdx_configs'], |
| 1388 |
{ |
| 1389 |
'foo': {'foo_option': 'foo value'}, |
| 1390 |
'bar': {'bar_option': 'bar value'}, |
| 1391 |
}, |
| 1392 |
) |
| 1393 |
|
| 1394 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1395 |
def test_mixed_list(self): |
| 1396 |
class Schema: |
| 1397 |
markdown_extensions = c.MarkdownExtensions() |
| 1398 |
mdx_configs = c.Private() |
| 1399 |
|
| 1400 |
config = { |
| 1401 |
'markdown_extensions': [ |
| 1402 |
'foo', |
| 1403 |
{'bar': {'bar_option': 'bar value'}}, |
| 1404 |
] |
| 1405 |
} |
| 1406 |
conf = self.get_config(Schema, config) |
| 1407 |
self.assertEqual(conf['markdown_extensions'], ['foo', 'bar']) |
| 1408 |
self.assertEqual( |
| 1409 |
conf['mdx_configs'], |
| 1410 |
{ |
| 1411 |
'bar': {'bar_option': 'bar value'}, |
| 1412 |
}, |
| 1413 |
) |
| 1414 |
|
| 1415 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1416 |
def test_dict_of_dicts(self): |
| 1417 |
class Schema: |
| 1418 |
markdown_extensions = c.MarkdownExtensions() |
| 1419 |
mdx_configs = c.Private() |
| 1420 |
|
| 1421 |
config = { |
| 1422 |
'markdown_extensions': { |
| 1423 |
'foo': {'foo_option': 'foo value'}, |
| 1424 |
'bar': {'bar_option': 'bar value'}, |
| 1425 |
'baz': {}, |
| 1426 |
} |
| 1427 |
} |
| 1428 |
conf = self.get_config(Schema, config) |
| 1429 |
self.assertEqual(conf['markdown_extensions'], ['foo', 'bar', 'baz']) |
| 1430 |
self.assertEqual( |
| 1431 |
conf['mdx_configs'], |
| 1432 |
{ |
| 1433 |
'foo': {'foo_option': 'foo value'}, |
| 1434 |
'bar': {'bar_option': 'bar value'}, |
| 1435 |
}, |
| 1436 |
) |
| 1437 |
|
| 1438 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1439 |
def test_builtins(self): |
| 1440 |
class Schema: |
| 1441 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1442 |
mdx_configs = c.Private() |
| 1443 |
|
| 1444 |
config = { |
| 1445 |
'markdown_extensions': ['foo', 'bar'], |
| 1446 |
} |
| 1447 |
conf = self.get_config(Schema, config) |
| 1448 |
self.assertEqual(conf['markdown_extensions'], ['meta', 'toc', 'foo', 'bar']) |
| 1449 |
self.assertEqual(conf['mdx_configs'], {}) |
| 1450 |
|
| 1451 |
def test_duplicates(self): |
| 1452 |
class Schema: |
| 1453 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1454 |
mdx_configs = c.Private() |
| 1455 |
|
| 1456 |
config = { |
| 1457 |
'markdown_extensions': ['meta', 'toc'], |
| 1458 |
} |
| 1459 |
conf = self.get_config(Schema, config) |
| 1460 |
self.assertEqual(conf['markdown_extensions'], ['meta', 'toc']) |
| 1461 |
self.assertEqual(conf['mdx_configs'], {}) |
| 1462 |
|
| 1463 |
def test_builtins_config(self): |
| 1464 |
class Schema: |
| 1465 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1466 |
mdx_configs = c.Private() |
| 1467 |
|
| 1468 |
config = { |
| 1469 |
'markdown_extensions': [ |
| 1470 |
{'toc': {'permalink': True}}, |
| 1471 |
], |
| 1472 |
} |
| 1473 |
conf = self.get_config(Schema, config) |
| 1474 |
self.assertEqual(conf['markdown_extensions'], ['meta', 'toc']) |
| 1475 |
self.assertEqual(conf['mdx_configs'], {'toc': {'permalink': True}}) |
| 1476 |
|
| 1477 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1478 |
def test_configkey(self): |
| 1479 |
class Schema: |
| 1480 |
markdown_extensions = c.MarkdownExtensions(configkey='bar') |
| 1481 |
bar = c.Private() |
| 1482 |
|
| 1483 |
config = { |
| 1484 |
'markdown_extensions': [ |
| 1485 |
{'foo': {'foo_option': 'foo value'}}, |
| 1486 |
] |
| 1487 |
} |
| 1488 |
conf = self.get_config(Schema, config) |
| 1489 |
self.assertEqual(conf['markdown_extensions'], ['foo']) |
| 1490 |
self.assertEqual( |
| 1491 |
conf['bar'], |
| 1492 |
{ |
| 1493 |
'foo': {'foo_option': 'foo value'}, |
| 1494 |
}, |
| 1495 |
) |
| 1496 |
|
| 1497 |
def test_missing_default(self): |
| 1498 |
class Schema: |
| 1499 |
markdown_extensions = c.MarkdownExtensions() |
| 1500 |
mdx_configs = c.Private() |
| 1501 |
|
| 1502 |
conf = self.get_config(Schema, {}) |
| 1503 |
self.assertEqual(conf['markdown_extensions'], []) |
| 1504 |
self.assertEqual(conf['mdx_configs'], {}) |
| 1505 |
|
| 1506 |
def test_none(self): |
| 1507 |
class Schema: |
| 1508 |
markdown_extensions = c.MarkdownExtensions(default=[]) |
| 1509 |
mdx_configs = c.Private() |
| 1510 |
|
| 1511 |
config = { |
| 1512 |
'markdown_extensions': None, |
| 1513 |
} |
| 1514 |
conf = self.get_config(Schema, config) |
| 1515 |
self.assertEqual(conf['markdown_extensions'], []) |
| 1516 |
self.assertEqual(conf['mdx_configs'], {}) |
| 1517 |
|
| 1518 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1519 |
def test_not_list(self): |
| 1520 |
class Schema: |
| 1521 |
option = c.MarkdownExtensions() |
| 1522 |
|
| 1523 |
with self.expect_error(option="Invalid Markdown Extensions configuration"): |
| 1524 |
self.get_config(Schema, {'option': 'not a list'}) |
| 1525 |
|
| 1526 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1527 |
def test_invalid_config_option(self): |
| 1528 |
class Schema: |
| 1529 |
markdown_extensions = c.MarkdownExtensions() |
| 1530 |
|
| 1531 |
config = { |
| 1532 |
'markdown_extensions': [ |
| 1533 |
{'foo': 'not a dict'}, |
| 1534 |
], |
| 1535 |
} |
| 1536 |
with self.expect_error( |
| 1537 |
markdown_extensions="Invalid config options for Markdown Extension 'foo'." |
| 1538 |
): |
| 1539 |
self.get_config(Schema, config) |
| 1540 |
|
| 1541 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1542 |
def test_invalid_config_item(self): |
| 1543 |
class Schema: |
| 1544 |
markdown_extensions = c.MarkdownExtensions() |
| 1545 |
|
| 1546 |
config = { |
| 1547 |
'markdown_extensions': [ |
| 1548 |
['not a dict'], |
| 1549 |
], |
| 1550 |
} |
| 1551 |
with self.expect_error(markdown_extensions="Invalid Markdown Extensions configuration"): |
| 1552 |
self.get_config(Schema, config) |
| 1553 |
|
| 1554 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1555 |
def test_invalid_dict_item(self): |
| 1556 |
class Schema: |
| 1557 |
markdown_extensions = c.MarkdownExtensions() |
| 1558 |
|
| 1559 |
config = { |
| 1560 |
'markdown_extensions': [ |
| 1561 |
{'key1': 'value', 'key2': 'too many keys'}, |
| 1562 |
], |
| 1563 |
} |
| 1564 |
with self.expect_error(markdown_extensions="Invalid Markdown Extensions configuration"): |
| 1565 |
self.get_config(Schema, config) |
| 1566 |
|
| 1567 |
def test_unknown_extension(self): |
| 1568 |
class Schema: |
| 1569 |
markdown_extensions = c.MarkdownExtensions() |
| 1570 |
|
| 1571 |
config = { |
| 1572 |
'markdown_extensions': ['unknown'], |
| 1573 |
} |
| 1574 |
with self.expect_error( |
| 1575 |
markdown_extensions=re.compile(r"Failed to load extension 'unknown'.\n.+") |
| 1576 |
): |
| 1577 |
self.get_config(Schema, config) |
| 1578 |
|
| 1579 |
def test_multiple_markdown_config_instances(self): |
| 1580 |
# This had a bug where an extension config would persist to separate |
| 1581 |
# config instances that didn't specify extensions. |
| 1582 |
class Schema: |
| 1583 |
markdown_extensions = c.MarkdownExtensions() |
| 1584 |
mdx_configs = c.Private() |
| 1585 |
|
| 1586 |
conf = self.get_config( |
| 1587 |
Schema, |
| 1588 |
{ |
| 1589 |
'markdown_extensions': [{'toc': {'permalink': '##'}}], |
| 1590 |
}, |
| 1591 |
) |
| 1592 |
self.assertEqual(conf['mdx_configs']['toc'], {'permalink': '##'}) |
| 1593 |
|
| 1594 |
conf = self.get_config( |
| 1595 |
Schema, |
| 1596 |
{}, |
| 1597 |
) |
| 1598 |
self.assertIsNone(conf['mdx_configs'].get('toc')) |
| 1599 |
|
| 1600 |
|
| 1601 |
class HooksTest(TestCase): |
| 1602 |
class Schema: |
| 1603 |
plugins = c.Plugins(default=[]) |
| 1604 |
hooks = c.Hooks('plugins') |
| 1605 |
|
| 1606 |
@tempdir() |
| 1607 |
def test_hooks(self, src_dir): |
| 1608 |
write_file( |
| 1609 |
b'def on_page_markdown(markdown, **kwargs): return markdown.replace("f", "z")', |
| 1610 |
os.path.join(src_dir, 'hooks', 'my_hook.py'), |
| 1611 |
) |
| 1612 |
write_file( |
| 1613 |
b'foo foo', |
| 1614 |
os.path.join(src_dir, 'docs', 'index.md'), |
| 1615 |
) |
| 1616 |
conf = self.get_config( |
| 1617 |
self.Schema, |
| 1618 |
{'hooks': ['hooks/my_hook.py']}, |
| 1619 |
config_file_path=os.path.join(src_dir, 'mkdocs.yml'), |
| 1620 |
) |
| 1621 |
self.assertIn('hooks/my_hook.py', conf['plugins']) |
| 1622 |
hook = conf['plugins']['hooks/my_hook.py'] |
| 1623 |
self.assertTrue(hasattr(hook, 'on_page_markdown')) |
| 1624 |
self.assertEqual( |
| 1625 |
{**conf['plugins'].events, 'page_markdown': [hook.on_page_markdown]}, |
| 1626 |
conf['plugins'].events, |
| 1627 |
) |
| 1628 |
self.assertEqual(hook.on_page_markdown('foo foo'), 'zoo zoo') |
| 1629 |
self.assertFalse(hasattr(hook, 'on_nav')) |
| 1630 |
|
| 1631 |
|
| 1632 |
class SchemaTest(TestCase): |
| 1633 |
def test_copy(self): |
| 1634 |
copy.deepcopy( |
| 1635 |
base.LegacyConfig( |
| 1636 |
(('foo', c.MarkdownExtensions()),), |
| 1637 |
), |
| 1638 |
) |
| 1639 |
|
| 1640 |
copy.deepcopy(self.get_config(IpAddressTest.Schema, {'option': '1.2.3.4:5678'})) |
| 1641 |
copy.deepcopy(IpAddressTest.Schema) |
| 1642 |
copy.deepcopy(base.get_schema(IpAddressTest.Schema)) |
| 1643 |
|
| 1644 |
copy.deepcopy(self.get_config(EditURITest.Schema, {})) |
| 1645 |
copy.deepcopy(EditURITest.Schema) |
| 1646 |
copy.deepcopy(base.get_schema(EditURITest.Schema)) |