| 1 |
import os |
| 2 |
import unittest |
| 3 |
|
| 4 |
from mkdocs import exceptions |
| 5 |
from mkdocs.config import base |
| 6 |
from mkdocs.config import config_options as c |
| 7 |
from mkdocs.config import defaults |
| 8 |
from mkdocs.config.base import ValidationError |
| 9 |
from mkdocs.tests.base import change_dir, tempdir |
| 10 |
|
| 11 |
|
| 12 |
class ConfigBaseTests(unittest.TestCase): |
| 13 |
def test_unrecognised_keys(self): |
| 14 |
conf = defaults.MkDocsConfig() |
| 15 |
conf.load_dict( |
| 16 |
{ |
| 17 |
'not_a_valid_config_option': "test", |
| 18 |
} |
| 19 |
) |
| 20 |
|
| 21 |
failed, warnings = conf.validate() |
| 22 |
|
| 23 |
self.assertEqual( |
| 24 |
warnings, |
| 25 |
[ |
| 26 |
( |
| 27 |
'not_a_valid_config_option', |
| 28 |
'Unrecognised configuration name: not_a_valid_config_option', |
| 29 |
) |
| 30 |
], |
| 31 |
) |
| 32 |
|
| 33 |
def test_missing_required(self): |
| 34 |
conf = defaults.MkDocsConfig() |
| 35 |
|
| 36 |
errors, warnings = conf.validate() |
| 37 |
|
| 38 |
self.assertEqual( |
| 39 |
errors, [('site_name', ValidationError('Required configuration not provided.'))] |
| 40 |
) |
| 41 |
self.assertEqual(warnings, []) |
| 42 |
|
| 43 |
@tempdir() |
| 44 |
def test_load_from_file(self, temp_dir): |
| 45 |
""" |
| 46 |
Users can explicitly set the config file using the '--config' option. |
| 47 |
Allows users to specify a config other than the default `mkdocs.yml`. |
| 48 |
""" |
| 49 |
with open(os.path.join(temp_dir, 'mkdocs.yml'), 'w') as config_file: |
| 50 |
config_file.write("site_name: MkDocs Test\n") |
| 51 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 52 |
|
| 53 |
cfg = base.load_config(config_file=config_file.name) |
| 54 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 55 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 56 |
|
| 57 |
@tempdir() |
| 58 |
def test_load_default_file(self, temp_dir): |
| 59 |
"""Test that `mkdocs.yml` will be loaded when '--config' is not set.""" |
| 60 |
with open(os.path.join(temp_dir, 'mkdocs.yml'), 'w') as config_file: |
| 61 |
config_file.write("site_name: MkDocs Test\n") |
| 62 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 63 |
with change_dir(temp_dir): |
| 64 |
cfg = base.load_config(config_file=None) |
| 65 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 66 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 67 |
|
| 68 |
@tempdir() |
| 69 |
def test_load_default_file_with_yaml(self, temp_dir): |
| 70 |
"""Test that `mkdocs.yml` will be loaded when '--config' is not set.""" |
| 71 |
with open(os.path.join(temp_dir, 'mkdocs.yaml'), 'w') as config_file: |
| 72 |
config_file.write("site_name: MkDocs Test\n") |
| 73 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 74 |
with change_dir(temp_dir): |
| 75 |
cfg = base.load_config(config_file=None) |
| 76 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 77 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 78 |
|
| 79 |
@tempdir() |
| 80 |
def test_load_default_file_prefer_yml(self, temp_dir): |
| 81 |
"""Test that `mkdocs.yml` will be loaded when '--config' is not set.""" |
| 82 |
with open(os.path.join(temp_dir, 'mkdocs.yml'), 'w') as config_file1: |
| 83 |
config_file1.write("site_name: MkDocs Test1\n") |
| 84 |
with open(os.path.join(temp_dir, 'mkdocs.yaml'), 'w') as config_file2: |
| 85 |
config_file2.write("site_name: MkDocs Test2\n") |
| 86 |
|
| 87 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 88 |
with change_dir(temp_dir): |
| 89 |
cfg = base.load_config(config_file=None) |
| 90 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 91 |
self.assertEqual(cfg.site_name, 'MkDocs Test1') |
| 92 |
|
| 93 |
def test_load_from_missing_file(self): |
| 94 |
with self.assertRaisesRegex( |
| 95 |
exceptions.ConfigurationError, "Config file 'missing_file.yml' does not exist." |
| 96 |
): |
| 97 |
base.load_config(config_file='missing_file.yml') |
| 98 |
|
| 99 |
@tempdir() |
| 100 |
def test_load_from_open_file(self, temp_path): |
| 101 |
"""`load_config` can accept an open file descriptor.""" |
| 102 |
config_fname = os.path.join(temp_path, 'mkdocs.yml') |
| 103 |
config_file = open(config_fname, 'w+') |
| 104 |
config_file.write("site_name: MkDocs Test\n") |
| 105 |
config_file.flush() |
| 106 |
os.mkdir(os.path.join(temp_path, 'docs')) |
| 107 |
|
| 108 |
cfg = base.load_config(config_file=config_file) |
| 109 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 110 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 111 |
# load_config will always close the file |
| 112 |
self.assertTrue(config_file.closed) |
| 113 |
|
| 114 |
@tempdir() |
| 115 |
def test_load_from_closed_file(self, temp_dir): |
| 116 |
""" |
| 117 |
The `serve` command with auto-reload may pass in a closed file descriptor. |
| 118 |
Ensure `load_config` reloads the closed file. |
| 119 |
""" |
| 120 |
with open(os.path.join(temp_dir, 'mkdocs.yml'), 'w') as config_file: |
| 121 |
config_file.write("site_name: MkDocs Test\n") |
| 122 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 123 |
|
| 124 |
cfg = base.load_config(config_file=config_file) |
| 125 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 126 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 127 |
|
| 128 |
@tempdir() |
| 129 |
def test_load_missing_required(self, temp_dir): |
| 130 |
"""`site_name` is a required setting.""" |
| 131 |
with open(os.path.join(temp_dir, 'mkdocs.yml'), 'w') as config_file: |
| 132 |
config_file.write("site_dir: output\nsite_url: https://www.mkdocs.org\n") |
| 133 |
os.mkdir(os.path.join(temp_dir, 'docs')) |
| 134 |
|
| 135 |
with self.assertLogs('mkdocs') as cm: |
| 136 |
with self.assertRaises(exceptions.Abort): |
| 137 |
base.load_config(config_file=config_file.name) |
| 138 |
self.assertEqual( |
| 139 |
'\n'.join(cm.output), |
| 140 |
"ERROR:mkdocs.config:Config value 'site_name': Required configuration not provided.", |
| 141 |
) |
| 142 |
|
| 143 |
def test_pre_validation_error(self): |
| 144 |
class InvalidConfigOption(c.BaseConfigOption): |
| 145 |
def pre_validation(self, config, key_name): |
| 146 |
raise ValidationError('pre_validation error') |
| 147 |
|
| 148 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 149 |
|
| 150 |
errors, warnings = conf.validate() |
| 151 |
|
| 152 |
self.assertEqual(errors, [('invalid_option', ValidationError('pre_validation error'))]) |
| 153 |
self.assertEqual(warnings, []) |
| 154 |
|
| 155 |
def test_run_validation_error(self): |
| 156 |
class InvalidConfigOption(c.BaseConfigOption): |
| 157 |
def run_validation(self, value): |
| 158 |
raise ValidationError('run_validation error') |
| 159 |
|
| 160 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 161 |
|
| 162 |
errors, warnings = conf.validate() |
| 163 |
|
| 164 |
self.assertEqual(errors, [('invalid_option', ValidationError('run_validation error'))]) |
| 165 |
self.assertEqual(warnings, []) |
| 166 |
|
| 167 |
def test_post_validation_error(self): |
| 168 |
class InvalidConfigOption(c.BaseConfigOption): |
| 169 |
def post_validation(self, config, key_name): |
| 170 |
raise ValidationError('post_validation error') |
| 171 |
|
| 172 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 173 |
|
| 174 |
errors, warnings = conf.validate() |
| 175 |
|
| 176 |
self.assertEqual(errors, [('invalid_option', ValidationError('post_validation error'))]) |
| 177 |
self.assertEqual(warnings, []) |
| 178 |
|
| 179 |
def test_pre_and_run_validation_errors(self): |
| 180 |
"""A pre_validation error does not stop run_validation from running.""" |
| 181 |
|
| 182 |
class InvalidConfigOption(c.BaseConfigOption): |
| 183 |
def pre_validation(self, config, key_name): |
| 184 |
raise ValidationError('pre_validation error') |
| 185 |
|
| 186 |
def run_validation(self, value): |
| 187 |
raise ValidationError('run_validation error') |
| 188 |
|
| 189 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 190 |
|
| 191 |
errors, warnings = conf.validate() |
| 192 |
|
| 193 |
self.assertEqual( |
| 194 |
errors, |
| 195 |
[ |
| 196 |
('invalid_option', ValidationError('pre_validation error')), |
| 197 |
('invalid_option', ValidationError('run_validation error')), |
| 198 |
], |
| 199 |
) |
| 200 |
self.assertEqual(warnings, []) |
| 201 |
|
| 202 |
def test_run_and_post_validation_errors(self): |
| 203 |
"""A run_validation error stops post_validation from running.""" |
| 204 |
|
| 205 |
class InvalidConfigOption(c.BaseConfigOption): |
| 206 |
def run_validation(self, value): |
| 207 |
raise ValidationError('run_validation error') |
| 208 |
|
| 209 |
def post_validation(self, config, key_name): |
| 210 |
raise ValidationError('post_validation error') |
| 211 |
|
| 212 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 213 |
|
| 214 |
errors, warnings = conf.validate() |
| 215 |
|
| 216 |
self.assertEqual(errors, [('invalid_option', ValidationError('run_validation error'))]) |
| 217 |
self.assertEqual(warnings, []) |
| 218 |
|
| 219 |
def test_validation_warnings(self): |
| 220 |
class InvalidConfigOption(c.BaseConfigOption): |
| 221 |
def pre_validation(self, config, key_name): |
| 222 |
self.warnings.append('pre_validation warning') |
| 223 |
|
| 224 |
def run_validation(self, value): |
| 225 |
self.warnings.append('run_validation warning') |
| 226 |
|
| 227 |
def post_validation(self, config, key_name): |
| 228 |
self.warnings.append('post_validation warning') |
| 229 |
|
| 230 |
conf = base.Config(schema=(('invalid_option', InvalidConfigOption()),)) |
| 231 |
|
| 232 |
errors, warnings = conf.validate() |
| 233 |
|
| 234 |
self.assertEqual(errors, []) |
| 235 |
self.assertEqual( |
| 236 |
warnings, |
| 237 |
[ |
| 238 |
('invalid_option', 'pre_validation warning'), |
| 239 |
('invalid_option', 'run_validation warning'), |
| 240 |
('invalid_option', 'post_validation warning'), |
| 241 |
], |
| 242 |
) |
| 243 |
|
| 244 |
@tempdir() |
| 245 |
def test_load_from_file_with_relative_paths(self, config_dir): |
| 246 |
""" |
| 247 |
When explicitly setting a config file, paths should be relative to the |
| 248 |
config file, not the working directory. |
| 249 |
""" |
| 250 |
config_fname = os.path.join(config_dir, 'mkdocs.yml') |
| 251 |
with open(config_fname, 'w') as config_file: |
| 252 |
config_file.write("docs_dir: src\nsite_name: MkDocs Test\n") |
| 253 |
docs_dir = os.path.join(config_dir, 'src') |
| 254 |
os.mkdir(docs_dir) |
| 255 |
|
| 256 |
cfg = base.load_config(config_file=config_file) |
| 257 |
self.assertTrue(isinstance(cfg, defaults.MkDocsConfig)) |
| 258 |
self.assertEqual(cfg.site_name, 'MkDocs Test') |
| 259 |
self.assertEqual(cfg.docs_dir, docs_dir) |
| 260 |
self.assertEqual(cfg.config_file_path, config_fname) |
| 261 |
self.assertIsInstance(cfg.config_file_path, str) |
| 262 |
|
| 263 |
def test_get_schema(self): |
| 264 |
class FooConfig: |
| 265 |
z = c.URL() |
| 266 |
aa = c.Type(int) |
| 267 |
|
| 268 |
self.assertEqual( |
| 269 |
base.get_schema(FooConfig), |
| 270 |
( |
| 271 |
('z', FooConfig.z), |
| 272 |
('aa', FooConfig.aa), |
| 273 |
), |
| 274 |
) |