Files
voice-assistant/server/tests/test_config_example.py
T
Denis Defolie 5dff1b3aba F8 : modèle de configuration versionné (config.example.yaml), server/config.yaml local et ignoré
server/config.example.yaml est le modèle (mqtt.host 10.42.0.1, en-tête qui explique le mécanisme) ;
server/config.yaml n'est plus suivi par git (git rm --cached : le fichier de l'utilisateur reste sur son
disque) et entre dans .gitignore. Plus de risque de commiter une adresse de broker locale.

Makefile : make config crée server/config.yaml depuis le modèle sans jamais l'écraser, make install le
lance (comme pour server/.env), make config-diff montre l'écart avec le modèle ; MQTT_HOST se lit dans
config.yaml ou, à défaut, dans le modèle ; server-install exige config.yaml. __main__.py : un config.yaml
absent dit « make config le crée depuis config.example.yaml » (code 78).

Les tests lisent le modèle (7 fichiers et conftest.py) : ils ne dépendent plus de la copie locale et
passent sur un clone neuf (312 tests, vérifiés sur l'arbre indexé, sans config.yaml). 7 tests nouveaux :
modèle, .gitignore, make config (création, jamais d'écrasement), config-diff, message d'un fichier absent.
server/conf/*.yaml restent versionnés. Sur une autre machine qui avait déjà ce dépôt, le premier git pull
retire le config.yaml suivi jusque-là : le sauvegarder avant (voir README).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 15:29:28 +02:00

89 lines
3.6 KiB
Python

"""Étape F8 : config.example.yaml est le modèle versionné ; server/config.yaml est la copie locale, ignorée par git."""
from __future__ import annotations
import shutil
import subprocess
from pathlib import Path
import pytest
from voice_server.__main__ import EXIT_SETUP, main
from voice_server.settings import load_settings
SERVER_DIR = Path(__file__).resolve().parents[1]
REPO = SERVER_DIR.parent
EXAMPLE = SERVER_DIR / "config.example.yaml"
def make(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(["make", "-s", *args], cwd=REPO, capture_output=True, text=True, check=False)
def test_example_loads_and_keeps_the_repository_broker() -> None:
"""Le modèle se charge seul (un clone neuf n'a pas de config.yaml) et vise le broker du dépôt."""
settings = load_settings(EXAMPLE)
assert settings.mqtt.host == "10.42.0.1"
assert settings.home.rooms and settings.access.users
@pytest.mark.skipif(shutil.which("git") is None or not (REPO / ".git").exists(), reason="pas un dépôt git")
def test_local_config_is_ignored_by_git_and_the_example_is_not() -> None:
def ignored(path: str) -> bool:
return subprocess.run(["git", "check-ignore", "--no-index", "-q", path], cwd=REPO, check=False).returncode == 0
assert ignored("server/config.yaml")
assert not ignored("server/config.example.yaml")
@pytest.mark.skipif(shutil.which("make") is None, reason="make absent")
def test_make_config_creates_the_local_file_from_the_example(tmp_path: Path) -> None:
target = tmp_path / "config.yaml"
result = make("config", f"CONFIG_FILE={target}", f"CONFIG_EXAMPLE={EXAMPLE}")
assert result.returncode == 0, result.stderr
assert target.read_text(encoding="utf-8") == EXAMPLE.read_text(encoding="utf-8")
assert "verifiez mqtt.host" in result.stdout
@pytest.mark.skipif(shutil.which("make") is None, reason="make absent")
def test_make_config_never_overwrites_an_existing_file(tmp_path: Path) -> None:
target = tmp_path / "config.yaml"
target.write_text("mqtt:\n host: 192.168.1.4\n", encoding="utf-8")
result = make("config", f"CONFIG_FILE={target}", f"CONFIG_EXAMPLE={EXAMPLE}")
assert result.returncode == 0
assert target.read_text(encoding="utf-8") == "mqtt:\n host: 192.168.1.4\n"
assert "existe deja" in result.stdout
@pytest.mark.skipif(shutil.which("make") is None, reason="make absent")
def test_make_install_creates_the_config() -> None:
"""« make install » passe par la cible config (make -n n'exécute rien)."""
result = subprocess.run(["make", "-n", "install"], cwd=REPO, capture_output=True, text=True, check=False)
assert "cp server/config.example.yaml server/config.yaml" in result.stdout
@pytest.mark.skipif(shutil.which("make") is None, reason="make absent")
def test_config_diff_shows_what_changed_from_the_example(tmp_path: Path) -> None:
local = tmp_path / "config.yaml"
local.write_text(EXAMPLE.read_text(encoding="utf-8").replace("10.42.0.1", "192.168.1.4"), encoding="utf-8")
result = make("config-diff", f"CONFIG_FILE={local}", f"CONFIG_EXAMPLE={EXAMPLE}")
assert "- host: 10.42.0.1" in result.stdout and "+ host: 192.168.1.4" in result.stdout
def test_server_says_how_to_create_a_missing_config(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
"""Un clone neuf lance le serveur sans config.yaml : le message dit quoi faire."""
shutil.copy2(EXAMPLE, tmp_path / "config.example.yaml")
shutil.copytree(SERVER_DIR / "conf", tmp_path / "conf")
assert main(["--config", str(tmp_path / "config.yaml")]) == EXIT_SETUP
assert "make config" in capsys.readouterr().err