/usr/lib/python3/dist-packages/mkdocs/tests/config/base_tests.py is in mkdocs 0.16.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | from __future__ import unicode_literals
import os
import tempfile
import unittest
from mkdocs import exceptions
from mkdocs.config import base, defaults
from mkdocs.config.config_options import BaseConfigOption
class ConfigBaseTests(unittest.TestCase):
def test_unrecognised_keys(self):
c = base.Config(schema=defaults.DEFAULT_SCHEMA)
c.load_dict({
'not_a_valid_config_option': "test"
})
failed, warnings = c.validate()
self.assertEqual(warnings, [
('not_a_valid_config_option',
'Unrecognised configuration name: not_a_valid_config_option')
])
def test_missing_required(self):
c = base.Config(schema=defaults.DEFAULT_SCHEMA)
errors, warnings = c.validate()
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0][0], 'site_name')
self.assertEqual(str(errors[0][1]), 'Required configuration not provided.')
self.assertEqual(len(warnings), 0)
def test_load_from_file(self):
"""
Users can explicitly set the config file using the '--config' option.
Allows users to specify a config other than the default `mkdocs.yml`.
"""
config_file = tempfile.NamedTemporaryFile('w', delete=False)
try:
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
config_file.close()
cfg = base.load_config(config_file=config_file.name)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
finally:
os.remove(config_file.name)
def test_load_from_missing_file(self):
self.assertRaises(exceptions.ConfigurationError,
base.load_config, config_file='missing_file.yml')
def test_load_from_open_file(self):
"""
`load_config` can accept an open file descriptor.
"""
config_file = tempfile.NamedTemporaryFile('r+', delete=False)
try:
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
cfg = base.load_config(config_file=config_file)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
# load_config will always close the file
self.assertTrue(config_file.closed)
finally:
os.remove(config_file.name)
def test_load_from_closed_file(self):
"""
The `serve` command with auto-reload may pass in a closed file descriptor.
Ensure `load_config` reloads the closed file.
"""
config_file = tempfile.NamedTemporaryFile('w', delete=False)
try:
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
config_file.close()
cfg = base.load_config(config_file=config_file)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
finally:
os.remove(config_file.name)
def test_load_from_deleted_file(self):
"""
Deleting the config file could trigger a server reload.
"""
config_file = tempfile.NamedTemporaryFile('w', delete=False)
try:
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
config_file.close()
finally:
os.remove(config_file.name)
self.assertRaises(exceptions.ConfigurationError,
base.load_config, config_file=config_file)
def test_load_missing_required(self):
"""
`site_name` is a required setting.
"""
config_file = tempfile.NamedTemporaryFile('w', delete=False)
try:
config_file.write(
"site_dir: output\nsite_uri: http://www.mkdocs.org\n")
config_file.flush()
config_file.close()
self.assertRaises(exceptions.ConfigurationError,
base.load_config, config_file=config_file.name)
finally:
os.remove(config_file.name)
def test_pre_validation_error(self):
class InvalidConfigOption(BaseConfigOption):
def pre_validation(self, config, key_name):
raise base.ValidationError('pre_validation error')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0][0], 'invalid_option')
self.assertEqual(str(errors[0][1]), 'pre_validation error')
self.assertTrue(isinstance(errors[0][1], base.ValidationError))
self.assertEqual(len(warnings), 0)
def test_run_validation_error(self):
class InvalidConfigOption(BaseConfigOption):
def run_validation(self, value):
raise base.ValidationError('run_validation error')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0][0], 'invalid_option')
self.assertEqual(str(errors[0][1]), 'run_validation error')
self.assertTrue(isinstance(errors[0][1], base.ValidationError))
self.assertEqual(len(warnings), 0)
def test_post_validation_error(self):
class InvalidConfigOption(BaseConfigOption):
def post_validation(self, config, key_name):
raise base.ValidationError('post_validation error')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0][0], 'invalid_option')
self.assertEqual(str(errors[0][1]), 'post_validation error')
self.assertTrue(isinstance(errors[0][1], base.ValidationError))
self.assertEqual(len(warnings), 0)
def test_pre_and_run_validation_errors(self):
""" A pre_validation error does not stop run_validation from running. """
class InvalidConfigOption(BaseConfigOption):
def pre_validation(self, config, key_name):
raise base.ValidationError('pre_validation error')
def run_validation(self, value):
raise base.ValidationError('run_validation error')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 2)
self.assertEqual(errors[0][0], 'invalid_option')
self.assertEqual(str(errors[0][1]), 'pre_validation error')
self.assertTrue(isinstance(errors[0][1], base.ValidationError))
self.assertEqual(errors[1][0], 'invalid_option')
self.assertEqual(str(errors[1][1]), 'run_validation error')
self.assertTrue(isinstance(errors[1][1], base.ValidationError))
self.assertEqual(len(warnings), 0)
def test_run_and_post_validation_errors(self):
""" A run_validation error stops post_validation from running. """
class InvalidConfigOption(BaseConfigOption):
def run_validation(self, value):
raise base.ValidationError('run_validation error')
def post_validation(self, config, key_name):
raise base.ValidationError('post_validation error')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0][0], 'invalid_option')
self.assertEqual(str(errors[0][1]), 'run_validation error')
self.assertTrue(isinstance(errors[0][1], base.ValidationError))
self.assertEqual(len(warnings), 0)
def test_validation_warnings(self):
class InvalidConfigOption(BaseConfigOption):
def pre_validation(self, config, key_name):
self.warnings.append('pre_validation warning')
def run_validation(self, value):
self.warnings.append('run_validation warning')
def post_validation(self, config, key_name):
self.warnings.append('post_validation warning')
c = base.Config(schema=(('invalid_option', InvalidConfigOption()), ))
errors, warnings = c.validate()
self.assertEqual(len(errors), 0)
self.assertEqual(warnings, [
('invalid_option', 'pre_validation warning'),
('invalid_option', 'run_validation warning'),
('invalid_option', 'post_validation warning'),
])
|