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 ef774477025230cc109ffefa8834fc3db9f25b50..f186459fed912bf892868f67d619668d98bf2573 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 41cf3cf4713925c8f3d635f215b10978b3bdcd2e..79c3e82ae7574445d7dd684288542d66b2f15e09 100644 (file)
@@ -113,6 +113,12 @@ class ConfigGettingTest(unittest.TestCase):
         self.assertRaises(errors.ConfigError,
                           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'''