Fix configinit tests

This commit is contained in:
Florian Bruhin 2017-09-28 11:05:53 +02:00
parent 865fc2e0de
commit 3be0a78819

View File

@ -18,6 +18,7 @@
"""Tests for qutebrowser.config.configinit.""" """Tests for qutebrowser.config.configinit."""
import os
import sys import sys
import logging import logging
import unittest.mock import unittest.mock
@ -33,7 +34,6 @@ from qutebrowser.utils import objreg, usertypes
@pytest.fixture @pytest.fixture
def init_patch(qapp, fake_save_manager, monkeypatch, config_tmpdir, def init_patch(qapp, fake_save_manager, monkeypatch, config_tmpdir,
data_tmpdir): data_tmpdir):
monkeypatch.setattr(configdata, 'DATA', None)
monkeypatch.setattr(configfiles, 'state', None) monkeypatch.setattr(configfiles, 'state', None)
monkeypatch.setattr(config, 'instance', None) monkeypatch.setattr(config, 'instance', None)
monkeypatch.setattr(config, 'key_instance', None) monkeypatch.setattr(config, 'key_instance', None)
@ -46,10 +46,17 @@ def init_patch(qapp, fake_save_manager, monkeypatch, config_tmpdir,
pass pass
@pytest.fixture
def args(fake_args):
"""Arguments needed for the config to init."""
fake_args.temp_settings = []
return fake_args
class TestEarlyInit: class TestEarlyInit:
@pytest.mark.parametrize('config_py', [True, 'error', False]) @pytest.mark.parametrize('config_py', [True, 'error', False])
def test_config_py(self, init_patch, config_tmpdir, caplog, fake_args, def test_config_py(self, init_patch, config_tmpdir, caplog, args,
config_py): config_py):
"""Test loading with only a config.py.""" """Test loading with only a config.py."""
config_py_file = config_tmpdir / 'config.py' config_py_file = config_tmpdir / 'config.py'
@ -62,7 +69,7 @@ class TestEarlyInit:
'utf-8', ensure=True) 'utf-8', ensure=True)
with caplog.at_level(logging.ERROR): with caplog.at_level(logging.ERROR):
configinit.early_init(fake_args) configinit.early_init(args)
# Check error messages # Check error messages
expected_errors = [] expected_errors = []
@ -92,7 +99,7 @@ class TestEarlyInit:
@pytest.mark.parametrize('config_py', [True, 'error', False]) @pytest.mark.parametrize('config_py', [True, 'error', False])
@pytest.mark.parametrize('invalid_yaml', ['42', 'unknown', 'wrong-type', @pytest.mark.parametrize('invalid_yaml', ['42', 'unknown', 'wrong-type',
False]) False])
def test_autoconfig_yml(self, init_patch, config_tmpdir, caplog, fake_args, def test_autoconfig_yml(self, init_patch, config_tmpdir, caplog, args,
load_autoconfig, config_py, invalid_yaml): load_autoconfig, config_py, invalid_yaml):
"""Test interaction between config.py and autoconfig.yml.""" """Test interaction between config.py and autoconfig.yml."""
# pylint: disable=too-many-locals,too-many-branches # pylint: disable=too-many-locals,too-many-branches
@ -119,7 +126,7 @@ class TestEarlyInit:
'utf-8', ensure=True) 'utf-8', ensure=True)
with caplog.at_level(logging.ERROR): with caplog.at_level(logging.ERROR):
configinit.early_init(fake_args) configinit.early_init(args)
# Check error messages # Check error messages
expected_errors = [] expected_errors = []
@ -158,16 +165,46 @@ class TestEarlyInit:
else: else:
assert config.instance._values == {'colors.hints.fg': 'magenta'} assert config.instance._values == {'colors.hints.fg': 'magenta'}
def test_invalid_change_filter(self, init_patch, fake_args): def test_invalid_change_filter(self, init_patch, args):
config.change_filter('foobar') config.change_filter('foobar')
with pytest.raises(configexc.NoOptionError): with pytest.raises(configexc.NoOptionError):
configinit.early_init(fake_args) configinit.early_init(args)
def test_temp_settings_valid(self, init_patch, args):
args.temp_settings = [('colors.completion.fg', 'magenta')]
configinit.early_init(args)
assert config.instance._values['colors.completion.fg'] == 'magenta'
def test_temp_settings_invalid(self, caplog, init_patch, message_mock,
args):
"""Invalid temp settings should show an error."""
args.temp_settings = [('foo', 'bar')]
with caplog.at_level(logging.ERROR):
configinit.early_init(args)
msg = message_mock.getmsg()
assert msg.level == usertypes.MessageLevel.error
assert msg.text == "set: NoOptionError - No option 'foo'"
assert 'colors.completion.fg' not in config.instance._values
def test_force_software_rendering(self, monkeypatch, init_patch, args):
"""Setting force_software_rendering should set the environment var."""
envvar = 'QT_XCB_FORCE_SOFTWARE_OPENGL'
monkeypatch.setattr(configinit.objects, 'backend',
usertypes.Backend.QtWebEngine)
monkeypatch.delenv(envvar, raising=False)
args.temp_settings = [('force_software_rendering', 'true')]
configinit.early_init(args)
assert os.environ[envvar] == '1'
@pytest.mark.parametrize('errors', [True, False]) @pytest.mark.parametrize('errors', [True, False])
def test_late_init(init_patch, monkeypatch, fake_save_manager, fake_args, def test_late_init(init_patch, monkeypatch, fake_save_manager, args,
mocker, errors): mocker, errors):
configinit.early_init(fake_args) configinit.early_init(args)
if errors: if errors:
err = configexc.ConfigErrorDesc("Error text", Exception("Exception")) err = configexc.ConfigErrorDesc("Error text", Exception("Exception"))
errs = configexc.ConfigFileErrors("config.py", [err]) errs = configexc.ConfigFileErrors("config.py", [err])
@ -245,7 +282,7 @@ class TestQtArgs:
# set in config # set in config
(None, 'webkit', usertypes.Backend.QtWebKit), (None, 'webkit', usertypes.Backend.QtWebKit),
]) ])
def test_get_backend(monkeypatch, fake_args, config_stub, def test_get_backend(monkeypatch, args, config_stub,
arg, confval, used): arg, confval, used):
real_import = __import__ real_import = __import__
@ -254,8 +291,8 @@ def test_get_backend(monkeypatch, fake_args, config_stub,
return real_import(name, *args, **kwargs) return real_import(name, *args, **kwargs)
raise ImportError raise ImportError
fake_args.backend = arg args.backend = arg
config_stub.val.backend = confval config_stub.val.backend = confval
monkeypatch.setattr('builtins.__import__', fake_import) monkeypatch.setattr('builtins.__import__', fake_import)
assert configinit.get_backend(fake_args) == used assert configinit.get_backend(args) == used