Add TestYaml class to test_configfiles
This commit is contained in:
parent
f821fb793a
commit
3e0d49a4b3
@ -61,142 +61,142 @@ def test_state_config(fake_save_manager, data_tmpdir,
|
|||||||
assert statefile.read_text('utf-8') == new_data
|
assert statefile.read_text('utf-8') == new_data
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('old_config', [
|
class TestYaml:
|
||||||
None,
|
|
||||||
'global:\n colors.hints.fg: magenta',
|
|
||||||
# Unknown key
|
|
||||||
'global:\n hello: world',
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize('insert', [True, False])
|
|
||||||
def test_yaml_config(fake_save_manager, config_tmpdir, old_config, insert):
|
|
||||||
autoconfig = config_tmpdir / 'autoconfig.yml'
|
|
||||||
if old_config is not None:
|
|
||||||
autoconfig.write_text(old_config, 'utf-8')
|
|
||||||
|
|
||||||
yaml = configfiles.YamlConfig()
|
@pytest.mark.parametrize('old_config', [
|
||||||
yaml.load()
|
None,
|
||||||
|
'global:\n colors.hints.fg: magenta',
|
||||||
|
# Unknown key
|
||||||
|
'global:\n hello: world',
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize('insert', [True, False])
|
||||||
|
def test_yaml_config(self, fake_save_manager, config_tmpdir,
|
||||||
|
old_config, insert):
|
||||||
|
autoconfig = config_tmpdir / 'autoconfig.yml'
|
||||||
|
if old_config is not None:
|
||||||
|
autoconfig.write_text(old_config, 'utf-8')
|
||||||
|
|
||||||
if insert:
|
yaml = configfiles.YamlConfig()
|
||||||
yaml['tabs.show'] = 'never'
|
yaml.load()
|
||||||
|
|
||||||
yaml._save()
|
|
||||||
|
|
||||||
if not insert and old_config is None:
|
|
||||||
lines = []
|
|
||||||
else:
|
|
||||||
text = autoconfig.read_text('utf-8')
|
|
||||||
lines = text.splitlines()
|
|
||||||
|
|
||||||
if insert:
|
if insert:
|
||||||
assert lines[0].startswith('# DO NOT edit this file by hand,')
|
yaml['tabs.show'] = 'never'
|
||||||
assert 'config_version: {}'.format(yaml.VERSION) in lines
|
|
||||||
|
|
||||||
assert 'global:' in lines
|
yaml._save()
|
||||||
|
|
||||||
print(lines)
|
if not insert and old_config is None:
|
||||||
|
lines = []
|
||||||
|
else:
|
||||||
|
text = autoconfig.read_text('utf-8')
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
# WORKAROUND for https://github.com/PyCQA/pylint/issues/574
|
if insert:
|
||||||
if 'magenta' in (old_config or ''): # pylint: disable=superfluous-parens
|
assert lines[0].startswith('# DO NOT edit this file by hand,')
|
||||||
assert ' colors.hints.fg: magenta' in lines
|
assert 'config_version: {}'.format(yaml.VERSION) in lines
|
||||||
if insert:
|
|
||||||
assert ' tabs.show: never' in lines
|
|
||||||
assert ' hello:' not in lines
|
|
||||||
|
|
||||||
|
assert 'global:' in lines
|
||||||
|
|
||||||
@pytest.mark.parametrize('old_config', [
|
print(lines)
|
||||||
None,
|
|
||||||
'global:\n colors.hints.fg: magenta',
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize('key, value', [
|
|
||||||
('colors.hints.fg', 'green'),
|
|
||||||
('colors.hints.bg', None),
|
|
||||||
('confirm_quit', True),
|
|
||||||
('confirm_quit', False),
|
|
||||||
])
|
|
||||||
def test_yaml_config_changed(fake_save_manager, config_tmpdir, old_config,
|
|
||||||
key, value):
|
|
||||||
autoconfig = config_tmpdir / 'autoconfig.yml'
|
|
||||||
if old_config is not None:
|
|
||||||
autoconfig.write_text(old_config, 'utf-8')
|
|
||||||
|
|
||||||
yaml = configfiles.YamlConfig()
|
# WORKAROUND for https://github.com/PyCQA/pylint/issues/574
|
||||||
yaml.load()
|
# pylint: disable=superfluous-parens
|
||||||
|
if 'magenta' in (old_config or ''):
|
||||||
|
assert ' colors.hints.fg: magenta' in lines
|
||||||
|
if insert:
|
||||||
|
assert ' tabs.show: never' in lines
|
||||||
|
assert ' hello:' not in lines
|
||||||
|
|
||||||
yaml[key] = value
|
@pytest.mark.parametrize('old_config', [
|
||||||
assert key in yaml
|
None,
|
||||||
assert yaml[key] == value
|
'global:\n colors.hints.fg: magenta',
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize('key, value', [
|
||||||
|
('colors.hints.fg', 'green'),
|
||||||
|
('colors.hints.bg', None),
|
||||||
|
('confirm_quit', True),
|
||||||
|
('confirm_quit', False),
|
||||||
|
])
|
||||||
|
def test_changed(self, fake_save_manager, config_tmpdir, old_config,
|
||||||
|
key, value):
|
||||||
|
autoconfig = config_tmpdir / 'autoconfig.yml'
|
||||||
|
if old_config is not None:
|
||||||
|
autoconfig.write_text(old_config, 'utf-8')
|
||||||
|
|
||||||
yaml._save()
|
yaml = configfiles.YamlConfig()
|
||||||
|
|
||||||
yaml = configfiles.YamlConfig()
|
|
||||||
yaml.load()
|
|
||||||
|
|
||||||
assert key in yaml
|
|
||||||
assert yaml[key] == value
|
|
||||||
|
|
||||||
|
|
||||||
@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"),
|
|
||||||
('foo: 42', 'While loading data',
|
|
||||||
"Toplevel object does not contain 'global' key"),
|
|
||||||
('42', 'While loading data', "Toplevel object is not a dict"),
|
|
||||||
])
|
|
||||||
def test_yaml_config_invalid(fake_save_manager, config_tmpdir,
|
|
||||||
line, text, exception):
|
|
||||||
autoconfig = config_tmpdir / 'autoconfig.yml'
|
|
||||||
autoconfig.write_text(line, 'utf-8', ensure=True)
|
|
||||||
|
|
||||||
yaml = configfiles.YamlConfig()
|
|
||||||
|
|
||||||
with pytest.raises(configexc.ConfigFileErrors) as excinfo:
|
|
||||||
yaml.load()
|
yaml.load()
|
||||||
|
|
||||||
assert len(excinfo.value.errors) == 1
|
yaml[key] = value
|
||||||
error = excinfo.value.errors[0]
|
assert key in yaml
|
||||||
assert error.text == text
|
assert yaml[key] == value
|
||||||
assert str(error.exception).splitlines()[0] == exception
|
|
||||||
assert error.traceback is None
|
|
||||||
|
|
||||||
|
yaml._save()
|
||||||
|
|
||||||
def test_yaml_oserror(fake_save_manager, config_tmpdir):
|
yaml = configfiles.YamlConfig()
|
||||||
autoconfig = config_tmpdir / 'autoconfig.yml'
|
|
||||||
autoconfig.ensure()
|
|
||||||
autoconfig.chmod(0)
|
|
||||||
if os.access(str(autoconfig), os.R_OK):
|
|
||||||
# Docker container or similar
|
|
||||||
pytest.skip("File was still readable")
|
|
||||||
|
|
||||||
yaml = configfiles.YamlConfig()
|
|
||||||
with pytest.raises(configexc.ConfigFileErrors) as excinfo:
|
|
||||||
yaml.load()
|
yaml.load()
|
||||||
|
|
||||||
assert len(excinfo.value.errors) == 1
|
assert key in yaml
|
||||||
error = excinfo.value.errors[0]
|
assert yaml[key] == value
|
||||||
assert error.text == "While reading"
|
|
||||||
assert isinstance(error.exception, OSError)
|
@pytest.mark.parametrize('old_config', [
|
||||||
assert error.traceback is None
|
None,
|
||||||
|
'global:\n colors.hints.fg: magenta',
|
||||||
|
])
|
||||||
|
def test_unchanged(self, 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"),
|
||||||
|
('foo: 42', 'While loading data',
|
||||||
|
"Toplevel object does not contain 'global' key"),
|
||||||
|
('42', 'While loading data', "Toplevel object is not a dict"),
|
||||||
|
])
|
||||||
|
def test_invalid(self, fake_save_manager, config_tmpdir,
|
||||||
|
line, text, exception):
|
||||||
|
autoconfig = config_tmpdir / 'autoconfig.yml'
|
||||||
|
autoconfig.write_text(line, 'utf-8', ensure=True)
|
||||||
|
|
||||||
|
yaml = configfiles.YamlConfig()
|
||||||
|
|
||||||
|
with pytest.raises(configexc.ConfigFileErrors) as excinfo:
|
||||||
|
yaml.load()
|
||||||
|
|
||||||
|
assert len(excinfo.value.errors) == 1
|
||||||
|
error = excinfo.value.errors[0]
|
||||||
|
assert error.text == text
|
||||||
|
assert str(error.exception).splitlines()[0] == exception
|
||||||
|
assert error.traceback is None
|
||||||
|
|
||||||
|
def test_oserror(self, fake_save_manager, config_tmpdir):
|
||||||
|
autoconfig = config_tmpdir / 'autoconfig.yml'
|
||||||
|
autoconfig.ensure()
|
||||||
|
autoconfig.chmod(0)
|
||||||
|
if os.access(str(autoconfig), os.R_OK):
|
||||||
|
# Docker container or similar
|
||||||
|
pytest.skip("File was still readable")
|
||||||
|
|
||||||
|
yaml = configfiles.YamlConfig()
|
||||||
|
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"
|
||||||
|
assert isinstance(error.exception, OSError)
|
||||||
|
assert error.traceback is None
|
||||||
|
|
||||||
|
|
||||||
class TestConfigPy:
|
class TestConfigPy:
|
||||||
|
Loading…
Reference in New Issue
Block a user