test(settings): add unit tests for KiauhSettings

This commit is contained in:
dw-0
2026-07-11 17:49:51 +02:00
parent 477d737489
commit a3f9ee1faf
2 changed files with 95 additions and 0 deletions
@@ -0,0 +1,95 @@
from __future__ import annotations
from pathlib import Path
import pytest
from core.settings.kiauh_settings import KiauhSettings
DEFAULT_CFG_CONTENT = """\
[kiauh]
backup_before_update: False
[klipper]
repositories:
https://github.com/Klipper3d/klipper
[moonraker]
optional_speedups: True
repositories:
https://github.com/Arksine/moonraker
[mainsail]
port: 80
unstable_releases: False
[fluidd]
port: 80
unstable_releases: False
"""
@pytest.fixture
def reset_settings(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(KiauhSettings, "_KiauhSettings__instance", None)
monkeypatch.setattr(KiauhSettings, "_KiauhSettings__initialized", False)
@pytest.fixture
def cfg_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, reset_settings):
from core.settings import kiauh_settings as ks
default = tmp_path / "default.kiauh.cfg"
default.write_text(DEFAULT_CFG_CONTENT)
custom = tmp_path / "kiauh.cfg"
monkeypatch.setattr(ks, "DEFAULT_CFG", default)
monkeypatch.setattr(ks, "CUSTOM_CFG", custom)
return default, custom
class TestKiauhSettings:
def test_loads_default_when_custom_missing(self, cfg_paths) -> None:
settings = KiauhSettings()
assert settings.kiauh.backup_before_update is False
assert settings.mainsail.port == 80
assert settings.klipper.use_python_binary is None
def test_loads_custom_overrides(self, cfg_paths) -> None:
_, custom = cfg_paths
custom.write_text(
"[kiauh]\nbackup_before_update: True\n[mainsail]\nport: 8080\n"
)
settings = KiauhSettings()
assert settings.kiauh.backup_before_update is True
assert settings.mainsail.port == 8080
def test_save_writes_custom_config(self, cfg_paths) -> None:
_, custom = cfg_paths
settings = KiauhSettings()
settings.kiauh.backup_before_update = True
settings.save()
text = custom.read_text()
assert "backup_before_update: True" in text
def test_get_returns_value(self, cfg_paths) -> None:
settings = KiauhSettings()
assert settings.get("mainsail", "port") == 80
def test_missing_config_calls_kill(self, cfg_paths, monkeypatch) -> None:
from core.settings import kiauh_settings as ks
calls = []
def fake_kill(msg: str = "") -> None:
calls.append(msg)
raise SystemExit(1)
monkeypatch.setattr(ks, "DEFAULT_CFG", Path("/no/such/default.cfg"))
monkeypatch.setattr(ks, "CUSTOM_CFG", Path("/no/such/custom.cfg"))
monkeypatch.setattr(ks, "kill", fake_kill)
with pytest.raises(SystemExit):
KiauhSettings()
assert len(calls) == 1