Further simplify caplog.messages

This commit is contained in:
Florian Bruhin 2018-10-25 11:16:26 +02:00
parent 3b8964183e
commit a458615030
14 changed files with 21 additions and 39 deletions

View File

@ -154,9 +154,8 @@ class TestResources:
match="Path 'web/test' not found"): match="Path 'web/test' not found"):
pdfjs.get_pdfjs_res_and_path('web/test') pdfjs.get_pdfjs_res_and_path('web/test')
assert len(caplog.records) == 1 expected = 'OSError while reading PDF.js file: Message'
msg = caplog.messages[0] assert caplog.messages == [expected]
assert msg == 'OSError while reading PDF.js file: Message'
@pytest.mark.parametrize('path, expected', [ @pytest.mark.parametrize('path, expected', [

View File

@ -203,9 +203,9 @@ class TestPDFJSHandler:
with caplog.at_level(logging.WARNING, 'misc'): with caplog.at_level(logging.WARNING, 'misc'):
with pytest.raises(qutescheme.NotFoundError): with pytest.raises(qutescheme.NotFoundError):
qutescheme.data_for_url(QUrl('qute://pdfjs/no/file.html')) qutescheme.data_for_url(QUrl('qute://pdfjs/no/file.html'))
assert len(caplog.records) == 1
assert (caplog.messages[0] == expected = 'pdfjs resource requested but not found: /no/file.html'
'pdfjs resource requested but not found: /no/file.html') assert caplog.messages == [expected]
def test_viewer_page(self, data_tmpdir): def test_viewer_page(self, data_tmpdir):
"""Load the /web/viewer.html page.""" """Load the /web/viewer.html page."""

View File

@ -86,9 +86,8 @@ def test_logging(caplog, objects, tabbed_browser_stubs, index_of, verb):
with caplog.at_level(logging.DEBUG, logger='signals'): with caplog.at_level(logging.DEBUG, logger='signals'):
objects.signaller.signal.emit('foo') objects.signaller.signal.emit('foo')
assert len(caplog.records) == 1
expected_msg = "{}: filtered_signal('foo') (tab {})".format(verb, index_of) expected_msg = "{}: filtered_signal('foo') (tab {})".format(verb, index_of)
assert caplog.messages[0] == expected_msg assert caplog.messages == [expected_msg]
@pytest.mark.parametrize('index_of', [0, 1]) @pytest.mark.parametrize('index_of', [0, 1])

View File

@ -388,9 +388,8 @@ class TestConfig:
conf._set_value(opt, 'never') conf._set_value(opt, 'never')
assert blocker.args == ['tabs.show'] assert blocker.args == ['tabs.show']
assert len(caplog.records) == 1
expected_message = 'Config option changed: tabs.show = never' expected_message = 'Config option changed: tabs.show = never'
assert caplog.messages[0] == expected_message assert caplog.messages == [expected_message]
def test_set_value_no_backend(self, monkeypatch, conf): def test_set_value_no_backend(self, monkeypatch, conf):
"""Make sure setting values when the backend is still unknown works.""" """Make sure setting values when the backend is still unknown works."""

View File

@ -60,8 +60,7 @@ class TestDebugLog:
def test_log(self, keyparser, caplog): def test_log(self, keyparser, caplog):
keyparser._debug_log('foo') keyparser._debug_log('foo')
assert len(caplog.records) == 1 assert caplog.messages == ['foo']
assert caplog.messages[0] == 'foo'
def test_no_log(self, keyparser, caplog): def test_no_log(self, keyparser, caplog):
keyparser.do_log = False keyparser.do_log = False

View File

@ -698,8 +698,6 @@ class TestSendOrListen:
with pytest.raises(ipc.Error): with pytest.raises(ipc.Error):
ipc.send_or_listen(args) ipc.send_or_listen(args)
assert len(caplog.records) == 1
error_msgs = [ error_msgs = [
'Handling fatal misc.ipc.{} with --no-err-windows!'.format( 'Handling fatal misc.ipc.{} with --no-err-windows!'.format(
exc_name), exc_name),
@ -709,7 +707,7 @@ class TestSendOrListen:
'post_text: Maybe another instance is running but frozen?', 'post_text: Maybe another instance is running but frozen?',
'exception text: {}'.format(exc_msg), 'exception text: {}'.format(exc_msg),
] ]
assert caplog.messages[0] == '\n'.join(error_msgs) assert caplog.messages == ['\n'.join(error_msgs)]
@pytest.mark.posix(reason="Flaky on Windows") @pytest.mark.posix(reason="Flaky on Windows")
def test_error_while_listening(self, qlocalserver_mock, caplog, args): def test_error_while_listening(self, qlocalserver_mock, caplog, args):

View File

@ -215,9 +215,8 @@ class TestSave:
with caplog.at_level(logging.ERROR): with caplog.at_level(logging.ERROR):
sess_man.save(str(session_path), last_window=True) sess_man.save(str(session_path), last_window=True)
assert len(caplog.records) == 1
msg = "last_window_session is None while saving!" msg = "last_window_session is None while saving!"
assert caplog.messages[0] == msg assert caplog.messages == [msg]
assert not session_path.exists() assert not session_path.exists()
def test_last_window_session(self, sess_man, tmpdir): def test_last_window_session(self, sess_man, tmpdir):

View File

@ -35,6 +35,5 @@ def test_on_focus_changed_issue1484(monkeypatch, qapp, caplog):
buf = QBuffer() buf = QBuffer()
app.on_focus_changed(buf, buf) app.on_focus_changed(buf, buf)
assert len(caplog.records) == 1
expected = "on_focus_changed called with non-QWidget {!r}".format(buf) expected = "on_focus_changed called with non-QWidget {!r}".format(buf)
assert caplog.messages[0] == expected assert caplog.messages == [expected]

View File

@ -41,8 +41,7 @@ def test_log_events(qapp, caplog):
obj = EventObject() obj = EventObject()
qapp.sendEvent(obj, QEvent(QEvent.User)) qapp.sendEvent(obj, QEvent(QEvent.User))
qapp.processEvents() qapp.processEvents()
assert len(caplog.records) == 1 assert caplog.messages == ['Event in test_debug.EventObject: User']
assert caplog.messages[0] == 'Event in test_debug.EventObject: User'
class SignalObject(QObject): class SignalObject(QObject):
@ -74,9 +73,8 @@ def test_log_signals(caplog, signal_obj):
signal_obj.signal1.emit() signal_obj.signal1.emit()
signal_obj.signal2.emit('foo', 'bar') signal_obj.signal2.emit('foo', 'bar')
assert len(caplog.records) == 2 assert caplog.messages == ['Signal in <repr>: signal1()',
assert caplog.messages[0] == 'Signal in <repr>: signal1()' "Signal in <repr>: signal2('foo', 'bar')"]
assert caplog.messages[1] == "Signal in <repr>: signal2('foo', 'bar')"
class TestLogTime: class TestLogTime:

View File

@ -51,8 +51,6 @@ def test_no_err_windows(caplog, exc, name, exc_text, fake_args):
error.handle_fatal_exc(e, fake_args, 'title', pre_text='pre', error.handle_fatal_exc(e, fake_args, 'title', pre_text='pre',
post_text='post') post_text='post')
assert len(caplog.records) == 1
expected = [ expected = [
'Handling fatal {} with --no-err-windows!'.format(name), 'Handling fatal {} with --no-err-windows!'.format(name),
'', '',
@ -61,7 +59,7 @@ def test_no_err_windows(caplog, exc, name, exc_text, fake_args):
'post_text: post', 'post_text: post',
'exception text: {}'.format(exc_text), 'exception text: {}'.format(exc_text),
] ]
assert caplog.messages[0] == '\n'.join(expected) assert caplog.messages == ['\n'.join(expected)]
# This happens on Xvfb for some reason # This happens on Xvfb for some reason

View File

@ -270,8 +270,7 @@ class TestHideQtWarning:
def test_stub(caplog, suffix, expected): def test_stub(caplog, suffix, expected):
with caplog.at_level(logging.WARNING, 'misc'): with caplog.at_level(logging.WARNING, 'misc'):
log.stub(suffix) log.stub(suffix)
assert len(caplog.records) == 1 assert caplog.messages == [expected]
assert caplog.messages[0] == expected
def test_ignore_py_warnings(caplog): def test_ignore_py_warnings(caplog):
@ -300,5 +299,4 @@ class TestQtMessageHandler:
def test_empty_message(self, caplog): def test_empty_message(self, caplog):
"""Make sure there's no crash with an empty message.""" """Make sure there's no crash with an empty message."""
log.qt_message_handler(QtCore.QtDebugMsg, self.Context(), "") log.qt_message_handler(QtCore.QtDebugMsg, self.Context(), "")
assert len(caplog.records) == 1 assert caplog.messages == ["Logged empty message!"]
assert caplog.messages[0] == "Logged empty message!"

View File

@ -288,8 +288,7 @@ class TestInitCacheDirTag:
mocker.patch('builtins.open', side_effect=OSError) mocker.patch('builtins.open', side_effect=OSError)
with caplog.at_level(logging.ERROR, 'init'): with caplog.at_level(logging.ERROR, 'init'):
standarddir._init_cachedir_tag() standarddir._init_cachedir_tag()
assert len(caplog.records) == 1 assert caplog.messages == ['Failed to create CACHEDIR.TAG']
assert caplog.messages[0] == 'Failed to create CACHEDIR.TAG'
assert not tmpdir.listdir() assert not tmpdir.listdir()

View File

@ -469,9 +469,8 @@ class TestPreventExceptions:
ret = self.func_raising() ret = self.func_raising()
# pylint: enable=assignment-from-no-return # pylint: enable=assignment-from-no-return
assert ret == 42 assert ret == 42
assert len(caplog.records) == 1
expected = 'Error in test_utils.TestPreventExceptions.func_raising' expected = 'Error in test_utils.TestPreventExceptions.func_raising'
assert caplog.messages[0] == expected assert caplog.messages == [expected]
@utils.prevent_exceptions(42) @utils.prevent_exceptions(42)
def func_not_raising(self): def func_not_raising(self):

View File

@ -294,8 +294,7 @@ class TestGitStr:
commit_file_mock.return_value = '0deadcode' commit_file_mock.return_value = '0deadcode'
with caplog.at_level(logging.ERROR, 'misc'): with caplog.at_level(logging.ERROR, 'misc'):
assert version._git_str() == '0deadcode' assert version._git_str() == '0deadcode'
assert len(caplog.records) == 1 assert caplog.messages == ["Error while getting git path"]
assert caplog.messages[0] == "Error while getting git path"
def _has_git(): def _has_git():
@ -456,8 +455,7 @@ def test_release_info(files, expected, caplog, monkeypatch):
with caplog.at_level(logging.ERROR, 'misc'): with caplog.at_level(logging.ERROR, 'misc'):
assert version._release_info() == expected assert version._release_info() == expected
if files is None: if files is None:
assert len(caplog.records) == 1 assert caplog.messages == ["Error while reading fake-file."]
assert caplog.messages[0] == "Error while reading fake-file."
@pytest.mark.parametrize('equal', [True, False]) @pytest.mark.parametrize('equal', [True, False])