From 7b192d426ebe95c4f9054f1ffbac41ece0c039ef Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 19 Sep 2017 15:30:28 +0200 Subject: [PATCH] add unit test and fix issues with it --- qutebrowser/config/configfiles.py | 3 +- tests/unit/config/test_configfiles.py | 40 +++++++++++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py index 9bad78394..89c679976 100644 --- a/qutebrowser/config/configfiles.py +++ b/qutebrowser/config/configfiles.py @@ -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]) diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index 73b091293..fdf709744 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -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"),