From 7cad8f41f2d80806f15f4e3983996fcf5324ebcd Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 21 Sep 2017 16:29:40 +0200 Subject: [PATCH] Remove unknown YAML data from config I considered introducing another list of deleted options (or a "deleted: True" in configdata.yml), similar to what we had with the old config. However, let's take the easier route and just delete everything we don't know from configdata.yml. If someone edits it by hand, it's their fault :P See #2772, #2847 --- qutebrowser/config/configfiles.py | 8 +++++++- tests/unit/config/test_config.py | 9 ++------- tests/unit/config/test_configfiles.py | 3 +++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py index e26a03454..a155726a8 100644 --- a/qutebrowser/config/configfiles.py +++ b/qutebrowser/config/configfiles.py @@ -30,7 +30,7 @@ import yaml from PyQt5.QtCore import QSettings import qutebrowser -from qutebrowser.config import configexc, config +from qutebrowser.config import configexc, config, configdata from qutebrowser.utils import standarddir, utils, qtutils @@ -153,6 +153,12 @@ class YamlConfig: "'global' object is not a dict") raise configexc.ConfigFileErrors('autoconfig.yml', [desc]) + # Delete unknown values + # (e.g. options which were removed from configdata.yml) + for name in list(global_obj): + if name not in configdata.DATA: + del global_obj[name] + self._values = global_obj self._dirty = False diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index 6dff3c30a..36dc90c10 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -932,14 +932,9 @@ def test_early_init(init_patch, config_tmpdir, caplog, fake_args, expected_errors.append( "Errors occurred while reading config.py:\n" " While setting 'foo': No option 'foo'") - if invalid_yaml and (load_autoconfig or not config_py): + if invalid_yaml == '42' and (load_autoconfig or not config_py): error = "Errors occurred while reading autoconfig.yml:\n" - if invalid_yaml == '42': - error += " While loading data: Toplevel object is not a dict" - elif invalid_yaml == 'unknown': - error += " Error: No option 'colors.foobar'" - else: - assert False, invalid_yaml + error += " While loading data: Toplevel object is not a dict" expected_errors.append(error) actual_errors = [str(err) for err in config._init_errors] diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index 219b92395..fdfda4033 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -57,6 +57,8 @@ def test_state_config(fake_save_manager, data_tmpdir, @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): @@ -91,6 +93,7 @@ def test_yaml_config(fake_save_manager, config_tmpdir, old_config, insert): assert ' colors.hints.fg: magenta' in lines if insert: assert ' tabs.show: never' in lines + assert ' hello:' not in lines @pytest.mark.parametrize('old_config', [