From 62458c7a841666e19ccbf7ae434038795eba6671 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 13 Sep 2018 22:29:38 +0200 Subject: [PATCH] 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. --- tests/conftest.py | 26 ++++++++++++++++++++++++++ tests/unit/browser/test_caret.py | 3 +-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7168cd812..0169ff3d2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,6 +24,7 @@ import os import sys import warnings +import mimetypes import pytest import hypothesis @@ -35,6 +36,7 @@ from helpers import logfail from helpers.logfail import fail_on_logging from helpers.messagemock import message_mock from helpers.fixtures import * # noqa: F403 +from qutebrowser.browser import qutescheme from qutebrowser.utils import qtutils, standarddir, usertypes, utils, version from qutebrowser.misc import objects from qutebrowser.qt import sip @@ -43,6 +45,7 @@ import qutebrowser.app # To register commands ON_CI = 'CI' in os.environ +_qute_scheme_handler = None # Set hypothesis settings @@ -284,3 +287,26 @@ def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() 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 diff --git a/tests/unit/browser/test_caret.py b/tests/unit/browser/test_caret.py index 0fd434a77..0864a9947 100644 --- a/tests/unit/browser/test_caret.py +++ b/tests/unit/browser/test_caret.py @@ -31,9 +31,8 @@ from helpers import utils @pytest.fixture def caret(web_tab, qtbot, mode_manager): - path = os.path.join(utils.abs_datapath(), 'caret.html') 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)