Merge remote-tracking branch 'origin/pr/3594'

This commit is contained in:
Florian Bruhin 2018-02-21 10:15:27 +01:00
commit 2fbc7b4e1d
4 changed files with 101 additions and 28 deletions

View File

@ -453,7 +453,7 @@ def opengl_vendor(): # pragma: no cover
old_context.makeCurrent(old_surface)
def pastebin_version():
def pastebin_version(pbclient=None):
"""Pastebin the version and log the url to messages."""
def _yank_url(url):
utils.set_clipboard(url)
@ -478,8 +478,8 @@ def pastebin_version():
http_client = httpclient.HTTPClient()
misc_api = pastebin.PastebinClient.MISC_API_URL
pbclient = pastebin.PastebinClient(http_client, parent=app,
api_url=misc_api)
pbclient = pbclient or pastebin.PastebinClient(http_client, parent=app,
api_url=misc_api)
pbclient.success.connect(_on_paste_version_success)
pbclient.error.connect(_on_paste_version_err)

View File

@ -563,3 +563,25 @@ class ApplicationStub(QObject):
"""Stub to insert as the app object in objreg."""
new_window = pyqtSignal(mainwindow.MainWindow)
class HTTPPostStub(QObject):
"""A stub class for HTTPClient.
Attributes:
url: the last url send by post()
data: the last data send by post()
"""
success = pyqtSignal(str)
error = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.url = None
self.data = None
def post(self, url, data=None):
self.url = url
self.data = data

View File

@ -18,36 +18,14 @@
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import pytest
from PyQt5.QtCore import pyqtSignal, QUrl, QObject
from PyQt5.QtCore import QUrl
from qutebrowser.misc import httpclient, pastebin
class HTTPPostStub(QObject):
"""A stub class for HTTPClient.
Attributes:
url: the last url send by post()
data: the last data send by post()
"""
success = pyqtSignal(str)
error = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.url = None
self.data = None
def post(self, url, data=None):
self.url = url
self.data = data
@pytest.fixture
def pbclient():
http_stub = HTTPPostStub()
def pbclient(stubs):
http_stub = stubs.HTTPPostStub()
client = pastebin.PastebinClient(http_stub)
return client

View File

@ -38,6 +38,7 @@ import pytest
import qutebrowser
from qutebrowser.utils import version, usertypes, utils
from qutebrowser.misc import pastebin
from qutebrowser.browser import pdfjs
@ -950,3 +951,75 @@ def test_opengl_vendor():
"""Simply call version.opengl_vendor() and see if it doesn't crash."""
pytest.importorskip("PyQt5.QtOpenGL")
return version.opengl_vendor()
@pytest.fixture
def pbclient(stubs):
http_stub = stubs.HTTPPostStub()
client = pastebin.PastebinClient(http_stub)
return client
def test_pastebin_version(pbclient, message_mock, monkeypatch, qtbot):
"""Test version.pastebin_version() sets the url."""
monkeypatch.setattr('qutebrowser.utils.version.version',
lambda: "dummy")
monkeypatch.setattr('qutebrowser.utils.utils.log_clipboard', True)
version.pastebin_version(pbclient)
pbclient.success.emit("test")
msg = message_mock.getmsg(usertypes.MessageLevel.info)
assert msg.text == "Version url test yanked to clipboard."
assert version.pastebin_url == "test"
version.pastebin_url = None
def test_pastebin_version_twice(pbclient, monkeypatch):
"""Test whether calling pastebin_version twice sends no data."""
monkeypatch.setattr('qutebrowser.utils.version.version',
lambda: "dummy")
version.pastebin_version(pbclient)
pbclient.success.emit("test")
pbclient.url = None
pbclient.data = None
version.pastebin_url = "test2"
version.pastebin_version(pbclient)
assert pbclient.url is None
assert pbclient.data is None
assert version.pastebin_url == "test2"
version.pastebin_url = None
def test_pastebin_version_error(pbclient, caplog, message_mock, monkeypatch):
"""Test version.pastebin_version() with errors."""
monkeypatch.setattr('qutebrowser.utils.version.version',
lambda: "dummy")
version.pastebin_url = None
with caplog.at_level(logging.ERROR):
version.pastebin_version(pbclient)
pbclient._client.error.emit("test")
assert version.pastebin_url is None
msg = message_mock.getmsg(usertypes.MessageLevel.error)
assert msg.text == "Failed to pastebin version info: test"
def test_uptime(monkeypatch, qapp):
"""Test _uptime runs and check if microseconds are dropped."""
launch_time = datetime.datetime(1, 1, 1, 1, 1, 1, 1)
monkeypatch.setattr(qapp, "launch_time", launch_time, raising=False)
class FakeDateTime(datetime.datetime):
now = lambda x=datetime.datetime(1, 1, 1, 1, 1, 1, 2): x
monkeypatch.setattr('datetime.datetime', FakeDateTime)
uptime_delta = version._uptime()
assert uptime_delta == datetime.timedelta(0)