Skip to content

Commit 5cea67e

Browse files
committed
dbus testing fixed
1 parent 90fcc41 commit 5cea67e

1 file changed

Lines changed: 35 additions & 36 deletions

File tree

tests/test_plugin_dbus.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,48 +43,47 @@
4343

4444

4545
@pytest.fixture
46-
def enabled_dbus_environment(mocker):
47-
"""Fully mocked D-Bus and GI environment with plugin reloaded and
48-
enabled."""
49-
50-
# Step 1: Mock DBus Interface
51-
interface_mock = mocker.patch("dbus.Interface", spec=True, Notify=Mock())
52-
mocker.patch(
53-
"dbus.SessionBus",
54-
spec=True,
55-
**{"get_object.return_value": interface_mock},
56-
)
57-
58-
# Step 2: Inject valid dbus.mainloop.glib and .qt into sys.modules
59-
fake_loop = Mock(name="FakeMainLoop")
60-
sys.modules["dbus.mainloop.glib"] = types.SimpleNamespace(
61-
DBusGMainLoop=lambda: fake_loop
62-
)
63-
sys.modules["dbus.mainloop.qt"] = types.SimpleNamespace(
64-
DBusQtMainLoop=lambda set_as_default=False: fake_loop
65-
)
46+
def enabled_dbus_environment(monkeypatch):
47+
"""
48+
Fully mocked DBus and GI environment that works in local and CI environments.
49+
"""
6650

67-
# Step 3: Mock GI and GdkPixbuf (also covers image handling)
51+
# --- Handle dbus (real or fake) ---
52+
try:
53+
import dbus
54+
except ImportError:
55+
dbus = types.ModuleType("dbus")
56+
dbus.DBusException = type("DBusException", (Exception,), {})
57+
dbus.Interface = Mock()
58+
dbus.SessionBus = Mock()
59+
60+
sys.modules["dbus"] = dbus
61+
62+
# Inject mainloop support if not already present
63+
if "dbus.mainloop.glib" not in sys.modules:
64+
glib_loop = types.ModuleType("dbus.mainloop.glib")
65+
glib_loop.DBusGMainLoop = lambda: Mock(name="FakeLoop")
66+
sys.modules["dbus.mainloop.glib"] = glib_loop
67+
68+
if "dbus.mainloop" not in sys.modules:
69+
sys.modules["dbus.mainloop"] = types.ModuleType("dbus.mainloop")
70+
71+
# Patch specific attributes always, even if real module is present
72+
monkeypatch.setattr("dbus.Interface", Mock())
73+
monkeypatch.setattr("dbus.SessionBus", Mock())
74+
monkeypatch.setattr("dbus.DBusException", type("DBusException", (Exception,), {}))
75+
76+
# --- Mock GI / GdkPixbuf ---
6877
gi = types.ModuleType("gi")
69-
gi.repository = types.ModuleType("gi.repository")
70-
71-
mock_pixbuf = Mock()
72-
mock_image = Mock()
73-
mock_pixbuf.new_from_file.return_value = mock_image
74-
mock_image.get_width.return_value = 100
75-
mock_image.get_height.return_value = 100
76-
mock_image.get_rowstride.return_value = 1
77-
mock_image.get_has_alpha.return_value = 0
78-
mock_image.get_bits_per_sample.return_value = 8
79-
mock_image.get_n_channels.return_value = 1
80-
mock_image.get_pixels.return_value = b""
81-
82-
gi.repository.GdkPixbuf = types.SimpleNamespace(Pixbuf=mock_pixbuf)
8378
gi.require_version = Mock()
79+
gi.repository = types.SimpleNamespace(
80+
GdkPixbuf=types.SimpleNamespace(Pixbuf=Mock())
81+
)
82+
8483
sys.modules["gi"] = gi
8584
sys.modules["gi.repository"] = gi.repository
8685

87-
# Step 4: Reload plugin after all mocks are in place
86+
# --- Reload plugin with controlled env ---
8887
reload_plugin("dbus")
8988

9089

0 commit comments

Comments
 (0)