From 0da1c6ec7bef686f2d271e899fe7926479b9751d 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 2a7c7aee..1b46c71f 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 4f45b89b..ddf574d7 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 7a1866e8..4b4d3a7c 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())