Permalink
Browse files

fix bug were config parsing did not cover all error cases

  • Loading branch information...
1 parent 2e18542 commit ab9db5a0a2093615665d791ec86abfabb249ccb7 @domenkozar committed Dec 30, 2012
Showing with 27 additions and 14 deletions.
  1. +5 −2 mrbob/parsing.py
  2. +22 −12 mrbob/tests/test_parsing.py
View
@@ -20,10 +20,13 @@ def nest_variables(variables):
if not isinstance(location, dict):
raise ConfigurationError('Cannot assign "%s" to group "%s", subgroup is already used.' % (value, key))
+ k = segments[-1]
+ if isinstance(location.get(k, None), dict):
+ raise ConfigurationError('Cannot assign "%s" to group "%s", subgroup is already used.' % (value, k))
if PY3: # pragma: no cover
- location[segments[-1]] = value
+ location[k] = value
else: # pragma: no cover
- location[segments[-1]] = value.decode('utf-8')
+ location[k] = value.decode('utf-8')
return nested
@@ -3,6 +3,7 @@
import unittest
import tempfile
import codecs
+import collections
import six
@@ -46,18 +47,6 @@ def test_parse_deeply_nested_variables(self):
}
self.assertEqual(c, expected_config)
- def test_overwrite_dict_with_value(self):
- """ providing a value for a key that already contains a
- dictionary raises a ConfigurationError """
- from ..configurator import ConfigurationError
- self.assertRaises(ConfigurationError, self.call_FUT, 'example3.ini')
-
- def test_overwrite_value_with_dict(self):
- """ providing a dict for a key that already contains a
- string raises a ConfigurationError """
- from ..configurator import ConfigurationError
- self.assertRaises(ConfigurationError, self.call_FUT, 'example4.ini')
-
def test_parse_config_utf8(self):
from ..parsing import pretty_format_config
c = self.call_FUT('example6.ini')
@@ -210,3 +199,24 @@ def test_complex(self):
'foo = bar',
'z = z'],
)
+
+
+class nest_variablesTest(unittest.TestCase):
+
+ def call_FUT(self, d):
+ from ..parsing import nest_variables
+ return nest_variables(d)
+
+ def test_overwrite_dict_with_value(self):
+ """ providing a value for a key that already contains a
+ dictionary raises a ConfigurationError """
+ from ..configurator import ConfigurationError
+ d = collections.OrderedDict([('foo.bar', '1'), ('foo', '2')])
+ self.assertRaises(ConfigurationError, self.call_FUT, d)
+
+ def test_overwrite_value_with_dict(self):
+ """ providing a dict for a key that already contains a
+ string raises a ConfigurationError """
+ from ..configurator import ConfigurationError
+ d = collections.OrderedDict([('foo', '2'), ('foo.bar', '1')])
+ self.assertRaises(ConfigurationError, self.call_FUT, d)

0 comments on commit ab9db5a

Please sign in to comment.