qutebrowser/tests/unit/config/test_config.py

373 lines
14 KiB
Python
Raw Normal View History

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.config.config."""
2015-02-20 09:09:35 +01:00
import os
import os.path
import configparser
2015-02-20 09:09:35 +01:00
import types
2015-03-23 07:58:28 +01:00
import argparse
import collections
import shutil
2015-02-20 09:09:35 +01:00
from unittest import mock
2015-02-20 09:09:35 +01:00
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QColor
2015-04-04 17:02:32 +02:00
import pytest
import qutebrowser
from qutebrowser.config import config, configexc, configdata
from qutebrowser.config.parsers import keyconf
from qutebrowser.commands import runners
2015-02-22 19:13:51 +01:00
from qutebrowser.utils import objreg, standarddir
class TestConfigParser:
"""Test reading of ConfigParser."""
Objects = collections.namedtuple('Objects', ['cp', 'cfg'])
@pytest.fixture
def objects(self):
cp = configparser.ConfigParser(interpolation=None,
comment_prefixes='#')
cp.optionxform = lambda opt: opt # be case-insensitive
cfg = config.ConfigManager()
cfg.read(None, None)
return self.Objects(cp=cp, cfg=cfg)
def test_simple(self, objects):
"""Test a simple option which is not transformed."""
objects.cp.read_dict({'general': {'ignore-case': 'false'}})
objects.cfg._from_cp(objects.cp)
assert not objects.cfg.get('general', 'ignore-case')
def test_transformed_section_old(self, objects):
"""Test a transformed section with the old name."""
objects.cp.read_dict({'permissions': {'allow-plugins': 'true'}})
objects.cfg._from_cp(objects.cp)
assert objects.cfg.get('content', 'allow-plugins')
def test_transformed_section_new(self, objects):
"""Test a transformed section with the new name."""
objects.cp.read_dict({'content': {'allow-plugins': 'true'}})
objects.cfg._from_cp(objects.cp)
assert objects.cfg.get('content', 'allow-plugins')
def test_transformed_option_old(self, objects):
"""Test a transformed option with the old name."""
objects.cp.read_dict({'colors': {'tab.fg.odd': 'pink'}})
objects.cfg._from_cp(objects.cp)
actual = objects.cfg.get('colors', 'tabs.fg.odd').name()
2015-04-05 20:30:31 +02:00
expected = QColor('pink').name()
assert actual == expected
def test_transformed_option_new(self, objects):
"""Test a transformed section with the new name."""
objects.cp.read_dict({'colors': {'tabs.fg.odd': 'pink'}})
objects.cfg._from_cp(objects.cp)
actual = objects.cfg.get('colors', 'tabs.fg.odd').name()
2015-04-05 20:30:31 +02:00
expected = QColor('pink').name()
assert actual == expected
def test_invalid_value(self, objects):
"""Test setting an invalid value."""
objects.cp.read_dict({'general': {'ignore-case': 'invalid'}})
objects.cfg._from_cp(objects.cp)
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.ValidationError):
objects.cfg._validate_all()
def test_invalid_value_interpolated(self, objects):
"""Test setting an invalid interpolated value."""
objects.cp.read_dict({'general': {'ignore-case': 'smart',
'wrap-search': '${ignore-case}'}})
objects.cfg._from_cp(objects.cp)
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.ValidationError):
objects.cfg._validate_all()
def test_interpolation(self, objects):
"""Test setting an interpolated value."""
objects.cp.read_dict({'general': {'ignore-case': 'false',
'wrap-search': '${ignore-case}'}})
objects.cfg._from_cp(objects.cp)
assert not objects.cfg.get('general', 'ignore-case')
assert not objects.cfg.get('general', 'wrap-search')
def test_interpolation_cross_section(self, objects):
"""Test setting an interpolated value from another section."""
2016-04-27 18:30:54 +02:00
objects.cp.read_dict({
'general': {'ignore-case': '${network:do-not-track}'},
'network': {'do-not-track': 'false'},
})
objects.cfg._from_cp(objects.cp)
assert not objects.cfg.get('general', 'ignore-case')
assert not objects.cfg.get('network', 'do-not-track')
def test_invalid_interpolation(self, objects):
"""Test an invalid interpolation."""
objects.cp.read_dict({'general': {'ignore-case': '${foo}'}})
objects.cfg._from_cp(objects.cp)
2015-04-04 17:02:32 +02:00
with pytest.raises(configparser.InterpolationError):
objects.cfg._validate_all()
def test_invalid_interpolation_syntax(self, objects):
"""Test an invalid interpolation syntax."""
objects.cp.read_dict({'general': {'ignore-case': '${'}})
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.InterpolationSyntaxError):
objects.cfg._from_cp(objects.cp)
def test_invalid_section(self, objects):
"""Test an invalid section."""
objects.cp.read_dict({'foo': {'bar': 'baz'}})
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.NoSectionError):
objects.cfg._from_cp(objects.cp)
def test_invalid_option(self, objects):
"""Test an invalid option."""
objects.cp.read_dict({'general': {'bar': 'baz'}})
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.NoOptionError):
objects.cfg._from_cp(objects.cp)
def test_invalid_section_relaxed(self, objects):
2015-03-23 07:58:28 +01:00
"""Test an invalid section with relaxed=True."""
objects.cp.read_dict({'foo': {'bar': 'baz'}})
objects.cfg._from_cp(objects.cp, relaxed=True)
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.NoSectionError):
objects.cfg.get('foo', 'bar')
2015-03-23 07:58:28 +01:00
def test_invalid_option_relaxed(self, objects):
2015-03-23 07:58:28 +01:00
"""Test an invalid option with relaxed=True."""
objects.cp.read_dict({'general': {'bar': 'baz'}})
objects.cfg._from_cp(objects.cp, relaxed=True)
2015-04-04 17:02:32 +02:00
with pytest.raises(configexc.NoOptionError):
objects.cfg.get('general', 'bar')
2015-03-23 07:58:28 +01:00
def test_fallback(self, objects):
"""Test getting an option with fallback.
This is done during interpolation in later Python 3.4 versions.
See https://github.com/The-Compiler/qutebrowser/issues/968
"""
assert objects.cfg.get('general', 'blabla', fallback='blub') == 'blub'
def test_sectionproxy(self, objects):
"""Test getting an option via the section proxy."""
objects.cp.read_dict({'general': {'ignore-case': 'false'}})
objects.cfg._from_cp(objects.cp)
assert not objects.cfg['general'].get('ignore-case')
def test_sectionproxy_keyerror(self, objects):
"""Test getting an inexistent option via the section proxy."""
with pytest.raises(configexc.NoOptionError):
objects.cfg['general'].get('blahblahblub')
@pytest.mark.parametrize('old_sect, new_sect',
config.ConfigManager.RENAMED_SECTIONS.items())
def test_renamed_sections(self, old_sect, new_sect):
"""Make sure renamed sections don't exist anymore."""
assert old_sect not in configdata.DATA
assert new_sect in configdata.DATA
@pytest.mark.parametrize('old_tuple, new_option',
sorted(config.ConfigManager.RENAMED_OPTIONS.items()))
def test_renamed_options(self, old_tuple, new_option):
"""Make sure renamed options exist under the new name."""
section, old_option = old_tuple
assert old_option not in configdata.DATA[section]
assert new_option in configdata.DATA[section]
@pytest.mark.parametrize('section, option',
config.ConfigManager.DELETED_OPTIONS)
def test_deleted_options(self, section, option):
"""Make sure renamed options don't exist anymore."""
assert option not in configdata.DATA[section]
class TestKeyConfigParser:
"""Test config.parsers.keyconf.KeyConfigParser."""
def test_cmd_binding(self, cmdline_test):
"""Test various command bindings.
See https://github.com/The-Compiler/qutebrowser/issues/615
Args:
cmdline_test: A pytest fixture which provides testcases.
"""
kcp = keyconf.KeyConfigParser(None, None)
kcp._cur_section = 'normal'
if cmdline_test.valid:
kcp._read_command(cmdline_test.cmd)
else:
with pytest.raises(keyconf.KeyConfigError):
kcp._read_command(cmdline_test.cmd)
@pytest.mark.parametrize('rgx', [rgx for rgx, _repl
in configdata.CHANGED_KEY_COMMANDS])
def test_default_config_no_deprecated(self, rgx):
"""Make sure the default config contains no deprecated commands."""
for sect in configdata.KEY_DATA.values():
for command in sect:
assert rgx.match(command) is None
@pytest.mark.parametrize(
'old, new_expected',
[
('open -t about:blank', 'open -t'),
('open -w about:blank', 'open -w'),
('open -b about:blank', 'open -b'),
('open about:blank', None),
('open -t example.com', None),
('download-page', 'download'),
('cancel-download', 'download-cancel'),
('search ""', 'clear-keychain ;; search'),
("search ''", 'clear-keychain ;; search'),
("search", 'clear-keychain ;; search'),
2015-06-05 17:45:32 +02:00
("search ;; foobar", None),
('search "foo"', None),
('set-cmd-text "foo bar"', 'set-cmd-text foo bar'),
("set-cmd-text 'foo bar'", 'set-cmd-text foo bar'),
('set-cmd-text foo bar', None),
('set-cmd-text "foo bar "', 'set-cmd-text -s foo bar'),
("set-cmd-text 'foo bar '", 'set-cmd-text -s foo bar'),
('hint links rapid', 'hint --rapid links tab-bg'),
('hint links rapid-win', 'hint --rapid links window'),
('scroll -50 0', 'scroll left'),
('scroll 0 50', 'scroll down'),
('scroll 0 -50', 'scroll up'),
('scroll 50 0', 'scroll right'),
('scroll -50 10', 'scroll-px -50 10'),
('scroll 50 50', 'scroll-px 50 50'),
('scroll 0 0', 'scroll-px 0 0'),
('scroll 23 42', 'scroll-px 23 42'),
('search ;; clear-keychain', 'clear-keychain ;; search'),
('search;;clear-keychain', 'clear-keychain ;; search'),
('search;;foo', None),
('leave-mode', 'clear-keychain ;; leave-mode'),
('leave-mode ;; foo', None),
('download-remove --all', 'download-clear'),
]
)
def test_migrations(self, old, new_expected):
"""Make sure deprecated commands get migrated correctly."""
if new_expected is None:
new_expected = old
new = old
for rgx, repl in configdata.CHANGED_KEY_COMMANDS:
if rgx.match(new):
new = rgx.sub(repl, new)
break
assert new == new_expected
@pytest.mark.integration
class TestDefaultConfig:
"""Test validating of the default config."""
@pytest.mark.usefixtures('qapp')
def test_default_config(self):
"""Test validating of the default config."""
conf = config.ConfigManager()
conf.read(None, None)
conf._validate_all()
def test_default_key_config(self):
"""Test validating of the default key config."""
# We import qutebrowser.app so the cmdutils.register decorators run.
2015-12-01 20:55:38 +01:00
import qutebrowser.app
conf = keyconf.KeyConfigParser(None, None)
runner = runners.CommandRunner(win_id=0)
for sectname in configdata.KEY_DATA:
for cmd in conf.get_bindings_for(sectname).values():
runner.parse(cmd, aliases=False)
def test_upgrade_version(self):
"""Fail when the qutebrowser version changed.
The aim of this is to remind us to add a new file to old_configs.
If the config file of the current release didn't change compared to the
last one in old_configs, just increment the version here.
If it did change, place a new qutebrowser-vx.y.z.conf in old_configs
and then increment the version.
"""
2016-04-30 15:05:19 +02:00
assert qutebrowser.__version__ == '0.6.2'
@pytest.mark.parametrize('filename',
os.listdir(os.path.join(os.path.dirname(__file__), 'old_configs')),
ids=os.path.basename)
def test_old_config(self, qapp, tmpdir, filename):
"""Check if upgrading old configs works correctly."""
full_path = os.path.join(os.path.dirname(__file__), 'old_configs',
filename)
shutil.copy(full_path, str(tmpdir / 'qutebrowser.conf'))
conf = config.ConfigManager()
conf.read(str(tmpdir), 'qutebrowser.conf')
@pytest.mark.integration
class TestConfigInit:
2015-02-20 09:09:35 +01:00
"""Test initializing of the config."""
2015-04-04 17:02:32 +02:00
@pytest.yield_fixture(autouse=True)
def patch(self):
2015-02-20 09:09:35 +01:00
objreg.register('app', QObject())
objreg.register('save-manager', mock.MagicMock())
2015-03-23 07:58:28 +01:00
args = argparse.Namespace(relaxed_config=False)
objreg.register('args', args)
old_standarddir_args = standarddir._args
2015-04-04 17:02:32 +02:00
yield
2015-02-20 09:09:35 +01:00
objreg.global_registry.clear()
standarddir._args = old_standarddir_args
2015-02-20 09:09:35 +01:00
@pytest.fixture
def env(self, tmpdir):
conf_path = (tmpdir / 'config').ensure(dir=1)
data_path = (tmpdir / 'data').ensure(dir=1)
cache_path = (tmpdir / 'cache').ensure(dir=1)
env = {
'XDG_CONFIG_HOME': str(conf_path),
'XDG_DATA_HOME': str(data_path),
'XDG_CACHE_HOME': str(cache_path),
}
return env
def test_config_none(self, monkeypatch, env):
2015-02-20 09:09:35 +01:00
"""Test initializing with config path set to None."""
args = types.SimpleNamespace(confdir='', datadir='', cachedir='',
basedir=None)
for k, v in env.items():
2015-04-04 17:02:32 +02:00
monkeypatch.setenv(k, v)
standarddir.init(args)
config.init()
assert not os.listdir(env['XDG_CONFIG_HOME'])