1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2018-2021 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 <https://www.gnu.org/licenses/>.
import types
import pytest
from qutebrowser.extensions import loader
from qutebrowser.misc import objects
pytestmark = pytest.mark.usefixtures('data_tmpdir', 'config_tmpdir',
'fake_args')
def test_on_walk_error():
with pytest.raises(ImportError, match='Failed to import foo'):
loader._on_walk_error('foo')
def test_walk_normal():
names = [info.name for info in loader.walk_components()]
assert 'qutebrowser.components.scrollcommands' in names
def test_load_component(monkeypatch):
monkeypatch.setattr(objects, 'commands', {})
info = loader.ExtensionInfo(name='qutebrowser.components.scrollcommands')
mod = loader._load_component(info, skip_hooks=True)
assert hasattr(mod, 'scroll_to_perc')
assert 'scroll-to-perc' in objects.commands
@pytest.fixture
def module(monkeypatch, request):
mod = types.ModuleType('testmodule')
monkeypatch.setattr(loader, '_module_infos', [])
monkeypatch.setattr(loader.importlib, 'import_module',
lambda _name: mod)
mod.info = loader.add_module_info(mod)
return mod
def test_get_init_context(data_tmpdir, config_tmpdir, fake_args):
ctx = loader._get_init_context()
assert str(ctx.data_dir) == data_tmpdir
assert str(ctx.config_dir) == config_tmpdir
assert ctx.args == fake_args
def test_add_module_info():
# pylint: disable=no-member
mod = types.ModuleType('testmodule')
info1 = loader.add_module_info(mod)
assert mod.__qute_module_info is info1
info2 = loader.add_module_info(mod)
assert mod.__qute_module_info is info1
assert info2 is info1
class _Hook:
"""Hook to use in tests."""
__name__ = '_Hook'
def __init__(self):
self.called = False
self.raising = False
def __call__(self, *args):
if self.raising:
raise Exception("Should not be called!")
self.called = True
@pytest.fixture
def hook():
return _Hook()
def test_skip_hooks(hook, module):
hook.raising = True
module.info.init_hook = hook
module.info.config_changed_hooks = [(None, hook)]
info = loader.ExtensionInfo(name='testmodule')
loader._load_component(info, skip_hooks=True)
loader._on_config_changed('test')
assert not hook.called
@pytest.mark.parametrize('option_filter, option, called', [
(None, 'content.javascript.enabled', True),
('content.javascript', 'content.javascript.enabled', True),
('content.javascript.enabled', 'content.javascript.enabled', True),
('content.javascript.log', 'content.javascript.enabled', False),
])
def test_on_config_changed(configdata_init, hook, module,
option_filter, option, called):
module.info.config_changed_hooks = [(option_filter, hook)]
info = loader.ExtensionInfo(name='testmodule')
loader._load_component(info)
loader._on_config_changed(option)
assert hook.called == called
def test_init_hook(hook, module):
module.info.init_hook = hook
info = loader.ExtensionInfo(name='testmodule')
loader._load_component(info)
assert hook.called
|