From addf2ab0e3d449955c85070a52b82f3895534d93 Mon Sep 17 00:00:00 2001 From: Eduard Ralph Date: Sat, 13 Jun 2026 16:38:40 +0200 Subject: [PATCH] Pin GTK and GDK to 3.0 for the repo-root test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tests/ suite (run by `python3 -m unittest discover -s tests`) imports gramps.gui.* modules directly, which bypasses the Gramps GUI launcher's own gi.require_version. On a host where GTK 4 is the default GI resolution, Gtk/Gdk then load unpinned — emitting PyGIWarning and risking the wrong stack (and the GUI-touching tests silently skipping). Pin both in tests/__init__.py, which `unittest discover -s tests` imports before any test module, so the whole repo-root suite runs on the GTK 3 / GDK 3 stack a real Gramps session uses. tests/test_gtk_version_pin.py guards it (skips where the 3.0 typelibs are absent). Verified: `python3 -m unittest discover -s tests -p "test_*.py" -t .` pins both (get_required_version == "3.0"); the guard is red without the pin. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/__init__.py | 19 +++++++++++++++++++ tests/test_gtk_version_pin.py | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/test_gtk_version_pin.py diff --git a/tests/__init__.py b/tests/__init__.py index e69de29bb..ff68cee43 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,19 @@ +"""Pin the GTK / GDK introspection versions for the repo-root test suite. + +``python3 -m unittest discover -s tests`` imports this package before any test +module under ``tests/``, so requiring the versions here pins the whole suite to +the GTK 3 / GDK 3 stack a real Gramps GUI session uses. A test that imports a +``gramps.gui.*`` module directly never runs the launcher's own +``require_version``; without this the GI stack can resolve to GTK 4 on a host +where that is the default — emitting ``PyGIWarning`` and risking the wrong stack. +""" + +try: + import gi + + gi.require_version("Gdk", "3.0") + gi.require_version("Gtk", "3.0") +except Exception: + # No PyGObject, or the 3.0 typelibs are unavailable — leave the environment + # untouched; this only fixes the version when it can. + pass diff --git a/tests/test_gtk_version_pin.py b/tests/test_gtk_version_pin.py new file mode 100644 index 000000000..3d9c32633 --- /dev/null +++ b/tests/test_gtk_version_pin.py @@ -0,0 +1,18 @@ +"""Guard: the repo-root test suite pins GTK/GDK to 3 (via tests/__init__.py).""" + +import unittest + + +class GtkGdkVersionPin(unittest.TestCase): + def test_suite_pins_gtk_and_gdk_to_3(self): + try: + import gi + except ImportError: + self.skipTest("PyGObject not available") + repo = gi.Repository.get_default() + if "3.0" not in repo.enumerate_versions("Gtk") or \ + "3.0" not in repo.enumerate_versions("Gdk"): + self.skipTest("GTK/GDK 3.0 introspection typelibs not installed") + # Importing the tests package ran tests/__init__.py, which must pin both. + self.assertEqual(gi.get_required_version("Gtk"), "3.0") + self.assertEqual(gi.get_required_version("Gdk"), "3.0")