Use a real config object in unit tests

This commit is contained in:
Florian Bruhin 2017-07-02 21:07:38 +02:00
parent 5aac991446
commit 725ffef5f3
6 changed files with 43 additions and 60 deletions

View File

@ -365,7 +365,7 @@ class Config(QObject):
Those contain the type, default value, etc. Those contain the type, default value, etc.
_values: A dict mapping setting names to their values. _values: A dict mapping setting names to their values.
_mutables: A list of mutable objects to be checked for changes. _mutables: A list of mutable objects to be checked for changes.
_yaml: A YamlConfig object. _yaml: A YamlConfig object or None.
Signals: Signals:
changed: Emitted with the option name when an option changed. changed: Emitted with the option name when an option changed.
@ -373,12 +373,12 @@ class Config(QObject):
changed = pyqtSignal(str) changed = pyqtSignal(str)
def __init__(self, parent=None): def __init__(self, yaml_config, parent=None):
super().__init__(parent) super().__init__(parent)
self.options = {} self.options = {}
self._values = {} self._values = {}
self._mutables = [] self._mutables = []
self._yaml = configfiles.YamlConfig() self._yaml = yaml_config
def _changed(self, name, value): def _changed(self, name, value):
"""Emit changed signal and log change.""" """Emit changed signal and log change."""
@ -605,7 +605,8 @@ def init(parent=None):
""" """
configdata.init() configdata.init()
config = Config(parent) yaml_config = configfiles.YamlConfig()
config = Config(yaml_config=yaml_config, parent=parent)
config.read_configdata() config.read_configdata()
objreg.register('config', config) objreg.register('config', config)

View File

@ -2070,6 +2070,7 @@ bindings.default:
'prompt', 'caret', 'register'] 'prompt', 'caret', 'register']
valtype: valtype:
name: Dict name: Dict
none_ok: true
keytype: Key keytype: Key
valtype: Command valtype: Command
desc: >- desc: >-

View File

@ -38,7 +38,7 @@ import pytest
import py.path # pylint: disable=no-name-in-module import py.path # pylint: disable=no-name-in-module
import helpers.stubs as stubsmod import helpers.stubs as stubsmod
from qutebrowser.config import config from qutebrowser.config import config, configdata
from qutebrowser.utils import objreg, standarddir from qutebrowser.utils import objreg, standarddir
from qutebrowser.browser.webkit import cookies from qutebrowser.browser.webkit import cookies
from qutebrowser.misc import savemanager from qutebrowser.misc import savemanager
@ -205,13 +205,15 @@ def cmdline_test(request):
@pytest.fixture @pytest.fixture
def config_stub(stubs, monkeypatch): def config_stub(stubs, monkeypatch):
"""Fixture which provides a fake config object.""" """Fixture which provides a fake config object."""
conf = stubs.ConfigStub() configdata.init()
conf = config.Config(yaml_config=None)
conf.read_configdata()
monkeypatch.setattr(config, 'instance', conf) monkeypatch.setattr(config, 'instance', conf)
container = config.ConfigContainer(conf) container = config.ConfigContainer(conf)
monkeypatch.setattr(config, 'val', container) monkeypatch.setattr(config, 'val', container)
conf.val = container conf.val = container # For easier use in tests
return conf return conf

View File

@ -412,43 +412,6 @@ class StatusBarCommandStub(QLineEdit):
return self.text()[0] return self.text()[0]
class ConfigStub(QObject):
"""Stub for the config module.
Attributes:
data: The config data to return.
val: A ConfigContainer
"""
changed = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
if configdata.DATA is None:
configdata.init()
self.data = {}
self.val = None
def __getitem__(self, name):
return self.section(name)
def get(self, name):
"""Get a value from the config."""
try:
return self.data[name]
except KeyError:
raise configexc.NoOptionError(name)
def set_obj(self, name, value):
"""Set a value in the config."""
try:
self.data[name] = value
except KeyError:
raise configexc.NoOptionError(name)
self.changed.emit(name)
class UrlMarkManagerStub(QObject): class UrlMarkManagerStub(QObject):
"""Stub for the quickmark-manager or bookmark-manager object.""" """Stub for the quickmark-manager or bookmark-manager object."""

View File

@ -23,6 +23,7 @@ import copy
import pytest import pytest
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
import qutebrowser.app # To register commands
from qutebrowser.config import config, configdata, configexc from qutebrowser.config import config, configdata, configexc
@ -85,18 +86,28 @@ class TestKeyConfig:
@pytest.fixture @pytest.fixture
def keyconf(self, config_stub): def keyconf(self, config_stub):
config_stub.val.aliases = {}
return config.KeyConfig(config_stub) return config.KeyConfig(config_stub)
@pytest.mark.parametrize('commands, expected', [ @pytest.mark.parametrize('commands, expected', [
# Unbinding default key # Unbinding default key
({'a': None}, {'b': 'bar'}), ({'a': None}, {'b': 'message-info bar'}),
# Additional binding # Additional binding
({'c': 'baz'}, {'a': 'foo', 'b': 'bar', 'c': 'baz'}), ({'c': 'message-info baz'},
{'a': 'message-info foo', 'b': 'message-info bar', 'c': 'message-info baz'}),
# Unbinding unknown key # Unbinding unknown key
({'x': None}, {'a': 'foo', 'b': 'bar'}), ({'x': None}, {'a': 'message-info foo', 'b': 'message-info bar'}),
]) ])
def test_get_bindings_for(self, keyconf, config_stub, commands, expected): def test_get_bindings_for(self, keyconf, config_stub, commands, expected):
orig_default_bindings = {'normal': {'a': 'foo', 'b': 'bar'}} orig_default_bindings = {'normal': {'a': 'message-info foo',
'b': 'message-info bar'},
'insert': {},
'hint': {},
'passthrough': {},
'command': {},
'prompt': {},
'caret': {},
'register': {}}
config_stub.val.bindings.default = copy.deepcopy(orig_default_bindings) config_stub.val.bindings.default = copy.deepcopy(orig_default_bindings)
config_stub.val.bindings.commands = {'normal': commands} config_stub.val.bindings.commands = {'normal': commands}
bindings = keyconf.get_bindings_for('normal') bindings = keyconf.get_bindings_for('normal')
@ -107,13 +118,17 @@ class TestKeyConfig:
@pytest.mark.parametrize('bindings, expected', [ @pytest.mark.parametrize('bindings, expected', [
# Simple # Simple
({'a': 'foo', 'b': 'bar'}, {'foo': ['a'], 'bar': ['b']}), ({'a': 'message-info foo', 'b': 'message-info bar'},
{'message-info foo': ['a'], 'message-info bar': ['b']}),
# Multiple bindings # Multiple bindings
({'a': 'foo', 'b': 'foo'}, {'foo': ['b', 'a']}), ({'a': 'message-info foo', 'b': 'message-info foo'},
# With special keys (should be listed last) {'message-info foo': ['b', 'a']}),
({'a': 'foo', '<Escape>': 'foo'}, {'foo': ['a', '<Escape>']}), # With special keys (should be listed last and normalized)
({'a': 'message-info foo', '<Escape>': 'message-info foo'},
{'message-info foo': ['a', '<escape>']}),
# Chained command # Chained command
({'a': 'foo ;; bar'}, {'foo': ['a'], 'bar': ['a']}), ({'a': 'message-info foo ;; message-info bar'},
{'message-info foo': ['a'], 'message-info bar': ['a']}),
]) ])
def test_get_reverse_bindings_for(self, keyconf, config_stub, bindings, def test_get_reverse_bindings_for(self, keyconf, config_stub, bindings,
expected): expected):
@ -148,9 +163,9 @@ class StyleObj(QObject):
def test_get_stylesheet(config_stub): def test_get_stylesheet(config_stub):
config_stub.val.colors.completion.bg = 'magenta' config_stub.val.colors.hints.fg = 'magenta'
observer = config.StyleSheetObserver( observer = config.StyleSheetObserver(
StyleObj(), stylesheet="{{ conf.colors.completion.bg }}") StyleObj(), stylesheet="{{ conf.colors.hints.fg }}")
assert observer._get_stylesheet() == 'magenta' assert observer._get_stylesheet() == 'magenta'
@ -159,8 +174,8 @@ def test_get_stylesheet(config_stub):
@pytest.mark.parametrize('update', [True, False]) @pytest.mark.parametrize('update', [True, False])
def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot, def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot,
config_stub, caplog): config_stub, caplog):
config_stub.val.colors.completion.fg = 'magenta' config_stub.val.colors.hints.fg = 'magenta'
stylesheet = "{{ conf.colors.completion.fg }}" stylesheet = "{{ conf.colors.hints.fg }}"
with caplog.at_level(9): # VDEBUG with caplog.at_level(9): # VDEBUG
if stylesheet_param: if stylesheet_param:
@ -171,8 +186,7 @@ def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot,
obj = StyleObj(stylesheet) obj = StyleObj(stylesheet)
config.set_register_stylesheet(obj, update=update) config.set_register_stylesheet(obj, update=update)
assert len(caplog.records) == 1 assert caplog.records[-1].message == 'stylesheet for StyleObj: magenta'
assert caplog.records[0].message == 'stylesheet for StyleObj: magenta'
assert obj.rendered_stylesheet == 'magenta' assert obj.rendered_stylesheet == 'magenta'
@ -180,7 +194,7 @@ def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot,
with qtbot.waitSignal(obj.destroyed): with qtbot.waitSignal(obj.destroyed):
obj.deleteLater() obj.deleteLater()
config_stub.val.colors.completion.fg = 'yellow' config_stub.val.colors.hints.fg = 'yellow'
if delete or not update: if delete or not update:
expected = 'magenta' expected = 'magenta'

View File

@ -1036,7 +1036,9 @@ class TestCommand:
def patch_aliases(self, config_stub): def patch_aliases(self, config_stub):
"""Patch the aliases setting.""" """Patch the aliases setting."""
# FIXME:conf use the real config so we can test the RecursionError # FIXME:conf use the real config so we can test the RecursionError
configtypes.Command.unvalidated = True
config_stub.val.aliases = {'alias': 'cmd1'} config_stub.val.aliases = {'alias': 'cmd1'}
configtypes.Command.unvalidated = False
@pytest.fixture @pytest.fixture
def klass(self): def klass(self):