add unit test and fix issues with it

This commit is contained in:
Felix Van der Jeugt 2017-09-19 15:30:28 +02:00
parent 0e743f0e09
commit 7b192d426e
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 33 additions and 10 deletions

View File

@ -85,6 +85,7 @@ class YamlConfig:
self._filename = os.path.join(standarddir.config(auto=True),
'autoconfig.yml')
self.values = {}
self._initial_values = {}
def init_save_manager(self, save_manager):
"""Make sure the config gets saved properly.
@ -119,7 +120,7 @@ class YamlConfig:
with open(self._filename, 'r', encoding='utf-8') as f:
yaml_data = utils.yaml_load(f)
except FileNotFoundError:
return
return {}
except OSError as e:
desc = configexc.ConfigErrorDesc("While reading", e)
raise configexc.ConfigFileErrors('autoconfig.yml', [desc])

View File

@ -72,16 +72,17 @@ def test_yaml_config(fake_save_manager, config_tmpdir, old_config, insert):
yaml._save()
text = autoconfig.read_text('utf-8')
lines = text.splitlines()
print(lines)
assert lines[0].startswith('# DO NOT edit this file by hand,')
assert 'config_version: {}'.format(yaml.VERSION) in lines
if old_config is None and not insert:
assert 'global: {}' in lines
if not insert and old_config is None:
lines = []
else:
text = autoconfig.read_text('utf-8')
lines = text.splitlines()
print(lines)
if insert:
assert lines[0].startswith('# DO NOT edit this file by hand,')
assert 'config_version: {}'.format(yaml.VERSION) in lines
assert 'global:' in lines
# WORKAROUND for https://github.com/PyCQA/pylint/issues/574
@ -91,6 +92,27 @@ def test_yaml_config(fake_save_manager, config_tmpdir, old_config, insert):
assert ' tabs.show: never' in lines
@pytest.mark.parametrize('old_config', [
None,
'global:\n colors.hints.fg: magenta',
])
def test_yaml_config_unchanged(fake_save_manager, config_tmpdir, old_config):
autoconfig = config_tmpdir / 'autoconfig.yml'
mtime = None
if old_config is not None:
autoconfig.write_text(old_config, 'utf-8')
mtime = autoconfig.stat().mtime
yaml = configfiles.YamlConfig()
yaml.load()
yaml._save()
if old_config is None:
assert not autoconfig.exists()
else:
assert autoconfig.stat().mtime == mtime
@pytest.mark.parametrize('line, text, exception', [
('%', 'While parsing', 'while scanning a directive'),
('global: 42', 'While loading data', "'global' object is not a dict"),