qutebrowser/tests/unit/utils/test_error.py

98 lines
3.3 KiB
Python
Raw Normal View History

2017-05-09 21:37:03 +02:00
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2015-08-17 22:48:05 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# 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.utils.error."""
import logging
import pytest
from qutebrowser.utils import error, utils
from qutebrowser.misc import ipc
2015-08-17 22:48:05 +02:00
from PyQt5.QtCore import QTimer
2015-08-17 22:48:05 +02:00
from PyQt5.QtWidgets import QMessageBox
2015-09-06 16:43:23 +02:00
class Error(Exception):
pass
@pytest.mark.parametrize('exc, name, exc_text', [
# "builtins." stripped
(ValueError('exception'), 'ValueError', 'exception'),
(ValueError, 'ValueError', 'none'),
# "qutebrowser." stripped
(ipc.Error, 'misc.ipc.Error', 'none'),
2015-09-06 16:43:23 +02:00
(Error, 'test_error.Error', 'none'),
])
2016-07-08 19:30:17 +02:00
def test_no_err_windows(caplog, exc, name, exc_text, fake_args):
"""Test handle_fatal_exc with no_err_windows = True."""
2016-07-08 19:30:17 +02:00
fake_args.no_err_windows = True
2015-08-17 22:48:05 +02:00
try:
raise exc
except Exception as e:
with caplog.at_level(logging.ERROR):
2016-07-08 19:30:17 +02:00
error.handle_fatal_exc(e, fake_args, 'title', pre_text='pre',
post_text='post')
assert len(caplog.records) == 1
expected = [
'Handling fatal {} with --no-err-windows!'.format(name),
'',
'title: title',
'pre_text: pre',
'post_text: post',
'exception text: {}'.format(exc_text),
]
assert caplog.records[0].msg == '\n'.join(expected)
2015-08-17 22:48:05 +02:00
2015-10-02 07:07:38 +02:00
# This happens on Xvfb for some reason
2017-02-05 00:13:11 +01:00
# See https://github.com/qutebrowser/qutebrowser/issues/984
2015-10-02 07:07:38 +02:00
@pytest.mark.qt_log_ignore(r'^QXcbConnection: XCB error: 8 \(BadMatch\), '
r'sequence: \d+, resource id: \d+, major code: 42 '
r'\(SetInputFocus\), minor code: 0$',
r'^QIODevice::write: device not open')
2015-08-17 22:48:05 +02:00
@pytest.mark.parametrize('pre_text, post_text, expected', [
('', '', 'exception'),
('foo', '', 'foo: exception'),
('foo', 'bar', 'foo: exception\n\nbar'),
('', 'bar', 'exception\n\nbar'),
2015-09-16 20:25:02 +02:00
], ids=repr)
2016-07-08 19:30:17 +02:00
def test_err_windows(qtbot, qapp, fake_args, pre_text, post_text, expected):
2015-08-17 22:48:05 +02:00
def err_window_check():
w = qapp.activeModalWidget()
try:
qtbot.add_widget(w)
if not utils.is_mac:
assert w.windowTitle() == 'title'
2015-08-17 22:48:05 +02:00
assert w.icon() == QMessageBox.Critical
assert w.standardButtons() == QMessageBox.Ok
assert w.text() == expected
finally:
w.close()
2016-07-08 19:30:17 +02:00
fake_args.no_err_windows = False
2015-08-17 22:48:05 +02:00
QTimer.singleShot(0, err_window_check)
2016-07-08 19:30:17 +02:00
error.handle_fatal_exc(ValueError("exception"), fake_args, 'title',
pre_text=pre_text, post_text=post_text)