Fix #14: Exit with message and code on config errors

This commit is contained in:
Ng Zhi An
2014-10-19 10:59:16 -04:00
parent 0f5b6e14fa
commit e127d4906e
2 changed files with 17 additions and 2 deletions

View File

@@ -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

View File

@@ -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.