feat(l4d2): add spec parser with required port and permissive fields

This commit is contained in:
mwiegand 2026-04-23 00:53:59 +02:00
parent f2ef7e2f24
commit 7d3cf66ed4
No known key found for this signature in database
2 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,22 @@
from dataclasses import dataclass, field
from pathlib import Path
import yaml
@dataclass(slots=True)
class InstanceSpec:
port: int
overlays: list[str] = field(default_factory=list)
arguments: list[str] = field(default_factory=list)
config: list[str] = field(default_factory=list)
def load_spec(path: Path) -> InstanceSpec:
raw = yaml.safe_load(path.read_text()) or {}
return InstanceSpec(
port=int(raw["port"]),
overlays=[str(item) for item in raw.get("overlays", [])],
arguments=[str(item) for item in raw.get("arguments", [])],
config=[str(item) for item in raw.get("config", [])],
)

View file

@ -0,0 +1,36 @@
from pathlib import Path
import pytest
from l4d2host.spec import load_spec
def test_minimal_spec_parses(tmp_path: Path) -> None:
path = tmp_path / "server.yaml"
path.write_text("port: 27015\noverlays: [standard]\n")
spec = load_spec(path)
assert spec.port == 27015
assert spec.overlays == ["standard"]
def test_defaults_are_empty_lists(tmp_path: Path) -> None:
path = tmp_path / "server.yaml"
path.write_text("port: 27015\n")
spec = load_spec(path)
assert spec.overlays == []
assert spec.arguments == []
assert spec.config == []
def test_missing_port_fails(tmp_path: Path) -> None:
path = tmp_path / "server.yaml"
path.write_text("overlays: [standard]\n")
with pytest.raises((KeyError, ValueError, TypeError)):
load_spec(path)
def test_unknown_keys_ignored(tmp_path: Path) -> None:
path = tmp_path / "server.yaml"
path.write_text("port: 27015\nfoo: bar\n")
spec = load_spec(path)
assert spec.port == 27015