Add TOML registry migration tests
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from cthulhu import settings_manager
|
||||||
|
|
||||||
|
|
||||||
|
def _manager(tmp_path: Path):
|
||||||
|
manager = settings_manager.SettingsManager(mock.Mock())
|
||||||
|
manager.activate(prefsDir=str(tmp_path))
|
||||||
|
return manager
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_registry_defaults_to_default_profile(tmp_path):
|
||||||
|
_manager(tmp_path)
|
||||||
|
from cthulhu import gsettings_registry
|
||||||
|
|
||||||
|
registry = gsettings_registry.get_registry()
|
||||||
|
registry.clear_runtime_values()
|
||||||
|
|
||||||
|
assert registry.get_active_profile() == "default"
|
||||||
|
assert registry.get_active_app() is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_runtime_override_wins_over_toml(tmp_path):
|
||||||
|
_manager(tmp_path)
|
||||||
|
from cthulhu import gsettings_registry
|
||||||
|
|
||||||
|
registry = gsettings_registry.get_registry()
|
||||||
|
registry.set_runtime_value("keybindings", "keyboard-layout", "laptop")
|
||||||
|
|
||||||
|
assert registry.layered_lookup(
|
||||||
|
"keybindings",
|
||||||
|
"keyboard-layout",
|
||||||
|
"",
|
||||||
|
default="desktop",
|
||||||
|
) == "laptop"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_save_schema_writes_toml_not_dconf(tmp_path):
|
||||||
|
_manager(tmp_path)
|
||||||
|
from cthulhu import gsettings_registry
|
||||||
|
|
||||||
|
registry = gsettings_registry.get_registry()
|
||||||
|
registry.save_schema(
|
||||||
|
"keybindings",
|
||||||
|
{"keyboard-layout": "laptop", "laptop-modifier-keys": ["Caps_Lock"]},
|
||||||
|
"default",
|
||||||
|
"",
|
||||||
|
skip_defaults=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
text = Path(tmp_path, "user-settings.toml").read_text(encoding="utf-8")
|
||||||
|
assert "keyboard-layout" in text
|
||||||
|
assert "laptop-modifier-keys" in text
|
||||||
|
assert "org.gnome.Orca" not in text
|
||||||
|
assert "dconf" not in text
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from tomlkit import dumps, document
|
||||||
|
|
||||||
|
|
||||||
|
def _write_legacy_settings(path: Path) -> None:
|
||||||
|
data = document()
|
||||||
|
data["general"] = {
|
||||||
|
"keyboardLayout": 1,
|
||||||
|
"cthulhuModifierKeys": ["Insert", "KP_Insert"],
|
||||||
|
"progressBarVerbosity": 1,
|
||||||
|
"progressBarUpdateInterval": 10,
|
||||||
|
"ocrLanguageCode": "eng",
|
||||||
|
"ocrScaleFactor": 3,
|
||||||
|
"ocrCopyToClipboard": True,
|
||||||
|
}
|
||||||
|
data["profiles"] = {
|
||||||
|
"default": {
|
||||||
|
"profile": ["Default", "default"],
|
||||||
|
"keybindings": {},
|
||||||
|
"pronunciations": {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path.write_text(dumps(data), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_migrates_legacy_keys_to_schema_tables(tmp_path):
|
||||||
|
settings_path = Path(tmp_path, "user-settings.toml")
|
||||||
|
_write_legacy_settings(settings_path)
|
||||||
|
|
||||||
|
from cthulhu import gsettings_registry
|
||||||
|
|
||||||
|
registry = gsettings_registry.get_registry()
|
||||||
|
registry.migrate_all(str(tmp_path))
|
||||||
|
|
||||||
|
migrated = settings_path.read_text(encoding="utf-8")
|
||||||
|
assert "format-version = 2" in migrated
|
||||||
|
assert "[profiles.default.keybindings]" in migrated
|
||||||
|
assert 'keyboard-layout = "desktop"' in migrated
|
||||||
|
assert 'desktop-modifier-keys = ["Insert", "KP_Insert"]' in migrated
|
||||||
|
assert "[profiles.default.ocr]" in migrated
|
||||||
|
assert 'language-code = "eng"' in migrated
|
||||||
|
assert "cthulhuModifierKeys" not in migrated
|
||||||
|
assert "progressBarUpdateInterval" not in migrated
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_migration_is_idempotent(tmp_path):
|
||||||
|
settings_path = Path(tmp_path, "user-settings.toml")
|
||||||
|
_write_legacy_settings(settings_path)
|
||||||
|
|
||||||
|
from cthulhu import gsettings_registry
|
||||||
|
|
||||||
|
registry = gsettings_registry.get_registry()
|
||||||
|
registry.migrate_all(str(tmp_path))
|
||||||
|
first = settings_path.read_text(encoding="utf-8")
|
||||||
|
registry.migrate_all(str(tmp_path))
|
||||||
|
second = settings_path.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
assert second == first
|
||||||
Reference in New Issue
Block a user