From 49f8fa6d76b1508e8f01ca0dee16aebdf64c1072 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 16 Sep 2016 08:35:55 +0200 Subject: [PATCH] Add some tests for earlyinit.fix_harfbuzz See #1948 --- tests/unit/misc/test_earlyinit.py | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/unit/misc/test_earlyinit.py b/tests/unit/misc/test_earlyinit.py index 2a5ba1933..e61b06475 100644 --- a/tests/unit/misc/test_earlyinit.py +++ b/tests/unit/misc/test_earlyinit.py @@ -19,7 +19,11 @@ """Test qutebrowser.misc.earlyinit.""" +import os import sys +import types +import logging +import pkg_resources import pytest @@ -31,3 +35,76 @@ def test_init_faulthandler_stderr_none(monkeypatch, attr): """Make sure init_faulthandler works when sys.stderr/__stderr__ is None.""" monkeypatch.setattr(sys, attr, None) earlyinit.init_faulthandler() + + +class TestFixHarfbuzz: + + @pytest.fixture(autouse=True) + def clear_harfbuzz(self): + """Clear QT_HARFBUZZ before/after tests.""" + old_harfbuzz = os.environ.pop('QT_HARFBUZZ', None) + yield + if old_harfbuzz is None: + os.environ.pop('QT_HARFBUZZ', None) + else: + os.environ['QT_HARFBUZZ'] = old_harfbuzz + + @pytest.fixture + def args(self): + """Get a fake argparse namespace.""" + return types.SimpleNamespace(harfbuzz='auto') + + @pytest.mark.parametrize('harfbuzz, qt_version, platform, expected', [ + ('auto', '5.2.1', 'linux', 'old'), + ('auto', '5.3.0', 'linux', 'new'), + ('auto', '5.3.2', 'linux', 'old'), + ('auto', '5.4.0', 'linux', None), + + ('auto', '5.2.1', 'windows', None), + + ('old', '5.3.0', 'linux', 'old'), + ('old', '5.4.0', 'linux', 'old'), + + ('new', '5.2.1', 'linux', 'new'), + ('new', '5.3.2', 'linux', 'new'), + ('new', '5.4.0', 'linux', 'new'), + ]) + def test_fix_harfbuzz(self, clear_harfbuzz, args, monkeypatch, caplog, + harfbuzz, qt_version, platform, expected): + """Check the QT_HARFBUZZ env var.""" + args.harfbuzz = harfbuzz + monkeypatch.setattr(earlyinit, '_qt_version', + lambda: pkg_resources.parse_version(qt_version)) + monkeypatch.setattr(earlyinit.sys, 'platform', platform) + + with caplog.at_level(logging.WARNING): + # Because QtWidgets is already imported + earlyinit.fix_harfbuzz(args) + + assert os.environ.get('QT_HARFBUZZ', None) == expected + + @pytest.mark.parametrize('frozen, level', [ + (True, logging.DEBUG), + (False, logging.WARNING), + ]) + def test_widgets_warning(self, args, monkeypatch, caplog, frozen, level): + """Make sure fix_harfbuzz warns when QtWidgets is imported.""" + # Make sure QtWidgets is in sys.modules + from PyQt5 import QtWidgets # pylint: disable=unused-variable + if frozen: + monkeypatch.setattr(earlyinit.sys, 'frozen', True, raising=False) + else: + monkeypatch.delattr(earlyinit.sys, 'frozen', raising=False) + + with caplog.at_level(level): + earlyinit.fix_harfbuzz(args) + + record = caplog.records[0] + assert record.levelno == level + msg = "Harfbuzz fix attempted but QtWidgets is already imported!" + assert record.message == msg + + def test_no_warning(self, args, monkeypatch): + """Without QtWidgets in sys.modules, no warning should be shown.""" + monkeypatch.setattr(earlyinit.sys, 'modules', {}) + earlyinit.fix_harfbuzz(args)