| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import copy |
| 5 |
import io |
| 6 |
import logging |
| 7 |
import os |
| 8 |
import re |
| 9 |
import sys |
| 10 |
import textwrap |
| 11 |
import unittest |
| 12 |
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeVar, Union |
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from typing_extensions import assert_type |
| 17 |
else: |
| 18 |
|
| 19 |
def assert_type(val, typ): |
| 20 |
return None |
| 21 |
|
| 22 |
|
| 23 |
import mkdocs |
| 24 |
from mkdocs.config import config_options as c |
| 25 |
from mkdocs.config import defaults |
| 26 |
from mkdocs.config.base import Config |
| 27 |
from mkdocs.plugins import BasePlugin, PluginCollection |
| 28 |
from mkdocs.tests.base import tempdir |
| 29 |
from mkdocs.theme import Theme |
| 30 |
from mkdocs.utils import write_file, yaml_load |
| 31 |
|
| 32 |
SomeConfig = TypeVar('SomeConfig', bound=Config) |
| 33 |
|
| 34 |
|
| 35 |
class UnexpectedError(Exception): |
| 36 |
pass |
| 37 |
|
| 38 |
|
| 39 |
class TestCase(unittest.TestCase): |
| 40 |
@contextlib.contextmanager |
| 41 |
def expect_error(self, **kwargs): |
| 42 |
[(key, msg)] = kwargs.items() |
| 43 |
with self.assertRaises(UnexpectedError) as cm: |
| 44 |
yield |
| 45 |
if isinstance(msg, re.Pattern): |
| 46 |
self.assertRegex(str(cm.exception), f'^{key}="{msg.pattern}"$') |
| 47 |
else: |
| 48 |
self.assertEqual(f'{key}="{msg}"', str(cm.exception)) |
| 49 |
|
| 50 |
def get_config( |
| 51 |
self, |
| 52 |
config_class: type[SomeConfig], |
| 53 |
cfg: dict[str, Any], |
| 54 |
warnings: dict[str, str] = {}, |
| 55 |
config_file_path=None, |
| 56 |
) -> SomeConfig: |
| 57 |
config = config_class(config_file_path=config_file_path) |
| 58 |
config.load_dict(cfg) |
| 59 |
actual_errors, actual_warnings = config.validate() |
| 60 |
if actual_errors: |
| 61 |
raise UnexpectedError(', '.join(f'{key}="{msg}"' for key, msg in actual_errors)) |
| 62 |
self.assertEqual(warnings, dict(actual_warnings)) |
| 63 |
return config |
| 64 |
|
| 65 |
|
| 66 |
class TypeTest(TestCase): |
| 67 |
def test_single_type(self) -> None: |
| 68 |
class Schema(Config): |
| 69 |
option = c.Type(str) |
| 70 |
|
| 71 |
conf = self.get_config(Schema, {'option': "Testing"}) |
| 72 |
assert_type(conf.option, str) |
| 73 |
self.assertEqual(conf.option, "Testing") |
| 74 |
|
| 75 |
def test_multiple_types(self) -> None: |
| 76 |
class Schema(Config): |
| 77 |
option = c.Type((list, tuple)) |
| 78 |
|
| 79 |
conf = self.get_config(Schema, {'option': [1, 2, 3]}) |
| 80 |
self.assertEqual(conf.option, [1, 2, 3]) |
| 81 |
|
| 82 |
conf = self.get_config(Schema, {'option': (1, 2, 3)}) |
| 83 |
self.assertEqual(conf.option, (1, 2, 3)) |
| 84 |
|
| 85 |
with self.expect_error( |
| 86 |
option="Expected type: (<class 'list'>, <class 'tuple'>) but received: <class 'dict'>" |
| 87 |
): |
| 88 |
self.get_config(Schema, {'option': {'a': 1}}) |
| 89 |
|
| 90 |
def test_length(self) -> None: |
| 91 |
class Schema(Config): |
| 92 |
option = c.Type(str, length=7) |
| 93 |
|
| 94 |
conf = self.get_config(Schema, {'option': "Testing"}) |
| 95 |
assert_type(conf.option, str) |
| 96 |
self.assertEqual(conf.option, "Testing") |
| 97 |
|
| 98 |
with self.expect_error( |
| 99 |
option="Expected type: <class 'str'> with length 7 but received: 'Testing Long' with length 12" |
| 100 |
): |
| 101 |
self.get_config(Schema, {'option': "Testing Long"}) |
| 102 |
|
| 103 |
def test_optional_with_default(self) -> None: |
| 104 |
with self.assertRaisesRegex(ValueError, "doesn't need to be wrapped into Optional"): |
| 105 |
c.Optional(c.Type(int, default=5)) |
| 106 |
|
| 107 |
|
| 108 |
class ChoiceTest(TestCase): |
| 109 |
def test_required(self) -> None: |
| 110 |
class Schema(Config): |
| 111 |
option = c.Choice(('python', 'node')) |
| 112 |
|
| 113 |
conf = self.get_config(Schema, {'option': 'python'}) |
| 114 |
assert_type(conf.option, str) |
| 115 |
self.assertEqual(conf.option, 'python') |
| 116 |
|
| 117 |
def test_optional(self) -> None: |
| 118 |
class Schema(Config): |
| 119 |
option = c.Optional(c.Choice(('python', 'node'))) |
| 120 |
|
| 121 |
conf = self.get_config(Schema, {'option': 'python'}) |
| 122 |
assert_type(conf.option, Optional[str]) |
| 123 |
self.assertEqual(conf.option, 'python') |
| 124 |
|
| 125 |
conf = self.get_config(Schema, {}) |
| 126 |
self.assertEqual(conf.option, None) |
| 127 |
|
| 128 |
conf = self.get_config(Schema, {'option': None}) |
| 129 |
self.assertEqual(conf.option, None) |
| 130 |
|
| 131 |
def test_default(self) -> None: |
| 132 |
class Schema(Config): |
| 133 |
option = c.Choice(('a', 'b', 'c'), default='b') |
| 134 |
|
| 135 |
conf = self.get_config(Schema, {}) |
| 136 |
assert_type(conf.option, str) |
| 137 |
self.assertEqual(conf.option, 'b') |
| 138 |
|
| 139 |
conf = self.get_config(Schema, {}) |
| 140 |
self.assertEqual(conf.option, 'b') |
| 141 |
|
| 142 |
conf = self.get_config(Schema, {'option': None}) |
| 143 |
self.assertEqual(conf.option, 'b') |
| 144 |
|
| 145 |
with self.expect_error(option="Expected one of: ('a', 'b', 'c') but received: 'go'"): |
| 146 |
self.get_config(Schema, {'option': 'go'}) |
| 147 |
|
| 148 |
def test_invalid_default(self) -> None: |
| 149 |
with self.assertRaises(ValueError): |
| 150 |
c.Choice(('a', 'b'), default='c') |
| 151 |
with self.assertRaises(ValueError): |
| 152 |
c.Choice(('a', 'b'), default='c', required=True) |
| 153 |
|
| 154 |
def test_invalid_choice(self) -> None: |
| 155 |
class Schema(Config): |
| 156 |
option = c.Choice(('python', 'node')) |
| 157 |
|
| 158 |
with self.expect_error(option="Expected one of: ('python', 'node') but received: 'go'"): |
| 159 |
self.get_config(Schema, {'option': 'go'}) |
| 160 |
|
| 161 |
def test_invalid_choices(self) -> None: |
| 162 |
with self.assertRaises(ValueError): |
| 163 |
c.Choice('') |
| 164 |
with self.assertRaises(ValueError): |
| 165 |
c.Choice([]) |
| 166 |
|
| 167 |
|
| 168 |
class DeprecatedTest(TestCase): |
| 169 |
def test_deprecated_option_simple(self) -> None: |
| 170 |
class Schema(Config): |
| 171 |
d = c.Deprecated() |
| 172 |
|
| 173 |
self.get_config( |
| 174 |
Schema, |
| 175 |
{'d': 'value'}, |
| 176 |
warnings=dict( |
| 177 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 178 |
"future release." |
| 179 |
), |
| 180 |
) |
| 181 |
|
| 182 |
def test_deprecated_option_message(self) -> None: |
| 183 |
class Schema(Config): |
| 184 |
d = c.Deprecated(message='custom message for {} key') |
| 185 |
|
| 186 |
self.get_config(Schema, {'d': 'value'}, warnings={'d': 'custom message for d key'}) |
| 187 |
|
| 188 |
def test_deprecated_option_with_type(self) -> None: |
| 189 |
class Schema(Config): |
| 190 |
d = c.Deprecated(option_type=c.Type(str)) |
| 191 |
|
| 192 |
self.get_config( |
| 193 |
Schema, |
| 194 |
{'d': 'value'}, |
| 195 |
warnings=dict( |
| 196 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 197 |
"future release." |
| 198 |
), |
| 199 |
) |
| 200 |
|
| 201 |
def test_deprecated_option_with_invalid_type(self) -> None: |
| 202 |
class Schema(Config): |
| 203 |
d = c.Deprecated(option_type=c.Type(list)) |
| 204 |
|
| 205 |
with self.expect_error(d="Expected type: <class 'list'> but received: <class 'str'>"): |
| 206 |
self.get_config( |
| 207 |
Schema, |
| 208 |
{'d': 'value'}, |
| 209 |
warnings=dict( |
| 210 |
d="The configuration option 'd' has been deprecated and will be removed in a " |
| 211 |
"future release." |
| 212 |
), |
| 213 |
) |
| 214 |
|
| 215 |
def test_removed_option(self) -> None: |
| 216 |
class Schema(Config): |
| 217 |
d = c.Deprecated(removed=True, moved_to='foo') |
| 218 |
|
| 219 |
with self.expect_error( |
| 220 |
d="The configuration option 'd' was removed from MkDocs. Use 'foo' instead.", |
| 221 |
): |
| 222 |
self.get_config(Schema, {'d': 'value'}) |
| 223 |
|
| 224 |
def test_deprecated_option_with_type_undefined(self) -> None: |
| 225 |
class Schema(Config): |
| 226 |
option = c.Deprecated(option_type=c.Type(str)) |
| 227 |
|
| 228 |
self.get_config(Schema, {'option': None}) |
| 229 |
|
| 230 |
def test_deprecated_option_move(self) -> None: |
| 231 |
class Schema(Config): |
| 232 |
new = c.Type(str) |
| 233 |
old = c.Deprecated(moved_to='new') |
| 234 |
|
| 235 |
conf = self.get_config( |
| 236 |
Schema, |
| 237 |
{'old': 'value'}, |
| 238 |
warnings=dict( |
| 239 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 240 |
"future release. Use 'new' instead." |
| 241 |
), |
| 242 |
) |
| 243 |
self.assertEqual(conf, {'new': 'value', 'old': None}) |
| 244 |
|
| 245 |
def test_deprecated_option_move_complex(self) -> None: |
| 246 |
class Schema(Config): |
| 247 |
foo = c.Type(dict) |
| 248 |
old = c.Deprecated(moved_to='foo.bar') |
| 249 |
|
| 250 |
conf = self.get_config( |
| 251 |
Schema, |
| 252 |
{'old': 'value'}, |
| 253 |
warnings=dict( |
| 254 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 255 |
"future release. Use 'foo.bar' instead." |
| 256 |
), |
| 257 |
) |
| 258 |
self.assertEqual(conf, {'foo': {'bar': 'value'}, 'old': None}) |
| 259 |
|
| 260 |
def test_deprecated_option_move_existing(self) -> None: |
| 261 |
class Schema(Config): |
| 262 |
foo = c.Type(dict) |
| 263 |
old = c.Deprecated(moved_to='foo.bar') |
| 264 |
|
| 265 |
conf = self.get_config( |
| 266 |
Schema, |
| 267 |
{'old': 'value', 'foo': {'existing': 'existing'}}, |
| 268 |
warnings=dict( |
| 269 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 270 |
"future release. Use 'foo.bar' instead." |
| 271 |
), |
| 272 |
) |
| 273 |
self.assertEqual(conf, {'foo': {'existing': 'existing', 'bar': 'value'}, 'old': None}) |
| 274 |
|
| 275 |
def test_deprecated_option_move_invalid(self) -> None: |
| 276 |
class Schema(Config): |
| 277 |
foo = c.Type(dict) |
| 278 |
old = c.Deprecated(moved_to='foo.bar') |
| 279 |
|
| 280 |
with self.expect_error(foo="Expected type: <class 'dict'> but received: <class 'str'>"): |
| 281 |
self.get_config( |
| 282 |
Schema, |
| 283 |
{'old': 'value', 'foo': 'wrong type'}, |
| 284 |
warnings=dict( |
| 285 |
old="The configuration option 'old' has been deprecated and will be removed in a " |
| 286 |
"future release. Use 'foo.bar' instead." |
| 287 |
), |
| 288 |
) |
| 289 |
|
| 290 |
|
| 291 |
class IpAddressTest(TestCase): |
| 292 |
class Schema(Config): |
| 293 |
option = c.IpAddress() |
| 294 |
|
| 295 |
def test_valid_address(self) -> None: |
| 296 |
addr = '127.0.0.1:8000' |
| 297 |
|
| 298 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 299 |
|
| 300 |
assert_type(conf.option, c._IpAddressValue) |
| 301 |
assert_type(conf.option.host, str) |
| 302 |
assert_type(conf.option.port, int) |
| 303 |
|
| 304 |
self.assertEqual(str(conf.option), addr) |
| 305 |
self.assertEqual(conf.option.host, '127.0.0.1') |
| 306 |
self.assertEqual(conf.option.port, 8000) |
| 307 |
|
| 308 |
def test_valid_IPv6_address(self) -> None: |
| 309 |
addr = '::1:8000' |
| 310 |
|
| 311 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 312 |
self.assertEqual(str(conf.option), addr) |
| 313 |
self.assertEqual(conf.option.host, '::1') |
| 314 |
self.assertEqual(conf.option.port, 8000) |
| 315 |
|
| 316 |
def test_valid_full_IPv6_address(self) -> None: |
| 317 |
addr = '[2001:db8:85a3::8a2e:370:7334]:123' |
| 318 |
|
| 319 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 320 |
self.assertEqual(conf.option.host, '2001:db8:85a3::8a2e:370:7334') |
| 321 |
self.assertEqual(conf.option.port, 123) |
| 322 |
|
| 323 |
def test_named_address(self) -> None: |
| 324 |
addr = 'localhost:8000' |
| 325 |
|
| 326 |
conf = self.get_config(self.Schema, {'option': addr}) |
| 327 |
self.assertEqual(str(conf.option), addr) |
| 328 |
self.assertEqual(conf.option.host, 'localhost') |
| 329 |
self.assertEqual(conf.option.port, 8000) |
| 330 |
|
| 331 |
def test_default_address(self) -> None: |
| 332 |
addr = '127.0.0.1:8000' |
| 333 |
|
| 334 |
class Schema(Config): |
| 335 |
option = c.IpAddress(default=addr) |
| 336 |
|
| 337 |
conf = self.get_config(Schema, {'option': None}) |
| 338 |
self.assertEqual(str(conf.option), addr) |
| 339 |
self.assertEqual(conf.option.host, '127.0.0.1') |
| 340 |
self.assertEqual(conf.option.port, 8000) |
| 341 |
|
| 342 |
def test_bind_all_IPv4_address(self): |
| 343 |
addr = '0.0.0.0:8000' |
| 344 |
|
| 345 |
class Schema(Config): |
| 346 |
option = c.IpAddress(default=addr) |
| 347 |
|
| 348 |
conf = self.get_config(Schema, {'option': None}) |
| 349 |
self.assertEqual(str(conf['option']), addr) |
| 350 |
self.assertEqual(conf['option'].host, '0.0.0.0') |
| 351 |
self.assertEqual(conf['option'].port, 8000) |
| 352 |
|
| 353 |
def test_bind_all_IPv6_address(self): |
| 354 |
addr = ':::8000' |
| 355 |
|
| 356 |
class Schema(Config): |
| 357 |
option = c.IpAddress(default=addr) |
| 358 |
|
| 359 |
conf = self.get_config(Schema, {'option': None}) |
| 360 |
self.assertEqual(str(conf['option']), addr) |
| 361 |
self.assertEqual(conf['option'].host, '::') |
| 362 |
self.assertEqual(conf['option'].port, 8000) |
| 363 |
|
| 364 |
@unittest.skipIf( |
| 365 |
sys.version_info < (3, 9, 5), |
| 366 |
"Leading zeros allowed in IP addresses before Python3.9.5", |
| 367 |
) |
| 368 |
def test_invalid_leading_zeros(self) -> None: |
| 369 |
with self.expect_error( |
| 370 |
option="'127.000.000.001' does not appear to be an IPv4 or IPv6 address" |
| 371 |
): |
| 372 |
self.get_config(self.Schema, {'option': '127.000.000.001:8000'}) |
| 373 |
|
| 374 |
def test_invalid_address_range(self) -> None: |
| 375 |
with self.expect_error(option="'277.0.0.1' does not appear to be an IPv4 or IPv6 address"): |
| 376 |
self.get_config(self.Schema, {'option': '277.0.0.1:8000'}) |
| 377 |
|
| 378 |
def test_invalid_address_format(self) -> None: |
| 379 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 380 |
self.get_config(self.Schema, {'option': '127.0.0.18000'}) |
| 381 |
|
| 382 |
def test_invalid_address_type(self) -> None: |
| 383 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 384 |
self.get_config(self.Schema, {'option': 123}) |
| 385 |
|
| 386 |
def test_invalid_address_port(self) -> None: |
| 387 |
with self.expect_error(option="'foo' is not a valid port"): |
| 388 |
self.get_config(self.Schema, {'option': '127.0.0.1:foo'}) |
| 389 |
|
| 390 |
def test_invalid_address_missing_port(self) -> None: |
| 391 |
with self.expect_error(option="Must be a string of format 'IP:PORT'"): |
| 392 |
self.get_config(self.Schema, {'option': '127.0.0.1'}) |
| 393 |
|
| 394 |
|
| 395 |
class URLTest(TestCase): |
| 396 |
def test_valid_url(self) -> None: |
| 397 |
class Schema(Config): |
| 398 |
option = c.URL() |
| 399 |
|
| 400 |
conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) |
| 401 |
assert_type(conf.option, str) |
| 402 |
self.assertEqual(conf.option, "https://mkdocs.org") |
| 403 |
|
| 404 |
conf = self.get_config(Schema, {'option': ""}) |
| 405 |
self.assertEqual(conf.option, "") |
| 406 |
|
| 407 |
def test_valid_url_is_dir(self) -> None: |
| 408 |
class Schema(Config): |
| 409 |
option = c.URL(is_dir=True) |
| 410 |
|
| 411 |
conf = self.get_config(Schema, {'option': "http://mkdocs.org/"}) |
| 412 |
self.assertEqual(conf.option, "http://mkdocs.org/") |
| 413 |
|
| 414 |
conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) |
| 415 |
self.assertEqual(conf.option, "https://mkdocs.org/") |
| 416 |
|
| 417 |
def test_optional(self): |
| 418 |
class Schema(Config): |
| 419 |
option = c.Optional(c.URL(is_dir=True)) |
| 420 |
|
| 421 |
conf = self.get_config(Schema, {'option': ''}) |
| 422 |
self.assertEqual(conf.option, '') |
| 423 |
|
| 424 |
conf = self.get_config(Schema, {'option': None}) |
| 425 |
self.assertEqual(conf.option, None) |
| 426 |
|
| 427 |
def test_invalid_url(self) -> None: |
| 428 |
class Schema(Config): |
| 429 |
option = c.URL() |
| 430 |
|
| 431 |
with self.expect_error(option="Required configuration not provided."): |
| 432 |
self.get_config(Schema, {'option': None}) |
| 433 |
|
| 434 |
for url in "www.mkdocs.org", "//mkdocs.org/test", "http:/mkdocs.org/", "/hello/": |
| 435 |
with self.subTest(url=url): |
| 436 |
with self.expect_error( |
| 437 |
option="The URL isn't valid, it should include the http:// (scheme)" |
| 438 |
): |
| 439 |
self.get_config(Schema, {'option': url}) |
| 440 |
|
| 441 |
def test_invalid_type(self) -> None: |
| 442 |
class Schema(Config): |
| 443 |
option = c.URL() |
| 444 |
|
| 445 |
with self.expect_error(option="Expected a string, got <class 'int'>"): |
| 446 |
self.get_config(Schema, {'option': 1}) |
| 447 |
|
| 448 |
|
| 449 |
class EditURITest(TestCase): |
| 450 |
class Schema(Config): |
| 451 |
repo_url = c.Optional(c.URL()) |
| 452 |
repo_name = c.Optional(c.RepoName('repo_url')) |
| 453 |
edit_uri_template = c.Optional(c.EditURITemplate('edit_uri')) |
| 454 |
edit_uri = c.Optional(c.EditURI('repo_url')) |
| 455 |
|
| 456 |
def test_repo_name_github(self) -> None: |
| 457 |
conf = self.get_config( |
| 458 |
self.Schema, |
| 459 |
{'repo_url': "https://github.com/mkdocs/mkdocs"}, |
| 460 |
) |
| 461 |
assert_type(conf.repo_name, Optional[str]) |
| 462 |
self.assertEqual(conf.repo_name, "GitHub") |
| 463 |
|
| 464 |
def test_repo_name_bitbucket(self) -> None: |
| 465 |
conf = self.get_config( |
| 466 |
self.Schema, |
| 467 |
{'repo_url': "https://bitbucket.org/gutworth/six/"}, |
| 468 |
) |
| 469 |
self.assertEqual(conf.repo_name, "Bitbucket") |
| 470 |
|
| 471 |
def test_repo_name_gitlab(self) -> None: |
| 472 |
conf = self.get_config( |
| 473 |
self.Schema, |
| 474 |
{'repo_url': "https://gitlab.com/gitlab-org/gitlab-ce/"}, |
| 475 |
) |
| 476 |
self.assertEqual(conf.repo_name, "GitLab") |
| 477 |
|
| 478 |
def test_repo_name_custom(self) -> None: |
| 479 |
conf = self.get_config( |
| 480 |
self.Schema, |
| 481 |
{'repo_url': "https://launchpad.net/python-tuskarclient"}, |
| 482 |
) |
| 483 |
self.assertEqual(conf.repo_name, "Launchpad") |
| 484 |
|
| 485 |
def test_edit_uri_github(self) -> None: |
| 486 |
conf = self.get_config( |
| 487 |
self.Schema, |
| 488 |
{'repo_url': "https://github.com/mkdocs/mkdocs"}, |
| 489 |
) |
| 490 |
assert_type(conf.edit_uri, Optional[str]) |
| 491 |
assert_type(conf.repo_url, Optional[str]) |
| 492 |
self.assertEqual(conf.edit_uri, 'edit/master/docs/') |
| 493 |
self.assertEqual(conf.repo_url, "https://github.com/mkdocs/mkdocs") |
| 494 |
|
| 495 |
def test_edit_uri_bitbucket(self) -> None: |
| 496 |
conf = self.get_config( |
| 497 |
self.Schema, |
| 498 |
{'repo_url': "https://bitbucket.org/gutworth/six/"}, |
| 499 |
) |
| 500 |
self.assertEqual(conf.edit_uri, 'src/default/docs/') |
| 501 |
self.assertEqual(conf.repo_url, "https://bitbucket.org/gutworth/six/") |
| 502 |
|
| 503 |
def test_edit_uri_gitlab(self) -> None: |
| 504 |
conf = self.get_config( |
| 505 |
self.Schema, |
| 506 |
{'repo_url': "https://gitlab.com/gitlab-org/gitlab-ce/"}, |
| 507 |
) |
| 508 |
self.assertEqual(conf.edit_uri, 'edit/master/docs/') |
| 509 |
|
| 510 |
def test_edit_uri_custom(self) -> None: |
| 511 |
conf = self.get_config( |
| 512 |
self.Schema, |
| 513 |
{'repo_url': "https://launchpad.net/python-tuskarclient"}, |
| 514 |
) |
| 515 |
self.assertEqual(conf.edit_uri, None) |
| 516 |
self.assertEqual(conf.repo_url, "https://launchpad.net/python-tuskarclient") |
| 517 |
|
| 518 |
def test_repo_name_custom_and_empty_edit_uri(self) -> None: |
| 519 |
conf = self.get_config( |
| 520 |
self.Schema, |
| 521 |
{'repo_url': "https://github.com/mkdocs/mkdocs", 'repo_name': 'mkdocs'}, |
| 522 |
) |
| 523 |
self.assertEqual(conf.edit_uri, 'edit/master/docs/') |
| 524 |
|
| 525 |
def test_edit_uri_template_ok(self) -> None: |
| 526 |
conf = self.get_config( |
| 527 |
self.Schema, |
| 528 |
{ |
| 529 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 530 |
'edit_uri_template': 'edit/foo/docs/{path}', |
| 531 |
}, |
| 532 |
) |
| 533 |
assert_type(conf.edit_uri_template, Optional[str]) |
| 534 |
self.assertEqual(conf.edit_uri_template, 'edit/foo/docs/{path}') |
| 535 |
|
| 536 |
def test_edit_uri_template_errors(self) -> None: |
| 537 |
with self.expect_error( |
| 538 |
edit_uri_template=re.compile(r'.*[{}].*') # Complains about unclosed '{' or missing '}' |
| 539 |
): |
| 540 |
self.get_config( |
| 541 |
self.Schema, |
| 542 |
{ |
| 543 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 544 |
'edit_uri_template': 'edit/master/{path', |
| 545 |
}, |
| 546 |
) |
| 547 |
|
| 548 |
with self.expect_error(edit_uri_template=re.compile(r'.*\bz\b.*')): |
| 549 |
self.get_config( |
| 550 |
self.Schema, |
| 551 |
{ |
| 552 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 553 |
'edit_uri_template': 'edit/master/{path!z}', |
| 554 |
}, |
| 555 |
) |
| 556 |
|
| 557 |
with self.expect_error(edit_uri_template="Unknown template substitute: 'foo'"): |
| 558 |
self.get_config( |
| 559 |
self.Schema, |
| 560 |
{ |
| 561 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 562 |
'edit_uri_template': 'edit/master/{foo}', |
| 563 |
}, |
| 564 |
) |
| 565 |
|
| 566 |
def test_edit_uri_template_warning(self) -> None: |
| 567 |
conf = self.get_config( |
| 568 |
self.Schema, |
| 569 |
{ |
| 570 |
'repo_url': "https://github.com/mkdocs/mkdocs", |
| 571 |
'edit_uri': 'edit', |
| 572 |
'edit_uri_template': 'edit/master/{path}', |
| 573 |
}, |
| 574 |
warnings=dict( |
| 575 |
edit_uri_template="The option 'edit_uri' has no effect when 'edit_uri_template' is set." |
| 576 |
), |
| 577 |
) |
| 578 |
self.assertEqual(conf.edit_uri_template, 'edit/master/{path}') |
| 579 |
|
| 580 |
|
| 581 |
class ListOfItemsTest(TestCase): |
| 582 |
def test_int_type(self) -> None: |
| 583 |
class Schema(Config): |
| 584 |
option = c.ListOfItems(c.Type(int)) |
| 585 |
|
| 586 |
conf = self.get_config(Schema, {'option': [1, 2, 3]}) |
| 587 |
assert_type(conf.option, List[int]) |
| 588 |
self.assertEqual(conf.option, [1, 2, 3]) |
| 589 |
|
| 590 |
with self.expect_error( |
| 591 |
option="Expected type: <class 'int'> but received: <class 'NoneType'>" |
| 592 |
): |
| 593 |
conf = self.get_config(Schema, {'option': [1, None, 3]}) |
| 594 |
|
| 595 |
def test_combined_float_type(self) -> None: |
| 596 |
class Schema(Config): |
| 597 |
option = c.ListOfItems(c.Type((int, float))) |
| 598 |
|
| 599 |
conf = self.get_config(Schema, {'option': [1.4, 2, 3]}) |
| 600 |
self.assertEqual(conf.option, [1.4, 2, 3]) |
| 601 |
|
| 602 |
with self.expect_error( |
| 603 |
option="Expected type: (<class 'int'>, <class 'float'>) but received: <class 'str'>" |
| 604 |
): |
| 605 |
self.get_config(Schema, {'option': ['a']}) |
| 606 |
|
| 607 |
def test_list_default(self) -> None: |
| 608 |
class Schema(Config): |
| 609 |
option = c.ListOfItems(c.Type(int), default=[]) |
| 610 |
|
| 611 |
conf = self.get_config(Schema, {}) |
| 612 |
assert_type(conf.option, List[int]) |
| 613 |
self.assertEqual(conf.option, []) |
| 614 |
|
| 615 |
with self.expect_error(option="Required configuration not provided."): |
| 616 |
conf = self.get_config(Schema, {'option': None}) |
| 617 |
|
| 618 |
def test_none_without_default(self) -> None: |
| 619 |
class Schema(Config): |
| 620 |
option = c.ListOfItems(c.Type(str)) |
| 621 |
|
| 622 |
with self.expect_error(option="Required configuration not provided."): |
| 623 |
conf = self.get_config(Schema, {}) |
| 624 |
|
| 625 |
with self.expect_error(option="Required configuration not provided."): |
| 626 |
conf = self.get_config(Schema, {'option': None}) |
| 627 |
|
| 628 |
conf = self.get_config(Schema, {'option': ['foo']}) |
| 629 |
self.assertEqual(conf.option, ['foo']) |
| 630 |
|
| 631 |
def test_optional(self) -> None: |
| 632 |
class Schema(Config): |
| 633 |
option = c.Optional(c.ListOfItems(c.Type(str))) |
| 634 |
|
| 635 |
conf = self.get_config(Schema, {}) |
| 636 |
assert_type(conf.option, Optional[List[str]]) |
| 637 |
self.assertEqual(conf.option, None) |
| 638 |
|
| 639 |
conf = self.get_config(Schema, {'option': None}) |
| 640 |
self.assertEqual(conf.option, None) |
| 641 |
|
| 642 |
conf = self.get_config(Schema, {'option': ['foo']}) |
| 643 |
self.assertEqual(conf.option, ['foo']) |
| 644 |
|
| 645 |
def test_list_of_optional(self) -> None: |
| 646 |
class Schema(Config): |
| 647 |
option = c.ListOfItems(c.Optional(c.Type(int)), default=[]) |
| 648 |
|
| 649 |
conf = self.get_config(Schema, {}) |
| 650 |
assert_type(conf.option, List[Optional[int]]) |
| 651 |
self.assertEqual(conf.option, []) |
| 652 |
|
| 653 |
conf = self.get_config(Schema, {'option': [4, None]}) |
| 654 |
self.assertEqual(conf.option, [4, None]) |
| 655 |
|
| 656 |
with self.expect_error(option="Expected type: <class 'int'> but received: <class 'str'>"): |
| 657 |
conf = self.get_config(Schema, {'option': ['foo']}) |
| 658 |
self.assertEqual(conf.option, ['foo']) |
| 659 |
|
| 660 |
def test_string_not_a_list_of_strings(self) -> None: |
| 661 |
class Schema(Config): |
| 662 |
option = c.ListOfItems(c.Type(str)) |
| 663 |
|
| 664 |
with self.expect_error(option="Expected a list of items, but a <class 'str'> was given."): |
| 665 |
self.get_config(Schema, {'option': 'foo'}) |
| 666 |
|
| 667 |
def test_post_validation_error(self) -> None: |
| 668 |
class Schema(Config): |
| 669 |
option = c.ListOfItems(c.IpAddress()) |
| 670 |
|
| 671 |
with self.expect_error(option="'asdf' is not a valid port"): |
| 672 |
self.get_config(Schema, {'option': ["localhost:8000", "1.2.3.4:asdf"]}) |
| 673 |
|
| 674 |
def test_warning(self) -> None: |
| 675 |
class Schema(Config): |
| 676 |
option = c.ListOfItems(c.Deprecated()) |
| 677 |
|
| 678 |
self.get_config( |
| 679 |
Schema, |
| 680 |
{'option': ['a']}, |
| 681 |
warnings=dict( |
| 682 |
option="The configuration option 'option[0]' has been " |
| 683 |
"deprecated and will be removed in a future release." |
| 684 |
), |
| 685 |
) |
| 686 |
|
| 687 |
|
| 688 |
class ExtraScriptsTest(TestCase): |
| 689 |
def test_js_async(self) -> None: |
| 690 |
class Schema(Config): |
| 691 |
option = c.ListOfItems(c.ExtraScript(), default=[]) |
| 692 |
|
| 693 |
conf = self.get_config(Schema, {'option': ['foo.js', {'path': 'bar.js', 'async': True}]}) |
| 694 |
assert_type(conf.option, List[Union[c.ExtraScriptValue, str]]) |
| 695 |
self.assertEqual(len(conf.option), 2) |
| 696 |
self.assertIsInstance(conf.option[1], c.ExtraScriptValue) |
| 697 |
self.assertEqual( |
| 698 |
conf.option, |
| 699 |
[ |
| 700 |
'foo.js', |
| 701 |
{'path': 'bar.js', 'type': '', 'defer': False, 'async': True}, |
| 702 |
], |
| 703 |
) |
| 704 |
|
| 705 |
def test_mjs(self) -> None: |
| 706 |
class Schema(Config): |
| 707 |
option = c.ListOfItems(c.ExtraScript(), default=[]) |
| 708 |
|
| 709 |
conf = self.get_config( |
| 710 |
Schema, {'option': ['foo.mjs', {'path': 'bar.js', 'type': 'module'}]} |
| 711 |
) |
| 712 |
assert_type(conf.option, List[Union[c.ExtraScriptValue, str]]) |
| 713 |
self.assertEqual(len(conf.option), 2) |
| 714 |
self.assertIsInstance(conf.option[0], c.ExtraScriptValue) |
| 715 |
self.assertEqual( |
| 716 |
conf.option, |
| 717 |
[ |
| 718 |
{'path': 'foo.mjs', 'type': 'module', 'defer': False, 'async': False}, |
| 719 |
{'path': 'bar.js', 'type': 'module', 'defer': False, 'async': False}, |
| 720 |
], |
| 721 |
) |
| 722 |
|
| 723 |
def test_wrong_type(self) -> None: |
| 724 |
class Schema(Config): |
| 725 |
option = c.ListOfItems(c.ExtraScript(), default=[]) |
| 726 |
|
| 727 |
with self.expect_error( |
| 728 |
option="The configuration is invalid. Expected a key-value mapping (dict) but received: <class 'int'>" |
| 729 |
): |
| 730 |
self.get_config(Schema, {'option': [1]}) |
| 731 |
|
| 732 |
def test_unknown_key(self) -> None: |
| 733 |
class Schema(Config): |
| 734 |
option = c.ListOfItems(c.ExtraScript(), default=[]) |
| 735 |
|
| 736 |
conf = self.get_config( |
| 737 |
Schema, |
| 738 |
{'option': [{'path': 'foo.js', 'foo': 'bar'}]}, |
| 739 |
warnings=dict(option="Sub-option 'foo': Unrecognised configuration name: foo"), |
| 740 |
) |
| 741 |
self.assertEqual( |
| 742 |
conf.option, |
| 743 |
[{'path': 'foo.js', 'type': '', 'defer': False, 'async': False, 'foo': 'bar'}], |
| 744 |
) |
| 745 |
|
| 746 |
|
| 747 |
class DictOfItemsTest(TestCase): |
| 748 |
def test_int_type(self) -> None: |
| 749 |
class Schema(Config): |
| 750 |
option = c.DictOfItems(c.Type(int)) |
| 751 |
|
| 752 |
conf = self.get_config(Schema, {'option': {"a": 1, "b": 2}}) |
| 753 |
assert_type(conf.option, Dict[str, int]) |
| 754 |
self.assertEqual(conf.option, {"a": 1, "b": 2}) |
| 755 |
|
| 756 |
with self.expect_error( |
| 757 |
option="Expected type: <class 'int'> but received: <class 'NoneType'>" |
| 758 |
): |
| 759 |
conf = self.get_config(Schema, {'option': {"a": 1, "b": None}}) |
| 760 |
|
| 761 |
def test_combined_float_type(self) -> None: |
| 762 |
class Schema(Config): |
| 763 |
option = c.DictOfItems(c.Type((int, float))) |
| 764 |
|
| 765 |
conf = self.get_config(Schema, {'option': {"a": 1, "b": 2.3}}) |
| 766 |
self.assertEqual(conf.option, {"a": 1, "b": 2.3}) |
| 767 |
|
| 768 |
with self.expect_error( |
| 769 |
option="Expected type: (<class 'int'>, <class 'float'>) but received: <class 'str'>" |
| 770 |
): |
| 771 |
self.get_config(Schema, {'option': {"a": 1, "b": "2"}}) |
| 772 |
|
| 773 |
def test_dict_default(self) -> None: |
| 774 |
class Schema(Config): |
| 775 |
option = c.DictOfItems(c.Type(int), default={}) |
| 776 |
|
| 777 |
conf = self.get_config(Schema, {}) |
| 778 |
assert_type(conf.option, Dict[str, int]) |
| 779 |
self.assertEqual(conf.option, {}) |
| 780 |
|
| 781 |
with self.expect_error(option="Required configuration not provided."): |
| 782 |
conf = self.get_config(Schema, {'option': None}) |
| 783 |
|
| 784 |
def test_none_without_default(self) -> None: |
| 785 |
class Schema(Config): |
| 786 |
option = c.DictOfItems(c.Type(str)) |
| 787 |
|
| 788 |
with self.expect_error(option="Required configuration not provided."): |
| 789 |
conf = self.get_config(Schema, {}) |
| 790 |
|
| 791 |
with self.expect_error(option="Required configuration not provided."): |
| 792 |
conf = self.get_config(Schema, {'option': None}) |
| 793 |
|
| 794 |
conf = self.get_config(Schema, {'option': {"foo": "bar"}}) |
| 795 |
self.assertEqual(conf.option, {"foo": "bar"}) |
| 796 |
|
| 797 |
def test_optional(self) -> None: |
| 798 |
class Schema(Config): |
| 799 |
option = c.Optional(c.DictOfItems(c.Type(str))) |
| 800 |
|
| 801 |
conf = self.get_config(Schema, {}) |
| 802 |
assert_type(conf.option, Optional[Dict[str, str]]) |
| 803 |
self.assertEqual(conf.option, None) |
| 804 |
|
| 805 |
conf = self.get_config(Schema, {'option': None}) |
| 806 |
self.assertEqual(conf.option, None) |
| 807 |
|
| 808 |
conf = self.get_config(Schema, {'option': {"foo": "bar"}}) |
| 809 |
self.assertEqual(conf.option, {"foo": "bar"}) |
| 810 |
|
| 811 |
def test_dict_of_optional(self) -> None: |
| 812 |
class Schema(Config): |
| 813 |
option = c.DictOfItems(c.Optional(c.Type(int)), default={}) |
| 814 |
|
| 815 |
conf = self.get_config(Schema, {}) |
| 816 |
assert_type(conf.option, Dict[str, Optional[int]]) |
| 817 |
self.assertEqual(conf.option, {}) |
| 818 |
|
| 819 |
conf = self.get_config(Schema, {'option': {"a": 1, "b": None}}) |
| 820 |
self.assertEqual(conf.option, {"a": 1, "b": None}) |
| 821 |
|
| 822 |
with self.expect_error(option="Expected type: <class 'int'> but received: <class 'str'>"): |
| 823 |
conf = self.get_config(Schema, {'option': {"foo": "bar"}}) |
| 824 |
self.assertEqual(conf.option, {"foo": "bar"}) |
| 825 |
|
| 826 |
def test_string_not_a_dict_of_strings(self) -> None: |
| 827 |
class Schema(Config): |
| 828 |
option = c.DictOfItems(c.Type(str)) |
| 829 |
|
| 830 |
with self.expect_error(option="Expected a dict of items, but a <class 'str'> was given."): |
| 831 |
self.get_config(Schema, {'option': 'foo'}) |
| 832 |
|
| 833 |
def test_post_validation_error(self) -> None: |
| 834 |
class Schema(Config): |
| 835 |
option = c.DictOfItems(c.IpAddress()) |
| 836 |
|
| 837 |
with self.expect_error(option="'asdf' is not a valid port"): |
| 838 |
self.get_config( |
| 839 |
Schema, {'option': {"ip_foo": "localhost:8000", "ip_bar": "1.2.3.4:asdf"}} |
| 840 |
) |
| 841 |
|
| 842 |
def test_all_keys_are_strings(self) -> None: |
| 843 |
class Schema(Config): |
| 844 |
option = c.DictOfItems(c.Type(int)) |
| 845 |
|
| 846 |
with self.expect_error( |
| 847 |
option="Expected type: <class 'str'> for keys, but received: <class 'int'> (key=2)" |
| 848 |
): |
| 849 |
self.get_config(Schema, {'option': {"a": 1, 2: 3}}) |
| 850 |
|
| 851 |
|
| 852 |
class FilesystemObjectTest(TestCase): |
| 853 |
def test_valid_dir(self) -> None: |
| 854 |
for cls in c.Dir, c.FilesystemObject: |
| 855 |
with self.subTest(cls): |
| 856 |
d = os.path.dirname(__file__) |
| 857 |
|
| 858 |
class Schema(Config): |
| 859 |
option = cls(exists=True) |
| 860 |
|
| 861 |
conf = self.get_config(Schema, {'option': d}) |
| 862 |
assert_type(conf.option, str) |
| 863 |
self.assertEqual(conf.option, d) |
| 864 |
|
| 865 |
def test_valid_file(self) -> None: |
| 866 |
for cls in c.File, c.FilesystemObject: |
| 867 |
with self.subTest(cls): |
| 868 |
f = __file__ |
| 869 |
|
| 870 |
class Schema(Config): |
| 871 |
option = cls(exists=True) |
| 872 |
|
| 873 |
conf = self.get_config(Schema, {'option': f}) |
| 874 |
assert_type(conf.option, str) |
| 875 |
self.assertEqual(conf.option, f) |
| 876 |
|
| 877 |
def test_missing_without_exists(self) -> None: |
| 878 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 879 |
with self.subTest(cls): |
| 880 |
d = os.path.join("not", "a", "real", "path", "I", "hope") |
| 881 |
|
| 882 |
class Schema(Config): |
| 883 |
option = cls() |
| 884 |
|
| 885 |
conf = self.get_config(Schema, {'option': d}) |
| 886 |
assert_type(conf.option, str) |
| 887 |
self.assertEqual(conf.option, os.path.abspath(d)) |
| 888 |
|
| 889 |
def test_missing_but_required(self) -> None: |
| 890 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 891 |
with self.subTest(cls): |
| 892 |
d = os.path.join("not", "a", "real", "path", "I", "hope") |
| 893 |
|
| 894 |
class Schema(Config): |
| 895 |
option = cls(exists=True) |
| 896 |
|
| 897 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing .+")): |
| 898 |
self.get_config(Schema, {'option': d}) |
| 899 |
|
| 900 |
def test_not_a_dir(self) -> None: |
| 901 |
d = __file__ |
| 902 |
|
| 903 |
class Schema(Config): |
| 904 |
option = c.Dir(exists=True) |
| 905 |
|
| 906 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing directory.")): |
| 907 |
self.get_config(Schema, {'option': d}) |
| 908 |
|
| 909 |
def test_not_a_file(self) -> None: |
| 910 |
d = os.path.dirname(__file__) |
| 911 |
|
| 912 |
class Schema(Config): |
| 913 |
option = c.File(exists=True) |
| 914 |
|
| 915 |
with self.expect_error(option=re.compile(r"The path '.+' isn't an existing file.")): |
| 916 |
self.get_config(Schema, {'option': d}) |
| 917 |
|
| 918 |
def test_incorrect_type_error(self) -> None: |
| 919 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 920 |
with self.subTest(cls): |
| 921 |
|
| 922 |
class Schema(Config): |
| 923 |
option = cls() |
| 924 |
|
| 925 |
with self.expect_error( |
| 926 |
option="Expected type: <class 'str'> but received: <class 'int'>" |
| 927 |
): |
| 928 |
self.get_config(Schema, {'option': 1}) |
| 929 |
with self.expect_error( |
| 930 |
option="Expected type: <class 'str'> but received: <class 'list'>" |
| 931 |
): |
| 932 |
self.get_config(Schema, {'option': []}) |
| 933 |
|
| 934 |
def test_with_unicode(self) -> None: |
| 935 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 936 |
with self.subTest(cls): |
| 937 |
|
| 938 |
class Schema(Config): |
| 939 |
dir = cls() |
| 940 |
|
| 941 |
conf = self.get_config(Schema, {'dir': 'юникод'}) |
| 942 |
self.assertIsInstance(conf.dir, str) |
| 943 |
|
| 944 |
def test_dir_bytes(self) -> None: |
| 945 |
class Schema(Config): |
| 946 |
dir = c.Dir() |
| 947 |
|
| 948 |
with self.expect_error(dir="Expected type: <class 'str'> but received: <class 'bytes'>"): |
| 949 |
self.get_config(Schema, {'dir': b'foo'}) |
| 950 |
|
| 951 |
def test_config_dir_prepended(self) -> None: |
| 952 |
for cls in c.Dir, c.File, c.FilesystemObject: |
| 953 |
with self.subTest(cls): |
| 954 |
base_path = os.path.dirname(os.path.abspath(__file__)) |
| 955 |
|
| 956 |
class Schema(Config): |
| 957 |
dir = cls() |
| 958 |
|
| 959 |
conf = self.get_config( |
| 960 |
Schema, |
| 961 |
{'dir': 'foo'}, |
| 962 |
config_file_path=os.path.join(base_path, 'mkdocs.yml'), |
| 963 |
) |
| 964 |
self.assertEqual(conf.dir, os.path.join(base_path, 'foo')) |
| 965 |
|
| 966 |
def test_site_dir_is_config_dir_fails(self) -> None: |
| 967 |
class Schema(Config): |
| 968 |
dir = c.DocsDir() |
| 969 |
|
| 970 |
with self.expect_error( |
| 971 |
dir="The 'dir' should not be the parent directory of the config file. " |
| 972 |
"Use a child directory instead so that the 'dir' is a sibling of the config file." |
| 973 |
): |
| 974 |
self.get_config( |
| 975 |
Schema, |
| 976 |
{'dir': '.'}, |
| 977 |
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'), |
| 978 |
) |
| 979 |
|
| 980 |
|
| 981 |
class ListOfPathsTest(TestCase): |
| 982 |
def test_valid_path(self) -> None: |
| 983 |
paths = [os.path.dirname(__file__)] |
| 984 |
|
| 985 |
class Schema(Config): |
| 986 |
option = c.ListOfPaths() |
| 987 |
|
| 988 |
self.get_config(Schema, {'option': paths}) |
| 989 |
|
| 990 |
def test_missing_path(self) -> None: |
| 991 |
paths = [os.path.join("does", "not", "exist", "i", "hope")] |
| 992 |
|
| 993 |
class Schema(Config): |
| 994 |
option = c.ListOfPaths() |
| 995 |
|
| 996 |
with self.expect_error( |
| 997 |
option=f"The path '{paths[0]}' isn't an existing file or directory." |
| 998 |
): |
| 999 |
self.get_config(Schema, {'option': paths}) |
| 1000 |
|
| 1001 |
def test_non_path(self) -> None: |
| 1002 |
paths = [os.path.dirname(__file__), None] |
| 1003 |
|
| 1004 |
class Schema(Config): |
| 1005 |
option = c.ListOfPaths() |
| 1006 |
|
| 1007 |
with self.expect_error( |
| 1008 |
option="Expected type: <class 'str'> but received: <class 'NoneType'>" |
| 1009 |
): |
| 1010 |
self.get_config(Schema, {'option': paths}) |
| 1011 |
|
| 1012 |
def test_empty_list(self) -> None: |
| 1013 |
class Schema(Config): |
| 1014 |
option = c.ListOfPaths() |
| 1015 |
|
| 1016 |
conf = self.get_config(Schema, {'option': []}) |
| 1017 |
assert_type(conf.option, List[str]) |
| 1018 |
self.assertEqual(conf.option, []) |
| 1019 |
|
| 1020 |
def test_none(self) -> None: |
| 1021 |
class Schema(Config): |
| 1022 |
option = c.ListOfPaths() |
| 1023 |
|
| 1024 |
with self.expect_error(option="Required configuration not provided."): |
| 1025 |
self.get_config(Schema, {'option': None}) |
| 1026 |
|
| 1027 |
def test_non_list(self) -> None: |
| 1028 |
paths = os.path.dirname(__file__) |
| 1029 |
|
| 1030 |
class Schema(Config): |
| 1031 |
option = c.ListOfPaths() |
| 1032 |
|
| 1033 |
with self.expect_error(option="Expected a list of items, but a <class 'str'> was given."): |
| 1034 |
self.get_config(Schema, {'option': paths}) |
| 1035 |
|
| 1036 |
def test_file(self) -> None: |
| 1037 |
paths = [__file__] |
| 1038 |
|
| 1039 |
class Schema(Config): |
| 1040 |
option = c.ListOfPaths() |
| 1041 |
|
| 1042 |
conf = self.get_config(Schema, {'option': paths}) |
| 1043 |
assert_type(conf.option, List[str]) |
| 1044 |
|
| 1045 |
@tempdir() |
| 1046 |
def test_paths_localized_to_config(self, base_path) -> None: |
| 1047 |
with open(os.path.join(base_path, 'foo'), 'w') as f: |
| 1048 |
f.write('hi') |
| 1049 |
|
| 1050 |
class Schema(Config): |
| 1051 |
watch = c.ListOfPaths() |
| 1052 |
|
| 1053 |
conf = self.get_config( |
| 1054 |
Schema, |
| 1055 |
{'watch': ['foo']}, |
| 1056 |
config_file_path=os.path.join(base_path, 'mkdocs.yml'), |
| 1057 |
) |
| 1058 |
|
| 1059 |
self.assertEqual(conf.watch, [os.path.join(base_path, 'foo')]) |
| 1060 |
|
| 1061 |
|
| 1062 |
class SiteDirTest(TestCase): |
| 1063 |
class Schema(Config): |
| 1064 |
site_dir = c.SiteDir() |
| 1065 |
docs_dir = c.Dir() |
| 1066 |
|
| 1067 |
def test_doc_dir_in_site_dir(self) -> None: |
| 1068 |
j = os.path.join |
| 1069 |
# The parent dir is not the same on every system, so use the actual dir name |
| 1070 |
parent_dir = mkdocs.__file__.split(os.sep)[-3] |
| 1071 |
|
| 1072 |
test_configs = ( |
| 1073 |
{'docs_dir': j('site', 'docs'), 'site_dir': 'site'}, |
| 1074 |
{'docs_dir': 'docs', 'site_dir': '.'}, |
| 1075 |
{'docs_dir': '.', 'site_dir': '.'}, |
| 1076 |
{'docs_dir': 'docs', 'site_dir': ''}, |
| 1077 |
{'docs_dir': '', 'site_dir': ''}, |
| 1078 |
{'docs_dir': j('..', parent_dir, 'docs'), 'site_dir': 'docs'}, |
| 1079 |
{'docs_dir': 'docs', 'site_dir': '/'}, |
| 1080 |
) |
| 1081 |
|
| 1082 |
for test_config in test_configs: |
| 1083 |
with self.subTest(test_config): |
| 1084 |
with self.expect_error( |
| 1085 |
site_dir=re.compile(r"The 'docs_dir' should not be within the 'site_dir'.*") |
| 1086 |
): |
| 1087 |
self.get_config(self.Schema, test_config) |
| 1088 |
|
| 1089 |
def test_site_dir_in_docs_dir(self) -> None: |
| 1090 |
j = os.path.join |
| 1091 |
|
| 1092 |
test_configs = ( |
| 1093 |
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')}, |
| 1094 |
{'docs_dir': '.', 'site_dir': 'site'}, |
| 1095 |
{'docs_dir': '', 'site_dir': 'site'}, |
| 1096 |
{'docs_dir': '/', 'site_dir': 'site'}, |
| 1097 |
) |
| 1098 |
|
| 1099 |
for test_config in test_configs: |
| 1100 |
with self.subTest(test_config): |
| 1101 |
with self.expect_error( |
| 1102 |
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*") |
| 1103 |
): |
| 1104 |
self.get_config(self.Schema, test_config) |
| 1105 |
|
| 1106 |
def test_common_prefix(self) -> None: |
| 1107 |
"""Legitimate settings with common prefixes should not fail validation.""" |
| 1108 |
test_configs = ( |
| 1109 |
{'docs_dir': 'docs', 'site_dir': 'docs-site'}, |
| 1110 |
{'docs_dir': 'site-docs', 'site_dir': 'site'}, |
| 1111 |
) |
| 1112 |
|
| 1113 |
for test_config in test_configs: |
| 1114 |
with self.subTest(test_config): |
| 1115 |
self.get_config(self.Schema, test_config) |
| 1116 |
|
| 1117 |
|
| 1118 |
class ThemeTest(TestCase): |
| 1119 |
def test_theme_as_string(self) -> None: |
| 1120 |
class Schema(Config): |
| 1121 |
option = c.Theme() |
| 1122 |
|
| 1123 |
conf = self.get_config(Schema, {'option': "mkdocs"}) |
| 1124 |
assert_type(conf.option, Theme) |
| 1125 |
assert_type(conf.option.name, Optional[str]) |
| 1126 |
self.assertEqual(conf.option.name, 'mkdocs') |
| 1127 |
|
| 1128 |
def test_uninstalled_theme_as_string(self) -> None: |
| 1129 |
class Schema(Config): |
| 1130 |
theme = c.Theme() |
| 1131 |
plugins = c.Plugins(theme_key='theme') |
| 1132 |
|
| 1133 |
with self.expect_error( |
| 1134 |
theme=re.compile( |
| 1135 |
r"Unrecognised theme name: 'mkdocs2'. The available installed themes are: .+" |
| 1136 |
) |
| 1137 |
): |
| 1138 |
self.get_config(Schema, {'theme': "mkdocs2", 'plugins': "search"}) |
| 1139 |
|
| 1140 |
def test_theme_default(self) -> None: |
| 1141 |
class Schema(Config): |
| 1142 |
option = c.Theme(default='mkdocs') |
| 1143 |
|
| 1144 |
conf = self.get_config(Schema, {'option': None}) |
| 1145 |
self.assertEqual(conf.option.name, 'mkdocs') |
| 1146 |
|
| 1147 |
def test_theme_as_simple_config(self) -> None: |
| 1148 |
config = { |
| 1149 |
'name': 'mkdocs', |
| 1150 |
} |
| 1151 |
|
| 1152 |
class Schema(Config): |
| 1153 |
option = c.Theme() |
| 1154 |
|
| 1155 |
conf = self.get_config(Schema, {'option': config}) |
| 1156 |
self.assertEqual(conf.option.name, 'mkdocs') |
| 1157 |
|
| 1158 |
@tempdir() |
| 1159 |
def test_theme_as_complex_config(self, custom_dir) -> None: |
| 1160 |
config = { |
| 1161 |
'name': 'mkdocs', |
| 1162 |
'custom_dir': custom_dir, |
| 1163 |
'static_templates': ['sitemap.html'], |
| 1164 |
'show_sidebar': False, |
| 1165 |
} |
| 1166 |
|
| 1167 |
class Schema(Config): |
| 1168 |
option = c.Theme() |
| 1169 |
|
| 1170 |
conf = self.get_config(Schema, {'option': config}) |
| 1171 |
self.assertEqual(conf.option.name, 'mkdocs') |
| 1172 |
self.assertEqual(conf.option.custom_dir, custom_dir) |
| 1173 |
self.assertIn(custom_dir, conf.option.dirs) |
| 1174 |
self.assertEqual( |
| 1175 |
conf.option.static_templates, |
| 1176 |
{'404.html', 'sitemap.xml', 'sitemap.html'}, |
| 1177 |
) |
| 1178 |
self.assertEqual(conf.option['show_sidebar'], False) |
| 1179 |
|
| 1180 |
def test_theme_name_is_none(self) -> None: |
| 1181 |
config = { |
| 1182 |
'name': None, |
| 1183 |
} |
| 1184 |
|
| 1185 |
class Schema(Config): |
| 1186 |
option = c.Theme() |
| 1187 |
|
| 1188 |
with self.expect_error(option="At least one of 'name' or 'custom_dir' must be defined."): |
| 1189 |
self.get_config(Schema, {'option': config}) |
| 1190 |
|
| 1191 |
def test_theme_config_missing_name(self) -> None: |
| 1192 |
config = { |
| 1193 |
'custom_dir': 'custom', |
| 1194 |
} |
| 1195 |
|
| 1196 |
class Schema(Config): |
| 1197 |
option = c.Theme() |
| 1198 |
|
| 1199 |
with self.expect_error(option="No theme name set."): |
| 1200 |
self.get_config(Schema, {'option': config}) |
| 1201 |
|
| 1202 |
def test_uninstalled_theme_as_config(self) -> None: |
| 1203 |
config = { |
| 1204 |
'name': 'mkdocs2', |
| 1205 |
} |
| 1206 |
|
| 1207 |
class Schema(Config): |
| 1208 |
option = c.Theme() |
| 1209 |
|
| 1210 |
with self.expect_error( |
| 1211 |
option=re.compile( |
| 1212 |
r"Unrecognised theme name: 'mkdocs2'. The available installed themes are: .+" |
| 1213 |
) |
| 1214 |
): |
| 1215 |
self.get_config(Schema, {'option': config}) |
| 1216 |
|
| 1217 |
def test_theme_invalid_type(self) -> None: |
| 1218 |
config = ['mkdocs2'] |
| 1219 |
|
| 1220 |
class Schema(Config): |
| 1221 |
option = c.Theme() |
| 1222 |
|
| 1223 |
with self.expect_error( |
| 1224 |
option="Invalid type <class 'list'>. Expected a string or key/value pairs." |
| 1225 |
): |
| 1226 |
self.get_config(Schema, {'option': config}) |
| 1227 |
|
| 1228 |
def test_post_validation_none_theme_name_and_missing_custom_dir(self) -> None: |
| 1229 |
config = { |
| 1230 |
'theme': { |
| 1231 |
'name': None, |
| 1232 |
}, |
| 1233 |
} |
| 1234 |
|
| 1235 |
class Schema(Config): |
| 1236 |
theme = c.Theme() |
| 1237 |
|
| 1238 |
with self.expect_error(theme="At least one of 'name' or 'custom_dir' must be defined."): |
| 1239 |
self.get_config(Schema, config) |
| 1240 |
|
| 1241 |
@tempdir() |
| 1242 |
def test_post_validation_inexisting_custom_dir(self, abs_base_path) -> None: |
| 1243 |
path = os.path.join(abs_base_path, 'inexisting_custom_dir') |
| 1244 |
config = { |
| 1245 |
'theme': { |
| 1246 |
'name': None, |
| 1247 |
'custom_dir': path, |
| 1248 |
}, |
| 1249 |
} |
| 1250 |
|
| 1251 |
class Schema(Config): |
| 1252 |
theme = c.Theme() |
| 1253 |
|
| 1254 |
with self.expect_error(theme=f"The path set in custom_dir ('{path}') does not exist."): |
| 1255 |
self.get_config(Schema, config) |
| 1256 |
|
| 1257 |
def test_post_validation_locale_none(self) -> None: |
| 1258 |
config = { |
| 1259 |
'theme': { |
| 1260 |
'name': 'mkdocs', |
| 1261 |
'locale': None, |
| 1262 |
}, |
| 1263 |
} |
| 1264 |
|
| 1265 |
class Schema(Config): |
| 1266 |
theme = c.Theme() |
| 1267 |
|
| 1268 |
with self.expect_error(theme="'locale' must be a string."): |
| 1269 |
self.get_config(Schema, config) |
| 1270 |
|
| 1271 |
def test_post_validation_locale_invalid_type(self) -> None: |
| 1272 |
config = { |
| 1273 |
'theme': { |
| 1274 |
'name': 'mkdocs', |
| 1275 |
'locale': 0, |
| 1276 |
}, |
| 1277 |
} |
| 1278 |
|
| 1279 |
class Schema(Config): |
| 1280 |
theme = c.Theme() |
| 1281 |
|
| 1282 |
with self.expect_error(theme="'locale' must be a string."): |
| 1283 |
self.get_config(Schema, config) |
| 1284 |
|
| 1285 |
def test_post_validation_locale(self) -> None: |
| 1286 |
config = { |
| 1287 |
'theme': { |
| 1288 |
'name': 'mkdocs', |
| 1289 |
'locale': 'fr', |
| 1290 |
}, |
| 1291 |
} |
| 1292 |
|
| 1293 |
class Schema(Config): |
| 1294 |
theme = c.Theme() |
| 1295 |
|
| 1296 |
conf = self.get_config(Schema, config) |
| 1297 |
self.assertEqual(conf.theme.locale.language, 'fr') |
| 1298 |
|
| 1299 |
|
| 1300 |
class NavTest(TestCase): |
| 1301 |
class Schema(Config): |
| 1302 |
option = c.Nav() |
| 1303 |
|
| 1304 |
def test_old_format(self) -> None: |
| 1305 |
with self.expect_error( |
| 1306 |
option="Expected nav item to be a string or dict, got a list: ['index.md']" |
| 1307 |
): |
| 1308 |
self.get_config(self.Schema, {'option': [['index.md']]}) |
| 1309 |
|
| 1310 |
def test_provided_dict(self) -> None: |
| 1311 |
conf = self.get_config(self.Schema, {'option': ['index.md', {"Page": "page.md"}]}) |
| 1312 |
self.assertEqual(conf.option, ['index.md', {'Page': 'page.md'}]) |
| 1313 |
|
| 1314 |
def test_provided_empty(self) -> None: |
| 1315 |
conf = self.get_config(self.Schema, {'option': []}) |
| 1316 |
self.assertEqual(conf.option, None) |
| 1317 |
|
| 1318 |
def test_normal_nav(self) -> None: |
| 1319 |
nav_yaml = textwrap.dedent( |
| 1320 |
'''\ |
| 1321 |
- Home: index.md |
| 1322 |
- getting-started.md |
| 1323 |
- User Guide: |
| 1324 |
- Overview: user-guide/index.md |
| 1325 |
- Installation: user-guide/installation.md |
| 1326 |
''' |
| 1327 |
) |
| 1328 |
nav = yaml_load(io.StringIO(nav_yaml)) |
| 1329 |
|
| 1330 |
conf = self.get_config(self.Schema, {'option': nav}) |
| 1331 |
self.assertEqual(conf.option, nav) |
| 1332 |
|
| 1333 |
def test_invalid_type_dict(self) -> None: |
| 1334 |
with self.expect_error(option="Expected nav to be a list, got a dict: {}"): |
| 1335 |
self.get_config(self.Schema, {'option': {}}) |
| 1336 |
|
| 1337 |
def test_invalid_type_int(self) -> None: |
| 1338 |
with self.expect_error(option="Expected nav to be a list, got a int: 5"): |
| 1339 |
self.get_config(self.Schema, {'option': 5}) |
| 1340 |
|
| 1341 |
def test_invalid_item_int(self) -> None: |
| 1342 |
with self.expect_error(option="Expected nav item to be a string or dict, got a int: 1"): |
| 1343 |
self.get_config(self.Schema, {'option': [1]}) |
| 1344 |
|
| 1345 |
def test_invalid_item_none(self) -> None: |
| 1346 |
with self.expect_error(option="Expected nav item to be a string or dict, got None"): |
| 1347 |
self.get_config(self.Schema, {'option': [None]}) |
| 1348 |
|
| 1349 |
def test_invalid_children_config_int(self) -> None: |
| 1350 |
with self.expect_error(option="Expected nav to be a list, got a int: 1"): |
| 1351 |
self.get_config(self.Schema, {'option': [{"foo.md": [{"bar.md": 1}]}]}) |
| 1352 |
|
| 1353 |
def test_invalid_children_config_none(self) -> None: |
| 1354 |
with self.expect_error(option="Expected nav to be a list, got None"): |
| 1355 |
self.get_config(self.Schema, {'option': [{"foo.md": None}]}) |
| 1356 |
|
| 1357 |
def test_invalid_children_empty_dict(self) -> None: |
| 1358 |
nav = ['foo', {}] |
| 1359 |
with self.expect_error(option="Expected nav item to be a dict of size 1, got a dict: {}"): |
| 1360 |
self.get_config(self.Schema, {'option': nav}) |
| 1361 |
|
| 1362 |
def test_invalid_nested_list(self) -> None: |
| 1363 |
nav = [{'aaa': [[{"bbb": "user-guide/index.md"}]]}] |
| 1364 |
with self.expect_error( |
| 1365 |
option="Expected nav item to be a string or dict, got a list: [{'bbb': 'user-guide/index.md'}]" |
| 1366 |
): |
| 1367 |
self.get_config(self.Schema, {'option': nav}) |
| 1368 |
|
| 1369 |
def test_invalid_children_oversized_dict(self) -> None: |
| 1370 |
nav = [{"aaa": [{"bbb": "user-guide/index.md", "ccc": "user-guide/installation.md"}]}] |
| 1371 |
with self.expect_error( |
| 1372 |
option="Expected nav item to be a dict of size 1, got dict with keys ('bbb', 'ccc')" |
| 1373 |
): |
| 1374 |
self.get_config(self.Schema, {'option': nav}) |
| 1375 |
|
| 1376 |
def test_warns_for_dict(self) -> None: |
| 1377 |
self.get_config( |
| 1378 |
self.Schema, |
| 1379 |
{'option': [{"a": {"b": "c.md", "d": "e.md"}}]}, |
| 1380 |
warnings=dict(option="Expected nav to be a list, got dict with keys ('b', 'd')"), |
| 1381 |
) |
| 1382 |
|
| 1383 |
|
| 1384 |
class PrivateTest(TestCase): |
| 1385 |
def test_defined(self) -> None: |
| 1386 |
class Schema(Config): |
| 1387 |
option = c.Private[Any]() |
| 1388 |
|
| 1389 |
with self.expect_error(option="For internal use only."): |
| 1390 |
self.get_config(Schema, {'option': 'somevalue'}) |
| 1391 |
|
| 1392 |
|
| 1393 |
class SubConfigTest(TestCase): |
| 1394 |
def test_subconfig_wrong_type(self) -> None: |
| 1395 |
# Test that an error is raised if subconfig does not receive a dict |
| 1396 |
class Schema(Config): |
| 1397 |
option = c.SubConfig() |
| 1398 |
|
| 1399 |
for val in "not_a_dict", ("not_a_dict",), ["not_a_dict"]: |
| 1400 |
with self.subTest(val): |
| 1401 |
with self.expect_error( |
| 1402 |
option=re.compile( |
| 1403 |
r"The configuration is invalid. Expected a key-value mapping " |
| 1404 |
r"\(dict\) but received: .+" |
| 1405 |
) |
| 1406 |
): |
| 1407 |
self.get_config(Schema, {'option': val}) |
| 1408 |
|
| 1409 |
def test_subconfig_unknown_option(self) -> None: |
| 1410 |
class Schema(Config): |
| 1411 |
option = c.SubConfig(validate=True) |
| 1412 |
|
| 1413 |
conf = self.get_config( |
| 1414 |
Schema, |
| 1415 |
{'option': {'unknown': 0}}, |
| 1416 |
warnings=dict(option="Sub-option 'unknown': Unrecognised configuration name: unknown"), |
| 1417 |
) |
| 1418 |
self.assertEqual(conf.option, {"unknown": 0}) |
| 1419 |
|
| 1420 |
def test_subconfig_invalid_option(self) -> None: |
| 1421 |
class Sub(Config): |
| 1422 |
cc = c.Choice(('foo', 'bar')) |
| 1423 |
|
| 1424 |
class Schema(Config): |
| 1425 |
option = c.SubConfig(Sub) |
| 1426 |
|
| 1427 |
with self.expect_error( |
| 1428 |
option="Sub-option 'cc': Expected one of: ('foo', 'bar') but received: True" |
| 1429 |
): |
| 1430 |
self.get_config(Schema, {'option': {'cc': True}}) |
| 1431 |
|
| 1432 |
def test_subconfig_normal(self) -> None: |
| 1433 |
class Sub(Config): |
| 1434 |
cc = c.Choice(('foo', 'bar')) |
| 1435 |
|
| 1436 |
class Schema(Config): |
| 1437 |
option = c.SubConfig(Sub) |
| 1438 |
|
| 1439 |
conf = self.get_config(Schema, {'option': {'cc': 'foo'}}) |
| 1440 |
assert_type(conf.option, Sub) |
| 1441 |
self.assertEqual(conf.option, {'cc': 'foo'}) |
| 1442 |
assert_type(conf.option.cc, str) |
| 1443 |
self.assertEqual(conf.option.cc, 'foo') |
| 1444 |
|
| 1445 |
def test_subconfig_with_multiple_items(self) -> None: |
| 1446 |
# This had a bug where subsequent items would get merged into the same dict. |
| 1447 |
class Sub(Config): |
| 1448 |
value = c.Type(str) |
| 1449 |
|
| 1450 |
class Schema(Config): |
| 1451 |
the_items = c.ListOfItems(c.SubConfig(Sub)) |
| 1452 |
|
| 1453 |
conf = self.get_config( |
| 1454 |
Schema, |
| 1455 |
{ |
| 1456 |
'the_items': [ |
| 1457 |
{'value': 'a'}, |
| 1458 |
{'value': 'b'}, |
| 1459 |
] |
| 1460 |
}, |
| 1461 |
) |
| 1462 |
assert_type(conf.the_items, List[Sub]) |
| 1463 |
self.assertEqual(conf.the_items, [{'value': 'a'}, {'value': 'b'}]) |
| 1464 |
assert_type(conf.the_items[1].value, str) |
| 1465 |
self.assertEqual(conf.the_items[1].value, 'b') |
| 1466 |
|
| 1467 |
def test_optional(self) -> None: |
| 1468 |
class Sub(Config): |
| 1469 |
opt = c.Optional(c.Type(int)) |
| 1470 |
|
| 1471 |
class Schema(Config): |
| 1472 |
sub = c.Optional(c.ListOfItems(c.SubConfig(Sub))) |
| 1473 |
|
| 1474 |
conf = self.get_config(Schema, {}) |
| 1475 |
self.assertEqual(conf.sub, None) |
| 1476 |
|
| 1477 |
conf = self.get_config(Schema, {'sub': None}) |
| 1478 |
self.assertEqual(conf.sub, None) |
| 1479 |
|
| 1480 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {}]}) |
| 1481 |
assert_type(conf.sub, Optional[List[Sub]]) |
| 1482 |
self.assertEqual(conf.sub, [{'opt': 1}, {'opt': None}]) |
| 1483 |
assert conf.sub is not None |
| 1484 |
assert_type(conf.sub[0].opt, Optional[int]) |
| 1485 |
self.assertEqual(conf.sub[0].opt, 1) |
| 1486 |
|
| 1487 |
conf = self.get_config(Schema, {'sub': []}) |
| 1488 |
|
| 1489 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {'opt': 2}]}) |
| 1490 |
self.assertEqual(conf.sub, [{'opt': 1}, {'opt': 2}]) |
| 1491 |
|
| 1492 |
def test_required(self) -> None: |
| 1493 |
class Sub(Config): |
| 1494 |
opt = c.Type(int) |
| 1495 |
|
| 1496 |
class Schema(Config): |
| 1497 |
sub = c.ListOfItems(c.SubConfig(Sub)) |
| 1498 |
|
| 1499 |
with self.expect_error(sub="Required configuration not provided."): |
| 1500 |
conf = self.get_config(Schema, {}) |
| 1501 |
|
| 1502 |
with self.expect_error(sub="Required configuration not provided."): |
| 1503 |
conf = self.get_config(Schema, {'sub': None}) |
| 1504 |
|
| 1505 |
with self.expect_error( |
| 1506 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1507 |
): |
| 1508 |
conf = self.get_config(Schema, {'sub': [{'opt': 'asdf'}, {}]}) |
| 1509 |
|
| 1510 |
conf = self.get_config(Schema, {'sub': []}) |
| 1511 |
|
| 1512 |
conf = self.get_config(Schema, {'sub': [{'opt': 1}, {'opt': 2}]}) |
| 1513 |
assert_type(conf.sub, List[Sub]) |
| 1514 |
self.assertEqual(conf.sub, [{'opt': 1}, {'opt': 2}]) |
| 1515 |
assert_type(conf.sub[0].opt, int) |
| 1516 |
self.assertEqual(conf.sub[0].opt, 1) |
| 1517 |
|
| 1518 |
with self.expect_error( |
| 1519 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1520 |
): |
| 1521 |
self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]}) |
| 1522 |
|
| 1523 |
with self.expect_error( |
| 1524 |
sub="Sub-option 'opt': Expected type: <class 'int'> but received: <class 'str'>" |
| 1525 |
): |
| 1526 |
conf = self.get_config(Schema, {'sub': [{'opt': 'z'}, {'opt': 2}]}) |
| 1527 |
|
| 1528 |
with self.expect_error( |
| 1529 |
sub="The configuration is invalid. Expected a key-value mapping " |
| 1530 |
"(dict) but received: <class 'int'>" |
| 1531 |
): |
| 1532 |
conf = self.get_config(Schema, {'sub': [1, 2]}) |
| 1533 |
|
| 1534 |
def test_default(self) -> None: |
| 1535 |
class Sub(Config): |
| 1536 |
opt = c.Type(int) |
| 1537 |
|
| 1538 |
class Schema(Config): |
| 1539 |
sub = c.ListOfItems(c.SubConfig(Sub), default=[]) |
| 1540 |
|
| 1541 |
conf = self.get_config(Schema, {}) |
| 1542 |
assert_type(conf.sub, List[Sub]) |
| 1543 |
self.assertEqual(conf.sub, []) |
| 1544 |
|
| 1545 |
with self.expect_error(sub="Required configuration not provided."): |
| 1546 |
conf = self.get_config(Schema, {'sub': None}) |
| 1547 |
|
| 1548 |
def test_config_file_path_pass_through(self) -> None: |
| 1549 |
"""Necessary to ensure FilesystemObject validates the correct path.""" |
| 1550 |
passed_config_path = None |
| 1551 |
|
| 1552 |
class SubType(c.BaseConfigOption): |
| 1553 |
def pre_validation(self, config: Config, key_name: str) -> None: |
| 1554 |
nonlocal passed_config_path |
| 1555 |
passed_config_path = config.config_file_path |
| 1556 |
|
| 1557 |
class Sub(Config): |
| 1558 |
opt = SubType() |
| 1559 |
|
| 1560 |
class Schema(Config): |
| 1561 |
sub = c.ListOfItems(c.SubConfig(Sub), default=[]) |
| 1562 |
|
| 1563 |
config_path = "foo/mkdocs.yaml" |
| 1564 |
self.get_config(Schema, {"sub": [{"opt": "bar"}]}, config_file_path=config_path) |
| 1565 |
self.assertEqual(passed_config_path, config_path) |
| 1566 |
|
| 1567 |
|
| 1568 |
class NestedSubConfigTest(TestCase): |
| 1569 |
def defaults(self): |
| 1570 |
return { |
| 1571 |
'nav': { |
| 1572 |
'omitted_files': logging.INFO, |
| 1573 |
'not_found': logging.WARNING, |
| 1574 |
'absolute_links': logging.INFO, |
| 1575 |
}, |
| 1576 |
'links': { |
| 1577 |
'not_found': logging.WARNING, |
| 1578 |
'absolute_links': logging.INFO, |
| 1579 |
'unrecognized_links': logging.INFO, |
| 1580 |
'anchors': logging.INFO, |
| 1581 |
}, |
| 1582 |
} |
| 1583 |
|
| 1584 |
class Schema(Config): |
| 1585 |
validation = c.PropagatingSubConfig[defaults.MkDocsConfig.Validation]() |
| 1586 |
|
| 1587 |
def test_unspecified(self) -> None: |
| 1588 |
cfgs: list[dict] = [{}, {'validation': {}}] |
| 1589 |
for cfg in cfgs: |
| 1590 |
with self.subTest(cfg): |
| 1591 |
conf = self.get_config( |
| 1592 |
self.Schema, |
| 1593 |
{}, |
| 1594 |
) |
| 1595 |
self.assertEqual(conf.validation, self.defaults()) |
| 1596 |
|
| 1597 |
def test_sets_nested_and_not_nested(self) -> None: |
| 1598 |
conf = self.get_config( |
| 1599 |
self.Schema, |
| 1600 |
{'validation': {'not_found': 'ignore', 'links': {'absolute_links': 'warn'}}}, |
| 1601 |
) |
| 1602 |
expected = self.defaults() |
| 1603 |
expected['nav']['not_found'] = logging.DEBUG |
| 1604 |
expected['links']['not_found'] = logging.DEBUG |
| 1605 |
expected['links']['absolute_links'] = logging.WARNING |
| 1606 |
self.assertEqual(conf.validation, expected) |
| 1607 |
|
| 1608 |
def test_sets_nested_different(self) -> None: |
| 1609 |
conf = self.get_config( |
| 1610 |
self.Schema, |
| 1611 |
{'validation': {'not_found': 'ignore', 'links': {'not_found': 'warn'}}}, |
| 1612 |
) |
| 1613 |
expected = self.defaults() |
| 1614 |
expected['nav']['not_found'] = logging.DEBUG |
| 1615 |
expected['links']['not_found'] = logging.WARNING |
| 1616 |
self.assertEqual(conf.validation, expected) |
| 1617 |
|
| 1618 |
def test_sets_only_one_nested(self) -> None: |
| 1619 |
conf = self.get_config( |
| 1620 |
self.Schema, |
| 1621 |
{'validation': {'omitted_files': 'ignore'}}, |
| 1622 |
) |
| 1623 |
expected = self.defaults() |
| 1624 |
expected['nav']['omitted_files'] = logging.DEBUG |
| 1625 |
self.assertEqual(conf.validation, expected) |
| 1626 |
|
| 1627 |
def test_sets_nested_not_dict(self) -> None: |
| 1628 |
with self.expect_error( |
| 1629 |
validation="Sub-option 'links': Sub-option 'unrecognized_links': Expected a string, but a <class 'list'> was given." |
| 1630 |
): |
| 1631 |
self.get_config( |
| 1632 |
self.Schema, |
| 1633 |
{'validation': {'unrecognized_links': [], 'links': {'absolute_links': 'warn'}}}, |
| 1634 |
) |
| 1635 |
|
| 1636 |
def test_wrong_key_nested(self) -> None: |
| 1637 |
conf = self.get_config( |
| 1638 |
self.Schema, |
| 1639 |
{'validation': {'foo': 'warn', 'not_found': 'warn'}}, |
| 1640 |
warnings=dict(validation="Sub-option 'foo': Unrecognised configuration name: foo"), |
| 1641 |
) |
| 1642 |
expected = self.defaults() |
| 1643 |
expected['nav']['not_found'] = logging.WARNING |
| 1644 |
expected['links']['not_found'] = logging.WARNING |
| 1645 |
expected['foo'] = 'warn' |
| 1646 |
self.assertEqual(conf.validation, expected) |
| 1647 |
|
| 1648 |
def test_wrong_type_nested(self) -> None: |
| 1649 |
with self.expect_error( |
| 1650 |
validation="Sub-option 'nav': Sub-option 'omitted_files': Expected one of ['warn', 'info', 'ignore'], got 'hi'" |
| 1651 |
): |
| 1652 |
self.get_config( |
| 1653 |
self.Schema, |
| 1654 |
{'validation': {'omitted_files': 'hi'}}, |
| 1655 |
) |
| 1656 |
|
| 1657 |
|
| 1658 |
class MarkdownExtensionsTest(TestCase): |
| 1659 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1660 |
def test_simple_list(self) -> None: |
| 1661 |
class Schema(Config): |
| 1662 |
markdown_extensions = c.MarkdownExtensions() |
| 1663 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1664 |
|
| 1665 |
config = { |
| 1666 |
'markdown_extensions': ['foo', 'bar'], |
| 1667 |
} |
| 1668 |
conf = self.get_config(Schema, config) |
| 1669 |
assert_type(conf.markdown_extensions, List[str]) |
| 1670 |
assert_type(conf.mdx_configs, Dict[str, dict]) |
| 1671 |
self.assertEqual(conf.markdown_extensions, ['foo', 'bar']) |
| 1672 |
self.assertEqual(conf.mdx_configs, {}) |
| 1673 |
|
| 1674 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1675 |
def test_list_dicts(self) -> None: |
| 1676 |
class Schema(Config): |
| 1677 |
markdown_extensions = c.MarkdownExtensions() |
| 1678 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1679 |
|
| 1680 |
config = { |
| 1681 |
'markdown_extensions': [ |
| 1682 |
{'foo': {'foo_option': 'foo value'}}, |
| 1683 |
{'bar': {'bar_option': 'bar value'}}, |
| 1684 |
{'baz': None}, |
| 1685 |
] |
| 1686 |
} |
| 1687 |
conf = self.get_config(Schema, config) |
| 1688 |
self.assertEqual(conf.markdown_extensions, ['foo', 'bar', 'baz']) |
| 1689 |
self.assertEqual( |
| 1690 |
conf.mdx_configs, |
| 1691 |
{ |
| 1692 |
'foo': {'foo_option': 'foo value'}, |
| 1693 |
'bar': {'bar_option': 'bar value'}, |
| 1694 |
}, |
| 1695 |
) |
| 1696 |
|
| 1697 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1698 |
def test_mixed_list(self) -> None: |
| 1699 |
class Schema(Config): |
| 1700 |
markdown_extensions = c.MarkdownExtensions() |
| 1701 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1702 |
|
| 1703 |
config = { |
| 1704 |
'markdown_extensions': [ |
| 1705 |
'foo', |
| 1706 |
{'bar': {'bar_option': 'bar value'}}, |
| 1707 |
] |
| 1708 |
} |
| 1709 |
conf = self.get_config(Schema, config) |
| 1710 |
self.assertEqual(conf.markdown_extensions, ['foo', 'bar']) |
| 1711 |
self.assertEqual( |
| 1712 |
conf.mdx_configs, |
| 1713 |
{ |
| 1714 |
'bar': {'bar_option': 'bar value'}, |
| 1715 |
}, |
| 1716 |
) |
| 1717 |
|
| 1718 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1719 |
def test_dict_of_dicts(self) -> None: |
| 1720 |
class Schema(Config): |
| 1721 |
markdown_extensions = c.MarkdownExtensions() |
| 1722 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1723 |
|
| 1724 |
config = { |
| 1725 |
'markdown_extensions': { |
| 1726 |
'foo': {'foo_option': 'foo value'}, |
| 1727 |
'bar': {'bar_option': 'bar value'}, |
| 1728 |
'baz': {}, |
| 1729 |
} |
| 1730 |
} |
| 1731 |
conf = self.get_config(Schema, config) |
| 1732 |
self.assertEqual(conf.markdown_extensions, ['foo', 'bar', 'baz']) |
| 1733 |
self.assertEqual( |
| 1734 |
conf.mdx_configs, |
| 1735 |
{ |
| 1736 |
'foo': {'foo_option': 'foo value'}, |
| 1737 |
'bar': {'bar_option': 'bar value'}, |
| 1738 |
}, |
| 1739 |
) |
| 1740 |
|
| 1741 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1742 |
def test_builtins(self) -> None: |
| 1743 |
class Schema(Config): |
| 1744 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1745 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1746 |
|
| 1747 |
config = { |
| 1748 |
'markdown_extensions': ['foo', 'bar'], |
| 1749 |
} |
| 1750 |
conf = self.get_config(Schema, config) |
| 1751 |
self.assertEqual(conf.markdown_extensions, ['meta', 'toc', 'foo', 'bar']) |
| 1752 |
self.assertEqual(conf.mdx_configs, {}) |
| 1753 |
|
| 1754 |
def test_duplicates(self) -> None: |
| 1755 |
class Schema(Config): |
| 1756 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1757 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1758 |
|
| 1759 |
config = { |
| 1760 |
'markdown_extensions': ['meta', 'toc'], |
| 1761 |
} |
| 1762 |
conf = self.get_config(Schema, config) |
| 1763 |
self.assertEqual(conf.markdown_extensions, ['meta', 'toc']) |
| 1764 |
self.assertEqual(conf.mdx_configs, {}) |
| 1765 |
|
| 1766 |
def test_builtins_config(self) -> None: |
| 1767 |
class Schema(Config): |
| 1768 |
markdown_extensions = c.MarkdownExtensions(builtins=['meta', 'toc']) |
| 1769 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1770 |
|
| 1771 |
config = { |
| 1772 |
'markdown_extensions': [ |
| 1773 |
{'toc': {'permalink': True}}, |
| 1774 |
], |
| 1775 |
} |
| 1776 |
conf = self.get_config(Schema, config) |
| 1777 |
self.assertEqual(conf.markdown_extensions, ['meta', 'toc']) |
| 1778 |
self.assertEqual(conf.mdx_configs, {'toc': {'permalink': True}}) |
| 1779 |
|
| 1780 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1781 |
def test_configkey(self) -> None: |
| 1782 |
class Schema(Config): |
| 1783 |
markdown_extensions = c.MarkdownExtensions(configkey='bar') |
| 1784 |
bar = c.Private[Dict[str, dict]]() |
| 1785 |
|
| 1786 |
config = { |
| 1787 |
'markdown_extensions': [ |
| 1788 |
{'foo': {'foo_option': 'foo value'}}, |
| 1789 |
] |
| 1790 |
} |
| 1791 |
conf = self.get_config(Schema, config) |
| 1792 |
self.assertEqual(conf.markdown_extensions, ['foo']) |
| 1793 |
self.assertEqual( |
| 1794 |
conf.bar, |
| 1795 |
{ |
| 1796 |
'foo': {'foo_option': 'foo value'}, |
| 1797 |
}, |
| 1798 |
) |
| 1799 |
|
| 1800 |
def test_missing_default(self) -> None: |
| 1801 |
class Schema(Config): |
| 1802 |
markdown_extensions = c.MarkdownExtensions() |
| 1803 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1804 |
|
| 1805 |
conf = self.get_config(Schema, {}) |
| 1806 |
self.assertEqual(conf.markdown_extensions, []) |
| 1807 |
self.assertEqual(conf.mdx_configs, {}) |
| 1808 |
|
| 1809 |
def test_none(self) -> None: |
| 1810 |
class Schema(Config): |
| 1811 |
markdown_extensions = c.MarkdownExtensions(default=[]) |
| 1812 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1813 |
|
| 1814 |
config = { |
| 1815 |
'markdown_extensions': None, |
| 1816 |
} |
| 1817 |
conf = self.get_config(Schema, config) |
| 1818 |
self.assertEqual(conf.markdown_extensions, []) |
| 1819 |
self.assertEqual(conf.mdx_configs, {}) |
| 1820 |
|
| 1821 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1822 |
def test_not_list(self) -> None: |
| 1823 |
class Schema(Config): |
| 1824 |
option = c.MarkdownExtensions() |
| 1825 |
|
| 1826 |
with self.expect_error(option="Invalid Markdown Extensions configuration"): |
| 1827 |
self.get_config(Schema, {'option': 'not a list'}) |
| 1828 |
|
| 1829 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1830 |
def test_invalid_config_option(self) -> None: |
| 1831 |
class Schema(Config): |
| 1832 |
markdown_extensions = c.MarkdownExtensions() |
| 1833 |
|
| 1834 |
config = { |
| 1835 |
'markdown_extensions': [ |
| 1836 |
{'foo': 'not a dict'}, |
| 1837 |
], |
| 1838 |
} |
| 1839 |
with self.expect_error( |
| 1840 |
markdown_extensions="Invalid config options for Markdown Extension 'foo'." |
| 1841 |
): |
| 1842 |
self.get_config(Schema, config) |
| 1843 |
|
| 1844 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1845 |
def test_invalid_config_item(self) -> None: |
| 1846 |
class Schema(Config): |
| 1847 |
markdown_extensions = c.MarkdownExtensions() |
| 1848 |
|
| 1849 |
config = { |
| 1850 |
'markdown_extensions': [ |
| 1851 |
['not a dict'], |
| 1852 |
], |
| 1853 |
} |
| 1854 |
with self.expect_error(markdown_extensions="Invalid Markdown Extensions configuration"): |
| 1855 |
self.get_config(Schema, config) |
| 1856 |
|
| 1857 |
@mock.patch('markdown.Markdown', mock.Mock()) |
| 1858 |
def test_invalid_dict_item(self) -> None: |
| 1859 |
class Schema(Config): |
| 1860 |
markdown_extensions = c.MarkdownExtensions() |
| 1861 |
|
| 1862 |
config = { |
| 1863 |
'markdown_extensions': [ |
| 1864 |
{'key1': 'value', 'key2': 'too many keys'}, |
| 1865 |
], |
| 1866 |
} |
| 1867 |
with self.expect_error(markdown_extensions="Invalid Markdown Extensions configuration"): |
| 1868 |
self.get_config(Schema, config) |
| 1869 |
|
| 1870 |
def test_unknown_extension(self) -> None: |
| 1871 |
class Schema(Config): |
| 1872 |
markdown_extensions = c.MarkdownExtensions() |
| 1873 |
|
| 1874 |
config = { |
| 1875 |
'markdown_extensions': ['unknown'], |
| 1876 |
} |
| 1877 |
with self.expect_error( |
| 1878 |
markdown_extensions=re.compile(r"Failed to load extension 'unknown'.\n.+") |
| 1879 |
): |
| 1880 |
self.get_config(Schema, config) |
| 1881 |
|
| 1882 |
def test_multiple_markdown_config_instances(self) -> None: |
| 1883 |
# This had a bug where an extension config would persist to separate |
| 1884 |
# config instances that didn't specify extensions. |
| 1885 |
class Schema(Config): |
| 1886 |
markdown_extensions = c.MarkdownExtensions() |
| 1887 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 1888 |
|
| 1889 |
conf = self.get_config( |
| 1890 |
Schema, |
| 1891 |
{ |
| 1892 |
'markdown_extensions': [{'toc': {'permalink': '##'}}], |
| 1893 |
}, |
| 1894 |
) |
| 1895 |
self.assertEqual(conf.mdx_configs['toc'], {'permalink': '##'}) |
| 1896 |
|
| 1897 |
conf = self.get_config( |
| 1898 |
Schema, |
| 1899 |
{}, |
| 1900 |
) |
| 1901 |
self.assertIsNone(conf.mdx_configs.get('toc')) |
| 1902 |
|
| 1903 |
|
| 1904 |
class _FakePluginConfig(Config): |
| 1905 |
foo = c.Type(str, default='default foo') |
| 1906 |
bar = c.Type(int, default=0) |
| 1907 |
dir = c.Optional(c.Dir(exists=False)) |
| 1908 |
|
| 1909 |
|
| 1910 |
class FakePlugin(BasePlugin[_FakePluginConfig]): |
| 1911 |
pass |
| 1912 |
|
| 1913 |
|
| 1914 |
class _FakePlugin2Config(_FakePluginConfig): |
| 1915 |
depr = c.Deprecated() |
| 1916 |
|
| 1917 |
|
| 1918 |
class FakePlugin2(BasePlugin[_FakePlugin2Config]): |
| 1919 |
supports_multiple_instances = True |
| 1920 |
|
| 1921 |
|
| 1922 |
class _EnabledPluginConfig(Config): |
| 1923 |
enabled = c.Type(bool, default=True) |
| 1924 |
bar = c.Type(int, default=0) |
| 1925 |
|
| 1926 |
|
| 1927 |
class EnabledPlugin(BasePlugin[_EnabledPluginConfig]): |
| 1928 |
pass |
| 1929 |
|
| 1930 |
|
| 1931 |
class ThemePlugin(BasePlugin[_FakePluginConfig]): |
| 1932 |
pass |
| 1933 |
|
| 1934 |
|
| 1935 |
class ThemePlugin2(BasePlugin[_FakePluginConfig]): |
| 1936 |
pass |
| 1937 |
|
| 1938 |
|
| 1939 |
class FakeEntryPoint: |
| 1940 |
def __init__(self, name, cls): |
| 1941 |
self.name = name |
| 1942 |
self.cls = cls |
| 1943 |
|
| 1944 |
def load(self): |
| 1945 |
return self.cls |
| 1946 |
|
| 1947 |
|
| 1948 |
@mock.patch( |
| 1949 |
'mkdocs.plugins.entry_points', |
| 1950 |
mock.Mock( |
| 1951 |
return_value=[ |
| 1952 |
FakeEntryPoint('sample', FakePlugin), |
| 1953 |
FakeEntryPoint('sample2', FakePlugin2), |
| 1954 |
FakeEntryPoint('sample-e', EnabledPlugin), |
| 1955 |
FakeEntryPoint('readthedocs/sub_plugin', ThemePlugin), |
| 1956 |
FakeEntryPoint('overridden', FakePlugin2), |
| 1957 |
FakeEntryPoint('readthedocs/overridden', ThemePlugin2), |
| 1958 |
] |
| 1959 |
), |
| 1960 |
) |
| 1961 |
class PluginsTest(TestCase): |
| 1962 |
def test_plugin_config_without_options(self) -> None: |
| 1963 |
class Schema(Config): |
| 1964 |
plugins = c.Plugins() |
| 1965 |
|
| 1966 |
cfg = { |
| 1967 |
'plugins': ['sample'], |
| 1968 |
} |
| 1969 |
conf = self.get_config(Schema, cfg) |
| 1970 |
|
| 1971 |
assert_type(conf.plugins, PluginCollection) |
| 1972 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 1973 |
self.assertIn('sample', conf.plugins) |
| 1974 |
|
| 1975 |
plugin = conf.plugins['sample'] |
| 1976 |
assert_type(plugin, BasePlugin) |
| 1977 |
self.assertIsInstance(plugin, FakePlugin) |
| 1978 |
self.assertIsInstance(plugin.config, _FakePluginConfig) |
| 1979 |
expected = { |
| 1980 |
'foo': 'default foo', |
| 1981 |
'bar': 0, |
| 1982 |
'dir': None, |
| 1983 |
} |
| 1984 |
self.assertEqual(plugin.config, expected) |
| 1985 |
|
| 1986 |
def test_plugin_config_with_options(self) -> None: |
| 1987 |
class Schema(Config): |
| 1988 |
plugins = c.Plugins() |
| 1989 |
|
| 1990 |
cfg = { |
| 1991 |
'plugins': [ |
| 1992 |
{ |
| 1993 |
'sample': { |
| 1994 |
'foo': 'foo value', |
| 1995 |
'bar': 42, |
| 1996 |
}, |
| 1997 |
} |
| 1998 |
], |
| 1999 |
} |
| 2000 |
conf = self.get_config(Schema, cfg) |
| 2001 |
|
| 2002 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2003 |
self.assertIn('sample', conf.plugins) |
| 2004 |
self.assertIsInstance(conf.plugins['sample'], BasePlugin) |
| 2005 |
expected = { |
| 2006 |
'foo': 'foo value', |
| 2007 |
'bar': 42, |
| 2008 |
'dir': None, |
| 2009 |
} |
| 2010 |
self.assertEqual(conf.plugins['sample'].config, expected) |
| 2011 |
|
| 2012 |
def test_plugin_config_as_dict(self) -> None: |
| 2013 |
class Schema(Config): |
| 2014 |
plugins = c.Plugins() |
| 2015 |
|
| 2016 |
cfg = { |
| 2017 |
'plugins': { |
| 2018 |
'sample': { |
| 2019 |
'foo': 'foo value', |
| 2020 |
'bar': 42, |
| 2021 |
}, |
| 2022 |
}, |
| 2023 |
} |
| 2024 |
conf = self.get_config(Schema, cfg) |
| 2025 |
|
| 2026 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2027 |
self.assertIn('sample', conf.plugins) |
| 2028 |
self.assertIsInstance(conf.plugins['sample'], BasePlugin) |
| 2029 |
expected = { |
| 2030 |
'foo': 'foo value', |
| 2031 |
'bar': 42, |
| 2032 |
'dir': None, |
| 2033 |
} |
| 2034 |
self.assertEqual(conf.plugins['sample'].config, expected) |
| 2035 |
|
| 2036 |
def test_plugin_config_with_explicit_theme_namespace(self) -> None: |
| 2037 |
class Schema(Config): |
| 2038 |
theme = c.Theme(default='mkdocs') |
| 2039 |
plugins = c.Plugins(theme_key='theme') |
| 2040 |
|
| 2041 |
cfg = {'theme': 'readthedocs', 'plugins': ['readthedocs/sub_plugin']} |
| 2042 |
conf = self.get_config(Schema, cfg) |
| 2043 |
|
| 2044 |
self.assertEqual(set(conf.plugins), {'readthedocs/sub_plugin'}) |
| 2045 |
self.assertIsInstance(conf.plugins['readthedocs/sub_plugin'], ThemePlugin) |
| 2046 |
|
| 2047 |
cfg = {'plugins': ['readthedocs/sub_plugin']} |
| 2048 |
conf = self.get_config(Schema, cfg) |
| 2049 |
|
| 2050 |
self.assertEqual(set(conf.plugins), {'readthedocs/sub_plugin'}) |
| 2051 |
self.assertIsInstance(conf.plugins['readthedocs/sub_plugin'], ThemePlugin) |
| 2052 |
|
| 2053 |
def test_plugin_config_with_deduced_theme_namespace(self) -> None: |
| 2054 |
class Schema(Config): |
| 2055 |
theme = c.Theme(default='mkdocs') |
| 2056 |
plugins = c.Plugins(theme_key='theme') |
| 2057 |
|
| 2058 |
cfg = {'theme': 'readthedocs', 'plugins': ['sub_plugin']} |
| 2059 |
conf = self.get_config(Schema, cfg) |
| 2060 |
|
| 2061 |
self.assertEqual(set(conf.plugins), {'readthedocs/sub_plugin'}) |
| 2062 |
self.assertIsInstance(conf.plugins['readthedocs/sub_plugin'], ThemePlugin) |
| 2063 |
|
| 2064 |
cfg = {'plugins': ['sub_plugin']} |
| 2065 |
with self.expect_error(plugins='The "sub_plugin" plugin is not installed'): |
| 2066 |
self.get_config(Schema, cfg) |
| 2067 |
|
| 2068 |
def test_plugin_config_with_deduced_theme_namespace_overridden(self) -> None: |
| 2069 |
class Schema(Config): |
| 2070 |
theme = c.Theme(default='mkdocs') |
| 2071 |
plugins = c.Plugins(theme_key='theme') |
| 2072 |
|
| 2073 |
cfg = {'theme': 'readthedocs', 'plugins': ['overridden']} |
| 2074 |
conf = self.get_config(Schema, cfg) |
| 2075 |
|
| 2076 |
self.assertEqual(set(conf.plugins), {'readthedocs/overridden'}) |
| 2077 |
self.assertIsInstance(next(iter(conf.plugins.values())), ThemePlugin2) |
| 2078 |
|
| 2079 |
cfg = {'plugins': ['overridden']} |
| 2080 |
conf = self.get_config(Schema, cfg) |
| 2081 |
|
| 2082 |
self.assertEqual(set(conf.plugins), {'overridden'}) |
| 2083 |
self.assertIsInstance(conf.plugins['overridden'], FakePlugin2) |
| 2084 |
|
| 2085 |
def test_plugin_config_with_explicit_empty_namespace(self) -> None: |
| 2086 |
class Schema(Config): |
| 2087 |
theme = c.Theme(default='mkdocs') |
| 2088 |
plugins = c.Plugins(theme_key='theme') |
| 2089 |
|
| 2090 |
cfg = {'theme': 'readthedocs', 'plugins': ['/overridden']} |
| 2091 |
conf = self.get_config(Schema, cfg) |
| 2092 |
|
| 2093 |
self.assertEqual(set(conf.plugins), {'overridden'}) |
| 2094 |
self.assertIsInstance(next(iter(conf.plugins.values())), FakePlugin2) |
| 2095 |
|
| 2096 |
cfg = {'plugins': ['/overridden']} |
| 2097 |
conf = self.get_config(Schema, cfg) |
| 2098 |
|
| 2099 |
self.assertEqual(set(conf.plugins), {'overridden'}) |
| 2100 |
self.assertIsInstance(conf.plugins['overridden'], FakePlugin2) |
| 2101 |
|
| 2102 |
def test_plugin_config_enabled_for_any_plugin(self) -> None: |
| 2103 |
class Schema(Config): |
| 2104 |
theme = c.Theme(default='mkdocs') |
| 2105 |
plugins = c.Plugins(theme_key='theme') |
| 2106 |
|
| 2107 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample': {'enabled': False, 'bar': 3}}} |
| 2108 |
conf = self.get_config(Schema, cfg) |
| 2109 |
self.assertEqual(set(conf.plugins), set()) |
| 2110 |
|
| 2111 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample': {'enabled': True, 'bar': 3}}} |
| 2112 |
conf = self.get_config(Schema, cfg) |
| 2113 |
self.assertEqual(set(conf.plugins), {'sample'}) |
| 2114 |
self.assertEqual(conf.plugins['sample'].config.bar, 3) |
| 2115 |
|
| 2116 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample': {'enabled': 5}}} |
| 2117 |
with self.expect_error( |
| 2118 |
plugins="Plugin 'sample' option 'enabled': Expected boolean but received: <class 'int'>" |
| 2119 |
): |
| 2120 |
self.get_config(Schema, cfg) |
| 2121 |
|
| 2122 |
def test_plugin_config_enabled_for_plugin_with_setting(self) -> None: |
| 2123 |
class Schema(Config): |
| 2124 |
theme = c.Theme(default='mkdocs') |
| 2125 |
plugins = c.Plugins(theme_key='theme') |
| 2126 |
|
| 2127 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample-e': {'enabled': False, 'bar': 3}}} |
| 2128 |
conf = self.get_config(Schema, cfg) |
| 2129 |
self.assertEqual(set(conf.plugins), {'sample-e'}) |
| 2130 |
self.assertEqual(conf.plugins['sample-e'].config.enabled, False) |
| 2131 |
self.assertEqual(conf.plugins['sample-e'].config.bar, 3) |
| 2132 |
|
| 2133 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample-e': {'enabled': True, 'bar': 3}}} |
| 2134 |
conf = self.get_config(Schema, cfg) |
| 2135 |
self.assertEqual(set(conf.plugins), {'sample-e'}) |
| 2136 |
self.assertEqual(conf.plugins['sample-e'].config.enabled, True) |
| 2137 |
self.assertEqual(conf.plugins['sample-e'].config.bar, 3) |
| 2138 |
|
| 2139 |
cfg = {'theme': 'readthedocs', 'plugins': {'sample-e': {'enabled': 5}}} |
| 2140 |
with self.expect_error( |
| 2141 |
plugins="Plugin 'sample-e' option 'enabled': Expected type: <class 'bool'> but received: <class 'int'>" |
| 2142 |
): |
| 2143 |
self.get_config(Schema, cfg) |
| 2144 |
|
| 2145 |
def test_plugin_config_with_multiple_instances(self) -> None: |
| 2146 |
class Schema(Config): |
| 2147 |
theme = c.Theme(default='mkdocs') |
| 2148 |
plugins = c.Plugins(theme_key='theme') |
| 2149 |
|
| 2150 |
cfg = { |
| 2151 |
'plugins': [ |
| 2152 |
{'sample2': {'foo': 'foo value', 'bar': 42}}, |
| 2153 |
{'sample2': {'foo': 'foo2 value'}}, |
| 2154 |
], |
| 2155 |
} |
| 2156 |
conf = self.get_config(Schema, cfg) |
| 2157 |
|
| 2158 |
self.assertEqual( |
| 2159 |
set(conf.plugins), |
| 2160 |
{'sample2', 'sample2 #2'}, |
| 2161 |
) |
| 2162 |
self.assertEqual(conf.plugins['sample2'].config['bar'], 42) |
| 2163 |
self.assertEqual(conf.plugins['sample2 #2'].config['bar'], 0) |
| 2164 |
|
| 2165 |
def test_plugin_config_with_multiple_instances_and_warning(self) -> None: |
| 2166 |
class Schema(Config): |
| 2167 |
theme = c.Theme(default='mkdocs') |
| 2168 |
plugins = c.Plugins(theme_key='theme') |
| 2169 |
|
| 2170 |
test_cfgs: list[dict[str, Any]] = [ |
| 2171 |
{ |
| 2172 |
'theme': 'readthedocs', |
| 2173 |
'plugins': [{'sub_plugin': {}}, {'sample2': {}}, {'sub_plugin': {}}, 'sample2'], |
| 2174 |
}, |
| 2175 |
{ |
| 2176 |
'theme': 'readthedocs', |
| 2177 |
'plugins': ['sub_plugin', 'sample2', 'sample2', 'sub_plugin'], |
| 2178 |
}, |
| 2179 |
] |
| 2180 |
|
| 2181 |
for cfg in test_cfgs: |
| 2182 |
conf = self.get_config( |
| 2183 |
Schema, |
| 2184 |
cfg, |
| 2185 |
warnings=dict( |
| 2186 |
plugins="Plugin 'readthedocs/sub_plugin' was specified multiple times - " |
| 2187 |
"this is likely a mistake, because the plugin doesn't declare " |
| 2188 |
"`supports_multiple_instances`." |
| 2189 |
), |
| 2190 |
) |
| 2191 |
self.assertEqual( |
| 2192 |
set(conf.plugins), |
| 2193 |
{'readthedocs/sub_plugin', 'readthedocs/sub_plugin #2', 'sample2', 'sample2 #2'}, |
| 2194 |
) |
| 2195 |
|
| 2196 |
def test_plugin_config_empty_list_with_empty_default(self) -> None: |
| 2197 |
class Schema(Config): |
| 2198 |
plugins = c.Plugins(default=[]) |
| 2199 |
|
| 2200 |
cfg: dict[str, Any] = {'plugins': []} |
| 2201 |
conf = self.get_config(Schema, cfg) |
| 2202 |
|
| 2203 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2204 |
self.assertEqual(len(conf.plugins), 0) |
| 2205 |
|
| 2206 |
def test_plugin_config_empty_list_with_default(self) -> None: |
| 2207 |
class Schema(Config): |
| 2208 |
plugins = c.Plugins(default=['sample']) |
| 2209 |
|
| 2210 |
# Default is ignored |
| 2211 |
cfg: dict[str, Any] = {'plugins': []} |
| 2212 |
conf = self.get_config(Schema, cfg) |
| 2213 |
|
| 2214 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2215 |
self.assertEqual(len(conf.plugins), 0) |
| 2216 |
|
| 2217 |
def test_plugin_config_none_with_empty_default(self) -> None: |
| 2218 |
class Schema(Config): |
| 2219 |
plugins = c.Plugins(default=[]) |
| 2220 |
|
| 2221 |
cfg = {'plugins': None} |
| 2222 |
conf = self.get_config(Schema, cfg) |
| 2223 |
|
| 2224 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2225 |
self.assertEqual(len(conf.plugins), 0) |
| 2226 |
|
| 2227 |
def test_plugin_config_none_with_default(self) -> None: |
| 2228 |
class Schema(Config): |
| 2229 |
plugins = c.Plugins(default=['sample']) |
| 2230 |
|
| 2231 |
# Default is used. |
| 2232 |
cfg = {'plugins': None} |
| 2233 |
conf = self.get_config(Schema, cfg) |
| 2234 |
|
| 2235 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2236 |
self.assertIn('sample', conf.plugins) |
| 2237 |
self.assertIsInstance(conf.plugins['sample'], BasePlugin) |
| 2238 |
expected = { |
| 2239 |
'foo': 'default foo', |
| 2240 |
'bar': 0, |
| 2241 |
'dir': None, |
| 2242 |
} |
| 2243 |
self.assertEqual(conf.plugins['sample'].config, expected) |
| 2244 |
|
| 2245 |
def test_plugin_config_uninstalled(self) -> None: |
| 2246 |
class Schema(Config): |
| 2247 |
plugins = c.Plugins() |
| 2248 |
|
| 2249 |
cfg = {'plugins': ['uninstalled']} |
| 2250 |
with self.expect_error(plugins='The "uninstalled" plugin is not installed'): |
| 2251 |
self.get_config(Schema, cfg) |
| 2252 |
|
| 2253 |
def test_plugin_config_not_list(self) -> None: |
| 2254 |
class Schema(Config): |
| 2255 |
plugins = c.Plugins() |
| 2256 |
|
| 2257 |
cfg = {'plugins': 'sample'} |
| 2258 |
with self.expect_error(plugins="Invalid Plugins configuration. Expected a list or dict."): |
| 2259 |
self.get_config(Schema, cfg) |
| 2260 |
|
| 2261 |
def test_plugin_config_multivalue_dict(self) -> None: |
| 2262 |
class Schema(Config): |
| 2263 |
plugins = c.Plugins() |
| 2264 |
|
| 2265 |
cfg = { |
| 2266 |
'plugins': [ |
| 2267 |
{ |
| 2268 |
'sample': { |
| 2269 |
'foo': 'foo value', |
| 2270 |
'bar': 42, |
| 2271 |
}, |
| 2272 |
'extra_key': 'baz', |
| 2273 |
} |
| 2274 |
], |
| 2275 |
} |
| 2276 |
with self.expect_error(plugins="Invalid Plugins configuration"): |
| 2277 |
self.get_config(Schema, cfg) |
| 2278 |
|
| 2279 |
cfg = { |
| 2280 |
'plugins': [ |
| 2281 |
{}, |
| 2282 |
], |
| 2283 |
} |
| 2284 |
with self.expect_error(plugins="Invalid Plugins configuration"): |
| 2285 |
self.get_config(Schema, cfg) |
| 2286 |
|
| 2287 |
def test_plugin_config_not_string_or_dict(self) -> None: |
| 2288 |
class Schema(Config): |
| 2289 |
plugins = c.Plugins() |
| 2290 |
|
| 2291 |
cfg = { |
| 2292 |
'plugins': [('not a string or dict',)], |
| 2293 |
} |
| 2294 |
with self.expect_error(plugins="'('not a string or dict',)' is not a valid plugin name."): |
| 2295 |
self.get_config(Schema, cfg) |
| 2296 |
|
| 2297 |
def test_plugin_config_options_not_dict(self) -> None: |
| 2298 |
class Schema(Config): |
| 2299 |
plugins = c.Plugins() |
| 2300 |
|
| 2301 |
cfg = { |
| 2302 |
'plugins': [{'sample': 'not a dict'}], |
| 2303 |
} |
| 2304 |
with self.expect_error(plugins="Invalid config options for the 'sample' plugin."): |
| 2305 |
self.get_config(Schema, cfg) |
| 2306 |
|
| 2307 |
def test_plugin_config_sub_error(self) -> None: |
| 2308 |
class Schema(Config): |
| 2309 |
plugins = c.Plugins(default=['sample']) |
| 2310 |
|
| 2311 |
cfg = { |
| 2312 |
'plugins': { |
| 2313 |
'sample': {'bar': 'not an int'}, |
| 2314 |
} |
| 2315 |
} |
| 2316 |
with self.expect_error( |
| 2317 |
plugins="Plugin 'sample' option 'bar': Expected type: <class 'int'> but received: <class 'str'>" |
| 2318 |
): |
| 2319 |
self.get_config(Schema, cfg) |
| 2320 |
|
| 2321 |
def test_plugin_config_sub_warning(self) -> None: |
| 2322 |
class Schema(Config): |
| 2323 |
plugins = c.Plugins() |
| 2324 |
|
| 2325 |
cfg = { |
| 2326 |
'plugins': { |
| 2327 |
'sample2': {'depr': 'deprecated value'}, |
| 2328 |
} |
| 2329 |
} |
| 2330 |
conf = self.get_config( |
| 2331 |
Schema, |
| 2332 |
cfg, |
| 2333 |
warnings=dict( |
| 2334 |
plugins="Plugin 'sample2' option 'depr': The configuration option " |
| 2335 |
"'depr' has been deprecated and will be removed in a future release." |
| 2336 |
), |
| 2337 |
) |
| 2338 |
|
| 2339 |
self.assertIsInstance(conf.plugins, PluginCollection) |
| 2340 |
self.assertIn('sample2', conf.plugins) |
| 2341 |
|
| 2342 |
|
| 2343 |
class HooksTest(TestCase): |
| 2344 |
class Schema(Config): |
| 2345 |
plugins = c.Plugins(default=[]) |
| 2346 |
hooks = c.Hooks('plugins') |
| 2347 |
|
| 2348 |
@tempdir() |
| 2349 |
def test_hooks(self, src_dir) -> None: |
| 2350 |
write_file( |
| 2351 |
b'def on_page_markdown(markdown, **kwargs): return markdown.replace("f", "z")', |
| 2352 |
os.path.join(src_dir, 'hooks', 'my_hook.py'), |
| 2353 |
) |
| 2354 |
write_file( |
| 2355 |
b'foo foo', |
| 2356 |
os.path.join(src_dir, 'docs', 'index.md'), |
| 2357 |
) |
| 2358 |
conf = self.get_config( |
| 2359 |
self.Schema, |
| 2360 |
{'hooks': ['hooks/my_hook.py']}, |
| 2361 |
config_file_path=os.path.join(src_dir, 'mkdocs.yml'), |
| 2362 |
) |
| 2363 |
self.assertIn('hooks/my_hook.py', conf.plugins) |
| 2364 |
hook = conf.plugins['hooks/my_hook.py'] |
| 2365 |
self.assertTrue(hasattr(hook, 'on_page_markdown')) |
| 2366 |
self.assertEqual( |
| 2367 |
{**conf.plugins.events, 'page_markdown': [hook.on_page_markdown]}, |
| 2368 |
conf.plugins.events, |
| 2369 |
) |
| 2370 |
self.assertEqual(hook.on_page_markdown('foo foo'), 'zoo zoo') # type: ignore[call-arg] |
| 2371 |
self.assertFalse(hasattr(hook, 'on_nav')) |
| 2372 |
|
| 2373 |
def test_hooks_wrong_type(self) -> None: |
| 2374 |
with self.expect_error(hooks="Expected a list of items, but a <class 'int'> was given."): |
| 2375 |
self.get_config(self.Schema, {'hooks': 6}) |
| 2376 |
|
| 2377 |
with self.expect_error(hooks="Expected type: <class 'str'> but received: <class 'int'>"): |
| 2378 |
self.get_config(self.Schema, {'hooks': [7]}) |
| 2379 |
|
| 2380 |
|
| 2381 |
class SchemaTest(TestCase): |
| 2382 |
def test_copy(self) -> None: |
| 2383 |
class Schema(Config): |
| 2384 |
foo = c.MarkdownExtensions() |
| 2385 |
|
| 2386 |
copy.deepcopy(Schema()) |
| 2387 |
|
| 2388 |
copy.deepcopy(self.get_config(IpAddressTest.Schema, {'option': '1.2.3.4:5678'})) |
| 2389 |
copy.deepcopy(IpAddressTest.Schema) |
| 2390 |
copy.deepcopy(IpAddressTest.Schema()) |
| 2391 |
|
| 2392 |
copy.deepcopy(self.get_config(EditURITest.Schema, {})) |
| 2393 |
copy.deepcopy(EditURITest.Schema) |
| 2394 |
copy.deepcopy(EditURITest.Schema()) |
| 2395 |
|
| 2396 |
def test_subclass(self) -> None: |
| 2397 |
class A(Config): |
| 2398 |
foo = c.Type(int) |
| 2399 |
bar = c.Optional(c.Type(str)) |
| 2400 |
|
| 2401 |
class B(A): |
| 2402 |
baz = c.ListOfItems(c.Type(str)) |
| 2403 |
|
| 2404 |
conf = self.get_config(B, {'foo': 1, 'baz': ['b']}) |
| 2405 |
assert_type(conf.foo, int) |
| 2406 |
self.assertEqual(conf.foo, 1) |
| 2407 |
assert_type(conf.bar, Optional[str]) |
| 2408 |
self.assertEqual(conf.bar, None) |
| 2409 |
assert_type(conf.baz, List[str]) |
| 2410 |
self.assertEqual(conf.baz, ['b']) |
| 2411 |
|
| 2412 |
with self.expect_error(baz="Required configuration not provided."): |
| 2413 |
self.get_config(B, {'foo': 1}) |
| 2414 |
with self.expect_error(foo="Required configuration not provided."): |
| 2415 |
self.get_config(B, {'baz': ['b']}) |
| 2416 |
|
| 2417 |
bconf = self.get_config(A, {'foo': 2, 'bar': 'a'}) |
| 2418 |
assert_type(bconf.foo, int) |
| 2419 |
self.assertEqual(bconf.foo, 2) |
| 2420 |
self.assertEqual(bconf.bar, 'a') |