From 38449e3e2bc87d01b3173ae2bbe0bc2792775f37 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 26 Sep 2017 06:41:55 +0200 Subject: [PATCH] Make sure the autoconfig.yml is saved periodically Fixes #2982 --- qutebrowser/config/configfiles.py | 11 +++++++---- qutebrowser/config/configinit.py | 1 + tests/unit/config/test_configfiles.py | 6 ++++-- tests/unit/config/test_configinit.py | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py index a017d025d..7eba211f6 100644 --- a/qutebrowser/config/configfiles.py +++ b/qutebrowser/config/configfiles.py @@ -29,7 +29,7 @@ import configparser import contextlib import yaml -from PyQt5.QtCore import QSettings +from PyQt5.QtCore import pyqtSignal, QObject, QSettings import qutebrowser from qutebrowser.config import configexc, config, configdata @@ -72,7 +72,7 @@ class StateConfig(configparser.ConfigParser): self.write(f) -class YamlConfig: +class YamlConfig(QObject): """A config stored on disk as YAML file. @@ -81,8 +81,10 @@ class YamlConfig: """ VERSION = 1 + changed = pyqtSignal() - def __init__(self): + def __init__(self, parent=None): + super().__init__(parent) self._filename = os.path.join(standarddir.config(auto=True), 'autoconfig.yml') self._values = {} @@ -94,12 +96,13 @@ class YamlConfig: We do this outside of __init__ because the config gets created before the save_manager exists. """ - save_manager.add_saveable('yaml-config', self._save) + save_manager.add_saveable('yaml-config', self._save, self.changed) def __getitem__(self, name): return self._values[name] def __setitem__(self, name, value): + self.changed.emit() self._dirty = True self._values[name] = value diff --git a/qutebrowser/config/configinit.py b/qutebrowser/config/configinit.py index a64438b83..3ec98a293 100644 --- a/qutebrowser/config/configinit.py +++ b/qutebrowser/config/configinit.py @@ -43,6 +43,7 @@ def early_init(args): config.instance = config.Config(yaml_config=yaml_config) config.val = config.ConfigContainer(config.instance) config.key_instance = config.KeyConfig(config.instance) + yaml_config.setParent(config.instance) for cf in config.change_filters: cf.validate() diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index 7ce29484a..d987d7442 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -127,7 +127,7 @@ class TestYaml: ('confirm_quit', True), ('confirm_quit', False), ]) - def test_changed(self, config_tmpdir, old_config, key, value): + def test_changed(self, qtbot, config_tmpdir, old_config, key, value): autoconfig = config_tmpdir / 'autoconfig.yml' if old_config is not None: autoconfig.write_text(old_config, 'utf-8') @@ -135,7 +135,9 @@ class TestYaml: yaml = configfiles.YamlConfig() yaml.load() - yaml[key] = value + with qtbot.wait_signal(yaml.changed): + yaml[key] = value + assert key in yaml assert yaml[key] == value diff --git a/tests/unit/config/test_configinit.py b/tests/unit/config/test_configinit.py index 7ec371a6d..1332499de 100644 --- a/tests/unit/config/test_configinit.py +++ b/tests/unit/config/test_configinit.py @@ -183,7 +183,7 @@ def test_late_init(init_patch, monkeypatch, fake_save_manager, fake_args, fake_save_manager.add_saveable.assert_any_call( 'state-config', unittest.mock.ANY) fake_save_manager.add_saveable.assert_any_call( - 'yaml-config', unittest.mock.ANY) + 'yaml-config', unittest.mock.ANY, unittest.mock.ANY) if errors: assert len(msgbox_mock.call_args_list) == 1 _call_posargs, call_kwargs = msgbox_mock.call_args_list[0]