From 3e0d49a4b32bd586e8bcc80c18df01ea92dabbe3 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 21 Sep 2017 19:57:54 +0200 Subject: [PATCH] Add TestYaml class to test_configfiles --- tests/unit/config/test_configfiles.py | 236 +++++++++++++------------- 1 file changed, 118 insertions(+), 118 deletions(-) diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index c7f611f44..f1b7271f6 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -61,142 +61,142 @@ def test_state_config(fake_save_manager, data_tmpdir, assert statefile.read_text('utf-8') == new_data -@pytest.mark.parametrize('old_config', [ - 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') +class TestYaml: - yaml = configfiles.YamlConfig() - yaml.load() + @pytest.mark.parametrize('old_config', [ + 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['tabs.show'] = 'never' - - yaml._save() - - if not insert and old_config is None: - lines = [] - else: - text = autoconfig.read_text('utf-8') - lines = text.splitlines() + yaml = configfiles.YamlConfig() + yaml.load() if insert: - assert lines[0].startswith('# DO NOT edit this file by hand,') - assert 'config_version: {}'.format(yaml.VERSION) in lines + yaml['tabs.show'] = 'never' - 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 'magenta' in (old_config or ''): # pylint: disable=superfluous-parens - assert ' colors.hints.fg: magenta' in lines - if insert: - assert ' tabs.show: never' in lines - assert ' hello:' not in 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 -@pytest.mark.parametrize('old_config', [ - 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') + print(lines) - yaml = configfiles.YamlConfig() - yaml.load() + # WORKAROUND for https://github.com/PyCQA/pylint/issues/574 + # 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 - assert key in yaml - assert yaml[key] == value + @pytest.mark.parametrize('old_config', [ + 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_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.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 = configfiles.YamlConfig() 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 + yaml[key] = value + assert key in yaml + assert yaml[key] == value + yaml._save() -def test_yaml_oserror(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 = configfiles.YamlConfig() 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 + assert key in yaml + assert yaml[key] == value + + @pytest.mark.parametrize('old_config', [ + 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: