diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index 09a1a6efa..71e33886f 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -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) diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 64bc793cb..27a23650d 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -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 diff --git a/tests/unit/misc/test_pastebin.py b/tests/unit/misc/test_pastebin.py index b352f52c8..1d684dc4e 100644 --- a/tests/unit/misc/test_pastebin.py +++ b/tests/unit/misc/test_pastebin.py @@ -18,36 +18,14 @@ # along with qutebrowser. If not, see . 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 diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index da65422a7..eff67dcd8 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -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)