Fix unit tests
Apart from changed parameters, messages now log even when messagemock is used, so we needed to add a few caplog.at_level calls.
This commit is contained in:
parent
713a74cfd4
commit
fce9783570
@ -21,6 +21,7 @@
|
||||
import os
|
||||
import zipfile
|
||||
import shutil
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
@ -238,7 +239,7 @@ def test_without_datadir(config_stub, tmpdir, monkeypatch, win_registry):
|
||||
host_blocker = adblock.HostBlocker()
|
||||
|
||||
with pytest.raises(cmdexc.CommandError) as excinfo:
|
||||
host_blocker.adblock_update(0)
|
||||
host_blocker.adblock_update()
|
||||
assert str(excinfo.value) == "No data storage is configured!"
|
||||
|
||||
host_blocker.read_hosts()
|
||||
@ -250,7 +251,7 @@ def test_without_datadir(config_stub, tmpdir, monkeypatch, win_registry):
|
||||
|
||||
|
||||
def test_disabled_blocking_update(basedir, config_stub, download_stub,
|
||||
data_tmpdir, tmpdir, win_registry):
|
||||
data_tmpdir, tmpdir, win_registry, caplog):
|
||||
"""Ensure no URL is blocked when host blocking is disabled."""
|
||||
config_stub.data = {
|
||||
'content': {
|
||||
@ -259,10 +260,11 @@ def test_disabled_blocking_update(basedir, config_stub, download_stub,
|
||||
}
|
||||
}
|
||||
host_blocker = adblock.HostBlocker()
|
||||
host_blocker.adblock_update(0)
|
||||
host_blocker.adblock_update()
|
||||
while host_blocker._in_progress:
|
||||
current_download = host_blocker._in_progress[0]
|
||||
current_download.finished.emit()
|
||||
with caplog.at_level(logging.ERROR):
|
||||
current_download.finished.emit()
|
||||
host_blocker.read_hosts()
|
||||
for str_url in URLS_TO_CHECK:
|
||||
assert not host_blocker.is_blocked(QUrl(str_url))
|
||||
@ -278,14 +280,14 @@ def test_no_blocklist_update(config_stub, download_stub,
|
||||
}
|
||||
}
|
||||
host_blocker = adblock.HostBlocker()
|
||||
host_blocker.adblock_update(0)
|
||||
host_blocker.adblock_update()
|
||||
host_blocker.read_hosts()
|
||||
for str_url in URLS_TO_CHECK:
|
||||
assert not host_blocker.is_blocked(QUrl(str_url))
|
||||
|
||||
|
||||
def test_successful_update(config_stub, basedir, download_stub,
|
||||
data_tmpdir, tmpdir, win_registry):
|
||||
data_tmpdir, tmpdir, win_registry, caplog):
|
||||
"""Ensure hosts from host-block-lists are blocked after an update."""
|
||||
config_stub.data = {
|
||||
'content': {
|
||||
@ -295,17 +297,18 @@ def test_successful_update(config_stub, basedir, download_stub,
|
||||
}
|
||||
}
|
||||
host_blocker = adblock.HostBlocker()
|
||||
host_blocker.adblock_update(0)
|
||||
host_blocker.adblock_update()
|
||||
# Simulate download is finished
|
||||
while host_blocker._in_progress:
|
||||
current_download = host_blocker._in_progress[0]
|
||||
current_download.finished.emit()
|
||||
with caplog.at_level(logging.ERROR):
|
||||
current_download.finished.emit()
|
||||
host_blocker.read_hosts()
|
||||
assert_urls(host_blocker, whitelisted=[])
|
||||
|
||||
|
||||
def test_failed_dl_update(config_stub, basedir, download_stub,
|
||||
data_tmpdir, tmpdir, win_registry):
|
||||
data_tmpdir, tmpdir, win_registry, caplog):
|
||||
"""One blocklist fails to download.
|
||||
|
||||
Ensure hosts from this list are not blocked.
|
||||
@ -323,13 +326,14 @@ def test_failed_dl_update(config_stub, basedir, download_stub,
|
||||
}
|
||||
}
|
||||
host_blocker = adblock.HostBlocker()
|
||||
host_blocker.adblock_update(0)
|
||||
host_blocker.adblock_update()
|
||||
while host_blocker._in_progress:
|
||||
current_download = host_blocker._in_progress[0]
|
||||
# if current download is the file we want to fail, make it fail
|
||||
if current_download.name == dl_fail_blocklist.path():
|
||||
current_download.successful = False
|
||||
current_download.finished.emit()
|
||||
with caplog.at_level(logging.ERROR):
|
||||
current_download.finished.emit()
|
||||
host_blocker.read_hosts()
|
||||
assert_urls(host_blocker, whitelisted=[])
|
||||
|
||||
|
@ -64,7 +64,7 @@ def runner(request):
|
||||
request.param is userscripts._POSIXUserscriptRunner):
|
||||
pytest.skip("Requires a POSIX os")
|
||||
else:
|
||||
return request.param(0)
|
||||
return request.param()
|
||||
|
||||
|
||||
def test_command(qtbot, py_proc, runner):
|
||||
@ -143,7 +143,7 @@ def test_source(qtbot, py_proc, runner):
|
||||
assert not os.path.exists(parsed['html_file'])
|
||||
|
||||
|
||||
def test_command_with_error(qtbot, py_proc, runner):
|
||||
def test_command_with_error(qtbot, py_proc, runner, caplog):
|
||||
cmd, args = py_proc(r"""
|
||||
import sys, os, json
|
||||
|
||||
@ -154,17 +154,18 @@ def test_command_with_error(qtbot, py_proc, runner):
|
||||
sys.exit(1)
|
||||
""")
|
||||
|
||||
with qtbot.waitSignal(runner.finished, timeout=10000):
|
||||
with qtbot.waitSignal(runner.got_cmd, timeout=10000) as blocker:
|
||||
runner.prepare_run(cmd, *args)
|
||||
runner.store_text('Hello World')
|
||||
runner.store_html('')
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignal(runner.finished, timeout=10000):
|
||||
with qtbot.waitSignal(runner.got_cmd, timeout=10000) as blocker:
|
||||
runner.prepare_run(cmd, *args)
|
||||
runner.store_text('Hello World')
|
||||
runner.store_html('')
|
||||
|
||||
data = json.loads(blocker.args[0])
|
||||
assert not os.path.exists(data)
|
||||
|
||||
|
||||
def test_killed_command(qtbot, tmpdir, py_proc, runner):
|
||||
def test_killed_command(qtbot, tmpdir, py_proc, runner, caplog):
|
||||
data_file = tmpdir / 'data'
|
||||
watcher = QFileSystemWatcher()
|
||||
watcher.addPath(str(tmpdir))
|
||||
@ -201,8 +202,9 @@ def test_killed_command(qtbot, tmpdir, py_proc, runner):
|
||||
|
||||
data = json.load(data_file)
|
||||
|
||||
with qtbot.waitSignal(runner.finished):
|
||||
os.kill(int(data['pid']), signal.SIGTERM)
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignal(runner.finished):
|
||||
os.kill(int(data['pid']), signal.SIGTERM)
|
||||
|
||||
assert not os.path.exists(data['text_file'])
|
||||
|
||||
|
@ -21,12 +21,14 @@
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import logging
|
||||
from unittest import mock
|
||||
|
||||
from PyQt5.QtCore import QProcess
|
||||
import pytest
|
||||
|
||||
from qutebrowser.misc import editor as editormod
|
||||
from qutebrowser.utils import usertypes
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@ -41,11 +43,12 @@ def patch_things(config_stub, monkeypatch, stubs):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def editor():
|
||||
def editor(caplog):
|
||||
ed = editormod.ExternalEditor()
|
||||
ed.editing_finished = mock.Mock()
|
||||
yield ed
|
||||
ed._cleanup()
|
||||
with caplog.at_level(logging.ERROR):
|
||||
ed._cleanup()
|
||||
|
||||
|
||||
class TestArg:
|
||||
@ -121,24 +124,29 @@ class TestFileHandling:
|
||||
os.remove(filename)
|
||||
|
||||
@pytest.mark.posix
|
||||
def test_unreadable(self, message_mock, editor):
|
||||
def test_unreadable(self, message_mock, editor, caplog):
|
||||
"""Test file handling when closing with an unreadable file."""
|
||||
editor.edit("")
|
||||
filename = editor._file.name
|
||||
assert os.path.exists(filename)
|
||||
os.chmod(filename, 0o077)
|
||||
editor._proc.finished.emit(0, QProcess.NormalExit)
|
||||
with caplog.at_level(logging.ERROR):
|
||||
editor._proc.finished.emit(0, QProcess.NormalExit)
|
||||
assert not os.path.exists(filename)
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
||||
assert msg.text.startswith("Failed to read back edited file: ")
|
||||
|
||||
@pytest.mark.posix
|
||||
def test_unwritable(self, monkeypatch, message_mock, editor, tmpdir):
|
||||
def test_unwritable(self, monkeypatch, message_mock, editor, tmpdir,
|
||||
caplog):
|
||||
"""Test file handling when the initial file is not writable."""
|
||||
tmpdir.chmod(0)
|
||||
monkeypatch.setattr('qutebrowser.misc.editor.tempfile.tempdir',
|
||||
str(tmpdir))
|
||||
editor.edit("")
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
editor.edit("")
|
||||
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
||||
assert msg.text.startswith("Failed to create initial file: ")
|
||||
assert editor._proc is None
|
||||
|
@ -26,19 +26,21 @@ import pytest
|
||||
from PyQt5.QtCore import QProcess, QIODevice
|
||||
|
||||
from qutebrowser.misc import guiprocess
|
||||
from qutebrowser.utils import usertypes
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def proc(qtbot):
|
||||
def proc(qtbot, caplog):
|
||||
"""A fixture providing a GUIProcess and cleaning it up after the test."""
|
||||
p = guiprocess.GUIProcess('testprocess')
|
||||
yield p
|
||||
if p._proc.state() == QProcess.Running:
|
||||
with qtbot.waitSignal(p.finished, timeout=10000,
|
||||
raising=False) as blocker:
|
||||
p._proc.terminate()
|
||||
if not blocker.signal_triggered:
|
||||
p._proc.kill()
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignal(p.finished, timeout=10000,
|
||||
raising=False) as blocker:
|
||||
p._proc.terminate()
|
||||
if not blocker.signal_triggered:
|
||||
p._proc.kill()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@ -121,12 +123,13 @@ def test_start_detached(fake_proc):
|
||||
fake_proc._proc.startDetached.assert_called_with(*list(argv) + [None])
|
||||
|
||||
|
||||
def test_start_detached_error(fake_proc, message_mock):
|
||||
def test_start_detached_error(fake_proc, message_mock, caplog):
|
||||
"""Test starting a detached process with ok=False."""
|
||||
argv = ['foo', 'bar']
|
||||
fake_proc._proc.startDetached.return_value = (False, 0)
|
||||
fake_proc._proc.error.return_value = "Error message"
|
||||
fake_proc.start_detached(*argv)
|
||||
with caplog.at_level(logging.ERROR):
|
||||
fake_proc.start_detached(*argv)
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
||||
assert msg.text == "Error while spawning testprocess: Error message."
|
||||
|
||||
@ -183,9 +186,10 @@ def test_error(qtbot, proc, caplog, message_mock):
|
||||
assert msg.text == expected_msg
|
||||
|
||||
|
||||
def test_exit_unsuccessful(qtbot, proc, message_mock, py_proc):
|
||||
with qtbot.waitSignal(proc.finished, timeout=10000):
|
||||
proc.start(*py_proc('import sys; sys.exit(1)'))
|
||||
def test_exit_unsuccessful(qtbot, proc, message_mock, py_proc, caplog):
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with qtbot.waitSignal(proc.finished, timeout=10000):
|
||||
proc.start(*py_proc('import sys; sys.exit(1)'))
|
||||
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
||||
assert msg.text == "Testprocess exited with status 1."
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
import os.path
|
||||
import collections
|
||||
import logging
|
||||
|
||||
from PyQt5.QtCore import QUrl
|
||||
import pytest
|
||||
@ -218,13 +219,15 @@ class TestFuzzyUrl:
|
||||
(True, qtutils.QtValueError),
|
||||
(False, urlutils.InvalidUrlError),
|
||||
])
|
||||
def test_invalid_url(self, do_search, exception, is_url_mock, monkeypatch):
|
||||
def test_invalid_url(self, do_search, exception, is_url_mock, monkeypatch,
|
||||
caplog):
|
||||
"""Test with an invalid URL."""
|
||||
is_url_mock.return_value = True
|
||||
monkeypatch.setattr('qutebrowser.utils.urlutils.qurl_from_user_input',
|
||||
lambda url: QUrl())
|
||||
with pytest.raises(exception):
|
||||
urlutils.fuzzy_url('foo', do_search=do_search)
|
||||
with caplog.at_level(logging.ERROR):
|
||||
urlutils.fuzzy_url('foo', do_search=do_search)
|
||||
|
||||
@pytest.mark.parametrize('url', ['', ' '])
|
||||
def test_empty(self, url):
|
||||
@ -412,7 +415,7 @@ def test_qurl_from_user_input(user_input, output):
|
||||
('', False, False),
|
||||
('://', False, True),
|
||||
])
|
||||
def test_invalid_url_error(message_mock, url, valid, has_err_string):
|
||||
def test_invalid_url_error(message_mock, caplog, url, valid, has_err_string):
|
||||
"""Test invalid_url_error().
|
||||
|
||||
Args:
|
||||
@ -428,7 +431,8 @@ def test_invalid_url_error(message_mock, url, valid, has_err_string):
|
||||
assert not message_mock.messages
|
||||
else:
|
||||
assert bool(qurl.errorString()) == has_err_string
|
||||
urlutils.invalid_url_error(qurl, 'frozzle')
|
||||
with caplog.at_level(logging.ERROR):
|
||||
urlutils.invalid_url_error(qurl, 'frozzle')
|
||||
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.error)
|
||||
if has_err_string:
|
||||
|
Loading…
Reference in New Issue
Block a user