Allow decorating multiple methods per module #47

This commit is contained in:
Kivi Kaitaniemi
2026-02-07 20:52:32 +02:00
parent 7aa063f6b9
commit 37745d1730
9 changed files with 73 additions and 32 deletions
@@ -93,8 +93,12 @@ class Systemd(plugins.Plugin):
store["systemd_units_for_module"].setdefault(mod.name, set())
store["systemd_user_units_for_module"].setdefault(mod.name, {})
units = plugins.run_method_with_attribute(mod, "__systemd__units__") or set()
user_units = plugins.run_method_with_attribute(mod, "__systemd__user__units__") or {}
units = set().union(*plugins.run_methods_with_attribute(mod, "__systemd__units__"))
user_units = {
k: v
for d in plugins.run_methods_with_attribute(mod, "__systemd__user__units__")
for k, v in d.items()
}
if store["systemd_units_for_module"][mod.name] != units:
mod._changed = True
@@ -65,13 +65,13 @@ def test_process_modules_marks_changed_and_updates_store(monkeypatch, store, sys
def fake_run_method(mod, attr):
if mod is m1 and attr == "__systemd__units__":
return {"a.service"}
return [{"a.service"}]
if mod is m1 and attr == "__systemd__user__units__":
return {"alice": {"u1.service"}}
return [{"alice": {"u1.service"}}]
# m2 has no units
return None
return []
monkeypatch.setattr(systemd_mod.plugins, "run_method_with_attribute", fake_run_method)
monkeypatch.setattr(systemd_mod.plugins, "run_methods_with_attribute", fake_run_method)
systemd.process_modules(store, {m1, m2})
@@ -96,12 +96,12 @@ def test_process_modules_no_change_second_run(monkeypatch, store, systemd):
def fake_run_method(mod, attr):
if attr == "__systemd__units__":
return {"a.service"}
return [{"a.service"}]
if attr == "__systemd__user__units__":
return {"alice": {"u1.service"}}
return None
return [{"alice": {"u1.service"}}]
return []
monkeypatch.setattr(systemd_mod.plugins, "run_method_with_attribute", fake_run_method)
monkeypatch.setattr(systemd_mod.plugins, "run_methods_with_attribute", fake_run_method)
# first run populates store
systemd.process_modules(store, {m1})
@@ -109,7 +109,7 @@ def test_process_modules_no_change_second_run(monkeypatch, store, systemd):
# new instance (fresh per-process in real usage)
systemd2 = systemd_mod.Systemd()
monkeypatch.setattr(systemd_mod.plugins, "run_method_with_attribute", fake_run_method)
monkeypatch.setattr(systemd_mod.plugins, "run_methods_with_attribute", fake_run_method)
systemd2.process_modules(store, {m1})