Fix getting YAML values in test_configcommands.py

This commit is contained in:
Florian Bruhin 2018-02-19 19:34:30 +01:00
parent 75181e16fa
commit 0f907b1a77
2 changed files with 32 additions and 30 deletions

View File

@ -116,7 +116,7 @@ class Values:
"""Clear all customization for this value.""" """Clear all customization for this value."""
self._values = [] self._values = []
def _get_fallback(self): def _get_fallback(self, fallback):
"""Get the fallback global/default value.""" """Get the fallback global/default value."""
for scoped in self._values: for scoped in self._values:
if scoped.pattern is None: if scoped.pattern is None:
@ -124,7 +124,10 @@ class Values:
# default for a given URL. # default for a given URL.
return scoped.value return scoped.value
return self.opt.default if fallback:
return self.opt.default
else:
return UNSET
def get_for_url(self, url=None, *, fallback=True): def get_for_url(self, url=None, *, fallback=True):
"""Get a config value, falling back when needed. """Get a config value, falling back when needed.
@ -142,7 +145,7 @@ class Values:
if not fallback: if not fallback:
return UNSET return UNSET
return self._get_fallback() return self._get_fallback(fallback)
def get_for_pattern(self, pattern, *, fallback=True): def get_for_pattern(self, pattern, *, fallback=True):
"""Get a value only if it's been overridden for the given pattern. """Get a value only if it's been overridden for the given pattern.
@ -161,4 +164,4 @@ class Values:
if not fallback: if not fallback:
return UNSET return UNSET
return self._get_fallback() return self._get_fallback(fallback)

View File

@ -24,7 +24,7 @@ import unittest.mock
import pytest import pytest
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.config import configcommands from qutebrowser.config import configcommands, configutils
from qutebrowser.commands import cmdexc from qutebrowser.commands import cmdexc
from qutebrowser.utils import usertypes from qutebrowser.utils import usertypes
from qutebrowser.misc import objects from qutebrowser.misc import objects
@ -35,6 +35,14 @@ def commands(config_stub, key_config_stub):
return configcommands.ConfigCommands(config_stub, key_config_stub) return configcommands.ConfigCommands(config_stub, key_config_stub)
@pytest.fixture
def yaml_value(config_stub):
"""Fixture which provides a getter for a YAML value."""
def getter(option):
return config_stub._yaml._values[option].get_for_url(fallback=False)
return getter
class TestSet: class TestSet:
"""Tests for :set.""" """Tests for :set."""
@ -64,7 +72,7 @@ class TestSet:
['gvim', '-f', '{file}', '-c', 'normal {line}G{column0}l'], ['gvim', '-f', '{file}', '-c', 'normal {line}G{column0}l'],
'[emacs, "{}"]', ['emacs', '{}']), '[emacs, "{}"]', ['emacs', '{}']),
]) ])
def test_set_simple(self, monkeypatch, commands, config_stub, def test_set_simple(self, monkeypatch, commands, config_stub, yaml_value,
temp, option, old_value, inp, new_value): temp, option, old_value, inp, new_value):
"""Run ':set [-t] option value'. """Run ':set [-t] option value'.
@ -76,14 +84,10 @@ class TestSet:
commands.set(0, option, inp, temp=temp) commands.set(0, option, inp, temp=temp)
assert config_stub.get(option) == new_value assert config_stub.get(option) == new_value
assert yaml_value(option) == (configutils.UNSET if temp else new_value)
if temp:
assert option not in config_stub._yaml
else:
assert config_stub._yaml[option] == new_value
@pytest.mark.parametrize('temp', [True, False]) @pytest.mark.parametrize('temp', [True, False])
def test_set_temp_override(self, commands, config_stub, temp): def test_set_temp_override(self, commands, config_stub, yaml_value, temp):
"""Invoking :set twice. """Invoking :set twice.
:set url.auto_search dns :set url.auto_search dns
@ -96,7 +100,7 @@ class TestSet:
commands.set(0, 'url.auto_search', 'never', temp=True) commands.set(0, 'url.auto_search', 'never', temp=True)
assert config_stub.val.url.auto_search == 'never' assert config_stub.val.url.auto_search == 'never'
assert config_stub._yaml['url.auto_search'] == 'dns' assert yaml_value('url.auto_search') == 'dns'
def test_set_print(self, config_stub, commands, message_mock): def test_set_print(self, config_stub, commands, message_mock):
"""Run ':set -p url.auto_search never'. """Run ':set -p url.auto_search never'.
@ -177,13 +181,14 @@ class TestCycle:
# Value which is not in the list # Value which is not in the list
('red', 'green'), ('red', 'green'),
]) ])
def test_cycling(self, commands, config_stub, initial, expected): def test_cycling(self, commands, config_stub, yaml_value,
initial, expected):
"""Run ':set' with multiple values.""" """Run ':set' with multiple values."""
opt = 'colors.statusbar.normal.bg' opt = 'colors.statusbar.normal.bg'
config_stub.set_obj(opt, initial) config_stub.set_obj(opt, initial)
commands.config_cycle(opt, 'green', 'magenta', 'blue', 'yellow') commands.config_cycle(opt, 'green', 'magenta', 'blue', 'yellow')
assert config_stub.get(opt) == expected assert config_stub.get(opt) == expected
assert config_stub._yaml[opt] == expected assert yaml_value(opt) == expected
def test_different_representation(self, commands, config_stub): def test_different_representation(self, commands, config_stub):
"""When using a different representation, cycling should work. """When using a different representation, cycling should work.
@ -205,7 +210,7 @@ class TestCycle:
assert not config_stub.val.auto_save.session assert not config_stub.val.auto_save.session
commands.config_cycle('auto_save.session') commands.config_cycle('auto_save.session')
assert config_stub.val.auto_save.session assert config_stub.val.auto_save.session
assert config_stub._yaml['auto_save.session'] assert yaml_value('auto_save.session')
@pytest.mark.parametrize('args', [ @pytest.mark.parametrize('args', [
['url.auto_search'], ['url.auto_search', 'foo'] ['url.auto_search'], ['url.auto_search', 'foo']
@ -239,34 +244,28 @@ class TestUnsetAndClear:
"""Test :config-unset and :config-clear.""" """Test :config-unset and :config-clear."""
@pytest.mark.parametrize('temp', [True, False]) @pytest.mark.parametrize('temp', [True, False])
def test_unset(self, commands, config_stub, temp): def test_unset(self, commands, config_stub, yaml_value, temp):
name = 'tabs.show' name = 'tabs.show'
config_stub.set_obj(name, 'never', save_yaml=True) config_stub.set_obj(name, 'never', save_yaml=True)
commands.config_unset(name, temp=temp) commands.config_unset(name, temp=temp)
assert config_stub.get(name) == 'always' assert config_stub.get(name) == 'always'
if temp: assert yaml_value(name) == ('never' if temp else configutils.UNSET)
assert config_stub._yaml[name] == 'never'
else:
assert name not in config_stub._yaml
def test_unset_unknown_option(self, commands): def test_unset_unknown_option(self, commands):
with pytest.raises(cmdexc.CommandError, match="No option 'tabs'"): with pytest.raises(cmdexc.CommandError, match="No option 'tabs'"):
commands.config_unset('tabs') commands.config_unset('tabs')
@pytest.mark.parametrize('save', [True, False]) @pytest.mark.parametrize('save', [True, False])
def test_clear(self, commands, config_stub, save): def test_clear(self, commands, config_stub, yaml_value, save):
name = 'tabs.show' name = 'tabs.show'
config_stub.set_obj(name, 'never', save_yaml=True) config_stub.set_obj(name, 'never', save_yaml=True)
commands.config_clear(save=save) commands.config_clear(save=save)
assert config_stub.get(name) == 'always' assert config_stub.get(name) == 'always'
if save: assert yaml_value(name) == (configutils.UNSET if save else 'never')
assert name not in config_stub._yaml
else:
assert config_stub._yaml[name] == 'never'
class TestSource: class TestSource:
@ -453,7 +452,7 @@ class TestBind:
@pytest.mark.parametrize('command', ['nop', 'nope']) @pytest.mark.parametrize('command', ['nop', 'nope'])
def test_bind(self, commands, config_stub, no_bindings, key_config_stub, def test_bind(self, commands, config_stub, no_bindings, key_config_stub,
command): yaml_value, command):
"""Simple :bind test (and aliases).""" """Simple :bind test (and aliases)."""
config_stub.val.aliases = {'nope': 'nop'} config_stub.val.aliases = {'nope': 'nop'}
config_stub.val.bindings.default = no_bindings config_stub.val.bindings.default = no_bindings
@ -461,7 +460,7 @@ class TestBind:
commands.bind(0, 'a', command) commands.bind(0, 'a', command)
assert key_config_stub.get_command('a', 'normal') == command assert key_config_stub.get_command('a', 'normal') == command
yaml_bindings = config_stub._yaml['bindings.commands']['normal'] yaml_bindings = yaml_value('bindings.commands')['normal']
assert yaml_bindings['a'] == command assert yaml_bindings['a'] == command
@pytest.mark.parametrize('key, mode, expected', [ @pytest.mark.parametrize('key, mode, expected', [
@ -573,7 +572,7 @@ class TestBind:
('c', 'c'), # :bind then :unbind ('c', 'c'), # :bind then :unbind
('<Ctrl-X>', '<ctrl+x>') # normalized special binding ('<Ctrl-X>', '<ctrl+x>') # normalized special binding
]) ])
def test_unbind(self, commands, key_config_stub, config_stub, def test_unbind(self, commands, key_config_stub, config_stub, yaml_value,
key, normalized): key, normalized):
config_stub.val.bindings.default = { config_stub.val.bindings.default = {
'normal': {'a': 'nop', '<ctrl+x>': 'nop'}, 'normal': {'a': 'nop', '<ctrl+x>': 'nop'},
@ -590,7 +589,7 @@ class TestBind:
commands.unbind(key) commands.unbind(key)
assert key_config_stub.get_command(key, 'normal') is None assert key_config_stub.get_command(key, 'normal') is None
yaml_bindings = config_stub._yaml['bindings.commands']['normal'] yaml_bindings = yaml_value('bindings.commands')['normal']
if key in 'bc': if key in 'bc':
# Custom binding # Custom binding
assert normalized not in yaml_bindings assert normalized not in yaml_bindings