Validate config separately from migrations

It makes for cleaner code, and it makes sure the target of renamed options
actually exists.
This commit is contained in:
Florian Bruhin 2018-02-08 21:46:03 +01:00
parent e0dd7970d8
commit 298dcb4c90
2 changed files with 24 additions and 4 deletions

View File

@ -166,6 +166,7 @@ class YamlConfig(QObject):
self._dirty = False
self._handle_migrations()
self._validate()
def _handle_migrations(self):
"""Handle unknown/renamed keys."""
@ -178,9 +179,11 @@ class YamlConfig(QObject):
elif name in configdata.MIGRATIONS.deleted:
log.config.debug("Removing {}".format(name))
del self._values[name]
elif name in configdata.DATA:
pass
else:
def _validate(self):
"""Make sure all settings exist."""
for name in self._values:
if name not in configdata.DATA:
desc = configexc.ConfigErrorDesc(
"While loading options",
"Unknown option {}".format(name))

View File

@ -145,7 +145,8 @@ class TestYaml:
autoconfig = config_tmpdir / 'autoconfig.yml'
autoconfig.write_text('global:\n old: value', encoding='utf-8')
monkeypatch.setattr(configdata.MIGRATIONS, 'renamed', {'old': 'new'})
monkeypatch.setattr(configdata.MIGRATIONS, 'renamed',
{'old': 'tabs.show'})
yaml.load()
yaml._save()
@ -154,6 +155,22 @@ class TestYaml:
assert ' old:' not in lines
assert ' new:' not in lines
def test_renamed_key_unknown_target(self, monkeypatch, yaml, config_tmpdir):
"""A key marked as renamed with invalid name should raise an error."""
autoconfig = config_tmpdir / 'autoconfig.yml'
autoconfig.write_text('global:\n old: value', encoding='utf-8')
monkeypatch.setattr(configdata.MIGRATIONS, 'renamed',
{'old': 'new'})
with pytest.raises(configexc.ConfigFileErrors) as excinfo:
yaml.load()
assert len(excinfo.value.errors) == 1
error = excinfo.value.errors[0]
assert error.text == "While loading options"
assert str(error.exception) == "Unknown option new"
@pytest.mark.parametrize('old_config', [
None,
'global:\n colors.hints.fg: magenta',