config: Set self._initialized before validating.

With a setting with an interpolation this caused a ValueError because
validate_all called get before self._initialized was True.
This commit is contained in:
Florian Bruhin 2014-12-17 13:27:03 +01:00
parent 512d7c4448
commit a714f0b70c
2 changed files with 9 additions and 5 deletions

View File

@ -225,11 +225,13 @@ class ConfigManager(QObject):
self._fname = fname self._fname = fname
if configdir is None: if configdir is None:
self._configdir = None self._configdir = None
self._initialized = True
else: else:
self._configdir = configdir self._configdir = configdir
parser = ini.ReadConfigParser(configdir, fname) parser = ini.ReadConfigParser(configdir, fname)
self._from_cp(parser) self._from_cp(parser)
self._initialized = True self._initialized = True
self._validate_all()
def __getitem__(self, key): def __getitem__(self, key):
"""Get a section from the config.""" """Get a section from the config."""
@ -357,7 +359,6 @@ class ConfigManager(QObject):
if (sectname, k) in self.RENAMED_OPTIONS: if (sectname, k) in self.RENAMED_OPTIONS:
k = self.RENAMED_OPTIONS[sectname, k] k = self.RENAMED_OPTIONS[sectname, k]
self.set('conf', sectname, k, v, validate=False) self.set('conf', sectname, k, v, validate=False)
self._validate_all()
def _validate_all(self): def _validate_all(self):
"""Validate all values set in self._from_cp.""" """Validate all values set in self._from_cp."""

View File

@ -73,15 +73,17 @@ class ConfigParserTests(unittest.TestCase):
def test_invalid_value(self): def test_invalid_value(self):
"""Test setting an invalid value.""" """Test setting an invalid value."""
self.cp.read_dict({'general': {'ignore-case': 'invalid'}}) self.cp.read_dict({'general': {'ignore-case': 'invalid'}})
with self.assertRaises(configexc.ValidationError):
self.cfg._from_cp(self.cp) self.cfg._from_cp(self.cp)
with self.assertRaises(configexc.ValidationError):
self.cfg._validate_all()
def test_invalid_value_interpolated(self): def test_invalid_value_interpolated(self):
"""Test setting an invalid interpolated value.""" """Test setting an invalid interpolated value."""
self.cp.read_dict({'general': {'ignore-case': 'smart', self.cp.read_dict({'general': {'ignore-case': 'smart',
'wrap-search': '${ignore-case}'}}) 'wrap-search': '${ignore-case}'}})
with self.assertRaises(configexc.ValidationError):
self.cfg._from_cp(self.cp) self.cfg._from_cp(self.cp)
with self.assertRaises(configexc.ValidationError):
self.cfg._validate_all()
def test_interpolation(self): def test_interpolation(self):
"""Test setting an interpolated value.""" """Test setting an interpolated value."""
@ -106,8 +108,9 @@ class ConfigParserTests(unittest.TestCase):
def test_invalid_interpolation(self): def test_invalid_interpolation(self):
"""Test an invalid interpolation.""" """Test an invalid interpolation."""
self.cp.read_dict({'general': {'ignore-case': '${foo}'}}) self.cp.read_dict({'general': {'ignore-case': '${foo}'}})
with self.assertRaises(configparser.InterpolationError):
self.cfg._from_cp(self.cp) self.cfg._from_cp(self.cp)
with self.assertRaises(configparser.InterpolationError):
self.cfg._validate_all()
def test_invalid_interpolation_syntax(self): def test_invalid_interpolation_syntax(self):
"""Test an invalid interpolation syntax.""" """Test an invalid interpolation syntax."""