2015-08-18 20:43:42 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2018-02-05 12:19:50 +01:00
|
|
|
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-08-18 20:43:42 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""pytest helper to monkeypatch the message module."""
|
|
|
|
|
2015-08-18 21:28:22 +02:00
|
|
|
import logging
|
2015-08-18 20:43:42 +02:00
|
|
|
|
2017-09-19 22:18:02 +02:00
|
|
|
import attr
|
2015-08-18 21:28:22 +02:00
|
|
|
import pytest
|
|
|
|
|
2018-10-16 10:59:55 +02:00
|
|
|
from qutebrowser.utils import usertypes, message, objreg
|
2015-08-18 20:43:42 +02:00
|
|
|
|
|
|
|
|
2017-09-19 22:18:02 +02:00
|
|
|
@attr.s
|
|
|
|
class Message:
|
|
|
|
|
|
|
|
"""Information about a shown message."""
|
|
|
|
|
|
|
|
level = attr.ib()
|
|
|
|
text = attr.ib()
|
2015-08-18 21:28:22 +02:00
|
|
|
|
|
|
|
|
2015-08-18 20:43:42 +02:00
|
|
|
class MessageMock:
|
|
|
|
|
|
|
|
"""Helper object for message_mock.
|
|
|
|
|
|
|
|
Attributes:
|
2017-09-19 22:18:02 +02:00
|
|
|
Message: A object representing a message.
|
|
|
|
messages: A list of Message objects.
|
2015-08-18 20:43:42 +02:00
|
|
|
"""
|
|
|
|
|
2016-09-15 11:56:46 +02:00
|
|
|
def __init__(self):
|
2015-08-18 20:43:42 +02:00
|
|
|
self.messages = []
|
|
|
|
|
2016-09-15 11:56:46 +02:00
|
|
|
def _record_message(self, level, text):
|
2015-08-18 21:28:22 +02:00
|
|
|
log_levels = {
|
2016-09-15 11:56:46 +02:00
|
|
|
usertypes.MessageLevel.error: logging.ERROR,
|
|
|
|
usertypes.MessageLevel.info: logging.INFO,
|
|
|
|
usertypes.MessageLevel.warning: logging.WARNING,
|
2015-08-18 21:28:22 +02:00
|
|
|
}
|
|
|
|
log_level = log_levels[level]
|
|
|
|
|
tests: Use dedicated logger for message mock
The message mock might handle a message during pytest-qt's processEvents
during test setup. If that happens, depending on the fixture order,
pytest-caplog might not be set up first, which is why the
self._caplog.at_level call can fail:
File "c:\projects\qutebrowser\qutebrowser\misc\guiprocess.py", line 105, in on_finished
immediately=True)
File "C:\projects\qutebrowser\tests\helpers\messagemock.py", line 71, in _handle_error
self._handle(Level.error, *args, **kwargs)
File "C:\projects\qutebrowser\tests\helpers\messagemock.py", line 65, in _handle
with self._caplog.at_level(log_level): # needed so we don't fail
File "C:\projects\qutebrowser\.tox\py34\lib\site-packages\pytest_catchlog.py", line 232, in at_level
obj = logger and logging.getLogger(logger) or self.handler
File "C:\projects\qutebrowser\.tox\py34\lib\site-packages\pytest_catchlog.py", line 186, in handler
return self._item.catch_log_handler
AttributeError: 'Function' object has no attribute 'catch_log_handler'
Full stack:
c:\projects\qutebrowser-git\.tox\py34\lib\site-packages\pytestqt\plugin.py(100)pytest_runtest_setup()
-> _process_events()
c:\projects\qutebrowser-git\.tox\py34\lib\site-packages\pytestqt\plugin.py(140)_process_events()
-> app.processEvents()
c:\projects\qutebrowser-git\qutebrowser\misc\guiprocess.py(94)on_error()
-> self._what, msg), immediately=True)
c:\projects\qutebrowser-git\tests\helpers\messagemock.py(71)_handle_error()
-> self._handle(Level.error, *args, **kwargs)
c:\projects\qutebrowser-git\tests\helpers\messagemock.py(65)_handle()
-> with self._caplog.at_level(log_level): # needed so we don't fail
c:\projects\qutebrowser-git\.tox\py34\lib\site-packages\pytest_catchlog.py(235)at_level()
-> obj = logger and logging.getLogger(logger) or self.handler
> c:\projects\qutebrowser-git\.tox\py34\lib\site-packages\pytest_catchlog.py(189)handler()->None
This should fix broken AppVeyor builds.
Fixes #1662.
2016-07-20 14:19:27 +02:00
|
|
|
logging.getLogger('messagemock').log(log_level, text)
|
2016-09-15 11:56:46 +02:00
|
|
|
self.messages.append(Message(level, text))
|
2015-08-18 20:43:42 +02:00
|
|
|
|
2016-09-15 11:56:46 +02:00
|
|
|
def getmsg(self, level=None):
|
2015-08-18 20:43:42 +02:00
|
|
|
"""Get the only message in self.messages.
|
|
|
|
|
|
|
|
Raises ValueError if there are multiple or no messages.
|
2015-08-19 09:44:31 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
level: The message level to check against, or None.
|
2015-08-18 20:43:42 +02:00
|
|
|
"""
|
2015-08-19 09:44:31 +02:00
|
|
|
assert len(self.messages) == 1
|
|
|
|
msg = self.messages[0]
|
|
|
|
if level is not None:
|
|
|
|
assert msg.level == level
|
|
|
|
return msg
|
2015-08-18 20:43:42 +02:00
|
|
|
|
2016-09-15 11:56:46 +02:00
|
|
|
def patch(self):
|
|
|
|
"""Start recording messages."""
|
|
|
|
message.global_bridge.show_message.connect(self._record_message)
|
2017-02-23 06:43:00 +01:00
|
|
|
message.global_bridge._connected = True
|
2016-09-15 11:56:46 +02:00
|
|
|
|
|
|
|
def unpatch(self):
|
|
|
|
"""Stop recording messages."""
|
|
|
|
message.global_bridge.show_message.disconnect(self._record_message)
|
2015-08-18 20:43:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-09-15 11:56:46 +02:00
|
|
|
def message_mock():
|
2015-08-18 20:43:42 +02:00
|
|
|
"""Fixture to get a MessageMock."""
|
2016-09-15 11:56:46 +02:00
|
|
|
mmock = MessageMock()
|
|
|
|
mmock.patch()
|
|
|
|
yield mmock
|
|
|
|
mmock.unpatch()
|
2018-10-16 10:59:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def message_bridge(win_registry):
|
|
|
|
"""Fixture to get a MessageBridge."""
|
|
|
|
bridge = message.MessageBridge()
|
|
|
|
objreg.register('message-bridge', bridge, scope='window', window=0)
|
|
|
|
yield bridge
|
|
|
|
objreg.delete('message-bridge', scope='window', window=0)
|