فهرست منبع

Preventing ConfigParser from swallowing file read IOErrors, so that the user gets a more useful message.

Dan Helfman 10 سال پیش
والد
کامیت
f4d8469085
3فایلهای تغییر یافته به همراه16 افزوده شده و 6 حذف شده
  1. 10 3
      atticmatic/command.py
  2. 4 2
      atticmatic/config.py
  3. 2 1
      atticmatic/tests/unit/test_config.py

+ 10 - 3
atticmatic/command.py

@@ -7,7 +7,10 @@ from atticmatic.attic import create_archive, prune_archives
 from atticmatic.config import parse_configuration
 
 
-def main():
+def parse_arguments():
+    '''
+    Parse the command-line arguments from sys.argv and return them as an ArgumentParser instance.
+    '''
     parser = ArgumentParser()
     parser.add_argument(
         '--config',
@@ -26,13 +29,17 @@ def main():
         action='store_true',
         help='Display verbose progress information',
     )
-    args = parser.parse_args()
 
+    return parser.parse_args()
+
+
+def main():
     try:
+        args = parse_arguments()
         location_config, retention_config = parse_configuration(args.config_filename)
 
         create_archive(args.excludes_filename, args.verbose, *location_config)
         prune_archives(location_config.repository, args.verbose, *retention_config)
-    except (ValueError, CalledProcessError) as error:
+    except (ValueError, IOError, CalledProcessError) as error:
         print(error, file=sys.stderr)
         sys.exit(1)

+ 4 - 2
atticmatic/config.py

@@ -23,10 +23,12 @@ RetentionConfig = namedtuple('RetentionConfig', CONFIG_FORMAT[CONFIG_SECTION_RET
 def parse_configuration(config_filename):
     '''
     Given a config filename of the expected format, return the parse configuration as a tuple of
-    (LocationConfig, RetentionConfig). Raise if the format is not as expected.
+    (LocationConfig, RetentionConfig).
+
+    Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
     '''
     parser = ConfigParser()
-    parser.read((config_filename,))
+    parser.readfp(open(config_filename))
     section_names = parser.sections()
     expected_section_names = CONFIG_FORMAT.keys()
 

+ 2 - 1
atticmatic/tests/unit/test_config.py

@@ -6,8 +6,9 @@ from atticmatic import config as module
 
 def insert_mock_parser(section_names):
     parser = flexmock()
-    parser.should_receive('read')
+    parser.should_receive('readfp')
     parser.should_receive('sections').and_return(section_names)
+    flexmock(module).open = lambda filename: None
     flexmock(module).ConfigParser = parser
 
     return parser