YamlConfig: Refuse to read a newer config version

This commit is contained in:
Florian Bruhin 2018-02-20 17:08:28 +01:00
parent 03114ccf51
commit e482c76874
2 changed files with 20 additions and 1 deletions

View File

@ -172,13 +172,18 @@ class YamlConfig(QObject):
raise configexc.ConfigFileErrors('autoconfig.yml', [desc])
config_version = self._pop_object(yaml_data, 'config_version', int)
if config_version == 1:
settings = self._load_legacy_settings_object(yaml_data)
self._mark_changed()
elif config_version > self.VERSION:
desc = configexc.ConfigErrorDesc(
"While reading",
"Can't read config from incompatible newer version")
raise configexc.ConfigFileErrors('autoconfig.yml', [desc])
else:
settings = self._load_settings_object(yaml_data)
self._dirty = False
settings = self._handle_migrations(settings)
self._validate(settings)
self._build_values(settings)

View File

@ -330,6 +330,20 @@ class TestYaml:
}
}
def test_read_newer_version(self, yaml, autoconfig):
autoconfig.write_toplevel({
'config_version': 999,
'settings': {},
})
with pytest.raises(configexc.ConfigFileErrors) as excinfo:
yaml.load()
assert len(excinfo.value.errors) == 1
error = excinfo.value.errors[0]
assert error.text == "While reading"
msg = "Can't read config from incompatible newer version"
assert error.exception == msg
def test_oserror(self, yaml, autoconfig):
autoconfig.fobj.ensure()
autoconfig.fobj.chmod(0)