Register a qute://testdata/ scheme for unit tests

This is more lightweight than running a webserver (probably about the same as
file://), but allows us to use relative links in files.
This commit is contained in:
Florian Bruhin 2018-09-13 22:29:38 +02:00
parent 88cbc9bb12
commit 62458c7a84
2 changed files with 27 additions and 2 deletions

View File

@ -24,6 +24,7 @@
import os import os
import sys import sys
import warnings import warnings
import mimetypes
import pytest import pytest
import hypothesis import hypothesis
@ -35,6 +36,7 @@ from helpers import logfail
from helpers.logfail import fail_on_logging from helpers.logfail import fail_on_logging
from helpers.messagemock import message_mock from helpers.messagemock import message_mock
from helpers.fixtures import * # noqa: F403 from helpers.fixtures import * # noqa: F403
from qutebrowser.browser import qutescheme
from qutebrowser.utils import qtutils, standarddir, usertypes, utils, version from qutebrowser.utils import qtutils, standarddir, usertypes, utils, version
from qutebrowser.misc import objects from qutebrowser.misc import objects
from qutebrowser.qt import sip from qutebrowser.qt import sip
@ -43,6 +45,7 @@ import qutebrowser.app # To register commands
ON_CI = 'CI' in os.environ ON_CI = 'CI' in os.environ
_qute_scheme_handler = None
# Set hypothesis settings # Set hypothesis settings
@ -284,3 +287,26 @@ def pytest_runtest_makereport(item, call):
outcome = yield outcome = yield
rep = outcome.get_result() rep = outcome.get_result()
setattr(item, "rep_" + rep.when, rep) setattr(item, "rep_" + rep.when, rep)
@pytest.fixture(scope='session', autouse=True)
def register_testdata_scheme_handler(qapp, request):
try:
global _qute_scheme_handler
from qutebrowser.browser.webengine import webenginequtescheme
from PyQt5.QtWebEngineWidgets import QWebEngineProfile
_qute_scheme_handler = webenginequtescheme.QuteSchemeHandler(parent=qapp)
_qute_scheme_handler.install(QWebEngineProfile.defaultProfile())
except ImportError:
pass
@qutescheme.add_handler('testdata')
def handler(url):
file_abs = os.path.abspath(os.path.dirname(__file__))
filename = os.path.join(file_abs, 'end2end',
url.path().lstrip('/'))
with open(filename, 'rb') as f:
data = f.read()
mimetype, _encoding = mimetypes.guess_type(filename)
return mimetype, data

View File

@ -31,9 +31,8 @@ from helpers import utils
@pytest.fixture @pytest.fixture
def caret(web_tab, qtbot, mode_manager): def caret(web_tab, qtbot, mode_manager):
path = os.path.join(utils.abs_datapath(), 'caret.html')
with qtbot.wait_signal(web_tab.load_finished): with qtbot.wait_signal(web_tab.load_finished):
web_tab.openurl(QUrl.fromLocalFile(path)) web_tab.openurl(QUrl('qute://testdata/data/caret.html'))
mode_manager.enter(usertypes.KeyMode.caret) mode_manager.enter(usertypes.KeyMode.caret)