Debian Codegraph @

tests/integration_tests/gsettings_fixtures.py

raw
1 # Orca
2 #
3 # Copyright 2026 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 """GSettings fixtures for integration tests."""
22
23 from __future__ import annotations
24
25 import os
26 import shutil
27 import subprocess
28 import sys
29 import tempfile
30 import types
31 from typing import TYPE_CHECKING
32
33 import pytest
34
35 if TYPE_CHECKING:
36 from collections.abc import Callable, Generator
37
38
39 def _inject_generated_module_stubs() -> None:
40 """Injects minimal stubs for meson-generated modules into sys.modules."""
41
42 i18n = types.ModuleType("orca.orca_i18n")
43 i18n._ = lambda x: x # type: ignore[attr-defined]
44 i18n.C_ = lambda c, x: x # type: ignore[attr-defined]
45 i18n.ngettext = lambda s, p, n: s if n == 1 else p # type: ignore[attr-defined]
46 sys.modules["orca.orca_i18n"] = i18n
47
48 platform = types.ModuleType("orca.orca_platform")
49 platform.version = "0.0.0-test" # type: ignore[attr-defined]
50 platform.revision = "" # type: ignore[attr-defined]
51 platform.prefix = "/usr" # type: ignore[attr-defined]
52 platform.package = "orca" # type: ignore[attr-defined]
53 platform.datadir = "/usr/share" # type: ignore[attr-defined]
54 platform.tablesdir = "" # type: ignore[attr-defined]
55 sys.modules["orca.orca_platform"] = platform
56
57
58 def _build_schemas(source_root: str, output_dir: str) -> None:
59 """Generates and compiles GSettings schemas into output_dir."""
60
61 generator = os.path.join(source_root, "tools", "generate_gsettings_schemas.py")
62 src_dir = os.path.join(source_root, "src")
63 schema_xml = os.path.join(output_dir, "org.gnome.Orca.gschema.xml")
64
65 subprocess.check_call([sys.executable, generator, src_dir, schema_xml])
66 subprocess.check_call(["glib-compile-schemas", output_dir])
67
68
69 @pytest.fixture(scope="session", name="gsettings_registry")
70 def _gsettings_registry() -> Generator:
71 """Session-scoped fixture providing a GSettingsRegistry backed by in-memory storage."""
72
73 source_root = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
74 schema_dir = tempfile.mkdtemp(prefix="orca-test-schemas-")
75
76 try:
77 _build_schemas(source_root, schema_dir)
78 except (subprocess.CalledProcessError, FileNotFoundError) as error:
79 shutil.rmtree(schema_dir, ignore_errors=True)
80 pytest.skip(f"Schema generation failed: {error!s}")
81
82 os.environ["GSETTINGS_BACKEND"] = "memory"
83 os.environ["GSETTINGS_SCHEMA_DIR"] = schema_dir
84
85 _inject_generated_module_stubs()
86
87 from orca import (
88 speech_presenter, # noqa: F401 # pylint: disable=unused-import,import-outside-toplevel
89 )
90 from orca.gsettings_registry import get_registry # pylint: disable=import-outside-toplevel
91
92 yield get_registry()
93
94 shutil.rmtree(schema_dir, ignore_errors=True)
95 os.environ.pop("GSETTINGS_SCHEMA_DIR", None)
96
97
98 @pytest.fixture(name="gsettings_handle")
99 def _gsettings_handle(
100 gsettings_registry,
101 ) -> Callable:
102 """Factory fixture returning a GSettingsSchemaHandle that resets user keys on teardown."""
103
104 handles_used: list = []
105
106 def _get_handle(schema_name: str):
107 handle = gsettings_registry._get_handle(schema_name)
108 if handle is None:
109 pytest.fail(f"No handle found for schema {schema_name!r}")
110 handles_used.append(handle)
111 return handle
112
113 yield _get_handle
114
115 for handle in handles_used:
116 for gs in handle._cache.values():
117 for key in gs.props.settings_schema.list_keys():
118 gs.reset(key)
119
120
121 @pytest.fixture(name="gsettings_profile")
122 def _gsettings_profile(gsettings_registry) -> Generator:
123 """Ensures each test starts with the default profile and no active app."""
124
125 gsettings_registry.set_active_profile("default")
126 gsettings_registry.set_active_app(None)
127 yield gsettings_registry
128 gsettings_registry.set_active_profile("default")
129 gsettings_registry.set_active_app(None)