From a5c8a52dd5215dc748e7c2fbc856d7ca053ffb04 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 2 Jul 2017 15:09:18 +0200 Subject: [PATCH] Update config_stub for tests --- tests/helpers/fixtures.py | 50 +++++++++++++++------ tests/helpers/stubs.py | 63 +++++---------------------- tests/unit/config/test_configdata.py | 10 ++--- tests/unit/config/test_configtypes.py | 17 +++----- 4 files changed, 54 insertions(+), 86 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 3af7195f5..935b71709 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -203,31 +203,53 @@ def cmdline_test(request): @pytest.fixture -def config_stub(stubs): +def config_stub(stubs, monkeypatch): """Fixture which provides a fake config object.""" - stub = stubs.ConfigStub() - objreg.register('config', stub) - yield stub - objreg.delete('config') + conf = stubs.ConfigStub() + monkeypatch.setattr(config, 'instance', conf) + + container = config.ConfigContainer(conf) + monkeypatch.setattr(config, 'val', container) + + conf.val = container + return conf @pytest.fixture def default_config(): """Fixture that provides and registers an empty default config object.""" - config_obj = config.ConfigManager() - config_obj.read(configdir=None, fname=None, relaxed=True) - objreg.register('config', config_obj) - yield config_obj - objreg.delete('config') + # FIXME:conf + return None @pytest.fixture def key_config_stub(stubs): """Fixture which provides a fake key config object.""" - stub = stubs.KeyConfigStub() - objreg.register('key-config', stub) - yield stub - objreg.delete('key-config') + # FIXME:conf + # class KeyConfigStub: + # + # """Stub for the key-config object.""" + # + # def __init__(self): + # self.bindings = {} + # + # def get_bindings_for(self, section): + # return self.bindings.get(section) + # + # def set_bindings_for(self, section, bindings): + # self.bindings[section] = bindings + # + # def get_reverse_bindings_for(self, section): + # """Get a dict of commands to a list of bindings for the section.""" + # cmd_to_keys = collections.defaultdict(list) + # for key, cmd in self.bindings[section].items(): + # # put special bindings last + # if utils.is_special_key(key): + # cmd_to_keys[cmd].append(key) + # else: + # cmd_to_keys[cmd].insert(0, key) + # return cmd_to_keys + return None @pytest.fixture diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 3d7805f9a..cb9e55607 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -418,76 +418,33 @@ class ConfigStub(QObject): Attributes: data: The config data to return. + val: A ConfigContainer """ - # FIXME:conf refactor... - - changed = pyqtSignal(str, str) + changed = pyqtSignal(str) def __init__(self, parent=None): - """Constructor. - - Args: - signal: The signal to use for self.changed. - """ super().__init__(parent) self.data = {} + self.val = None def __getitem__(self, name): return self.section(name) - def section(self, name): - """Get a section from the config. - - Args: - name: The section name to get. - - Return: - The section as dict. - """ - return self.data[name] - - def get(self, sect, opt, raw=True): + def get(self, name): """Get a value from the config.""" - data = self.data[sect] try: - return data[opt] + return self.data[name] except KeyError: - raise configexc.NoOptionError(opt, sect) + raise configexc.NoOptionError(name) - def set(self, sect, opt, value): + def set_obj(self, name, value): """Set a value in the config.""" - data = self.data[sect] try: - data[opt] = value - self.changed.emit(sect, opt) + self.data[name] = value + self.changed.emit(name) except KeyError: - raise configexc.NoOptionError(opt, sect) - - -class KeyConfigStub: - - """Stub for the key-config object.""" - - def __init__(self): - self.bindings = {} - - def get_bindings_for(self, section): - return self.bindings.get(section) - - def set_bindings_for(self, section, bindings): - self.bindings[section] = bindings - - def get_reverse_bindings_for(self, section): - """Get a dict of commands to a list of bindings for the section.""" - cmd_to_keys = collections.defaultdict(list) - for key, cmd in self.bindings[section].items(): - # put special bindings last - if utils.is_special_key(key): - cmd_to_keys[cmd].append(key) - else: - cmd_to_keys[cmd].insert(0, key) - return cmd_to_keys + raise configexc.NoOptionError(name) class UrlMarkManagerStub(QObject): diff --git a/tests/unit/config/test_configdata.py b/tests/unit/config/test_configdata.py index 9363840e2..2f3b46e57 100644 --- a/tests/unit/config/test_configdata.py +++ b/tests/unit/config/test_configdata.py @@ -19,23 +19,19 @@ """Tests for qutebrowser.config.configdata.""" import textwrap -import types import yaml import pytest # To run cmdutils.register decorators from qutebrowser import app -from qutebrowser.config import config, configdata, configtypes +from qutebrowser.config import configdata, configtypes from qutebrowser.utils import usertypes -def test_init(monkeypatch): +def test_init(config_stub): """Test reading the default yaml file and validating the values.""" - # FIXME:conf use some kind of config_stub here - ns = types.SimpleNamespace() - ns.aliases = {} - monkeypatch.setattr('qutebrowser.config.config.val', ns) + config_stub.val.aliases = {} configdata.init() assert isinstance(configdata.DATA, dict) assert 'ignore_case' in configdata.DATA diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index f4bd98c46..3e83da403 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -27,7 +27,6 @@ import collections import itertools import warnings import inspect -import types import functools import pytest @@ -209,12 +208,9 @@ class TestAll: yield member @pytest.fixture(autouse=True) - def patch(self, monkeypatch): + def patch_aliases(self, config_stub): """Patch aliases so Command works.""" - # FIXME:conf use some kind of config_stub here - ns = types.SimpleNamespace() - ns.aliases = {} - monkeypatch.setattr('qutebrowser.config.config.val', ns) + config_stub.val.aliases = {} @pytest.fixture(params=list(gen_classes())) def klass(self, request): @@ -1037,13 +1033,10 @@ class TestCommand: monkeypatch.setattr('qutebrowser.commands.runners.cmdutils', cmd_utils) @pytest.fixture(autouse=True) - def patch_aliases(self, monkeypatch): + def patch_aliases(self, config_stub): """Patch the aliases setting.""" - # FIXME:conf use some kind of config_stub here - # also remove the no branch pragma from configtypes.Command then - ns = types.SimpleNamespace() - ns.aliases = {'alias': 'cmd1'} - monkeypatch.setattr('qutebrowser.config.config.val', ns) + # FIXME:conf use the real config so we can test the RecursionError + config_stub.val.aliases = {'alias': 'cmd1'} @pytest.fixture def klass(self):