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