瀏覽代碼

Better error message when configuration file is missing.

Dan Helfman 10 年之前
父節點
當前提交
8a58b72934
共有 3 個文件被更改,包括 12 次插入2 次删除
  1. 1 0
      NEWS
  2. 2 1
      atticmatic/config.py
  3. 9 1
      atticmatic/tests/unit/test_config.py

+ 1 - 0
NEWS

@@ -1,6 +1,7 @@
 0.1.7-dev
 0.1.7-dev
 
 
  * #11: Fixed parsing of punctuation in configuration file.
  * #11: Fixed parsing of punctuation in configuration file.
+ * Better error message when configuration file is missing.
 
 
 0.1.6
 0.1.6
 
 

+ 2 - 1
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.
     Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
     '''
     '''
     parser = RawConfigParser()
     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)
     validate_configuration_format(parser, config_format)
 
 

+ 9 - 1
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():
 def insert_mock_parser():
     parser = flexmock()
     parser = flexmock()
-    parser.should_receive('read')
+    parser.should_receive('read').and_return([flexmock()])
     module.RawConfigParser = lambda: parser
     module.RawConfigParser = lambda: parser
 
 
     return parser
     return parser
@@ -220,3 +220,11 @@ def test_parse_configuration_should_return_section_configs():
     parsed_config = module.parse_configuration('filename', config_format)
     parsed_config = module.parse_configuration('filename', config_format)
 
 
     assert parsed_config == type(parsed_config)(*mock_section_configs)
     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())