tests: Switch from pytest-capturelog to catchlog.
This commit is contained in:
parent
a5efbe7412
commit
416cfaf002
@ -33,7 +33,7 @@ import pytest
|
||||
|
||||
import helpers.stubs as stubsmod
|
||||
from helpers import logfail
|
||||
from helpers.logfail import fail_on_logging, caplog_bug_workaround
|
||||
from helpers.logfail import fail_on_logging
|
||||
from helpers.messagemock import message_mock
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import objreg
|
||||
|
@ -24,7 +24,7 @@ import logging
|
||||
import pytest
|
||||
|
||||
try:
|
||||
import pytest_capturelog as caplog_mod
|
||||
import pytest_catchlog as catchlog_mod
|
||||
except ImportError:
|
||||
# When using pytest for pyflakes/pep8/..., the plugin won't be available
|
||||
# but conftest.py will still be loaded.
|
||||
@ -47,18 +47,18 @@ class LogFailHandler(logging.Handler):
|
||||
root_logger = logging.getLogger()
|
||||
|
||||
for h in root_logger.handlers:
|
||||
if isinstance(h, caplog_mod.CaptureLogHandler):
|
||||
caplog_handler = h
|
||||
if isinstance(h, catchlog_mod.LogCaptureHandler):
|
||||
catchlog_handler = h
|
||||
break
|
||||
else:
|
||||
# The CaptureLogHandler is not available anymore during fixture
|
||||
# The LogCaptureHandler is not available anymore during fixture
|
||||
# teardown, so we ignore logging messages emitted there..
|
||||
return
|
||||
|
||||
if (logger.level == record.levelno or
|
||||
caplog_handler.level == record.levelno):
|
||||
# caplog.atLevel(...) was used with the level of this message, i.e.
|
||||
# it was expected.
|
||||
catchlog_handler.level == record.levelno):
|
||||
# caplog.at_level(...) was used with the level of this message,
|
||||
# i.e. it was expected.
|
||||
return
|
||||
if record.levelno < self._min_level:
|
||||
return
|
||||
@ -74,25 +74,3 @@ def fail_on_logging():
|
||||
yield
|
||||
logging.getLogger().removeHandler(handler)
|
||||
handler.close()
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def caplog_bug_workaround(request):
|
||||
"""WORKAROUND for pytest-capturelog bug.
|
||||
|
||||
https://bitbucket.org/memedough/pytest-capturelog/issues/7/
|
||||
|
||||
This would lead to LogFailHandler failing after skipped tests as there are
|
||||
multiple CaptureLogHandlers.
|
||||
"""
|
||||
yield
|
||||
if caplog_mod is None:
|
||||
return
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
caplog_handlers = [h for h in root_logger.handlers
|
||||
if isinstance(h, caplog_mod.CaptureLogHandler)]
|
||||
|
||||
for h in caplog_handlers:
|
||||
root_logger.removeHandler(h)
|
||||
h.close()
|
||||
|
@ -61,7 +61,7 @@ class MessageMock:
|
||||
}
|
||||
log_level = log_levels[level]
|
||||
|
||||
with self._caplog.atLevel(log_level): # needed so we don't fail
|
||||
with self._caplog.at_level(log_level): # needed so we don't fail
|
||||
logging.getLogger('message').log(log_level, text)
|
||||
|
||||
self.messages.append(Message(level, win_id, text, immediately))
|
||||
|
@ -23,7 +23,7 @@
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
import pytest_capturelog # pylint: disable=import-error
|
||||
import pytest_catchlog # pylint: disable=import-error
|
||||
|
||||
|
||||
def test_log_debug():
|
||||
@ -36,33 +36,33 @@ def test_log_warning():
|
||||
|
||||
|
||||
def test_log_expected(caplog):
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
logging.error('foo')
|
||||
|
||||
|
||||
def test_log_expected_logger(caplog):
|
||||
logger = 'logfail_test_logger'
|
||||
with caplog.atLevel(logging.ERROR, logger):
|
||||
with caplog.at_level(logging.ERROR, logger):
|
||||
logging.getLogger(logger).error('foo')
|
||||
|
||||
|
||||
def test_log_expected_wrong_level(caplog):
|
||||
with pytest.raises(pytest.fail.Exception):
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
logging.critical('foo')
|
||||
|
||||
|
||||
def test_log_expected_logger_wrong_level(caplog):
|
||||
logger = 'logfail_test_logger'
|
||||
with pytest.raises(pytest.fail.Exception):
|
||||
with caplog.atLevel(logging.ERROR, logger):
|
||||
with caplog.at_level(logging.ERROR, logger):
|
||||
logging.getLogger(logger).critical('foo')
|
||||
|
||||
|
||||
def test_log_expected_wrong_logger(caplog):
|
||||
logger = 'logfail_test_logger'
|
||||
with pytest.raises(pytest.fail.Exception):
|
||||
with caplog.atLevel(logging.ERROR, logger):
|
||||
with caplog.at_level(logging.ERROR, logger):
|
||||
logging.error('foo')
|
||||
|
||||
|
||||
@ -82,6 +82,6 @@ def test_caplog_bug_workaround_2():
|
||||
"""
|
||||
caplog_handler = None
|
||||
for h in logging.getLogger().handlers:
|
||||
if isinstance(h, pytest_capturelog.CaptureLogHandler):
|
||||
if isinstance(h, pytest_catchlog.LogCaptureHandler):
|
||||
assert caplog_handler is None
|
||||
caplog_handler = h
|
||||
|
@ -55,7 +55,7 @@ class HeaderChecker:
|
||||
"""Check if the passed header is ignored."""
|
||||
reply = self.stubs.FakeNetworkReply(
|
||||
headers={'Content-Disposition': header})
|
||||
with self.caplog.atLevel(logging.ERROR, 'rfc6266'):
|
||||
with self.caplog.at_level(logging.ERROR, 'rfc6266'):
|
||||
# with self.assertLogs(log.rfc6266, logging.ERROR):
|
||||
cd_inline, cd_filename = http.parse_content_disposition(reply)
|
||||
assert cd_filename == DEFAULT_NAME
|
||||
|
@ -41,7 +41,7 @@ def test_parse_content_disposition(caplog, template, stubs, s):
|
||||
"""Test parsing headers based on templates which hypothesis completes."""
|
||||
header = template.format(s)
|
||||
reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
|
||||
with caplog.atLevel(logging.ERROR, 'rfc6266'):
|
||||
with caplog.at_level(logging.ERROR, 'rfc6266'):
|
||||
http.parse_content_disposition(reply)
|
||||
|
||||
|
||||
|
@ -106,13 +106,12 @@ def test_logging(caplog, objects, tabbed_browser, index_of, verb):
|
||||
tabbed_browser.current_index = 0
|
||||
tabbed_browser.index_of = index_of
|
||||
|
||||
with caplog.atLevel(logging.DEBUG, logger='signals'):
|
||||
with caplog.at_level(logging.DEBUG, logger='signals'):
|
||||
objects.signaller.signal.emit('foo')
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert len(caplog.records) == 1
|
||||
expected_msg = "{}: filtered_signal('foo') (tab {})".format(verb, index_of)
|
||||
assert records[0].msg == expected_msg
|
||||
assert caplog.records[0].msg == expected_msg
|
||||
|
||||
|
||||
@pytest.mark.parametrize('index_of', [0, 1])
|
||||
@ -120,10 +119,10 @@ def test_no_logging(caplog, objects, tabbed_browser, index_of):
|
||||
tabbed_browser.current_index = 0
|
||||
tabbed_browser.index_of = index_of
|
||||
|
||||
with caplog.atLevel(logging.DEBUG, logger='signals'):
|
||||
with caplog.at_level(logging.DEBUG, logger='signals'):
|
||||
objects.signaller.statusbar_message.emit('foo')
|
||||
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
|
||||
def test_runtime_error(objects, tabbed_browser):
|
||||
|
@ -64,12 +64,11 @@ def test_set_register_stylesheet(delete, qtbot, config_stub, caplog):
|
||||
config_stub.data = {'fonts': {'foo': 'bar'}, 'colors': {}}
|
||||
obj = Obj("{{ font['foo'] }}")
|
||||
|
||||
with caplog.atLevel(9): # VDEBUG
|
||||
with caplog.at_level(9): # VDEBUG
|
||||
style.set_register_stylesheet(obj)
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert records[0].message == 'stylesheet for Obj: bar'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == 'stylesheet for Obj: bar'
|
||||
|
||||
assert obj.rendered_stylesheet == 'bar'
|
||||
|
||||
@ -104,11 +103,10 @@ class TestColorDict:
|
||||
|
||||
def test_key_error(self, caplog):
|
||||
d = style.ColorDict()
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
d['foo'] # pylint: disable=pointless-statement
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert records[0].message == 'No color defined for foo!'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == 'No color defined for foo!'
|
||||
|
||||
def test_qcolor(self):
|
||||
d = style.ColorDict()
|
||||
|
@ -74,14 +74,14 @@ class TestDebugLog:
|
||||
|
||||
def test_log(self, keyparser, caplog):
|
||||
keyparser._debug_log('foo')
|
||||
assert len(caplog.records()) == 1
|
||||
record = caplog.records()[0]
|
||||
assert len(caplog.records) == 1
|
||||
record = caplog.records[0]
|
||||
assert record.message == 'foo'
|
||||
|
||||
def test_no_log(self, keyparser, caplog):
|
||||
keyparser.do_log = False
|
||||
keyparser._debug_log('foo')
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
|
||||
@pytest.mark.parametrize('input_key, supports_count, expected', [
|
||||
@ -161,10 +161,10 @@ class TestReadConfig:
|
||||
0, supports_count=False, supports_chains=False)
|
||||
kp._warn_on_keychains = warn_on_keychains
|
||||
|
||||
with caplog.atLevel(logging.WARNING):
|
||||
with caplog.at_level(logging.WARNING):
|
||||
kp.read_config('normal')
|
||||
|
||||
assert bool(caplog.records()) == warn_on_keychains
|
||||
assert bool(caplog.records) == warn_on_keychains
|
||||
|
||||
|
||||
class TestSpecialKeys:
|
||||
|
@ -174,16 +174,16 @@ def test_start_logging(fake_proc, caplog):
|
||||
"""Make sure that starting logs the executed commandline."""
|
||||
cmd = 'does_not_exist'
|
||||
args = ['arg', 'arg with spaces']
|
||||
with caplog.atLevel(logging.DEBUG):
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
fake_proc.start(cmd, args)
|
||||
msgs = [e.msg for e in caplog.records()]
|
||||
msgs = [e.msg for e in caplog.records]
|
||||
assert msgs == ["Starting process.",
|
||||
"Executing: does_not_exist arg 'arg with spaces'"]
|
||||
|
||||
|
||||
def test_error(qtbot, proc, caplog, guiprocess_message_mock):
|
||||
"""Test the process emitting an error."""
|
||||
with caplog.atLevel(logging.ERROR, 'message'):
|
||||
with caplog.at_level(logging.ERROR, 'message'):
|
||||
with qtbot.waitSignal(proc.error, raising=True, timeout=5000):
|
||||
proc.start('this_does_not_exist_either', [])
|
||||
|
||||
|
@ -358,11 +358,10 @@ class TestListen:
|
||||
|
||||
@pytest.mark.posix
|
||||
def test_atime_update_no_name(self, qtbot, caplog, ipc_server):
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
ipc_server.update_atime()
|
||||
|
||||
records = caplog.records()
|
||||
assert records[-1].msg == "In update_atime with no server path!"
|
||||
assert caplog.records[-1].msg == "In update_atime with no server path!"
|
||||
|
||||
@pytest.mark.posix
|
||||
def test_atime_shutdown_typeerror(self, qtbot, ipc_server):
|
||||
@ -408,22 +407,21 @@ class TestHandleConnection:
|
||||
|
||||
def test_no_connection(self, ipc_server, caplog):
|
||||
ipc_server.handle_connection()
|
||||
record = caplog.records()[-1]
|
||||
assert record.message == "No new connection to handle."
|
||||
assert caplog.records[-1].message == "No new connection to handle."
|
||||
|
||||
def test_double_connection(self, qlocalsocket, ipc_server, caplog):
|
||||
ipc_server._socket = qlocalsocket
|
||||
ipc_server.handle_connection()
|
||||
message = ("Got new connection but ignoring it because we're still "
|
||||
"handling another one.")
|
||||
assert message in [rec.message for rec in caplog.records()]
|
||||
assert message in [rec.message for rec in caplog.records]
|
||||
|
||||
def test_disconnected_immediately(self, ipc_server, caplog):
|
||||
socket = FakeSocket(state=QLocalSocket.UnconnectedState)
|
||||
ipc_server._server = FakeServer(socket)
|
||||
ipc_server.handle_connection()
|
||||
msg = "Socket was disconnected immediately."
|
||||
all_msgs = [r.message for r in caplog.records()]
|
||||
all_msgs = [r.message for r in caplog.records]
|
||||
assert msg in all_msgs
|
||||
|
||||
def test_error_immediately(self, ipc_server, caplog):
|
||||
@ -436,7 +434,7 @@ class TestHandleConnection:
|
||||
exc_msg = 'Error while handling IPC connection: Error string (error 7)'
|
||||
assert str(excinfo.value) == exc_msg
|
||||
msg = "We got an error immediately."
|
||||
all_msgs = [r.message for r in caplog.records()]
|
||||
all_msgs = [r.message for r in caplog.records]
|
||||
assert msg in all_msgs
|
||||
|
||||
def test_read_line_immediately(self, qtbot, ipc_server, caplog):
|
||||
@ -454,7 +452,7 @@ class TestHandleConnection:
|
||||
assert spy[0][0] == ['foo']
|
||||
assert spy[0][1] == 'tab'
|
||||
|
||||
all_msgs = [r.message for r in caplog.records()]
|
||||
all_msgs = [r.message for r in caplog.records]
|
||||
assert "We can read a line immediately." in all_msgs
|
||||
|
||||
|
||||
@ -505,11 +503,11 @@ def test_invalid_data(qtbot, ipc_server, connected_socket, caplog, data, msg):
|
||||
got_args_spy = QSignalSpy(ipc_server.got_args)
|
||||
|
||||
signals = [ipc_server.got_invalid_data, connected_socket.disconnected]
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignals(signals, raising=True):
|
||||
connected_socket.write(data)
|
||||
|
||||
messages = [r.message for r in caplog.records()]
|
||||
messages = [r.message for r in caplog.records]
|
||||
assert messages[-1] == 'Ignoring invalid IPC data.'
|
||||
assert messages[-2].startswith(msg)
|
||||
assert not got_args_spy
|
||||
@ -542,7 +540,7 @@ class TestSendToRunningInstance:
|
||||
def test_no_server(self, caplog):
|
||||
sent = ipc.send_to_running_instance('qute-test', [], None)
|
||||
assert not sent
|
||||
msg = caplog.records()[-1].message
|
||||
msg = caplog.records[-1].message
|
||||
assert msg == "No existing instance present (error 2)"
|
||||
|
||||
@pytest.mark.parametrize('has_cwd', [True, False])
|
||||
@ -610,12 +608,12 @@ def test_timeout(qtbot, caplog, qlocalsocket, ipc_server):
|
||||
with qtbot.waitSignal(ipc_server._server.newConnection, raising=True):
|
||||
qlocalsocket.connectToServer('qute-test')
|
||||
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignal(qlocalsocket.disconnected, raising=True,
|
||||
timeout=5000):
|
||||
pass
|
||||
|
||||
assert caplog.records()[-1].message == "IPC connection timed out."
|
||||
assert caplog.records[-1].message == "IPC connection timed out."
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method, args, is_warning', [
|
||||
@ -628,13 +626,13 @@ def test_ipcserver_socket_none(ipc_server, caplog, method, args, is_warning):
|
||||
assert ipc_server._socket is None
|
||||
|
||||
if is_warning:
|
||||
with caplog.atLevel(logging.WARNING):
|
||||
with caplog.at_level(logging.WARNING):
|
||||
func(*args)
|
||||
else:
|
||||
func(*args)
|
||||
|
||||
msg = "In {} with None socket!".format(method)
|
||||
assert msg in [r.message for r in caplog.records()]
|
||||
assert msg in [r.message for r in caplog.records]
|
||||
|
||||
|
||||
class TestSendOrListen:
|
||||
@ -683,7 +681,7 @@ class TestSendOrListen:
|
||||
def test_normal_connection(self, caplog, qtbot, args):
|
||||
ret_server = ipc.send_or_listen(args)
|
||||
assert isinstance(ret_server, ipc.IPCServer)
|
||||
msgs = [e.message for e in caplog.records()]
|
||||
msgs = [e.message for e in caplog.records]
|
||||
assert "Starting IPC server..." in msgs
|
||||
objreg_server = objreg.get('ipc-server')
|
||||
assert objreg_server is ret_server
|
||||
@ -698,7 +696,7 @@ class TestSendOrListen:
|
||||
with qtbot.waitSignal(legacy_server.got_args, raising=True):
|
||||
ret = ipc.send_or_listen(args)
|
||||
assert ret is None
|
||||
msgs = [e.message for e in caplog.records()]
|
||||
msgs = [e.message for e in caplog.records]
|
||||
assert "Connecting to {}".format(legacy_server._socketname) in msgs
|
||||
|
||||
@pytest.mark.posix(reason="Unneeded on Windows")
|
||||
@ -775,7 +773,7 @@ class TestSendOrListen:
|
||||
|
||||
ret = ipc.send_or_listen(args)
|
||||
assert ret is None
|
||||
msgs = [e.message for e in caplog.records()]
|
||||
msgs = [e.message for e in caplog.records]
|
||||
assert "Got AddressInUseError, trying again." in msgs
|
||||
|
||||
@pytest.mark.parametrize('has_error, exc_name, exc_msg', [
|
||||
@ -812,12 +810,11 @@ class TestSendOrListen:
|
||||
QLocalSocket.ConnectionRefusedError, # error() gets called twice
|
||||
]
|
||||
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with pytest.raises(ipc.Error):
|
||||
ipc.send_or_listen(args)
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
error_msgs = [
|
||||
'Handling fatal misc.ipc.{} with --no-err-windows!'.format(
|
||||
@ -828,7 +825,7 @@ class TestSendOrListen:
|
||||
'post_text: Maybe another instance is running but frozen?',
|
||||
'exception text: {}'.format(exc_msg),
|
||||
]
|
||||
assert records[0].msg == '\n'.join(error_msgs)
|
||||
assert caplog.records[0].msg == '\n'.join(error_msgs)
|
||||
|
||||
@pytest.mark.posix(reason="Flaky on Windows")
|
||||
def test_error_while_listening(self, qlocalserver_mock, caplog, args):
|
||||
@ -837,12 +834,11 @@ class TestSendOrListen:
|
||||
err = QAbstractSocket.SocketResourceError
|
||||
qlocalserver_mock().serverError.return_value = err
|
||||
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with pytest.raises(ipc.Error):
|
||||
ipc.send_or_listen(args)
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
error_msgs = [
|
||||
'Handling fatal misc.ipc.ListenError with --no-err-windows!',
|
||||
@ -853,7 +849,7 @@ class TestSendOrListen:
|
||||
'exception text: Error while listening to IPC server: Error '
|
||||
'string (error 4)',
|
||||
]
|
||||
assert records[0].msg == '\n'.join(error_msgs)
|
||||
assert caplog.records[0].msg == '\n'.join(error_msgs)
|
||||
|
||||
|
||||
@pytest.mark.windows
|
||||
|
@ -821,10 +821,9 @@ class TestSessionDelete:
|
||||
tmpdir.chmod(0o555) # unwritable
|
||||
|
||||
with pytest.raises(cmdexc.CommandError) as excinfo:
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
sess_man.session_delete('foo')
|
||||
|
||||
assert str(excinfo.value).startswith('Error while deleting session: ')
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert records[0].message == 'Error while deleting session!'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == 'Error while deleting session!'
|
||||
|
@ -41,9 +41,8 @@ def test_log_events(qapp, caplog):
|
||||
obj = EventObject()
|
||||
qapp.postEvent(obj, QEvent(QEvent.User))
|
||||
qapp.processEvents()
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert records[0].msg == 'Event in test_debug.EventObject: User'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].msg == 'Event in test_debug.EventObject: User'
|
||||
|
||||
|
||||
class SignalObject(QObject):
|
||||
@ -75,10 +74,9 @@ def test_log_signals(caplog, signal_obj):
|
||||
signal_obj.signal1.emit()
|
||||
signal_obj.signal2.emit('foo', 'bar')
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 2
|
||||
assert records[0].msg == 'Signal in <repr>: signal1()'
|
||||
assert records[1].msg == "Signal in <repr>: signal2('foo', 'bar')"
|
||||
assert len(caplog.records) == 2
|
||||
assert caplog.records[0].msg == 'Signal in <repr>: signal1()'
|
||||
assert caplog.records[1].msg == "Signal in <repr>: signal2('foo', 'bar')"
|
||||
|
||||
|
||||
class TestLogTime:
|
||||
@ -86,15 +84,14 @@ class TestLogTime:
|
||||
def test_duration(self, caplog):
|
||||
logger_name = 'qt-tests'
|
||||
|
||||
with caplog.atLevel(logging.DEBUG, logger_name):
|
||||
with caplog.at_level(logging.DEBUG, logger_name):
|
||||
with debug.log_time(logger_name, action='foobar'):
|
||||
time.sleep(0.1)
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
pattern = re.compile(r'^Foobar took ([\d.]*) seconds\.$')
|
||||
match = pattern.match(records[0].msg)
|
||||
match = pattern.match(caplog.records[0].msg)
|
||||
assert match
|
||||
|
||||
duration = float(match.group(1))
|
||||
@ -104,11 +101,11 @@ class TestLogTime:
|
||||
"""Test with an explicit logger instead of a name."""
|
||||
logger_name = 'qt-tests'
|
||||
|
||||
with caplog.atLevel(logging.DEBUG, logger_name):
|
||||
with caplog.at_level(logging.DEBUG, logger_name):
|
||||
with debug.log_time(logging.getLogger(logger_name)):
|
||||
pass
|
||||
|
||||
assert len(caplog.records()) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
def test_decorator(self, caplog):
|
||||
logger_name = 'qt-tests'
|
||||
@ -118,12 +115,11 @@ class TestLogTime:
|
||||
assert arg == 1
|
||||
assert kwarg == 2
|
||||
|
||||
with caplog.atLevel(logging.DEBUG, logger_name):
|
||||
with caplog.at_level(logging.DEBUG, logger_name):
|
||||
func(1, kwarg=2)
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert records[0].msg.startswith('Foo took')
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].msg.startswith('Foo took')
|
||||
|
||||
|
||||
class TestQEnumKey:
|
||||
|
@ -52,12 +52,11 @@ def test_no_err_windows(caplog, exc, name, exc_text):
|
||||
try:
|
||||
raise exc
|
||||
except Exception as e:
|
||||
with caplog.atLevel(logging.ERROR):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
error.handle_fatal_exc(e, Args(no_err_windows=True), 'title',
|
||||
pre_text='pre', post_text='post')
|
||||
|
||||
records = caplog.records()
|
||||
assert len(records) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
expected = [
|
||||
'Handling fatal {} with --no-err-windows!'.format(name),
|
||||
@ -67,7 +66,7 @@ def test_no_err_windows(caplog, exc, name, exc_text):
|
||||
'post_text: post',
|
||||
'exception text: {}'.format(exc_text),
|
||||
]
|
||||
assert records[0].msg == '\n'.join(expected)
|
||||
assert caplog.records[0].msg == '\n'.join(expected)
|
||||
|
||||
|
||||
# This happens on Xvfb for some reason
|
||||
|
@ -25,7 +25,7 @@ import itertools
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
import pytest_capturelog # pylint: disable=import-error
|
||||
import pytest_catchlog # pylint: disable=import-error
|
||||
|
||||
from qutebrowser.utils import log
|
||||
|
||||
@ -60,10 +60,11 @@ def restore_loggers():
|
||||
while root_logger.handlers:
|
||||
h = root_logger.handlers[0]
|
||||
root_logger.removeHandler(h)
|
||||
h.close()
|
||||
if not isinstance(h, pytest_catchlog.LogCaptureHandler):
|
||||
h.close()
|
||||
root_logger.setLevel(original_logging_level)
|
||||
for h in root_handlers:
|
||||
if not isinstance(h, pytest_capturelog.CaptureLogHandler):
|
||||
if not isinstance(h, pytest_catchlog.LogCaptureHandler):
|
||||
# https://github.com/The-Compiler/qutebrowser/issues/856
|
||||
root_logger.addHandler(h)
|
||||
logging._acquireLock()
|
||||
@ -238,30 +239,30 @@ class TestHideQtWarning:
|
||||
def test_unfiltered(self, logger, caplog):
|
||||
"""Test a message which is not filtered."""
|
||||
with log.hide_qt_warning("World", 'qt-tests'):
|
||||
with caplog.atLevel(logging.WARNING, 'qt-tests'):
|
||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||
logger.warning("Hello World")
|
||||
assert len(caplog.records()) == 1
|
||||
record = caplog.records()[0]
|
||||
assert len(caplog.records) == 1
|
||||
record = caplog.records[0]
|
||||
assert record.levelname == 'WARNING'
|
||||
assert record.message == "Hello World"
|
||||
|
||||
def test_filtered_exact(self, logger, caplog):
|
||||
"""Test a message which is filtered (exact match)."""
|
||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
||||
with caplog.atLevel(logging.WARNING, 'qt-tests'):
|
||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||
logger.warning("Hello")
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
def test_filtered_start(self, logger, caplog):
|
||||
"""Test a message which is filtered (match at line start)."""
|
||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
||||
with caplog.atLevel(logging.WARNING, 'qt-tests'):
|
||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||
logger.warning("Hello World")
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
def test_filtered_whitespace(self, logger, caplog):
|
||||
"""Test a message which is filtered (match with whitespace)."""
|
||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
||||
with caplog.atLevel(logging.WARNING, 'qt-tests'):
|
||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||
logger.warning(" Hello World ")
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
@ -263,10 +263,10 @@ class TestInitCacheDirTag:
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
||||
lambda: str(tmpdir))
|
||||
mocker.patch('builtins.open', side_effect=OSError)
|
||||
with caplog.atLevel(logging.ERROR, 'init'):
|
||||
with caplog.at_level(logging.ERROR, 'init'):
|
||||
standarddir._init_cachedir_tag()
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == 'Failed to create CACHEDIR.TAG'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == 'Failed to create CACHEDIR.TAG'
|
||||
assert not tmpdir.listdir()
|
||||
|
||||
|
||||
|
@ -241,11 +241,11 @@ class TestActuteWarning:
|
||||
mocker.patch('qutebrowser.utils.utils.open', side_effect=OSError,
|
||||
create=True)
|
||||
|
||||
with caplog.atLevel(logging.ERROR, 'init'):
|
||||
with caplog.at_level(logging.ERROR, 'init'):
|
||||
utils.actute_warning()
|
||||
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == 'Failed to read Compose file'
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == 'Failed to read Compose file'
|
||||
out, _err = capsys.readouterr()
|
||||
assert not out
|
||||
|
||||
@ -669,12 +669,12 @@ class TestPreventExceptions:
|
||||
|
||||
def test_raising(self, caplog):
|
||||
"""Test with a raising function."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
ret = self.func_raising()
|
||||
assert ret == 42
|
||||
assert len(caplog.records()) == 1
|
||||
assert len(caplog.records) == 1
|
||||
expected = 'Error in test_utils.TestPreventExceptions.func_raising'
|
||||
actual = caplog.records()[0].message
|
||||
actual = caplog.records[0].message
|
||||
assert actual == expected
|
||||
|
||||
@utils.prevent_exceptions(42)
|
||||
@ -683,10 +683,10 @@ class TestPreventExceptions:
|
||||
|
||||
def test_not_raising(self, caplog):
|
||||
"""Test with a non-raising function."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
ret = self.func_not_raising()
|
||||
assert ret == 23
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
@utils.prevent_exceptions(42, True)
|
||||
def func_predicate_true(self):
|
||||
@ -694,10 +694,10 @@ class TestPreventExceptions:
|
||||
|
||||
def test_predicate_true(self, caplog):
|
||||
"""Test with a True predicate."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
ret = self.func_predicate_true()
|
||||
assert ret == 42
|
||||
assert len(caplog.records()) == 1
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
@utils.prevent_exceptions(42, False)
|
||||
def func_predicate_false(self):
|
||||
@ -705,10 +705,10 @@ class TestPreventExceptions:
|
||||
|
||||
def test_predicate_false(self, caplog):
|
||||
"""Test with a False predicate."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
with pytest.raises(Exception):
|
||||
self.func_predicate_false()
|
||||
assert not caplog.records()
|
||||
assert not caplog.records
|
||||
|
||||
|
||||
class Obj:
|
||||
|
@ -108,7 +108,7 @@ class TestGitStr:
|
||||
monkeypatch.setattr(qutebrowser.utils.version.sys, 'frozen', True,
|
||||
raising=False)
|
||||
commit_file_mock.side_effect = OSError
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
assert version._git_str() is None
|
||||
|
||||
@pytest.mark.not_frozen
|
||||
@ -136,7 +136,7 @@ class TestGitStr:
|
||||
m.path.join.side_effect = OSError
|
||||
mocker.patch('qutebrowser.utils.version.utils.read_file',
|
||||
side_effect=OSError)
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
assert version._git_str() is None
|
||||
|
||||
@pytest.mark.not_frozen
|
||||
@ -145,10 +145,10 @@ class TestGitStr:
|
||||
"""Test with undefined __file__ but available git-commit-id."""
|
||||
monkeypatch.delattr('qutebrowser.utils.version.__file__')
|
||||
commit_file_mock.return_value = '0deadcode'
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
assert version._git_str() == '0deadcode'
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == "Error while getting git path"
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == "Error while getting git path"
|
||||
|
||||
|
||||
def _has_git():
|
||||
@ -294,11 +294,11 @@ def test_release_info(files, expected, caplog, monkeypatch):
|
||||
fake = ReleaseInfoFake(files)
|
||||
monkeypatch.setattr('qutebrowser.utils.version.glob.glob', fake.glob_fake)
|
||||
monkeypatch.setattr(version, 'open', fake.open_fake, raising=False)
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
assert version._release_info() == expected
|
||||
if files is None:
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == "Error while reading fake-file."
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == "Error while reading fake-file."
|
||||
|
||||
|
||||
class ImportFake:
|
||||
|
@ -86,6 +86,6 @@ def test_abort_typeerror(question, qtbot, mocker, caplog):
|
||||
"""Test Question.abort() with .emit() raising a TypeError."""
|
||||
signal_mock = mocker.patch('qutebrowser.utils.usertypes.Question.aborted')
|
||||
signal_mock.emit.side_effect = TypeError
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with caplog.at_level(logging.ERROR, 'misc'):
|
||||
question.abort()
|
||||
assert caplog.records()[0].message == 'Error while aborting question'
|
||||
assert caplog.records[0].message == 'Error while aborting question'
|
||||
|
Loading…
Reference in New Issue
Block a user