mirror of
https://github.com/mkdocs/mkdocs.git
synced 2026-03-27 18:08:31 +07:00
Fix #14: Exit with message and code on config errors
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
from mkdocs import utils
|
||||
from mkdocs.compat import urlparse
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
@@ -73,7 +75,9 @@ def load_config(filename='mkdocs.yml', options=None):
|
||||
options = options or {}
|
||||
if 'config' in options:
|
||||
filename = options['config']
|
||||
assert os.path.exists(filename), "Config file '%s' does not exist." % filename
|
||||
if not os.path.exists(filename):
|
||||
sys.stderr.write("Config file '%s' does not exist." % filename)
|
||||
sys.exit(errno.ENOENT)
|
||||
with open(filename, 'r') as fp:
|
||||
user_config = yaml.load(fp)
|
||||
user_config.update(options)
|
||||
@@ -84,7 +88,9 @@ def validate_config(user_config):
|
||||
config = DEFAULT_CONFIG.copy()
|
||||
config.update(user_config)
|
||||
|
||||
assert config['site_name'], "Config must contain 'site_name' setting."
|
||||
if not config['site_name']:
|
||||
sys.stderr.write("Config must contain 'site_name' setting.")
|
||||
sys.exit(errno.EINVAL)
|
||||
|
||||
# If not specified, then the 'pages' config simply includes all
|
||||
# markdown files in the docs dir, without generating any header items
|
||||
|
||||
@@ -21,6 +21,15 @@ def ensure_utf(string):
|
||||
|
||||
|
||||
class ConfigTests(unittest.TestCase):
|
||||
def test_missing_config_file(self):
|
||||
options = {'config': 'bad_filename.yaml'}
|
||||
with self.assertRaises(SystemExit):
|
||||
config.load_config(options=options)
|
||||
|
||||
def test_missing_site_name(self):
|
||||
with self.assertRaises(SystemExit):
|
||||
config.validate_config({})
|
||||
|
||||
def test_config_option(self):
|
||||
"""
|
||||
Users can explicitly set the config file using the '--config' option.
|
||||
|
||||
Reference in New Issue
Block a user