Merge branch 'util-tests-1'
This commit is contained in:
commit
425fcdf8e4
@ -170,6 +170,7 @@ def _init_cachedir_tag():
|
||||
f.write("# This file is a cache directory tag created by "
|
||||
"qutebrowser.\n")
|
||||
f.write("# For information about cache directory tags, see:\n")
|
||||
f.write("# http://www.brynosaurus.com/cachedir/\n")
|
||||
f.write("# http://www.brynosaurus.com/" # pragma: no branch
|
||||
"cachedir/\n")
|
||||
except OSError:
|
||||
log.init.exception("Failed to create CACHEDIR.TAG")
|
||||
|
@ -50,8 +50,6 @@ def elide(text, length):
|
||||
def compact_text(text, elidelength=None):
|
||||
"""Remove leading whitespace and newlines from a text and maybe elide it.
|
||||
|
||||
FIXME: Add tests.
|
||||
|
||||
Args:
|
||||
text: The text to compact.
|
||||
elidelength: To how many chars to elide.
|
||||
@ -105,12 +103,12 @@ def actute_warning():
|
||||
try:
|
||||
if qtutils.version_check('5.3.0'):
|
||||
return
|
||||
except ValueError:
|
||||
except ValueError: # pragma: no cover
|
||||
pass
|
||||
try:
|
||||
with open('/usr/share/X11/locale/en_US.UTF-8/Compose', 'r',
|
||||
encoding='utf-8') as f:
|
||||
for line in f:
|
||||
for line in f: # pragma: no branch
|
||||
if '<dead_actute>' in line:
|
||||
if sys.stdout is not None:
|
||||
sys.stdout.flush()
|
||||
@ -118,7 +116,7 @@ def actute_warning():
|
||||
"that is not a bug in qutebrowser! See "
|
||||
"https://bugs.freedesktop.org/show_bug.cgi?id=69476 "
|
||||
"for details.")
|
||||
break
|
||||
break # pragma: no branch
|
||||
except OSError:
|
||||
log.init.exception("Failed to read Compose file")
|
||||
|
||||
|
@ -111,7 +111,7 @@ def _release_info():
|
||||
for fn in glob.glob("/etc/*-release"):
|
||||
try:
|
||||
with open(fn, 'r', encoding='utf-8') as f:
|
||||
data.append((fn, ''.join(f.readlines())))
|
||||
data.append((fn, ''.join(f.readlines()))) # pragma: no branch
|
||||
except OSError:
|
||||
log.misc.exception("Error while reading {}.".format(fn))
|
||||
return data
|
||||
|
@ -19,15 +19,51 @@
|
||||
|
||||
"""Tests for qutebrowser.utils.qtutils."""
|
||||
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import operator
|
||||
import os.path
|
||||
try:
|
||||
from test import test_file # pylint: disable=no-name-in-module
|
||||
except ImportError:
|
||||
# Debian patches Python to remove the tests...
|
||||
test_file = None
|
||||
|
||||
import pytest
|
||||
import unittest
|
||||
import unittest.mock
|
||||
from PyQt5.QtCore import (QDataStream, QPoint, QUrl, QByteArray, QIODevice,
|
||||
QTimer, QBuffer, QFile, QProcess)
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
||||
from qutebrowser import qutebrowser
|
||||
from qutebrowser.utils import qtutils
|
||||
import overflow_test_cases
|
||||
|
||||
|
||||
@pytest.mark.parametrize('qversion, version, op, expected', [
|
||||
('5.4.0', '5.4.0', operator.ge, True),
|
||||
('5.4.0', '5.4.0', operator.eq, True),
|
||||
('5.4.0', '5.4', operator.eq, True),
|
||||
('5.4.1', '5.4', operator.ge, True),
|
||||
('5.3.2', '5.4', operator.ge, False),
|
||||
('5.3.0', '5.3.2', operator.ge, False),
|
||||
])
|
||||
def test_version_check(monkeypatch, qversion, version, op, expected):
|
||||
"""Test for version_check().
|
||||
|
||||
Args:
|
||||
monkeypatch: The pytest monkeypatch fixture.
|
||||
qversion: The version to set as fake qVersion().
|
||||
version: The version to compare with.
|
||||
op: The operator to use when comparing.
|
||||
expected: The expected result.
|
||||
"""
|
||||
monkeypatch.setattr('qutebrowser.utils.qtutils.qVersion', lambda: qversion)
|
||||
assert qtutils.version_check(version, op) == expected
|
||||
|
||||
|
||||
class TestCheckOverflow:
|
||||
|
||||
"""Test check_overflow."""
|
||||
@ -68,20 +104,18 @@ class TestGetQtArgs:
|
||||
mocker.patch.object(parser, 'exit', side_effect=Exception)
|
||||
return parser
|
||||
|
||||
def test_no_qt_args(self, parser):
|
||||
@pytest.mark.parametrize('args, expected', [
|
||||
# No Qt arguments
|
||||
(['--debug'], [sys.argv[0]]),
|
||||
# Qt flag
|
||||
(['--debug', '--qt-reverse', '--nocolor'], [sys.argv[0], '-reverse']),
|
||||
# Qt argument with value
|
||||
(['--qt-stylesheet', 'foo'], [sys.argv[0], '-stylesheet', 'foo']),
|
||||
])
|
||||
def test_qt_args(self, args, expected, parser):
|
||||
"""Test commandline with no Qt arguments given."""
|
||||
args = parser.parse_args(['--debug'])
|
||||
assert qtutils.get_args(args) == [sys.argv[0]]
|
||||
|
||||
def test_qt_flag(self, parser):
|
||||
"""Test commandline with a Qt flag."""
|
||||
args = parser.parse_args(['--debug', '--qt-reverse', '--nocolor'])
|
||||
assert qtutils.get_args(args) == [sys.argv[0], '-reverse']
|
||||
|
||||
def test_qt_arg(self, parser):
|
||||
"""Test commandline with a Qt argument."""
|
||||
args = parser.parse_args(['--qt-stylesheet', 'foobar'])
|
||||
assert qtutils.get_args(args) == [sys.argv[0], '-stylesheet', 'foobar']
|
||||
parsed = parser.parse_args(args)
|
||||
assert qtutils.get_args(parsed) == expected
|
||||
|
||||
def test_qt_both(self, parser):
|
||||
"""Test commandline with a Qt argument and flag."""
|
||||
@ -91,3 +125,836 @@ class TestGetQtArgs:
|
||||
assert '-reverse' in qt_args
|
||||
assert '-stylesheet' in qt_args
|
||||
assert 'foobar' in qt_args
|
||||
|
||||
|
||||
@pytest.mark.parametrize('os_name, qversion, expected', [
|
||||
('linux', '5.2.1', True), # unaffected OS
|
||||
('linux', '5.4.1', True), # unaffected OS
|
||||
('nt', '5.2.1', False),
|
||||
('nt', '5.3.0', True), # unaffected Qt version
|
||||
('nt', '5.4.1', True), # unaffected Qt version
|
||||
])
|
||||
def test_check_print_compat(os_name, qversion, expected, monkeypatch):
|
||||
"""Test check_print_compat.
|
||||
|
||||
Args:
|
||||
os_name: The fake os.name to set.
|
||||
qversion: The fake qVersion() to set.
|
||||
expected: The expected return value.
|
||||
"""
|
||||
monkeypatch.setattr('qutebrowser.utils.qtutils.os.name', os_name)
|
||||
monkeypatch.setattr('qutebrowser.utils.qtutils.qVersion', lambda: qversion)
|
||||
assert qtutils.check_print_compat() == expected
|
||||
|
||||
|
||||
class QtObject:
|
||||
|
||||
"""Fake Qt object for test_ensure."""
|
||||
|
||||
def __init__(self, valid=True, null=False, error=None):
|
||||
self._valid = valid
|
||||
self._null = null
|
||||
self._error = error
|
||||
|
||||
def __repr__(self):
|
||||
return '<QtObject>'
|
||||
|
||||
def errorString(self):
|
||||
"""Get the fake error, or raise AttributeError if set to None."""
|
||||
if self._error is None:
|
||||
raise AttributeError
|
||||
else:
|
||||
return self._error
|
||||
|
||||
def isValid(self):
|
||||
return self._valid
|
||||
|
||||
def isNull(self):
|
||||
return self._null
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func_name, obj, raising, exc_reason, exc_str', [
|
||||
# ensure_valid, good examples
|
||||
('ensure_valid', QtObject(valid=True, null=True), False, None, None),
|
||||
('ensure_valid', QtObject(valid=True, null=False), False, None, None),
|
||||
# ensure_valid, bad examples
|
||||
('ensure_valid', QtObject(valid=False, null=True), True, None,
|
||||
'<QtObject> is not valid'),
|
||||
('ensure_valid', QtObject(valid=False, null=False), True, None,
|
||||
'<QtObject> is not valid'),
|
||||
('ensure_valid', QtObject(valid=False, null=True, error='Test'), True,
|
||||
'Test', '<QtObject> is not valid: Test'),
|
||||
# ensure_not_null, good examples
|
||||
('ensure_not_null', QtObject(valid=True, null=False), False, None, None),
|
||||
('ensure_not_null', QtObject(valid=False, null=False), False, None, None),
|
||||
# ensure_not_null, bad examples
|
||||
('ensure_not_null', QtObject(valid=True, null=True), True, None,
|
||||
'<QtObject> is null'),
|
||||
('ensure_not_null', QtObject(valid=False, null=True), True, None,
|
||||
'<QtObject> is null'),
|
||||
('ensure_not_null', QtObject(valid=False, null=True, error='Test'), True,
|
||||
'Test', '<QtObject> is null: Test'),
|
||||
])
|
||||
def test_ensure(func_name, obj, raising, exc_reason, exc_str):
|
||||
"""Test ensure_valid and ensure_not_null.
|
||||
|
||||
The function is parametrized as they do nearly the same.
|
||||
|
||||
Args:
|
||||
func_name: The name of the function to call.
|
||||
obj: The object to test with.
|
||||
raising: Whether QtValueError is expected to be raised.
|
||||
exc_reason: The expected .reason attribute of the exception.
|
||||
exc_str: The expected string of the exception.
|
||||
"""
|
||||
func = getattr(qtutils, func_name)
|
||||
if raising:
|
||||
with pytest.raises(qtutils.QtValueError) as excinfo:
|
||||
func(obj)
|
||||
assert excinfo.value.reason == exc_reason
|
||||
assert str(excinfo.value) == exc_str
|
||||
else:
|
||||
func(obj)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status, raising, message', [
|
||||
(QDataStream.Ok, False, None),
|
||||
(QDataStream.ReadPastEnd, True, "The data stream has read past the end of "
|
||||
"the data in the underlying device."),
|
||||
(QDataStream.ReadCorruptData, True, "The data stream has read corrupt "
|
||||
"data."),
|
||||
(QDataStream.WriteFailed, True, "The data stream cannot write to the "
|
||||
"underlying device."),
|
||||
])
|
||||
def test_check_qdatastream(status, raising, message):
|
||||
"""Test check_qdatastream.
|
||||
|
||||
Args:
|
||||
status: The status to set on the QDataStream we test with.
|
||||
raising: Whether check_qdatastream is expected to raise OSError.
|
||||
message: The expected exception string.
|
||||
"""
|
||||
stream = QDataStream()
|
||||
stream.setStatus(status)
|
||||
if raising:
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.check_qdatastream(stream)
|
||||
assert str(excinfo.value) == message
|
||||
else:
|
||||
qtutils.check_qdatastream(stream)
|
||||
|
||||
|
||||
def test_qdatastream_status_count():
|
||||
"""Make sure no new members are added to QDataStream.Status."""
|
||||
values = vars(QDataStream).values()
|
||||
status_vals = [e for e in values if isinstance(e, QDataStream.Status)]
|
||||
assert len(status_vals) == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obj', [
|
||||
QPoint(23, 42),
|
||||
QUrl('http://www.qutebrowser.org/'),
|
||||
])
|
||||
def test_serialize(obj):
|
||||
"""Test a serialize/deserialize round trip.
|
||||
|
||||
Args:
|
||||
obj: The object to test with.
|
||||
"""
|
||||
new_obj = type(obj)()
|
||||
qtutils.deserialize(qtutils.serialize(obj), new_obj)
|
||||
assert new_obj == obj
|
||||
|
||||
|
||||
class TestSerializeStream:
|
||||
|
||||
"""Tests for serialize_stream and deserialize_stream."""
|
||||
|
||||
def _set_status(self, stream, status):
|
||||
"""Helper function so mocks can set an error status when used."""
|
||||
stream.status.return_value = status
|
||||
|
||||
@pytest.fixture
|
||||
def stream_mock(self):
|
||||
"""Fixture providing a QDataStream-like mock."""
|
||||
m = unittest.mock.MagicMock(spec=QDataStream)
|
||||
m.status.return_value = QDataStream.Ok
|
||||
return m
|
||||
|
||||
def test_serialize_pre_error_mock(self, stream_mock):
|
||||
"""Test serialize_stream with an error already set."""
|
||||
stream_mock.status.return_value = QDataStream.ReadCorruptData
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.serialize_stream(stream_mock, QPoint())
|
||||
|
||||
assert not stream_mock.__lshift__.called
|
||||
assert str(excinfo.value) == "The data stream has read corrupt data."
|
||||
|
||||
def test_serialize_post_error_mock(self, stream_mock):
|
||||
"""Test serialize_stream with an error while serializing."""
|
||||
obj = QPoint()
|
||||
stream_mock.__lshift__.side_effect = lambda _other: self._set_status(
|
||||
stream_mock, QDataStream.ReadCorruptData)
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.serialize_stream(stream_mock, obj)
|
||||
|
||||
assert stream_mock.__lshift__.called_once_with(obj)
|
||||
assert str(excinfo.value) == "The data stream has read corrupt data."
|
||||
|
||||
def test_deserialize_pre_error_mock(self, stream_mock):
|
||||
"""Test deserialize_stream with an error already set."""
|
||||
stream_mock.status.return_value = QDataStream.ReadCorruptData
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.deserialize_stream(stream_mock, QPoint())
|
||||
|
||||
assert not stream_mock.__rshift__.called
|
||||
assert str(excinfo.value) == "The data stream has read corrupt data."
|
||||
|
||||
def test_deserialize_post_error_mock(self, stream_mock):
|
||||
"""Test deserialize_stream with an error while deserializing."""
|
||||
obj = QPoint()
|
||||
stream_mock.__rshift__.side_effect = lambda _other: self._set_status(
|
||||
stream_mock, QDataStream.ReadCorruptData)
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.deserialize_stream(stream_mock, obj)
|
||||
|
||||
assert stream_mock.__rshift__.called_once_with(obj)
|
||||
assert str(excinfo.value) == "The data stream has read corrupt data."
|
||||
|
||||
def test_round_trip_real_stream(self):
|
||||
"""Test a round trip with a real QDataStream."""
|
||||
src_obj = QPoint(23, 42)
|
||||
dest_obj = QPoint()
|
||||
data = QByteArray()
|
||||
|
||||
write_stream = QDataStream(data, QIODevice.WriteOnly)
|
||||
qtutils.serialize_stream(write_stream, src_obj)
|
||||
|
||||
read_stream = QDataStream(data, QIODevice.ReadOnly)
|
||||
qtutils.deserialize_stream(read_stream, dest_obj)
|
||||
|
||||
assert src_obj == dest_obj
|
||||
|
||||
@pytest.mark.qt_log_ignore('^QIODevice::write: ReadOnly device')
|
||||
def test_serialize_readonly_stream(self):
|
||||
"""Test serialize_stream with a read-only stream."""
|
||||
data = QByteArray()
|
||||
stream = QDataStream(data, QIODevice.ReadOnly)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.serialize_stream(stream, QPoint())
|
||||
assert str(excinfo.value) == ("The data stream cannot write to the "
|
||||
"underlying device.")
|
||||
|
||||
@pytest.mark.qt_log_ignore('QIODevice::read: WriteOnly device')
|
||||
def test_deserialize_writeonly_stream(self):
|
||||
"""Test deserialize_stream with a write-only stream."""
|
||||
data = QByteArray()
|
||||
obj = QPoint()
|
||||
stream = QDataStream(data, QIODevice.WriteOnly)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
qtutils.deserialize_stream(stream, obj)
|
||||
assert str(excinfo.value) == ("The data stream has read past the end "
|
||||
"of the data in the underlying device.")
|
||||
|
||||
|
||||
class SavefileTestException(Exception):
|
||||
|
||||
"""Exception raised in TestSavefileOpen for testing."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class TestSavefileOpen:
|
||||
|
||||
"""Tests for savefile_open."""
|
||||
|
||||
## Tests with a mock testing that the needed methods are called.
|
||||
|
||||
@pytest.yield_fixture
|
||||
def qsavefile_mock(self, mocker):
|
||||
"""Mock for QSaveFile."""
|
||||
m = mocker.patch('qutebrowser.utils.qtutils.QSaveFile')
|
||||
instance = m()
|
||||
yield instance
|
||||
instance.commit.assert_called_once_with()
|
||||
|
||||
def test_mock_open_error(self, qsavefile_mock):
|
||||
"""Test with a mock and a failing open()."""
|
||||
qsavefile_mock.open.return_value = False
|
||||
qsavefile_mock.errorString.return_value = "Hello World"
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
with qtutils.savefile_open('filename'):
|
||||
pass
|
||||
|
||||
qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly)
|
||||
qsavefile_mock.cancelWriting.assert_called_once_with()
|
||||
assert str(excinfo.value) == "Hello World"
|
||||
|
||||
def test_mock_exception(self, qsavefile_mock):
|
||||
"""Test with a mock and an exception in the block."""
|
||||
qsavefile_mock.open.return_value = True
|
||||
|
||||
with pytest.raises(SavefileTestException):
|
||||
with qtutils.savefile_open('filename'):
|
||||
raise SavefileTestException
|
||||
|
||||
qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly)
|
||||
qsavefile_mock.cancelWriting.assert_called_once_with()
|
||||
|
||||
def test_mock_commit_failed(self, qsavefile_mock):
|
||||
"""Test with a mock and an exception in the block."""
|
||||
qsavefile_mock.open.return_value = True
|
||||
qsavefile_mock.commit.return_value = False
|
||||
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
with qtutils.savefile_open('filename'):
|
||||
pass
|
||||
|
||||
qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly)
|
||||
assert not qsavefile_mock.cancelWriting.called
|
||||
assert not qsavefile_mock.errorString.called
|
||||
assert str(excinfo.value) == "Commit failed!"
|
||||
|
||||
def test_mock_successful(self, qsavefile_mock):
|
||||
"""Test with a mock and a successful write."""
|
||||
qsavefile_mock.open.return_value = True
|
||||
qsavefile_mock.errorString.return_value = "Hello World"
|
||||
qsavefile_mock.commit.return_value = True
|
||||
qsavefile_mock.write.side_effect = len
|
||||
qsavefile_mock.isOpen.return_value = True
|
||||
|
||||
with qtutils.savefile_open('filename') as f:
|
||||
f.write("Hello World")
|
||||
|
||||
qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly)
|
||||
assert not qsavefile_mock.cancelWriting.called
|
||||
qsavefile_mock.write.assert_called_once_with(b"Hello World")
|
||||
|
||||
## Tests with real files
|
||||
|
||||
@pytest.mark.parametrize('data', ["Hello World", "Snowman! ☃"])
|
||||
def test_utf8(self, data, tmpdir):
|
||||
"""Test with UTF8 data."""
|
||||
filename = tmpdir / 'foo'
|
||||
filename.write("Old data")
|
||||
with qtutils.savefile_open(str(filename)) as f:
|
||||
f.write(data)
|
||||
assert tmpdir.listdir() == [filename]
|
||||
assert filename.read_text(encoding='utf-8') == data
|
||||
|
||||
def test_binary(self, tmpdir):
|
||||
"""Test with binary data."""
|
||||
filename = tmpdir / 'foo'
|
||||
with qtutils.savefile_open(str(filename), binary=True) as f:
|
||||
f.write(b'\xde\xad\xbe\xef')
|
||||
assert tmpdir.listdir() == [filename]
|
||||
assert filename.read_binary() == b'\xde\xad\xbe\xef'
|
||||
|
||||
def test_exception(self, tmpdir):
|
||||
"""Test with an exception in the block."""
|
||||
filename = tmpdir / 'foo'
|
||||
filename.write("Old content")
|
||||
with pytest.raises(SavefileTestException):
|
||||
with qtutils.savefile_open(str(filename)) as f:
|
||||
f.write("Hello World!")
|
||||
raise SavefileTestException
|
||||
assert tmpdir.listdir() == [filename]
|
||||
assert filename.read_text(encoding='utf-8') == "Old content"
|
||||
|
||||
def test_existing_dir(self, tmpdir):
|
||||
"""Test with the filename already occupied by a directory."""
|
||||
filename = tmpdir / 'foo'
|
||||
filename.mkdir()
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
with qtutils.savefile_open(str(filename)):
|
||||
pass
|
||||
errors = ["Filename refers to a directory", # Qt >= 5.4
|
||||
"Commit failed!"] # older Qt versions
|
||||
assert str(excinfo.value) in errors
|
||||
assert tmpdir.listdir() == [filename]
|
||||
|
||||
def test_failing_commit(self, tmpdir):
|
||||
"""Test with the file being closed before comitting."""
|
||||
filename = tmpdir / 'foo'
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
with qtutils.savefile_open(str(filename), binary=True) as f:
|
||||
f.write(b'Hello')
|
||||
f.dev.commit() # provoke failing "real" commit
|
||||
|
||||
assert str(excinfo.value) == "Commit failed!"
|
||||
assert tmpdir.listdir() == [filename]
|
||||
|
||||
def test_line_endings(self, tmpdir):
|
||||
"""Make sure line endings are translated correctly.
|
||||
|
||||
See https://github.com/The-Compiler/qutebrowser/issues/309
|
||||
"""
|
||||
filename = tmpdir / 'foo'
|
||||
with qtutils.savefile_open(str(filename)) as f:
|
||||
f.write('foo\nbar\nbaz')
|
||||
data = filename.read_binary()
|
||||
if os.name == 'nt':
|
||||
assert data == b'foo\r\nbar\r\nbaz'
|
||||
else:
|
||||
assert data == b'foo\nbar\nbaz'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('orgname, expected', [(None, ''), ('test', 'test')])
|
||||
def test_unset_organization(orgname, expected):
|
||||
"""Test unset_organization.
|
||||
|
||||
Args:
|
||||
orgname: The organizationName to set initially.
|
||||
expected: The organizationName which is expected when reading back.
|
||||
"""
|
||||
app = QApplication.instance()
|
||||
app.setOrganizationName(orgname)
|
||||
assert app.organizationName() == expected # sanity check
|
||||
with qtutils.unset_organization():
|
||||
assert app.organizationName() == ''
|
||||
assert app.organizationName() == expected
|
||||
|
||||
|
||||
if test_file is not None:
|
||||
# If we were able to import Python's test_file module, we run some code
|
||||
# here which defines unittest TestCases to run the python tests over
|
||||
# PyQIODevice.
|
||||
|
||||
@pytest.yield_fixture(scope='session', autouse=True)
|
||||
def clean_up_python_testfile():
|
||||
"""Clean up the python testfile after tests if tests didn't."""
|
||||
yield
|
||||
try:
|
||||
os.remove(test_file.TESTFN)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
class PyIODeviceTestMixin:
|
||||
|
||||
"""Some helper code to run Python's tests with PyQIODevice.
|
||||
|
||||
Attributes:
|
||||
_data: A QByteArray containing the data in memory.
|
||||
f: The opened PyQIODevice.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up self.f using a PyQIODevice instead of a real file."""
|
||||
self._data = QByteArray()
|
||||
self.f = self.open(test_file.TESTFN, 'wb')
|
||||
|
||||
def open(self, _fname, mode):
|
||||
"""Open an in-memory PyQIODevice instead of a real file."""
|
||||
modes = {
|
||||
'wb': QIODevice.WriteOnly | QIODevice.Truncate,
|
||||
'w': QIODevice.WriteOnly | QIODevice.Text | QIODevice.Truncate,
|
||||
'rb': QIODevice.ReadOnly,
|
||||
'r': QIODevice.ReadOnly | QIODevice.Text,
|
||||
}
|
||||
try:
|
||||
qt_mode = modes[mode]
|
||||
except KeyError:
|
||||
raise ValueError("Invalid mode {}!".format(mode))
|
||||
f = QBuffer(self._data)
|
||||
f.open(qt_mode)
|
||||
qiodev = qtutils.PyQIODevice(f)
|
||||
# Make sure tests using name/mode don't blow up.
|
||||
qiodev.name = test_file.TESTFN
|
||||
qiodev.mode = mode
|
||||
# Create empty TESTFN file because the Python tests try to unlink
|
||||
# it.after the test.
|
||||
open(test_file.TESTFN, 'w', encoding='utf-8').close()
|
||||
return qiodev
|
||||
|
||||
class PyAutoFileTests(PyIODeviceTestMixin, test_file.AutoFileTests,
|
||||
unittest.TestCase):
|
||||
|
||||
"""Unittest testcase to run Python's AutoFileTests."""
|
||||
|
||||
def testReadinto_text(self):
|
||||
"""Skip this test as BufferedIOBase seems to fail it."""
|
||||
pass
|
||||
|
||||
class PyOtherFileTests(PyIODeviceTestMixin, test_file.OtherFileTests,
|
||||
unittest.TestCase):
|
||||
|
||||
"""Unittest testcase to run Python's OtherFileTests."""
|
||||
|
||||
def testSetBufferSize(self):
|
||||
"""Skip this test as setting buffer size is unsupported."""
|
||||
pass
|
||||
|
||||
def testTruncateOnWindows(self):
|
||||
"""Skip this test truncating is unsupported."""
|
||||
pass
|
||||
|
||||
|
||||
class FailingQIODevice(QIODevice):
|
||||
|
||||
"""A fake QIODevice where reads/writes fail."""
|
||||
|
||||
def isOpen(self):
|
||||
return True
|
||||
|
||||
def isReadable(self):
|
||||
return True
|
||||
|
||||
def isWritable(self):
|
||||
return True
|
||||
|
||||
def write(self, _data):
|
||||
"""Simulate failed write."""
|
||||
self.setErrorString("Writing failed")
|
||||
return -1
|
||||
|
||||
def read(self, _maxsize):
|
||||
"""Simulate failed read."""
|
||||
self.setErrorString("Reading failed")
|
||||
return None
|
||||
|
||||
def readAll(self):
|
||||
return self.read(0)
|
||||
|
||||
def readLine(self, maxsize):
|
||||
return self.read(maxsize)
|
||||
|
||||
|
||||
class TestPyQIODevice:
|
||||
|
||||
"""Tests for PyQIODevice."""
|
||||
|
||||
@pytest.yield_fixture
|
||||
def pyqiodev(self):
|
||||
"""Fixture providing a PyQIODevice with a QByteArray to test."""
|
||||
data = QByteArray()
|
||||
f = QBuffer(data)
|
||||
qiodev = qtutils.PyQIODevice(f)
|
||||
yield qiodev
|
||||
qiodev.close()
|
||||
|
||||
@pytest.fixture
|
||||
def pyqiodev_failing(self):
|
||||
"""Fixture providing a PyQIODevice with a FailingQIODevice to test."""
|
||||
failing = FailingQIODevice()
|
||||
return qtutils.PyQIODevice(failing)
|
||||
|
||||
@pytest.mark.parametrize('method, args', [
|
||||
('seek', [0]),
|
||||
('flush', []),
|
||||
('isatty', []),
|
||||
('readline', []),
|
||||
('tell', []),
|
||||
('write', [b'']),
|
||||
('read', []),
|
||||
])
|
||||
def test_closed_device(self, pyqiodev, method, args):
|
||||
"""Test various methods with a closed device.
|
||||
|
||||
Args:
|
||||
method: The name of the method to call.
|
||||
args: The arguments to pass.
|
||||
"""
|
||||
func = getattr(pyqiodev, method)
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
func(*args)
|
||||
assert str(excinfo.value) == "IO operation on closed device!"
|
||||
|
||||
@pytest.mark.parametrize('method', ['readline', 'read'])
|
||||
def test_unreadable(self, pyqiodev, method):
|
||||
"""Test methods with an unreadable device.
|
||||
|
||||
Args:
|
||||
method: The name of the method to call.
|
||||
"""
|
||||
pyqiodev.open(QIODevice.WriteOnly)
|
||||
func = getattr(pyqiodev, method)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
func()
|
||||
assert str(excinfo.value) == "Trying to read unreadable file!"
|
||||
|
||||
def test_unwritable(self, pyqiodev):
|
||||
"""Test writing with a read-only device."""
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
pyqiodev.write(b'')
|
||||
assert str(excinfo.value) == "Trying to write to unwritable file!"
|
||||
|
||||
@pytest.mark.parametrize('data', [b'12345', b''])
|
||||
def test_len(self, pyqiodev, data):
|
||||
"""Test len()/__len__.
|
||||
|
||||
Args:
|
||||
data: The data to write before checking if the length equals
|
||||
len(data).
|
||||
"""
|
||||
pyqiodev.open(QIODevice.WriteOnly)
|
||||
pyqiodev.write(data)
|
||||
assert len(pyqiodev) == len(data)
|
||||
|
||||
def test_failing_open(self, tmpdir):
|
||||
"""Test open() which fails (because it's an existant directory)."""
|
||||
qf = QFile(str(tmpdir))
|
||||
dev = qtutils.PyQIODevice(qf)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
dev.open(QIODevice.WriteOnly)
|
||||
errors = ['Access is denied.', # Linux/OS X
|
||||
'Is a directory'] # Windows
|
||||
assert str(excinfo.value) in errors
|
||||
assert dev.closed
|
||||
|
||||
def test_fileno(self, pyqiodev):
|
||||
with pytest.raises(io.UnsupportedOperation):
|
||||
pyqiodev.fileno()
|
||||
|
||||
@pytest.mark.qt_log_ignore('^QBuffer::seek: Invalid pos:')
|
||||
@pytest.mark.parametrize('offset, whence, pos, data, raising', [
|
||||
(0, io.SEEK_SET, 0, b'1234567890', False),
|
||||
(42, io.SEEK_SET, 0, b'1234567890', True),
|
||||
(8, io.SEEK_CUR, 8, b'90', False),
|
||||
(-5, io.SEEK_CUR, 0, b'1234567890', True),
|
||||
(-2, io.SEEK_END, 8, b'90', False),
|
||||
(2, io.SEEK_END, 0, b'1234567890', True),
|
||||
(0, io.SEEK_END, 10, b'', False),
|
||||
])
|
||||
def test_seek_tell(self, pyqiodev, offset, whence, pos, data, raising):
|
||||
"""Test seek() and tell().
|
||||
|
||||
The initial position when these tests run is 0.
|
||||
|
||||
Args:
|
||||
offset: The offset to pass to .seek().
|
||||
whence: The whence argument to pass to .seek().
|
||||
pos: The expected position after seeking.
|
||||
data: The expected data to read after seeking.
|
||||
raising: Whether seeking should raise OSError.
|
||||
"""
|
||||
with pyqiodev.open(QIODevice.WriteOnly) as f:
|
||||
f.write(b'1234567890')
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
if raising:
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
pyqiodev.seek(offset, whence)
|
||||
assert str(excinfo.value) == "seek failed!"
|
||||
else:
|
||||
pyqiodev.seek(offset, whence)
|
||||
assert pyqiodev.tell() == pos
|
||||
assert pyqiodev.read() == data
|
||||
|
||||
def test_seek_unsupported(self, pyqiodev):
|
||||
"""Test seeking with unsupported whence arguments."""
|
||||
if hasattr(os, 'SEEK_HOLE'):
|
||||
whence = os.SEEK_HOLE # pylint: disable=no-member
|
||||
elif hasattr(os, 'SEEK_DATA'):
|
||||
whence = os.SEEK_DATA # pylint: disable=no-member
|
||||
else:
|
||||
pytest.skip("Needs os.SEEK_HOLE or os.SEEK_DATA available.")
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
with pytest.raises(io.UnsupportedOperation):
|
||||
pyqiodev.seek(0, whence)
|
||||
|
||||
def test_qprocess(self):
|
||||
"""Test PyQIODevice with a QProcess which is non-sequential.
|
||||
|
||||
This also verifies seek() and tell() behave as expected.
|
||||
"""
|
||||
proc = QProcess()
|
||||
proc.start(sys.executable, ['-c', 'print("Hello World")'])
|
||||
dev = qtutils.PyQIODevice(proc)
|
||||
assert not dev.closed
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
dev.seek(0)
|
||||
assert str(excinfo.value) == 'Random access not allowed!'
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
dev.tell()
|
||||
assert str(excinfo.value) == 'Random access not allowed!'
|
||||
proc.waitForFinished(1000)
|
||||
proc.kill()
|
||||
assert bytes(dev.read()).rstrip() == b'Hello World'
|
||||
|
||||
def test_truncate(self, pyqiodev):
|
||||
with pytest.raises(io.UnsupportedOperation):
|
||||
pyqiodev.truncate()
|
||||
|
||||
def test_closed(self, pyqiodev):
|
||||
"""Test the closed attribute."""
|
||||
assert pyqiodev.closed
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
assert not pyqiodev.closed
|
||||
pyqiodev.close()
|
||||
assert pyqiodev.closed
|
||||
|
||||
def test_contextmanager(self, pyqiodev):
|
||||
"""Make sure using the PyQIODevice as context manager works."""
|
||||
assert pyqiodev.closed
|
||||
with pyqiodev.open(QIODevice.ReadOnly) as f:
|
||||
assert not f.closed
|
||||
assert f is pyqiodev
|
||||
assert pyqiodev.closed
|
||||
|
||||
def test_flush(self, pyqiodev):
|
||||
"""Make sure flushing doesn't raise an exception."""
|
||||
pyqiodev.open(QIODevice.WriteOnly)
|
||||
pyqiodev.write(b'test')
|
||||
pyqiodev.flush()
|
||||
|
||||
@pytest.mark.parametrize('method, ret', [
|
||||
('isatty', False),
|
||||
('seekable', True),
|
||||
])
|
||||
def test_bools(self, method, ret, pyqiodev):
|
||||
"""Make sure simple bool arguments return the right thing.
|
||||
|
||||
Args:
|
||||
method: The name of the method to call.
|
||||
ret: The return value we expect.
|
||||
"""
|
||||
pyqiodev.open(QIODevice.WriteOnly)
|
||||
func = getattr(pyqiodev, method)
|
||||
assert func() == ret
|
||||
|
||||
@pytest.mark.parametrize('mode, readable, writable', [
|
||||
(QIODevice.ReadOnly, True, False),
|
||||
(QIODevice.ReadWrite, True, True),
|
||||
(QIODevice.WriteOnly, False, True),
|
||||
])
|
||||
def test_readable_writable(self, mode, readable, writable, pyqiodev):
|
||||
"""Test readable() and writable().
|
||||
|
||||
Args:
|
||||
mode: The mode to open the PyQIODevice in.
|
||||
readable: Whether the device should be readable.
|
||||
writable: Whether the device should be writable.
|
||||
"""
|
||||
assert not pyqiodev.readable()
|
||||
assert not pyqiodev.writable()
|
||||
pyqiodev.open(mode)
|
||||
assert pyqiodev.readable() == readable
|
||||
assert pyqiodev.writable() == writable
|
||||
|
||||
@pytest.mark.parametrize('size, chunks', [
|
||||
(-1, [b'one\n', b'two\n', b'three', b'']),
|
||||
(0, [b'', b'', b'', b'']),
|
||||
(2, [b'on', b'e\n', b'tw', b'o\n', b'th', b're', b'e']),
|
||||
(10, [b'one\n', b'two\n', b'three', b'']),
|
||||
])
|
||||
def test_readline(self, size, chunks, pyqiodev):
|
||||
"""Test readline() with different sizes.
|
||||
|
||||
Args:
|
||||
size: The size to pass to readline()
|
||||
chunks: A list of expected chunks to read.
|
||||
"""
|
||||
with pyqiodev.open(QIODevice.WriteOnly) as f:
|
||||
f.write(b'one\ntwo\nthree')
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
for i, chunk in enumerate(chunks, start=1):
|
||||
print("Expecting chunk {}: {!r}".format(i, chunk))
|
||||
assert pyqiodev.readline(size) == chunk
|
||||
|
||||
def test_write(self, pyqiodev):
|
||||
"""Make sure writing and re-reading works."""
|
||||
with pyqiodev.open(QIODevice.WriteOnly) as f:
|
||||
f.write(b'foo\n')
|
||||
f.write(b'bar\n')
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
assert pyqiodev.read() == b'foo\nbar\n'
|
||||
|
||||
def test_write_error(self, pyqiodev_failing):
|
||||
"""Test writing with FailingQIODevice."""
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
pyqiodev_failing.write(b'x')
|
||||
assert str(excinfo.value) == 'Writing failed'
|
||||
|
||||
@pytest.mark.skipif(os.name != 'posix', reason="Needs a POSIX OS.")
|
||||
@pytest.mark.skipif(not os.path.exists('/dev/full'),
|
||||
reason="Needs /dev/full.")
|
||||
def test_write_error_real(self):
|
||||
"""Test a real write error with /dev/full on supported systems."""
|
||||
qf = QFile('/dev/full')
|
||||
qf.open(QIODevice.WriteOnly | QIODevice.Unbuffered)
|
||||
dev = qtutils.PyQIODevice(qf)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
dev.write(b'foo')
|
||||
qf.close()
|
||||
assert str(excinfo.value) == 'No space left on device'
|
||||
|
||||
@pytest.mark.parametrize('size, chunks', [
|
||||
(-1, [b'1234567890']),
|
||||
(0, [b'']),
|
||||
(3, [b'123', b'456', b'789', b'0']),
|
||||
(20, [b'1234567890'])
|
||||
])
|
||||
def test_read(self, size, chunks, pyqiodev):
|
||||
"""Test reading with different sizes.
|
||||
|
||||
Args:
|
||||
size: The size to pass to read()
|
||||
chunks: A list of expected data chunks.
|
||||
"""
|
||||
with pyqiodev.open(QIODevice.WriteOnly) as f:
|
||||
f.write(b'1234567890')
|
||||
pyqiodev.open(QIODevice.ReadOnly)
|
||||
for i, chunk in enumerate(chunks):
|
||||
print("Expecting chunk {}: {!r}".format(i, chunk))
|
||||
assert pyqiodev.read(size) == chunk
|
||||
|
||||
@pytest.mark.parametrize('method, args', [
|
||||
('read', []),
|
||||
('read', [5]),
|
||||
('readline', []),
|
||||
('readline', [5]),
|
||||
])
|
||||
def test_failing_reads(self, method, args, pyqiodev_failing):
|
||||
"""Test reading with a FailingQIODevice.
|
||||
|
||||
Args:
|
||||
method: The name of the method to call.
|
||||
args: A list of arguments to pass.
|
||||
"""
|
||||
func = getattr(pyqiodev_failing, method)
|
||||
with pytest.raises(OSError) as excinfo:
|
||||
func(*args)
|
||||
assert str(excinfo.value) == 'Reading failed'
|
||||
|
||||
|
||||
class TestEventLoop:
|
||||
|
||||
"""Tests for EventLoop.
|
||||
|
||||
Attributes:
|
||||
loop: The EventLoop we're testing.
|
||||
"""
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
def _assert_executing(self):
|
||||
"""Slot which gets called from timers to be sure the loop runs."""
|
||||
assert self.loop._executing
|
||||
|
||||
def _double_exec(self):
|
||||
"""Slot which gets called from timers to assert double-exec fails."""
|
||||
with pytest.raises(AssertionError):
|
||||
self.loop.exec_()
|
||||
|
||||
def test_normal_exec(self):
|
||||
"""Test exec_ without double-executing."""
|
||||
self.loop = qtutils.EventLoop()
|
||||
QTimer.singleShot(100, self._assert_executing)
|
||||
QTimer.singleShot(200, self.loop.quit)
|
||||
self.loop.exec_()
|
||||
assert not self.loop._executing
|
||||
|
||||
def test_double_exec(self):
|
||||
"""Test double-executing."""
|
||||
self.loop = qtutils.EventLoop()
|
||||
QTimer.singleShot(100, self._assert_executing)
|
||||
QTimer.singleShot(200, self._double_exec)
|
||||
QTimer.singleShot(300, self._assert_executing)
|
||||
QTimer.singleShot(400, self.loop.quit)
|
||||
self.loop.exec_()
|
||||
assert not self.loop._executing
|
||||
|
@ -17,6 +17,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
"""Tests for qutebrowser.utils.standarddir."""
|
||||
|
||||
import os
|
||||
@ -24,7 +26,10 @@ import os.path
|
||||
import sys
|
||||
import types
|
||||
import collections
|
||||
import logging
|
||||
import textwrap
|
||||
|
||||
from PyQt5.QtCore import QStandardPaths
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
import pytest
|
||||
|
||||
@ -44,8 +49,61 @@ def change_qapp_name():
|
||||
QApplication.instance().setApplicationName(old_name)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def no_cachedir_tag(monkeypatch):
|
||||
"""Fixture to prevent writing a CACHEDIR.TAG."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir._init_cachedir_tag',
|
||||
lambda: None)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
def reset_standarddir():
|
||||
standarddir.init(None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('data_subdir, config_subdir, expected', [
|
||||
('foo', 'foo', 'foo/data'),
|
||||
('foo', 'bar', 'foo'),
|
||||
])
|
||||
def test_get_fake_windows_equal_dir(data_subdir, config_subdir, expected,
|
||||
monkeypatch, tmpdir):
|
||||
"""Test _get with a fake Windows OS with equal data/config dirs."""
|
||||
locations = {
|
||||
QStandardPaths.DataLocation: str(tmpdir / data_subdir),
|
||||
QStandardPaths.ConfigLocation: str(tmpdir / config_subdir),
|
||||
}
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.os.name', 'nt')
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
||||
locations.get)
|
||||
expected = str(tmpdir / expected)
|
||||
assert standarddir.data() == expected
|
||||
|
||||
|
||||
class TestWritableLocation:
|
||||
|
||||
"""Tests for _writable_location."""
|
||||
|
||||
def test_empty(self, monkeypatch):
|
||||
"""Test QStandardPaths returning an empty value."""
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
||||
lambda typ: '')
|
||||
with pytest.raises(ValueError):
|
||||
standarddir._writable_location(QStandardPaths.DataLocation)
|
||||
|
||||
def test_sep(self, monkeypatch):
|
||||
"""Make sure the right kind of separator is used."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.os.sep', '\\')
|
||||
loc = standarddir._writable_location(QStandardPaths.DataLocation)
|
||||
assert '/' not in loc
|
||||
assert '\\' in loc
|
||||
|
||||
|
||||
@pytest.mark.skipif(not sys.platform.startswith("linux"),
|
||||
reason="requires Linux")
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
class TestGetStandardDirLinux:
|
||||
|
||||
"""Tests for standarddir under Linux."""
|
||||
@ -53,26 +111,22 @@ class TestGetStandardDirLinux:
|
||||
def test_data_explicit(self, monkeypatch, tmpdir):
|
||||
"""Test data dir with XDG_DATA_HOME explicitly set."""
|
||||
monkeypatch.setenv('XDG_DATA_HOME', str(tmpdir))
|
||||
standarddir.init(None)
|
||||
assert standarddir.data() == str(tmpdir / 'qutebrowser_test')
|
||||
|
||||
def test_config_explicit(self, monkeypatch, tmpdir):
|
||||
"""Test config dir with XDG_CONFIG_HOME explicitly set."""
|
||||
monkeypatch.setenv('XDG_CONFIG_HOME', str(tmpdir))
|
||||
standarddir.init(None)
|
||||
assert standarddir.config() == str(tmpdir / 'qutebrowser_test')
|
||||
|
||||
def test_cache_explicit(self, monkeypatch, tmpdir):
|
||||
"""Test cache dir with XDG_CACHE_HOME explicitly set."""
|
||||
monkeypatch.setenv('XDG_CACHE_HOME', str(tmpdir))
|
||||
standarddir.init(None)
|
||||
assert standarddir.cache() == str(tmpdir / 'qutebrowser_test')
|
||||
|
||||
def test_data(self, monkeypatch, tmpdir):
|
||||
"""Test data dir with XDG_DATA_HOME not set."""
|
||||
monkeypatch.setenv('HOME', str(tmpdir))
|
||||
monkeypatch.delenv('XDG_DATA_HOME', raising=False)
|
||||
standarddir.init(None)
|
||||
expected = tmpdir / '.local' / 'share' / 'qutebrowser_test'
|
||||
assert standarddir.data() == str(expected)
|
||||
|
||||
@ -80,7 +134,6 @@ class TestGetStandardDirLinux:
|
||||
"""Test config dir with XDG_CONFIG_HOME not set."""
|
||||
monkeypatch.setenv('HOME', str(tmpdir))
|
||||
monkeypatch.delenv('XDG_CONFIG_HOME', raising=False)
|
||||
standarddir.init(None)
|
||||
expected = tmpdir / '.config' / 'qutebrowser_test'
|
||||
assert standarddir.config() == str(expected)
|
||||
|
||||
@ -88,21 +141,17 @@ class TestGetStandardDirLinux:
|
||||
"""Test cache dir with XDG_CACHE_HOME not set."""
|
||||
monkeypatch.setenv('HOME', str(tmpdir))
|
||||
monkeypatch.delenv('XDG_CACHE_HOME', raising=False)
|
||||
standarddir.init(None)
|
||||
expected = tmpdir / '.cache' / 'qutebrowser_test'
|
||||
assert standarddir.cache() == expected
|
||||
|
||||
|
||||
@pytest.mark.skipif(not sys.platform.startswith("win"),
|
||||
reason="requires Windows")
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
class TestGetStandardDirWindows:
|
||||
|
||||
"""Tests for standarddir under Windows."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_standarddir(self):
|
||||
standarddir.init(None)
|
||||
|
||||
def test_data(self):
|
||||
"""Test data dir."""
|
||||
expected = ['qutebrowser_test', 'data']
|
||||
@ -121,6 +170,7 @@ class TestGetStandardDirWindows:
|
||||
DirArgTest = collections.namedtuple('DirArgTest', 'arg, expected')
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
class TestArguments:
|
||||
|
||||
"""Tests with confdir/cachedir/datadir arguments."""
|
||||
@ -131,6 +181,7 @@ class TestArguments:
|
||||
if request.param.expected is None:
|
||||
return request.param
|
||||
else:
|
||||
# prepend tmpdir to both
|
||||
arg = str(tmpdir / request.param.arg)
|
||||
return DirArgTest(arg, arg)
|
||||
|
||||
@ -155,6 +206,21 @@ class TestArguments:
|
||||
standarddir.init(args)
|
||||
assert standarddir.data() == testcase.expected
|
||||
|
||||
def test_confdir_none(self):
|
||||
"""Test --confdir with None given."""
|
||||
args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None)
|
||||
standarddir.init(args)
|
||||
assert standarddir.config().split(os.sep)[-1] == 'qutebrowser_test'
|
||||
|
||||
def test_runtimedir(self, tmpdir, monkeypatch):
|
||||
"""Test runtime dir (which has no args)."""
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.standarddir.QStandardPaths.writableLocation',
|
||||
lambda _typ: str(tmpdir))
|
||||
args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None)
|
||||
standarddir.init(args)
|
||||
assert standarddir.runtime() == str(tmpdir)
|
||||
|
||||
@pytest.mark.parametrize('typ', ['config', 'data', 'cache', 'download',
|
||||
'runtime'])
|
||||
def test_basedir(self, tmpdir, typ):
|
||||
@ -164,3 +230,51 @@ class TestArguments:
|
||||
standarddir.init(args)
|
||||
func = getattr(standarddir, typ)
|
||||
assert func() == expected
|
||||
|
||||
|
||||
class TestInitCacheDirTag:
|
||||
|
||||
"""Tests for _init_cachedir_tag."""
|
||||
|
||||
def test_no_cache_dir(self, mocker, monkeypatch):
|
||||
"""Smoke test with cache() returning None."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
||||
lambda: None)
|
||||
mocker.patch('builtins.open', side_effect=AssertionError)
|
||||
standarddir._init_cachedir_tag()
|
||||
|
||||
def test_existant_cache_dir_tag(self, tmpdir, mocker, monkeypatch):
|
||||
"""Test with an existant CACHEDIR.TAG."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
||||
lambda: str(tmpdir))
|
||||
mocker.patch('builtins.open', side_effect=AssertionError)
|
||||
m = mocker.patch('qutebrowser.utils.standarddir.os.path.exists',
|
||||
return_value=True)
|
||||
standarddir._init_cachedir_tag()
|
||||
assert not tmpdir.listdir()
|
||||
m.assert_called_with(str(tmpdir / 'CACHEDIR.TAG'))
|
||||
|
||||
def test_new_cache_dir_tag(self, tmpdir, mocker, monkeypatch):
|
||||
"""Test creating a new CACHEDIR.TAG."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
||||
lambda: str(tmpdir))
|
||||
standarddir._init_cachedir_tag()
|
||||
assert tmpdir.listdir() == [(tmpdir / 'CACHEDIR.TAG')]
|
||||
data = (tmpdir / 'CACHEDIR.TAG').read_text('utf-8')
|
||||
assert data == textwrap.dedent("""
|
||||
Signature: 8a477f597d28d172789f06886806bc55
|
||||
# This file is a cache directory tag created by qutebrowser.
|
||||
# For information about cache directory tags, see:
|
||||
# http://www.brynosaurus.com/cachedir/
|
||||
""").lstrip()
|
||||
|
||||
def test_open_oserror(self, caplog, tmpdir, mocker, monkeypatch):
|
||||
"""Test creating a new CACHEDIR.TAG."""
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.cache',
|
||||
lambda: str(tmpdir))
|
||||
mocker.patch('builtins.open', side_effect=OSError)
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
standarddir._init_cachedir_tag()
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == 'Failed to create CACHEDIR.TAG'
|
||||
assert not tmpdir.listdir()
|
||||
|
@ -23,14 +23,22 @@ import sys
|
||||
import enum
|
||||
import datetime
|
||||
import os.path
|
||||
import io
|
||||
import logging
|
||||
import functools
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QColor
|
||||
import pytest
|
||||
|
||||
import qutebrowser
|
||||
import qutebrowser.utils # for test_qualname
|
||||
from qutebrowser.utils import utils, qtutils
|
||||
|
||||
|
||||
ELLIPSIS = '\u2026'
|
||||
|
||||
|
||||
class Color(QColor):
|
||||
|
||||
"""A QColor with a nicer repr()."""
|
||||
@ -41,39 +49,193 @@ class Color(QColor):
|
||||
alpha=self.alpha())
|
||||
|
||||
|
||||
class TestCompactText:
|
||||
|
||||
"""Test compact_text."""
|
||||
|
||||
@pytest.mark.parametrize('text, expected', [
|
||||
('foo\nbar', 'foobar'),
|
||||
(' foo \n bar ', 'foobar'),
|
||||
('\nfoo\n', 'foo'),
|
||||
])
|
||||
def test_compact_text(self, text, expected):
|
||||
"""Test folding of newlines."""
|
||||
assert utils.compact_text(text) == expected
|
||||
|
||||
@pytest.mark.parametrize('elidelength, text, expected', [
|
||||
(None, 'x' * 100, 'x' * 100),
|
||||
(6, 'foobar', 'foobar'),
|
||||
(5, 'foobar', 'foob' + ELLIPSIS),
|
||||
(5, 'foo\nbar', 'foob' + ELLIPSIS),
|
||||
(7, 'foo\nbar', 'foobar'),
|
||||
])
|
||||
def test_eliding(self, elidelength, text, expected):
|
||||
"""Test eliding."""
|
||||
assert utils.compact_text(text, elidelength) == expected
|
||||
|
||||
|
||||
class TestEliding:
|
||||
|
||||
"""Test elide."""
|
||||
|
||||
ELLIPSIS = '\u2026'
|
||||
|
||||
def test_too_small(self):
|
||||
"""Test eliding to 0 chars which should fail."""
|
||||
with pytest.raises(ValueError):
|
||||
utils.elide('foo', 0)
|
||||
|
||||
def test_length_one(self):
|
||||
"""Test eliding to 1 char which should yield ..."""
|
||||
assert utils.elide('foo', 1) == self.ELLIPSIS
|
||||
|
||||
def test_fits(self):
|
||||
"""Test eliding with a string which fits exactly."""
|
||||
assert utils.elide('foo', 3) == 'foo'
|
||||
|
||||
def test_elided(self):
|
||||
"""Test eliding with a string which should get elided."""
|
||||
assert utils.elide('foobar', 3) == 'fo' + self.ELLIPSIS
|
||||
@pytest.mark.parametrize('text, length, expected', [
|
||||
('foo', 1, ELLIPSIS),
|
||||
('foo', 3, 'foo'),
|
||||
('foobar', 3, 'fo' + ELLIPSIS),
|
||||
])
|
||||
def test_elided(self, text, length, expected):
|
||||
assert utils.elide(text, length) == expected
|
||||
|
||||
|
||||
class TestReadFile:
|
||||
|
||||
"""Test read_file."""
|
||||
|
||||
@pytest.fixture(autouse=True, params=[True, False])
|
||||
def freezer(self, request, monkeypatch):
|
||||
if request.param:
|
||||
monkeypatch.setattr(sys, 'frozen', True, raising=False)
|
||||
monkeypatch.setattr('sys.executable', qutebrowser.__file__)
|
||||
|
||||
def test_readfile(self):
|
||||
"""Read a test file."""
|
||||
content = utils.read_file(os.path.join('utils', 'testfile'))
|
||||
assert content.splitlines()[0] == "Hello World!"
|
||||
|
||||
def test_readfile_binary(self):
|
||||
"""Read a test file in binary mode."""
|
||||
content = utils.read_file(os.path.join('utils', 'testfile'),
|
||||
binary=True)
|
||||
assert content.splitlines()[0] == b"Hello World!"
|
||||
|
||||
|
||||
class Patcher:
|
||||
|
||||
"""Helper for TestActuteWarning.
|
||||
|
||||
Attributes:
|
||||
monkeypatch: The pytest monkeypatch fixture.
|
||||
"""
|
||||
|
||||
def __init__(self, monkeypatch):
|
||||
self.monkeypatch = monkeypatch
|
||||
|
||||
def patch_platform(self, platform='linux'):
|
||||
"""Patch sys.platform."""
|
||||
self.monkeypatch.setattr('sys.platform', platform)
|
||||
|
||||
def patch_exists(self, exists=True):
|
||||
"""Patch os.path.exists."""
|
||||
self.monkeypatch.setattr('qutebrowser.utils.utils.os.path.exists',
|
||||
lambda path: exists)
|
||||
|
||||
def patch_version(self, version='5.2.0'):
|
||||
"""Patch Qt version."""
|
||||
self.monkeypatch.setattr(
|
||||
'qutebrowser.utils.utils.qtutils.qVersion', lambda: version)
|
||||
|
||||
def patch_file(self, data):
|
||||
"""Patch open() to return the given data."""
|
||||
fake_file = io.StringIO(data)
|
||||
self.monkeypatch.setattr(utils, 'open',
|
||||
lambda filename, mode, encoding: fake_file,
|
||||
raising=False)
|
||||
|
||||
def patch_all(self, data):
|
||||
"""Patch everything so the issue would exist."""
|
||||
self.patch_platform()
|
||||
self.patch_exists()
|
||||
self.patch_version()
|
||||
self.patch_file(data)
|
||||
|
||||
|
||||
class TestActuteWarning:
|
||||
|
||||
"""Test actute_warning."""
|
||||
|
||||
@pytest.fixture
|
||||
def patcher(self, monkeypatch):
|
||||
"""Fixture providing a Patcher helper."""
|
||||
return Patcher(monkeypatch)
|
||||
|
||||
def test_non_linux(self, patcher, capsys):
|
||||
"""Test with a non-Linux OS."""
|
||||
patcher.patch_platform('toaster')
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
|
||||
def test_no_compose(self, patcher, capsys):
|
||||
"""Test with no compose file."""
|
||||
patcher.patch_platform()
|
||||
patcher.patch_exists(False)
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
|
||||
def test_newer_qt(self, patcher, capsys):
|
||||
"""Test with compose file but newer Qt version."""
|
||||
patcher.patch_platform()
|
||||
patcher.patch_exists()
|
||||
patcher.patch_version('5.4')
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
|
||||
def test_no_match(self, patcher, capsys):
|
||||
"""Test with compose file and affected Qt but no match."""
|
||||
patcher.patch_all('foobar')
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
|
||||
def test_empty(self, patcher, capsys):
|
||||
"""Test with empty compose file."""
|
||||
patcher.patch_all(None)
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
|
||||
def test_match(self, patcher, capsys):
|
||||
"""Test with compose file and affected Qt and a match."""
|
||||
patcher.patch_all('foobar\n<dead_actute>\nbaz')
|
||||
utils.actute_warning()
|
||||
out, err = capsys.readouterr()
|
||||
assert out.startswith('Note: If you got a')
|
||||
assert not err
|
||||
|
||||
def test_match_stdout_none(self, monkeypatch, patcher, capsys):
|
||||
"""Test with a match and stdout being None."""
|
||||
patcher.patch_all('foobar\n<dead_actute>\nbaz')
|
||||
monkeypatch.setattr('sys.stdout', None)
|
||||
utils.actute_warning()
|
||||
|
||||
def test_unreadable(self, mocker, patcher, capsys, caplog):
|
||||
"""Test with an unreadable compose file."""
|
||||
patcher.patch_platform()
|
||||
patcher.patch_exists()
|
||||
patcher.patch_version()
|
||||
mocker.patch('qutebrowser.utils.utils.open', side_effect=OSError,
|
||||
create=True)
|
||||
|
||||
with caplog.atLevel(logging.ERROR, 'init'):
|
||||
utils.actute_warning()
|
||||
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == 'Failed to read Compose file'
|
||||
out, _err = capsys.readouterr()
|
||||
assert not out
|
||||
|
||||
|
||||
class TestInterpolateColor:
|
||||
|
||||
@ -164,62 +326,40 @@ class TestInterpolateColor:
|
||||
assert Color(color) == expected
|
||||
|
||||
|
||||
class TestFormatSeconds:
|
||||
|
||||
"""Tests for format_seconds.
|
||||
|
||||
Class attributes:
|
||||
TESTS: A list of (input, output) tuples.
|
||||
"""
|
||||
|
||||
TESTS = [
|
||||
(-1, '-0:01'),
|
||||
(0, '0:00'),
|
||||
(59, '0:59'),
|
||||
(60, '1:00'),
|
||||
(60.4, '1:00'),
|
||||
(61, '1:01'),
|
||||
(-61, '-1:01'),
|
||||
(3599, '59:59'),
|
||||
(3600, '1:00:00'),
|
||||
(3601, '1:00:01'),
|
||||
(36000, '10:00:00'),
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize('seconds, out', TESTS)
|
||||
def test_format_seconds(self, seconds, out):
|
||||
"""Test format_seconds with several tests."""
|
||||
assert utils.format_seconds(seconds) == out
|
||||
@pytest.mark.parametrize('seconds, out', [
|
||||
(-1, '-0:01'),
|
||||
(0, '0:00'),
|
||||
(59, '0:59'),
|
||||
(60, '1:00'),
|
||||
(60.4, '1:00'),
|
||||
(61, '1:01'),
|
||||
(-61, '-1:01'),
|
||||
(3599, '59:59'),
|
||||
(3600, '1:00:00'),
|
||||
(3601, '1:00:01'),
|
||||
(36000, '10:00:00'),
|
||||
])
|
||||
def test_format_seconds(seconds, out):
|
||||
assert utils.format_seconds(seconds) == out
|
||||
|
||||
|
||||
class TestFormatTimedelta:
|
||||
|
||||
"""Tests for format_timedelta.
|
||||
|
||||
Class attributes:
|
||||
TESTS: A list of (input, output) tuples.
|
||||
"""
|
||||
|
||||
TESTS = [
|
||||
(datetime.timedelta(seconds=-1), '-1s'),
|
||||
(datetime.timedelta(seconds=0), '0s'),
|
||||
(datetime.timedelta(seconds=59), '59s'),
|
||||
(datetime.timedelta(seconds=120), '2m'),
|
||||
(datetime.timedelta(seconds=60.4), '1m'),
|
||||
(datetime.timedelta(seconds=63), '1m 3s'),
|
||||
(datetime.timedelta(seconds=-64), '-1m 4s'),
|
||||
(datetime.timedelta(seconds=3599), '59m 59s'),
|
||||
(datetime.timedelta(seconds=3600), '1h'),
|
||||
(datetime.timedelta(seconds=3605), '1h 5s'),
|
||||
(datetime.timedelta(seconds=3723), '1h 2m 3s'),
|
||||
(datetime.timedelta(seconds=3780), '1h 3m'),
|
||||
(datetime.timedelta(seconds=36000), '10h'),
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize('td, out', TESTS)
|
||||
def test_format_seconds(self, td, out):
|
||||
"""Test format_seconds with several tests."""
|
||||
assert utils.format_timedelta(td) == out
|
||||
@pytest.mark.parametrize('td, out', [
|
||||
(datetime.timedelta(seconds=-1), '-1s'),
|
||||
(datetime.timedelta(seconds=0), '0s'),
|
||||
(datetime.timedelta(seconds=59), '59s'),
|
||||
(datetime.timedelta(seconds=120), '2m'),
|
||||
(datetime.timedelta(seconds=60.4), '1m'),
|
||||
(datetime.timedelta(seconds=63), '1m 3s'),
|
||||
(datetime.timedelta(seconds=-64), '-1m 4s'),
|
||||
(datetime.timedelta(seconds=3599), '59m 59s'),
|
||||
(datetime.timedelta(seconds=3600), '1h'),
|
||||
(datetime.timedelta(seconds=3605), '1h 5s'),
|
||||
(datetime.timedelta(seconds=3723), '1h 2m 3s'),
|
||||
(datetime.timedelta(seconds=3780), '1h 3m'),
|
||||
(datetime.timedelta(seconds=36000), '10h'),
|
||||
])
|
||||
def test_format_timedelta(td, out):
|
||||
assert utils.format_timedelta(td) == out
|
||||
|
||||
|
||||
class TestFormatSize:
|
||||
@ -264,30 +404,24 @@ class TestKeyToString:
|
||||
|
||||
"""Test key_to_string."""
|
||||
|
||||
def test_unicode_garbage_keys(self):
|
||||
@pytest.mark.parametrize('key, expected', [
|
||||
(Qt.Key_Blue, 'Blue'),
|
||||
(Qt.Key_Backtab, 'Tab'),
|
||||
(Qt.Key_Escape, 'Escape'),
|
||||
(Qt.Key_A, 'A'),
|
||||
(Qt.Key_degree, '°'),
|
||||
])
|
||||
def test_normal(self, key, expected):
|
||||
"""Test a special key where QKeyEvent::toString works incorrectly."""
|
||||
assert utils.key_to_string(Qt.Key_Blue) == 'Blue'
|
||||
assert utils.key_to_string(key) == expected
|
||||
|
||||
def test_backtab(self):
|
||||
"""Test if backtab is normalized to tab correctly."""
|
||||
assert utils.key_to_string(Qt.Key_Backtab) == 'Tab'
|
||||
|
||||
def test_escape(self):
|
||||
"""Test if escape is normalized to escape correctly."""
|
||||
assert utils.key_to_string(Qt.Key_Escape) == 'Escape'
|
||||
|
||||
def test_letter(self):
|
||||
"""Test a simple letter key."""
|
||||
def test_missing(self, monkeypatch):
|
||||
"""Test with a missing key."""
|
||||
monkeypatch.delattr('qutebrowser.utils.utils.Qt.Key_Blue')
|
||||
# We don't want to test the key which is actually missing - we only
|
||||
# want to know if the mapping still behaves properly.
|
||||
assert utils.key_to_string(Qt.Key_A) == 'A'
|
||||
|
||||
def test_unicode(self):
|
||||
"""Test a printable unicode key."""
|
||||
assert utils.key_to_string(Qt.Key_degree) == '°'
|
||||
|
||||
def test_special(self):
|
||||
"""Test a non-printable key handled by QKeyEvent::toString."""
|
||||
assert utils.key_to_string(Qt.Key_F1) == 'F1'
|
||||
|
||||
|
||||
class TestKeyEventToString:
|
||||
|
||||
@ -323,26 +457,272 @@ class TestKeyEventToString:
|
||||
Qt.MetaModifier | Qt.ShiftModifier))
|
||||
assert utils.keyevent_to_string(evt) == 'Ctrl+Alt+Meta+Shift+A'
|
||||
|
||||
def test_mac(self, monkeypatch, fake_keyevent_factory):
|
||||
"""Test with a simulated mac."""
|
||||
monkeypatch.setattr('sys.platform', 'darwin')
|
||||
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
|
||||
assert utils.keyevent_to_string(evt) == 'Meta+A'
|
||||
|
||||
class TestNormalize:
|
||||
|
||||
"""Test normalize_keystr."""
|
||||
@pytest.mark.parametrize('orig, repl', [
|
||||
('Control+x', 'ctrl+x'),
|
||||
('Windows+x', 'meta+x'),
|
||||
('Mod1+x', 'alt+x'),
|
||||
('Mod4+x', 'meta+x'),
|
||||
('Control--', 'ctrl+-'),
|
||||
('Windows++', 'meta++'),
|
||||
('ctrl-x', 'ctrl+x'),
|
||||
('control+x', 'ctrl+x')
|
||||
])
|
||||
def test_normalize_keystr(orig, repl):
|
||||
assert utils.normalize_keystr(orig) == repl
|
||||
|
||||
STRINGS = (
|
||||
('Control+x', 'ctrl+x'),
|
||||
('Windows+x', 'meta+x'),
|
||||
('Mod1+x', 'alt+x'),
|
||||
('Mod4+x', 'meta+x'),
|
||||
('Control--', 'ctrl+-'),
|
||||
('Windows++', 'meta++'),
|
||||
('ctrl-x', 'ctrl+x'),
|
||||
('control+x', 'ctrl+x')
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize('orig, repl', STRINGS)
|
||||
def test_normalize(self, orig, repl):
|
||||
"""Test normalize with some strings."""
|
||||
assert utils.normalize_keystr(orig) == repl
|
||||
class TestFakeIOStream:
|
||||
|
||||
"""Test FakeIOStream."""
|
||||
|
||||
def _write_func(self, text):
|
||||
return text
|
||||
|
||||
def test_flush(self):
|
||||
"""Smoke-test to see if flushing works."""
|
||||
s = utils.FakeIOStream(self._write_func)
|
||||
s.flush()
|
||||
|
||||
def test_isatty(self):
|
||||
"""Make sure isatty() is always false."""
|
||||
s = utils.FakeIOStream(self._write_func)
|
||||
assert not s.isatty()
|
||||
|
||||
def test_write(self):
|
||||
"""Make sure writing works."""
|
||||
s = utils.FakeIOStream(self._write_func)
|
||||
assert s.write('echo') == 'echo'
|
||||
|
||||
|
||||
class TestFakeIO:
|
||||
|
||||
"""Test FakeIO."""
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def restore_streams(self):
|
||||
"""Restore sys.stderr/sys.stdout after tests."""
|
||||
old_stdout = sys.stdout
|
||||
old_stderr = sys.stderr
|
||||
yield
|
||||
sys.stdout = old_stdout
|
||||
sys.stderr = old_stderr
|
||||
|
||||
def test_normal(self, capsys):
|
||||
"""Test without changing sys.stderr/sys.stdout."""
|
||||
data = io.StringIO()
|
||||
with utils.fake_io(data.write):
|
||||
sys.stdout.write('hello\n')
|
||||
sys.stderr.write('world\n')
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
assert data.getvalue() == 'hello\nworld\n'
|
||||
|
||||
sys.stdout.write('back to\n')
|
||||
sys.stderr.write('normal\n')
|
||||
out, err = capsys.readouterr()
|
||||
assert out == 'back to\n'
|
||||
assert err == 'normal\n'
|
||||
|
||||
def test_stdout_replaced(self, capsys):
|
||||
"""Test with replaced stdout."""
|
||||
data = io.StringIO()
|
||||
new_stdout = io.StringIO()
|
||||
with utils.fake_io(data.write):
|
||||
sys.stdout.write('hello\n')
|
||||
sys.stderr.write('world\n')
|
||||
sys.stdout = new_stdout
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
assert data.getvalue() == 'hello\nworld\n'
|
||||
|
||||
sys.stdout.write('still new\n')
|
||||
sys.stderr.write('normal\n')
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert err == 'normal\n'
|
||||
assert new_stdout.getvalue() == 'still new\n'
|
||||
|
||||
def test_stderr_replaced(self, capsys):
|
||||
"""Test with replaced stderr."""
|
||||
data = io.StringIO()
|
||||
new_stderr = io.StringIO()
|
||||
with utils.fake_io(data.write):
|
||||
sys.stdout.write('hello\n')
|
||||
sys.stderr.write('world\n')
|
||||
sys.stderr = new_stderr
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert not err
|
||||
assert data.getvalue() == 'hello\nworld\n'
|
||||
|
||||
sys.stdout.write('normal\n')
|
||||
sys.stderr.write('still new\n')
|
||||
out, err = capsys.readouterr()
|
||||
assert out == 'normal\n'
|
||||
assert not err
|
||||
assert new_stderr.getvalue() == 'still new\n'
|
||||
|
||||
|
||||
class GotException(Exception):
|
||||
|
||||
"""Exception used for TestDisabledExcepthook."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def excepthook(_exc, _val, _tb):
|
||||
return
|
||||
|
||||
|
||||
def excepthook_2(_exc, _val, _tb):
|
||||
return
|
||||
|
||||
|
||||
class TestDisabledExcepthook:
|
||||
|
||||
"""Test disabled_excepthook.
|
||||
|
||||
This doesn't test much as some things are untestable without triggering
|
||||
the excepthook (which is hard to test).
|
||||
"""
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def restore_excepthook(self):
|
||||
"""Restore sys.excepthook and sys.__excepthook__ after tests."""
|
||||
old_excepthook = sys.excepthook
|
||||
old_dunder_excepthook = sys.__excepthook__
|
||||
yield
|
||||
sys.excepthook = old_excepthook
|
||||
sys.__excepthook__ = old_dunder_excepthook
|
||||
|
||||
def test_normal(self):
|
||||
"""Test without changing sys.excepthook."""
|
||||
sys.excepthook = excepthook
|
||||
assert sys.excepthook is excepthook
|
||||
with utils.disabled_excepthook():
|
||||
assert sys.excepthook is not excepthook
|
||||
assert sys.excepthook is excepthook
|
||||
|
||||
def test_changed(self):
|
||||
"""Test with changed sys.excepthook."""
|
||||
sys.excepthook = excepthook
|
||||
with utils.disabled_excepthook():
|
||||
assert sys.excepthook is not excepthook
|
||||
sys.excepthook = excepthook_2
|
||||
assert sys.excepthook is excepthook_2
|
||||
|
||||
|
||||
class TestPreventExceptions:
|
||||
|
||||
"""Test prevent_exceptions."""
|
||||
|
||||
@utils.prevent_exceptions(42)
|
||||
def func_raising(self):
|
||||
raise Exception
|
||||
|
||||
def test_raising(self, caplog):
|
||||
"""Test with a raising function."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
ret = self.func_raising()
|
||||
assert ret == 42
|
||||
assert len(caplog.records()) == 1
|
||||
expected = 'Error in test_utils.TestPreventExceptions.func_raising'
|
||||
actual = caplog.records()[0].message
|
||||
assert actual == expected
|
||||
|
||||
@utils.prevent_exceptions(42)
|
||||
def func_not_raising(self):
|
||||
return 23
|
||||
|
||||
def test_not_raising(self, caplog):
|
||||
"""Test with a non-raising function."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
ret = self.func_not_raising()
|
||||
assert ret == 23
|
||||
assert not caplog.records()
|
||||
|
||||
@utils.prevent_exceptions(42, True)
|
||||
def func_predicate_true(self):
|
||||
raise Exception
|
||||
|
||||
def test_predicate_true(self, caplog):
|
||||
"""Test with a True predicate."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
ret = self.func_predicate_true()
|
||||
assert ret == 42
|
||||
assert len(caplog.records()) == 1
|
||||
|
||||
@utils.prevent_exceptions(42, False)
|
||||
def func_predicate_false(self):
|
||||
raise Exception
|
||||
|
||||
def test_predicate_false(self, caplog):
|
||||
"""Test with a False predicate."""
|
||||
with caplog.atLevel(logging.ERROR, 'misc'):
|
||||
with pytest.raises(Exception):
|
||||
self.func_predicate_false()
|
||||
assert not caplog.records()
|
||||
|
||||
|
||||
class Obj:
|
||||
|
||||
"""Test object for test_get_repr()."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('constructor, attrs, expected', [
|
||||
(False, {}, '<test_utils.Obj>'),
|
||||
(False, {'foo': None}, '<test_utils.Obj foo=None>'),
|
||||
(False, {'foo': "b'ar", 'baz': 2}, '<test_utils.Obj baz=2 foo="b\'ar">'),
|
||||
(True, {}, 'test_utils.Obj()'),
|
||||
(True, {'foo': None}, 'test_utils.Obj(foo=None)'),
|
||||
(True, {'foo': "te'st", 'bar': 2}, 'test_utils.Obj(bar=2, foo="te\'st")'),
|
||||
])
|
||||
def test_get_repr(constructor, attrs, expected):
|
||||
"""Test get_repr()."""
|
||||
assert utils.get_repr(Obj(), constructor, **attrs) == expected
|
||||
|
||||
|
||||
class QualnameObj():
|
||||
|
||||
"""Test object for test_qualname."""
|
||||
|
||||
def func(self):
|
||||
"""Test method for test_qualname."""
|
||||
pass
|
||||
|
||||
|
||||
def qualname_func(_blah):
|
||||
"""Test function for test_qualname."""
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obj, expected', [
|
||||
(QualnameObj(), '<unknown>'), # instance - unknown
|
||||
(QualnameObj, 'test_utils.QualnameObj'), # class
|
||||
(QualnameObj.func, 'test_utils.QualnameObj.func'), # unbound method
|
||||
(QualnameObj().func, 'test_utils.QualnameObj.func'), # bound method
|
||||
(qualname_func, 'test_utils.qualname_func'), # function
|
||||
(functools.partial(qualname_func, True), 'test_utils.qualname_func'),
|
||||
(qutebrowser, 'qutebrowser'), # module
|
||||
(qutebrowser.utils, 'qutebrowser.utils'), # submodule
|
||||
(utils, 'qutebrowser.utils.utils'), # submodule (from-import)
|
||||
])
|
||||
def test_qualname(obj, expected):
|
||||
assert utils.qualname(obj) == expected
|
||||
|
||||
|
||||
class TestIsEnum:
|
||||
@ -412,20 +792,13 @@ class TestRaises:
|
||||
utils.raises(ValueError, self.do_raise)
|
||||
|
||||
|
||||
class TestForceEncoding:
|
||||
|
||||
"""Test force_encoding."""
|
||||
|
||||
TESTS = [
|
||||
('hello world', 'ascii', 'hello world'),
|
||||
('hellö wörld', 'utf-8', 'hellö wörld'),
|
||||
('hellö wörld', 'ascii', 'hell? w?rld'),
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize('inp, enc, expected', TESTS)
|
||||
def test_fitting_ascii(self, inp, enc, expected):
|
||||
"""Test force_encoding will yield expected text."""
|
||||
assert utils.force_encoding(inp, enc) == expected
|
||||
@pytest.mark.parametrize('inp, enc, expected', [
|
||||
('hello world', 'ascii', 'hello world'),
|
||||
('hellö wörld', 'utf-8', 'hellö wörld'),
|
||||
('hellö wörld', 'ascii', 'hell? w?rld'),
|
||||
])
|
||||
def test_force_encoding(inp, enc, expected):
|
||||
assert utils.force_encoding(inp, enc) == expected
|
||||
|
||||
|
||||
class TestNewestSlice:
|
||||
@ -437,44 +810,23 @@ class TestNewestSlice:
|
||||
with pytest.raises(ValueError):
|
||||
utils.newest_slice([], -2)
|
||||
|
||||
def test_count_minus_one(self):
|
||||
"""Test with a count of -1 (all elements)."""
|
||||
items = range(20)
|
||||
sliced = utils.newest_slice(items, -1)
|
||||
assert list(sliced) == list(items)
|
||||
|
||||
def test_count_zero(self):
|
||||
"""Test with a count of 0 (no elements)."""
|
||||
items = range(20)
|
||||
sliced = utils.newest_slice(items, 0)
|
||||
assert list(sliced) == []
|
||||
|
||||
def test_count_much_smaller(self):
|
||||
"""Test with a count which is much smaller than the iterable."""
|
||||
items = range(20)
|
||||
sliced = utils.newest_slice(items, 5)
|
||||
assert list(sliced) == [15, 16, 17, 18, 19]
|
||||
|
||||
def test_count_smaller(self):
|
||||
"""Test with a count which is exactly one smaller."""
|
||||
items = range(5)
|
||||
sliced = utils.newest_slice(items, 4)
|
||||
assert list(sliced) == [1, 2, 3, 4]
|
||||
|
||||
def test_count_equal(self):
|
||||
"""Test with a count which is just as large as the iterable."""
|
||||
items = range(5)
|
||||
sliced = utils.newest_slice(items, 5)
|
||||
assert list(sliced) == list(items)
|
||||
|
||||
def test_count_bigger(self):
|
||||
"""Test with a count which is one bigger than the iterable."""
|
||||
items = range(5)
|
||||
sliced = utils.newest_slice(items, 6)
|
||||
assert list(sliced) == list(items)
|
||||
|
||||
def test_count_much_bigger(self):
|
||||
"""Test with a count which is much bigger than the iterable."""
|
||||
items = range(5)
|
||||
sliced = utils.newest_slice(items, 50)
|
||||
assert list(sliced) == list(items)
|
||||
@pytest.mark.parametrize('items, count, expected', [
|
||||
# Count of -1 (all elements).
|
||||
(range(20), -1, range(20)),
|
||||
# Count of 0 (no elements).
|
||||
(range(20), 0, []),
|
||||
# Count which is much smaller than the iterable.
|
||||
(range(20), 5, [15, 16, 17, 18, 19]),
|
||||
# Count which is exactly one smaller."""
|
||||
(range(5), 4, [1, 2, 3, 4]),
|
||||
# Count which is just as large as the iterable."""
|
||||
(range(5), 5, range(5)),
|
||||
# Count which is one bigger than the iterable.
|
||||
(range(5), 6, range(5)),
|
||||
# Count which is much bigger than the iterable.
|
||||
(range(5), 50, range(5)),
|
||||
])
|
||||
def test_good(self, items, count, expected):
|
||||
"""Test slices which shouldn't raise an exception."""
|
||||
sliced = utils.newest_slice(items, count)
|
||||
assert list(sliced) == list(expected)
|
||||
|
657
tests/utils/test_version.py
Normal file
657
tests/utils/test_version.py
Normal file
@ -0,0 +1,657 @@
|
||||
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||
|
||||
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
"""Tests for qutebrowser.utils.version."""
|
||||
|
||||
import io
|
||||
import sys
|
||||
import os.path
|
||||
import subprocess
|
||||
import contextlib
|
||||
import builtins
|
||||
import types
|
||||
import importlib
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
import qutebrowser
|
||||
from qutebrowser.utils import version
|
||||
|
||||
|
||||
class GitStrSubprocessFake:
|
||||
|
||||
"""Object returned by the git_str_subprocess_fake fixture.
|
||||
|
||||
This provides a function which is used to patch _git_str_subprocess.
|
||||
|
||||
Attributes:
|
||||
retval: The value to return when called. Needs to be set before func is
|
||||
called.
|
||||
"""
|
||||
|
||||
UNSET = object()
|
||||
|
||||
def __init__(self):
|
||||
self.retval = self.UNSET
|
||||
|
||||
def func(self, gitpath):
|
||||
"""Function called instead of _git_str_subprocess.
|
||||
|
||||
Checks whether the path passed is what we expected, and returns
|
||||
self.retval.
|
||||
"""
|
||||
if self.retval is self.UNSET:
|
||||
raise ValueError("func got called without retval being set!")
|
||||
retval = self.retval
|
||||
self.retval = self.UNSET
|
||||
gitpath = os.path.normpath(gitpath)
|
||||
expected = os.path.abspath(os.path.join(
|
||||
os.path.dirname(qutebrowser.__file__), os.pardir))
|
||||
assert gitpath == expected
|
||||
return retval
|
||||
|
||||
|
||||
class TestGitStr:
|
||||
|
||||
"""Tests for _git_str()."""
|
||||
|
||||
@pytest.yield_fixture
|
||||
def commit_file_mock(self, mocker):
|
||||
"""Fixture providing a mock for utils.read_file for git-commit-id.
|
||||
|
||||
On fixture teardown, it makes sure it got called with git-commit-id as
|
||||
argument.
|
||||
"""
|
||||
mocker.patch('qutebrowser.utils.version.subprocess',
|
||||
side_effect=AssertionError)
|
||||
m = mocker.patch('qutebrowser.utils.version.utils.read_file')
|
||||
yield m
|
||||
m.assert_called_with('git-commit-id')
|
||||
|
||||
@pytest.fixture
|
||||
def git_str_subprocess_fake(self, mocker, monkeypatch):
|
||||
"""Fixture patching _git_str_subprocess with a GitStrSubprocessFake."""
|
||||
mocker.patch('qutebrowser.utils.version.subprocess',
|
||||
side_effect=AssertionError)
|
||||
fake = GitStrSubprocessFake()
|
||||
monkeypatch.setattr('qutebrowser.utils.version._git_str_subprocess',
|
||||
fake.func)
|
||||
return fake
|
||||
|
||||
def test_frozen_ok(self, commit_file_mock, monkeypatch):
|
||||
"""Test with sys.frozen=True and a successful git-commit-id read."""
|
||||
monkeypatch.setattr(qutebrowser.utils.version.sys, 'frozen', True,
|
||||
raising=False)
|
||||
commit_file_mock.return_value = 'deadbeef'
|
||||
assert version._git_str() == 'deadbeef'
|
||||
|
||||
def test_frozen_oserror(self, commit_file_mock, monkeypatch):
|
||||
"""Test with sys.frozen=True and OSError when reading git-commit-id."""
|
||||
monkeypatch.setattr(qutebrowser.utils.version.sys, 'frozen', True,
|
||||
raising=False)
|
||||
commit_file_mock.side_effect = OSError
|
||||
assert version._git_str() is None
|
||||
|
||||
def test_normal_successful(self, git_str_subprocess_fake):
|
||||
"""Test with git returning a successful result."""
|
||||
git_str_subprocess_fake.retval = 'c0ffeebabe'
|
||||
assert version._git_str() == 'c0ffeebabe'
|
||||
|
||||
def test_normal_error(self, commit_file_mock, git_str_subprocess_fake):
|
||||
"""Test without repo (but git-commit-id)."""
|
||||
git_str_subprocess_fake.retval = None
|
||||
commit_file_mock.return_value = '1b4d1dea'
|
||||
assert version._git_str() == '1b4d1dea'
|
||||
|
||||
def test_normal_path_oserror(self, mocker, git_str_subprocess_fake):
|
||||
"""Test with things raising OSError."""
|
||||
mocker.patch('qutebrowser.utils.version.os.path.join',
|
||||
side_effect=OSError)
|
||||
mocker.patch('qutebrowser.utils.version.utils.read_file',
|
||||
side_effect=OSError)
|
||||
assert version._git_str() is None
|
||||
|
||||
def test_normal_path_nofile(self, monkeypatch, caplog,
|
||||
git_str_subprocess_fake, commit_file_mock):
|
||||
"""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'):
|
||||
assert version._git_str() == '0deadcode'
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == "Error while getting git path"
|
||||
|
||||
|
||||
def _has_git():
|
||||
"""Check if git is installed."""
|
||||
try:
|
||||
subprocess.check_call(['git', '--version'], stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL)
|
||||
except (OSError, subprocess.CalledProcessError):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
# Decorator for tests needing git, so they get skipped when it's unavailable.
|
||||
needs_git = pytest.mark.skipif(not _has_git(), reason='Needs git installed.')
|
||||
|
||||
|
||||
class TestGitStrSubprocess:
|
||||
|
||||
"""Tests for _git_str_subprocess."""
|
||||
|
||||
@pytest.fixture
|
||||
def git_repo(self, tmpdir):
|
||||
"""A fixture to create a temporary git repo.
|
||||
|
||||
Some things are tested against a real repo so we notice if something in
|
||||
git would change, or we call git incorrectly.
|
||||
"""
|
||||
def _git(*args):
|
||||
"""Helper closure to call git."""
|
||||
env = {
|
||||
'GIT_AUTHOR_NAME': 'qutebrowser testsuite',
|
||||
'GIT_AUTHOR_EMAIL': 'mail@qutebrowser.org',
|
||||
'GIT_AUTHOR_DATE': 'Thu 1 Jan 01:00:00 CET 1970',
|
||||
'GIT_COMMITTER_NAME': 'qutebrowser testsuite',
|
||||
'GIT_COMMITTER_EMAIL': 'mail@qutebrowser.org',
|
||||
'GIT_COMMITTER_DATE': 'Thu 1 Jan 01:00:00 CET 1970',
|
||||
}
|
||||
subprocess.check_call(['git', '-C', str(tmpdir)] + list(args),
|
||||
env=env)
|
||||
|
||||
(tmpdir / 'file').write_text("Hello World!", encoding='utf-8')
|
||||
_git('init')
|
||||
_git('add', 'file')
|
||||
_git('commit', '-am', 'foo', '--no-verify', '--no-edit',
|
||||
'--no-post-rewrite', '--quiet', '--no-gpg-sign')
|
||||
_git('tag', 'foobar')
|
||||
return tmpdir
|
||||
|
||||
@needs_git
|
||||
def test_real_git(self, git_repo):
|
||||
"""Test with a real git repository."""
|
||||
ret = version._git_str_subprocess(str(git_repo))
|
||||
assert ret == 'foobar (1970-01-01 01:00:00 +0100)'
|
||||
|
||||
def test_missing_dir(self, tmpdir):
|
||||
"""Test with a directory which doesn't exist."""
|
||||
ret = version._git_str_subprocess(str(tmpdir / 'does-not-exist'))
|
||||
assert ret is None
|
||||
|
||||
@pytest.mark.parametrize('exc', [
|
||||
OSError,
|
||||
subprocess.CalledProcessError(1, 'foobar')
|
||||
])
|
||||
def test_exception(self, exc, mocker, tmpdir):
|
||||
"""Test with subprocess.check_output raising an exception.
|
||||
|
||||
Args:
|
||||
exc: The exception to raise.
|
||||
"""
|
||||
mocker.patch('qutebrowser.utils.version.subprocess.os.path.isdir',
|
||||
return_value=True)
|
||||
mocker.patch('qutebrowser.utils.version.subprocess.check_output',
|
||||
side_effect=exc)
|
||||
ret = version._git_str_subprocess(str(tmpdir))
|
||||
assert ret is None
|
||||
|
||||
|
||||
class ReleaseInfoFake:
|
||||
|
||||
"""An object providing fakes for glob.glob/open for test_release_info.
|
||||
|
||||
Attributes:
|
||||
_files: The files which should be returned, or None if an exception
|
||||
should be raised. A {filename: [lines]} dict.
|
||||
"""
|
||||
|
||||
def __init__(self, files):
|
||||
self._files = files
|
||||
|
||||
def glob_fake(self, pattern):
|
||||
"""Fake for glob.glob.
|
||||
|
||||
Verifies the arguments and returns the files listed in self._files, or
|
||||
a single fake file if an exception is expected.
|
||||
"""
|
||||
assert pattern == '/etc/*-release'
|
||||
if self._files is None:
|
||||
return ['fake-file']
|
||||
else:
|
||||
return sorted(list(self._files))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open_fake(self, filename, mode, encoding):
|
||||
"""Fake for open().
|
||||
|
||||
Verifies the arguments and returns a StringIO with the content listed
|
||||
in self._files.
|
||||
"""
|
||||
assert mode == 'r'
|
||||
assert encoding == 'utf-8'
|
||||
if self._files is None:
|
||||
raise OSError
|
||||
yield io.StringIO(''.join(self._files[filename]))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('files, expected', [
|
||||
({}, []),
|
||||
({'file': ['']}, [('file', '')]),
|
||||
({'file': []}, [('file', '')]),
|
||||
(
|
||||
{'file1': ['foo\n', 'bar\n'], 'file2': ['baz\n']},
|
||||
[('file1', 'foo\nbar\n'), ('file2', 'baz\n')]
|
||||
),
|
||||
(None, []),
|
||||
])
|
||||
def test_release_info(files, expected, caplog, monkeypatch):
|
||||
"""Test _release_info().
|
||||
|
||||
Args:
|
||||
files: The file dict passed to ReleaseInfoFake.
|
||||
expected: The expected _release_info output.
|
||||
"""
|
||||
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'):
|
||||
assert version._release_info() == expected
|
||||
if files is None:
|
||||
assert len(caplog.records()) == 1
|
||||
assert caplog.records()[0].message == "Error while reading fake-file."
|
||||
|
||||
|
||||
class ImportFake:
|
||||
|
||||
"""A fake for __import__ which is used by the import_fake fixture.
|
||||
|
||||
Attributes:
|
||||
exists: A dict mapping module names to bools. If True, the import will
|
||||
success. Otherwise, it'll fail with ImportError.
|
||||
version_attribute: The name to use in the fake modules for the version
|
||||
attribute.
|
||||
version: The version to use for the modules.
|
||||
_real_import: Saving the real __import__ builtin so the imports can be
|
||||
done normally for modules not in self.exists.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.exists = {
|
||||
'sip': True,
|
||||
'colorlog': True,
|
||||
'colorama': True,
|
||||
'pypeg2': True,
|
||||
'jinja2': True,
|
||||
'pygments': True,
|
||||
'yaml': True,
|
||||
}
|
||||
self.version_attribute = '__version__'
|
||||
self.version = '1.2.3'
|
||||
self._real_import = builtins.__import__
|
||||
|
||||
def _do_import(self, name):
|
||||
"""Helper for fake_import and fake_importlib_import to do the work.
|
||||
|
||||
Return:
|
||||
The imported fake module, or None if normal importing should be
|
||||
used.
|
||||
"""
|
||||
if name not in self.exists:
|
||||
# Not one of the modules to test -> use real import
|
||||
return None
|
||||
elif self.exists[name]:
|
||||
ns = types.SimpleNamespace()
|
||||
if self.version_attribute is not None:
|
||||
setattr(ns, self.version_attribute, self.version)
|
||||
return ns
|
||||
else:
|
||||
raise ImportError("Fake ImportError for {}.".format(name))
|
||||
|
||||
def fake_import(self, name, *args, **kwargs):
|
||||
"""Fake for the builtin __import__."""
|
||||
module = self._do_import(name)
|
||||
if module is not None:
|
||||
return module
|
||||
else:
|
||||
return self._real_import(name, *args, **kwargs)
|
||||
|
||||
def fake_importlib_import(self, name):
|
||||
"""Fake for importlib.import_module."""
|
||||
module = self._do_import(name)
|
||||
if module is not None:
|
||||
return module
|
||||
else:
|
||||
return importlib.import_module(name)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def import_fake(monkeypatch):
|
||||
"""Fixture to patch imports using ImportFake."""
|
||||
fake = ImportFake()
|
||||
monkeypatch.setattr('builtins.__import__', fake.fake_import)
|
||||
monkeypatch.setattr('qutebrowser.utils.version.importlib.import_module',
|
||||
fake.fake_importlib_import)
|
||||
return fake
|
||||
|
||||
|
||||
class TestModuleVersions:
|
||||
|
||||
"""Tests for _module_versions()."""
|
||||
|
||||
@pytest.mark.usefixtures('import_fake')
|
||||
def test_all_present(self):
|
||||
"""Test with all modules present in version 1.2.3."""
|
||||
expected = ['sip: yes', 'colorlog: yes', 'colorama: 1.2.3',
|
||||
'pypeg2: 1.2.3', 'jinja2: 1.2.3', 'pygments: 1.2.3',
|
||||
'yaml: 1.2.3']
|
||||
assert version._module_versions() == expected
|
||||
|
||||
@pytest.mark.parametrize('module, idx, expected', [
|
||||
('colorlog', 1, 'colorlog: no'),
|
||||
('colorama', 2, 'colorama: no'),
|
||||
])
|
||||
def test_missing_module(self, module, idx, expected, import_fake):
|
||||
"""Test with a module missing.
|
||||
|
||||
Args:
|
||||
module: The name of the missing module.
|
||||
idx: The index where the given text is expected.
|
||||
expected: The expected text.
|
||||
"""
|
||||
import_fake.exists[module] = False
|
||||
assert version._module_versions()[idx] == expected
|
||||
|
||||
@pytest.mark.parametrize('value, expected', [
|
||||
('VERSION', ['sip: yes', 'colorlog: yes', 'colorama: 1.2.3',
|
||||
'pypeg2: yes', 'jinja2: yes', 'pygments: yes',
|
||||
'yaml: yes']),
|
||||
('SIP_VERSION_STR', ['sip: 1.2.3', 'colorlog: yes', 'colorama: yes',
|
||||
'pypeg2: yes', 'jinja2: yes', 'pygments: yes',
|
||||
'yaml: yes']),
|
||||
(None, ['sip: yes', 'colorlog: yes', 'colorama: yes', 'pypeg2: yes',
|
||||
'jinja2: yes', 'pygments: yes', 'yaml: yes']),
|
||||
])
|
||||
def test_version_attribute(self, value, expected, import_fake):
|
||||
"""Test with a different version attribute.
|
||||
|
||||
VERSION is tested for old colorama versions, and None to make sure
|
||||
things still work if some package suddenly doesn't have __version__.
|
||||
|
||||
Args:
|
||||
value: The name of the version attribute.
|
||||
expected: The expected return value.
|
||||
"""
|
||||
import_fake.version_attribute = value
|
||||
assert version._module_versions() == expected
|
||||
|
||||
@pytest.mark.parametrize('name, has_version', [
|
||||
('colorlog', False),
|
||||
('sip', False),
|
||||
('colorama', True),
|
||||
('pypeg2', True),
|
||||
('jinja2', True),
|
||||
('pygments', True),
|
||||
('yaml', True),
|
||||
])
|
||||
def test_existing_attributes(self, name, has_version):
|
||||
"""Check if all dependencies have an expected __version__ attribute.
|
||||
|
||||
The aim of this test is to fail if modules suddenly don't have a
|
||||
__version__ attribute anymore in a newer version, or colorlog has one.
|
||||
|
||||
Args:
|
||||
name: The name of the module to check.
|
||||
has_version: Whether a __version__ attribute is expected.
|
||||
"""
|
||||
module = importlib.import_module(name)
|
||||
assert hasattr(module, '__version__') == has_version
|
||||
|
||||
def test_existing_sip_attribute(self):
|
||||
"""Test if sip has a SIP_VERSION_STR attribute.
|
||||
|
||||
The aim of this test is to fail if that gets missing in some future
|
||||
version of sip.
|
||||
"""
|
||||
import sip
|
||||
assert isinstance(sip.SIP_VERSION_STR, str)
|
||||
|
||||
|
||||
class TestOsInfo:
|
||||
|
||||
"""Tests for _os_info."""
|
||||
|
||||
@pytest.mark.parametrize('dist, dist_str', [
|
||||
(('x', '', 'y'), 'x, y'),
|
||||
(('a', 'b', 'c'), 'a, b, c'),
|
||||
(('', '', ''), ''),
|
||||
])
|
||||
def test_linux_fake(self, monkeypatch, dist, dist_str):
|
||||
"""Test with a fake Linux.
|
||||
|
||||
Args:
|
||||
dist: The value to set platform.dist() to.
|
||||
dist_str: The expected distribution string in version._os_info().
|
||||
"""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.sys.platform', 'linux')
|
||||
monkeypatch.setattr('qutebrowser.utils.version._release_info',
|
||||
lambda: [('releaseinfo', 'Hello World')])
|
||||
monkeypatch.setattr('qutebrowser.utils.version.platform.dist',
|
||||
lambda: dist)
|
||||
ret = version._os_info()
|
||||
expected = ['OS Version: {}'.format(dist_str), '',
|
||||
'--- releaseinfo ---', 'Hello World']
|
||||
assert ret == expected
|
||||
|
||||
def test_windows_fake(self, monkeypatch):
|
||||
"""Test with a fake Windows."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.sys.platform', 'win32')
|
||||
monkeypatch.setattr('qutebrowser.utils.version.platform.win32_ver',
|
||||
lambda: ('eggs', 'bacon', 'ham', 'spam'))
|
||||
ret = version._os_info()
|
||||
expected = ['OS Version: eggs, bacon, ham, spam']
|
||||
assert ret == expected
|
||||
|
||||
@pytest.mark.parametrize('mac_ver, mac_ver_str', [
|
||||
(('x', ('', '', ''), 'y'), 'x, y'),
|
||||
(('', ('', '', ''), ''), ''),
|
||||
(('x', ('1', '2', '3'), 'y'), 'x, 1.2.3, y'),
|
||||
])
|
||||
def test_os_x_fake(self, monkeypatch, mac_ver, mac_ver_str):
|
||||
"""Test with a fake OS X.
|
||||
|
||||
Args:
|
||||
mac_ver: The tuple to set platform.mac_ver() to.
|
||||
mac_ver_str: The expected Mac version string in version._os_info().
|
||||
"""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.sys.platform', 'darwin')
|
||||
monkeypatch.setattr('qutebrowser.utils.version.platform.mac_ver',
|
||||
lambda: mac_ver)
|
||||
ret = version._os_info()
|
||||
expected = ['OS Version: {}'.format(mac_ver_str)]
|
||||
assert ret == expected
|
||||
|
||||
def test_unknown_fake(self, monkeypatch):
|
||||
"""Test with a fake unknown sys.platform."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.sys.platform',
|
||||
'toaster')
|
||||
ret = version._os_info()
|
||||
expected = ['OS Version: ?']
|
||||
assert ret == expected
|
||||
|
||||
@pytest.mark.skipif(sys.platform != 'linux', reason="requires Linux")
|
||||
def test_linux_real(self):
|
||||
"""Make sure there are no exceptions with a real Linux."""
|
||||
version._os_info()
|
||||
|
||||
@pytest.mark.skipif(sys.platform != 'win32', reason="requires Windows")
|
||||
def test_windows_real(self):
|
||||
"""Make sure there are no exceptions with a real Windows."""
|
||||
version._os_info()
|
||||
|
||||
@pytest.mark.skipif(sys.platform != 'darwin', reason="requires OS X")
|
||||
def test_os_x_real(self):
|
||||
"""Make sure there are no exceptions with a real OS X."""
|
||||
version._os_info()
|
||||
|
||||
|
||||
class FakeQSslSocket:
|
||||
|
||||
"""Fake for the QSslSocket Qt class.
|
||||
|
||||
Attributes:
|
||||
_version: What QSslSocket::sslLibraryVersionString() should return.
|
||||
"""
|
||||
|
||||
def __init__(self, version=None):
|
||||
self._version = version
|
||||
|
||||
def supportsSsl(self):
|
||||
"""Fake for QSslSocket::supportsSsl()."""
|
||||
return True
|
||||
|
||||
def sslLibraryVersionString(self):
|
||||
"""Fake for QSslSocket::sslLibraryVersionString()."""
|
||||
if self._version is None:
|
||||
raise AssertionError("Got valled with version None!")
|
||||
return self._version
|
||||
|
||||
|
||||
class TestVersion:
|
||||
|
||||
"""Tests for version."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def patch(self, monkeypatch):
|
||||
"""Patch some sub-functions we're not interested in."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version._git_str', lambda: None)
|
||||
monkeypatch.setattr('qutebrowser.utils.version._module_versions',
|
||||
lambda: [])
|
||||
monkeypatch.setattr('qutebrowser.utils.version._os_info', lambda: [])
|
||||
|
||||
def test_qutebrowser_version(self, monkeypatch):
|
||||
"""Test the qutebrowser version in the output."""
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.version.qutebrowser.__version__', '23.42')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[0] == 'qutebrowser v23.42'
|
||||
|
||||
def test_git_commit(self, monkeypatch):
|
||||
"""Test the git commit in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version._git_str',
|
||||
lambda: 'deadbeef')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[1] == 'Git commit: deadbeef'
|
||||
|
||||
def test_no_git_commit(self, monkeypatch):
|
||||
"""Test the git commit with _git_str returning None."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version._git_str',
|
||||
lambda: None)
|
||||
lines = version.version().splitlines()
|
||||
assert not lines[1].startswith('Git commit:')
|
||||
|
||||
def test_python_version(self, monkeypatch):
|
||||
"""Test the python version in the output."""
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.version.platform.python_implementation',
|
||||
lambda: 'python_implementation')
|
||||
monkeypatch.setattr(
|
||||
'qutebrowser.utils.version.platform.python_version',
|
||||
lambda: 'python_version')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[2] == 'python_implementation: python_version'
|
||||
|
||||
def test_qt_version(self, monkeypatch):
|
||||
"""Test the python version in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.QT_VERSION_STR', '12.3')
|
||||
monkeypatch.setattr('qutebrowser.utils.version.qVersion',
|
||||
lambda: '45.6')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[3] == 'Qt: 12.3, runtime: 45.6'
|
||||
|
||||
def test_pyqt_version(self, monkeypatch):
|
||||
"""Test the PyQt version in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.PYQT_VERSION_STR',
|
||||
'78.9')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[4] == 'PyQt: 78.9'
|
||||
|
||||
def test_module_versions(self, monkeypatch):
|
||||
"""Test module versions in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version._module_versions',
|
||||
lambda: ['Hello', 'World'])
|
||||
lines = version.version().splitlines()
|
||||
assert (lines[5], lines[6]) == ('Hello', 'World')
|
||||
|
||||
def test_webkit_version(self, monkeypatch):
|
||||
"""Test the webkit version in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.qWebKitVersion',
|
||||
lambda: '567.1')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[5] == 'Webkit: 567.1'
|
||||
|
||||
def test_harfbuzz_none(self, monkeypatch):
|
||||
"""Test harfbuzz output with QT_HARFBUZZ unset."""
|
||||
monkeypatch.delenv('QT_HARFBUZZ', raising=False)
|
||||
lines = version.version().splitlines()
|
||||
assert lines[6] == 'Harfbuzz: system'
|
||||
|
||||
def test_harfbuzz_set(self, monkeypatch):
|
||||
"""Test harfbuzz output with QT_HARFBUZZ set."""
|
||||
monkeypatch.setenv('QT_HARFBUZZ', 'new')
|
||||
lines = version.version().splitlines()
|
||||
assert lines[6] == 'Harfbuzz: new'
|
||||
|
||||
def test_ssl(self, monkeypatch):
|
||||
"""Test SSL version in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.QSslSocket',
|
||||
FakeQSslSocket('1.0.1'))
|
||||
lines = version.version().splitlines()
|
||||
assert lines[7] == 'SSL: 1.0.1'
|
||||
|
||||
@pytest.mark.parametrize('frozen, expected', [(True, 'Frozen: True'),
|
||||
(False, 'Frozen: False')])
|
||||
def test_frozen(self, monkeypatch, frozen, expected):
|
||||
"""Test "Frozen: ..." in the version output."""
|
||||
if frozen:
|
||||
monkeypatch.setattr(sys, 'frozen', True, raising=False)
|
||||
else:
|
||||
monkeypatch.delattr(sys, 'frozen', raising=False)
|
||||
lines = version.version().splitlines()
|
||||
assert lines[9] == expected
|
||||
|
||||
def test_platform(self, monkeypatch):
|
||||
"""Test platform in the version output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version.platform.platform',
|
||||
lambda: 'toaster')
|
||||
monkeypatch.setattr('qutebrowser.utils.version.platform.architecture',
|
||||
lambda: ('64bit', ''))
|
||||
lines = version.version().splitlines()
|
||||
assert lines[10] == 'Platform: toaster, 64bit'
|
||||
|
||||
def test_os_info(self, monkeypatch):
|
||||
"""Test OS info in the output."""
|
||||
monkeypatch.setattr('qutebrowser.utils.version._os_info',
|
||||
lambda: ['Hello', 'World'])
|
||||
lines = version.version().splitlines()
|
||||
assert (lines[11], lines[12]) == ('Hello', 'World')
|
Loading…
Reference in New Issue
Block a user