From e127d4906e4f4e7edf1fbf241a3fa428d31086d0 Mon Sep 17 00:00:00 2001 From: Ng Zhi An Date: Sun, 19 Oct 2014 10:59:16 -0400 Subject: [PATCH] Fix #14: Exit with message and code on config errors --- mkdocs/config.py | 10 ++++++++-- mkdocs/test.py | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mkdocs/config.py b/mkdocs/config.py index c50aae7e..a9cf05c7 100644 --- a/mkdocs/config.py +++ b/mkdocs/config.py @@ -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 diff --git a/mkdocs/test.py b/mkdocs/test.py index 5d1f6c63..1f723e29 100755 --- a/mkdocs/test.py +++ b/mkdocs/test.py @@ -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.