From 8a58b72934c4f84d667dcce0402e0789f93c3af1 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 6 Sep 2015 15:55:14 -0700 Subject: [PATCH] Better error message when configuration file is missing. --- NEWS | 1 + atticmatic/config.py | 3 ++- atticmatic/tests/unit/test_config.py | 10 +++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 2a7c7ae..1b46c71 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ 0.1.7-dev * #11: Fixed parsing of punctuation in configuration file. + * Better error message when configuration file is missing. 0.1.6 diff --git a/atticmatic/config.py b/atticmatic/config.py index 4f45b89..ddf574d 100644 --- a/atticmatic/config.py +++ b/atticmatic/config.py @@ -109,7 +109,8 @@ def parse_configuration(config_filename, config_format): Raise IOError if the file cannot be read, or ValueError if the format is not as expected. ''' parser = RawConfigParser() - parser.read(config_filename) + if not parser.read(config_filename): + raise ValueError('Configuration file cannot be opened: {}'.format(config_filename)) validate_configuration_format(parser, config_format) diff --git a/atticmatic/tests/unit/test_config.py b/atticmatic/tests/unit/test_config.py index 7a1866e..4b4d3a7 100644 --- a/atticmatic/tests/unit/test_config.py +++ b/atticmatic/tests/unit/test_config.py @@ -197,7 +197,7 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict(): def insert_mock_parser(): parser = flexmock() - parser.should_receive('read') + parser.should_receive('read').and_return([flexmock()]) module.RawConfigParser = lambda: parser return parser @@ -220,3 +220,11 @@ def test_parse_configuration_should_return_section_configs(): parsed_config = module.parse_configuration('filename', config_format) assert parsed_config == type(parsed_config)(*mock_section_configs) + + +def test_parse_configuration_with_file_open_error_should_raise(): + parser = insert_mock_parser() + parser.should_receive('read').and_return([]) + + with assert_raises(ValueError): + module.parse_configuration('filename', config_format=flexmock())