From 73fb5c4c49a9f79b1820486dc11e819f859d593a Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 23 Jun 2023 23:38:54 +0200 Subject: qt: Switch to autoselection of Qt backend by default --- qutebrowser/qt/machinery.py | 6 +- tests/unit/test_qt_machinery.py | 191 ++++++++++++++++++++-------------------- 2 files changed, 99 insertions(+), 98 deletions(-) diff --git a/qutebrowser/qt/machinery.py b/qutebrowser/qt/machinery.py index bd5062120..a5705a933 100644 --- a/qutebrowser/qt/machinery.py +++ b/qutebrowser/qt/machinery.py @@ -150,7 +150,7 @@ def _select_wrapper(args: Optional[argparse.Namespace]) -> SelectionInfo: - If --qt-wrapper is given, use that. - Otherwise, if the QUTE_QT_WRAPPER environment variable is set, use that. - - Otherwise, use PyQt5 (FIXME:qt6 autoselect). + - Otherwise, try the wrappers in WRAPPER in order (PyQt6 -> PyQt5) """ # If any Qt wrapper has been imported before this, something strange might # be happening. @@ -174,11 +174,9 @@ def _select_wrapper(args: Optional[argparse.Namespace]) -> SelectionInfo: ) return SelectionInfo(wrapper=env_wrapper, reason=SelectionReason.env) - # FIXME:qt6 Go back to the auto-detection once ready # FIXME:qt6 Make sure to still consider _DEFAULT_WRAPPER for packagers # (rename to _WRAPPER_OVERRIDE since our sed command is broken anyways then?) - # return _autoselect_wrapper() - return SelectionInfo(wrapper=_DEFAULT_WRAPPER, reason=SelectionReason.default) + return _autoselect_wrapper() # Values are set in init(). If you see a NameError here, it means something tried to diff --git a/tests/unit/test_qt_machinery.py b/tests/unit/test_qt_machinery.py index b4d302ae5..40631accc 100644 --- a/tests/unit/test_qt_machinery.py +++ b/tests/unit/test_qt_machinery.py @@ -230,111 +230,114 @@ def test_autoselect( assert machinery._autoselect_wrapper() == expected -@pytest.mark.parametrize( - "args, env, expected", - [ - # Defaults with no overrides - ( - None, - None, - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.default - ), - ), - ( - argparse.Namespace(qt_wrapper=None), - None, - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.default - ), - ), - ( - argparse.Namespace(qt_wrapper=None), - "", - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.default - ), - ), - # Only argument given - ( - argparse.Namespace(qt_wrapper="PyQt6"), - None, - machinery.SelectionInfo( - wrapper="PyQt6", reason=machinery.SelectionReason.cli +class TestSelectWrapper: + @pytest.mark.parametrize( + "args, env, expected", + [ + # Only argument given + ( + argparse.Namespace(qt_wrapper="PyQt6"), + None, + machinery.SelectionInfo( + wrapper="PyQt6", reason=machinery.SelectionReason.cli + ), ), - ), - ( - argparse.Namespace(qt_wrapper="PyQt5"), - None, - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.cli + ( + argparse.Namespace(qt_wrapper="PyQt5"), + None, + machinery.SelectionInfo( + wrapper="PyQt5", reason=machinery.SelectionReason.cli + ), ), - ), - ( - argparse.Namespace(qt_wrapper="PyQt5"), - "", - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.cli + ( + argparse.Namespace(qt_wrapper="PyQt5"), + "", + machinery.SelectionInfo( + wrapper="PyQt5", reason=machinery.SelectionReason.cli + ), ), - ), - # Only environment variable given - ( - None, - "PyQt6", - machinery.SelectionInfo( - wrapper="PyQt6", reason=machinery.SelectionReason.env + # Only environment variable given + ( + None, + "PyQt6", + machinery.SelectionInfo( + wrapper="PyQt6", reason=machinery.SelectionReason.env + ), ), - ), - ( - None, - "PyQt5", - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.env + ( + None, + "PyQt5", + machinery.SelectionInfo( + wrapper="PyQt5", reason=machinery.SelectionReason.env + ), ), - ), - # Both given - ( - argparse.Namespace(qt_wrapper="PyQt5"), - "PyQt6", - machinery.SelectionInfo( - wrapper="PyQt5", reason=machinery.SelectionReason.cli + # Both given + ( + argparse.Namespace(qt_wrapper="PyQt5"), + "PyQt6", + machinery.SelectionInfo( + wrapper="PyQt5", reason=machinery.SelectionReason.cli + ), ), - ), - ( - argparse.Namespace(qt_wrapper="PyQt6"), - "PyQt5", - machinery.SelectionInfo( - wrapper="PyQt6", reason=machinery.SelectionReason.cli + ( + argparse.Namespace(qt_wrapper="PyQt6"), + "PyQt5", + machinery.SelectionInfo( + wrapper="PyQt6", reason=machinery.SelectionReason.cli + ), ), - ), - ( - argparse.Namespace(qt_wrapper="PyQt6"), - "PyQt6", - machinery.SelectionInfo( - wrapper="PyQt6", reason=machinery.SelectionReason.cli + ( + argparse.Namespace(qt_wrapper="PyQt6"), + "PyQt6", + machinery.SelectionInfo( + wrapper="PyQt6", reason=machinery.SelectionReason.cli + ), ), - ), - ], -) -def test_select_wrapper( - args: Optional[argparse.Namespace], - env: Optional[str], - expected: machinery.SelectionInfo, - monkeypatch: pytest.MonkeyPatch, - undo_init: None, -): - if env is None: - monkeypatch.delenv("QUTE_QT_WRAPPER", raising=False) - else: - monkeypatch.setenv("QUTE_QT_WRAPPER", env) + ], + ) + def test_select( + self, + args: Optional[argparse.Namespace], + env: Optional[str], + expected: machinery.SelectionInfo, + monkeypatch: pytest.MonkeyPatch, + ): + if env is None: + monkeypatch.delenv("QUTE_QT_WRAPPER", raising=False) + else: + monkeypatch.setenv("QUTE_QT_WRAPPER", env) - assert machinery._select_wrapper(args) == expected + assert machinery._select_wrapper(args) == expected + + @pytest.mark.parametrize( + "args, env", + [ + (None, None), + (argparse.Namespace(qt_wrapper=None), None), + (argparse.Namespace(qt_wrapper=None), ""), + ], + ) + def test_autoselect_by_default( + self, + args: Optional[argparse.Namespace], + env: Optional[str], + monkeypatch: pytest.MonkeyPatch, + ): + """Test that the default behavior is to autoselect a wrapper. + + Autoselection itself is tested further down. + """ + if env is None: + monkeypatch.delenv("QUTE_QT_WRAPPER", raising=False) + else: + monkeypatch.setenv("QUTE_QT_WRAPPER", env) + assert machinery._select_wrapper(args).reason == machinery.SelectionReason.auto -def test_select_wrapper_after_qt_import(monkeypatch: pytest.MonkeyPatch): - monkeypatch.setitem(sys.modules, "PyQt6", None) - with pytest.warns(UserWarning, match="PyQt6 already imported"): - machinery._select_wrapper(args=None) + def test_after_qt_import(self, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setitem(sys.modules, "PyQt6", None) + with pytest.warns(UserWarning, match="PyQt6 already imported"): + machinery._select_wrapper(args=None) class TestInit: -- cgit v1.2.3-54-g00ecf