Fix KeyError in ConfigMgr._get
authorHuang Hao <hao.h.huang@intel.com>
Fri, 7 Sep 2012 02:32:49 +0000 (10:32 +0800)
committerHuang Hao <hao.h.huang@intel.com>
Fri, 7 Sep 2012 02:36:44 +0000 (10:36 +0800)
In function ConfigMrg._get() if NoOptionError occurs and section
name is not in self.DEFAULTS, KeyError will be raised.

Change-Id: Iaa08c032eae45b1cc95294a4c7975d0aedb10405

gitbuildsys/conf.py
tests/test_config.py

index ef77447..f186459 100644 (file)
@@ -334,17 +334,14 @@ distconf = $build__distconf
             except NoOptionError:
                 sect_found = True
 
+        if section in self.DEFAULTS and opt in self.DEFAULTS[section]:
+            return self.DEFAULTS[section][opt]
+
         if not sect_found:
-            if section in self.DEFAULTS and opt in self.DEFAULTS[section]:
-                return self.DEFAULTS[section][opt]
-            else:
-                raise errors.ConfigError('no section %s' % section)
+            raise errors.ConfigError('no section %s' % section)
         else:
-            if opt in self.DEFAULTS[section]:
-                return self.DEFAULTS[section][opt]
-            else:
-                raise errors.ConfigError('no opt: %s in section %s' \
-                                         % (opt, section))
+            raise errors.ConfigError('no opt: %s in section %s' \
+                                     % (opt, section))
 
     def check_opt(self, opt, section='general'):
         if section in self.DEFAULTS and \
index 41cf3cf..79c3e82 100644 (file)
@@ -114,6 +114,12 @@ class ConfigGettingTest(unittest.TestCase):
                           self.get, 'not_exists_section', 'key')
 
     @Fixture(project='project1.ini')
+    def test_no_such_option(self):
+        '''test no such option'''
+        self.assertRaises(errors.ConfigError,
+                          self.get, 'section', 'not_exists_option')
+
+    @Fixture(project='project1.ini')
     def test_simple_get(self):
         '''get value when one config file provides'''
         self.assertEqual('projv2', self.get('section', 'proj_only_key'))