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>
This commit is contained in:
Denis Defolie
2026-10-19 15:29:28 +02:00
parent 5362014c6b
commit 5dff1b3aba
17 changed files with 216 additions and 34 deletions
+3 -2
View File
@@ -12,12 +12,13 @@ SERVER_DIR = Path(__file__).resolve().parents[1]
@pytest.fixture
def config_copy(tmp_path: Path) -> Path:
"""Copie de config.yaml et des fichiers qu'il inclut (server/conf) : modifiable sans toucher aux vrais.
"""Copie du modèle config.example.yaml (sous le nom config.yaml) et des fichiers qu'il inclut (server/conf) :
modifiable sans toucher aux vrais, et indépendante de la copie locale de l'utilisateur.
Rend le chemin du config.yaml copié ; les fichiers inclus sont dans le dossier conf voisin.
"""
destination = tmp_path / "server"
destination.mkdir()
shutil.copy2(SERVER_DIR / "config.yaml", destination / "config.yaml")
shutil.copy2(SERVER_DIR / "config.example.yaml", destination / "config.yaml")
shutil.copytree(SERVER_DIR / "conf", destination / "conf")
return destination / "config.yaml"
+2 -2
View File
@@ -159,8 +159,8 @@ def test_other_request_drops_the_pending_one(policy: AccessPolicy) -> None:
def test_project_config_is_valid() -> None:
"""Le config.yaml du dépôt se charge, et ses règles s'appliquent vraiment (profil standard)."""
settings = load_settings(Path(__file__).resolve().parents[1] / "config.yaml")
"""Le modèle config.example.yaml se charge, et ses règles s'appliquent vraiment (profil standard)."""
settings = load_settings(Path(__file__).resolve().parents[1] / "config.example.yaml")
policy = AccessPolicy(settings.access, settings.home, settings.machines)
for action in ("on", "off"): # on/off nus en YAML = booléens : Pierre refusé à l'essai réel
assert policy.check(DeviceCommand(action, "light", "salon"), "Pierre").allowed
+88
View File
@@ -0,0 +1,88 @@
"""É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
+5 -5
View File
@@ -181,24 +181,24 @@ def test_included_file_must_be_a_mapping(tmp_path: Path) -> None:
read_config(config)
# --------------------------------------------------------------------------- le config.yaml du dépôt
# --------------------------------------------------------------------------- le modèle du dépôt
def test_project_config_takes_its_sections_from_conf() -> None:
"""Les sections découpées arrivent bien de server/conf, et « include » ne reste pas dans le résultat."""
config = read_config(SERVER_DIR / "config.yaml")
config = read_config(SERVER_DIR / "config.example.yaml")
assert "include" not in config
for section in ("audio", "mqtt", "stt", "home", "machines", "access", "fingerprint"):
assert section in config, section
raw = (SERVER_DIR / "config.yaml").read_text(encoding="utf-8")
raw = (SERVER_DIR / "config.example.yaml").read_text(encoding="utf-8")
for moved in ("home", "machines", "access", "fingerprint"):
assert f"\n{moved}:" not in raw, f"{moved} devrait vivre dans conf/"
def test_mqtt_host_stays_readable_by_the_makefile() -> None:
"""Le Makefile lit mqtt.host dans config.yaml avec awk : la section mqtt doit y rester."""
"""Le Makefile lit mqtt.host dans config.yaml (à défaut, dans le modèle) avec awk : la section mqtt doit y rester."""
inside = False
host = ""
for line in (SERVER_DIR / "config.yaml").read_text(encoding="utf-8").splitlines():
for line in (SERVER_DIR / "config.example.yaml").read_text(encoding="utf-8").splitlines():
if line.startswith("mqtt:"):
inside = True
continue
+2 -2
View File
@@ -18,7 +18,7 @@ if str(TOOLS_DIR) not in sys.path:
from finger import command_delete, command_enroll, command_list, enrollment_message # noqa: E402
CONFIG = Path(__file__).resolve().parents[1] / "config.yaml"
CONFIG = Path(__file__).resolve().parents[1] / "config.example.yaml"
BOOK = FingerprintBook(FingerprintSettings(users={1: "Denis", 2: "Denis", 5: "Jessica"}))
@@ -64,7 +64,7 @@ def test_free_id_skips_taken_and_named_slots_and_keeps_zero_free() -> None:
def test_project_config_loads() -> None:
"""Le config.yaml du dépôt se charge ; son contenu appartient à l'utilisateur (ses doigts)."""
"""Le modèle config.example.yaml se charge (la copie locale server/config.yaml, elle, appartient à l'utilisateur)."""
users = load_settings(CONFIG).fingerprint.users
assert all(isinstance(number, int) and number >= 0 and name for number, name in users.items())
+2 -2
View File
@@ -103,6 +103,6 @@ def test_assistant_prefers_home_commands_then_chat() -> None:
def test_project_config_rooms_and_boards() -> None:
"""Le config.yaml du dépôt déclare la pièce de la carte parmi les pièces connues."""
home = load_settings(Path(__file__).resolve().parents[1] / "config.yaml").home
"""Le modèle config.example.yaml déclare la pièce de la carte parmi les pièces connues."""
home = load_settings(Path(__file__).resolve().parents[1] / "config.example.yaml").home
assert home.boards["esp32-01"] in home.rooms
+1 -1
View File
@@ -26,7 +26,7 @@ from voice_server.machine_control import (
from voice_server.mqtt_link import MqttLink
from voice_server.settings import MachinesSettings, load_settings
SETTINGS = load_settings(Path(__file__).resolve().parents[1] / "config.yaml")
SETTINGS = load_settings(Path(__file__).resolve().parents[1] / "config.example.yaml")
MACHINES = SETTINGS.machines
ONLINE = AgentStatus(online=True, actions=("lock", "reboot", "shutdown"))
ONLINE_PAYLOAD = json.dumps({"status": "online", "actions": ["lock", "reboot", "shutdown"]}).encode()
+1 -1
View File
@@ -15,7 +15,7 @@ from voice_server.__main__ import setup_logging
from voice_server.mqtt_link import presence_topic, refusal_hint, split_topic
from voice_server.settings import load_settings
CONFIG = Path(__file__).resolve().parents[1] / "config.yaml"
CONFIG = Path(__file__).resolve().parents[1] / "config.example.yaml"
def test_presence_topic_is_not_taken_for_a_board() -> None:
+2 -2
View File
@@ -170,8 +170,8 @@ def test_split_topic(topic: str, expected: tuple[str, str] | None) -> None:
def test_project_config_loads() -> None:
"""Le config.yaml du dépôt se charge et reste cohérent avec le firmware."""
settings = load_settings(Path(__file__).resolve().parents[1] / "config.yaml")
"""Le modèle config.example.yaml se charge et reste cohérent avec le firmware."""
settings = load_settings(Path(__file__).resolve().parents[1] / "config.example.yaml")
assert settings.audio.bytes_per_second == 32000
assert settings.mqtt.topic_prefix == "voice"
+1 -1
View File
@@ -19,7 +19,7 @@ from voice_server.speech_to_text import SpeechToText
from voice_server.text_normalize import normalize
from voice_server.text_to_speech import TextToSpeech
SETTINGS: Settings = load_settings(Path(__file__).resolve().parents[1] / "config.yaml")
SETTINGS: Settings = load_settings(Path(__file__).resolve().parents[1] / "config.example.yaml")
pytestmark = pytest.mark.skipif(
not (SETTINGS.stt.model_dir / "model.bin").exists() or not SETTINGS.tts.model_path.exists(),