diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index eea527779..1f17d9836 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -432,3 +432,14 @@ def data_tmpdir(monkeypatch, tmpdir): os.mkdir(path) monkeypatch.setattr('qutebrowser.utils.standarddir.data', lambda: path) return tmpdir + + +@pytest.fixture +def redirect_xdg_data(data_tmpdir): + """Set XDG_DATA_HOME to a temp location. + + While data_tmpdir covers most cases by redirecting standarddir.data(), this + is not enough for places Qt references the data dir internally. For these, + we need to set the environment variable to redirect data access. + """ + os.putenv('XDG_DATA_HOME', str(data_tmpdir)) diff --git a/tests/unit/browser/test_tab.py b/tests/unit/browser/test_tab.py index 2d7ac8d4c..667277d9c 100644 --- a/tests/unit/browser/test_tab.py +++ b/tests/unit/browser/test_tab.py @@ -25,6 +25,8 @@ from qutebrowser.browser import browsertab from qutebrowser.keyinput import modeman from qutebrowser.utils import objreg +pytestmark = pytest.mark.usefixtures('redirect_xdg_data') + try: from PyQt5.QtWebKitWidgets import QWebView diff --git a/tests/unit/browser/webkit/test_qt_javascript.py b/tests/unit/browser/webkit/test_qt_javascript.py index 37ff5d0da..5dc70bf0d 100644 --- a/tests/unit/browser/webkit/test_qt_javascript.py +++ b/tests/unit/browser/webkit/test_qt_javascript.py @@ -21,7 +21,6 @@ import pytest -import os from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtWebKit import QWebSettings @@ -65,18 +64,14 @@ def test_element_js_webkit(webview, js_enabled, expected): assert result == expected +@pytest.mark.usefixtures('redirect_xdg_data') @pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)]) -def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected, - data_tmpdir): +def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected): """With QtWebEngine, runJavaScript works even when JS is off.""" # pylint: disable=no-name-in-module,useless-suppression # If we get there (because of the webengineview fixture) we can be certain # QtWebEngine is available from PyQt5.QtWebEngineWidgets import QWebEngineSettings - # runJavaScript will write to the data directory. As the path is determined - # by Qt and bypasses standarddir.data(), the data_tmpdir fixture is not - # enough -- we need to set XDG_DATA_HOME as well - os.putenv('XDG_DATA_HOME', str(data_tmpdir)) webengineview.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, js_enabled)