Skip to content

Commit

Permalink
fix bug were config parsing did not cover all error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Dec 30, 2012
1 parent 2e18542 commit ab9db5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
7 changes: 5 additions & 2 deletions mrbob/parsing.py
Expand Up @@ -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


Expand Down
34 changes: 22 additions & 12 deletions mrbob/tests/test_parsing.py
Expand Up @@ -3,6 +3,7 @@
import unittest
import tempfile
import codecs
import collections

import six

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.