import sys from io import StringIO from pathlib import Path from l4d2host.instances import initialize_instance, _emit_step def test_initialize_writes_files(tmp_path: Path) -> None: spec = tmp_path / "spec.yaml" spec.write_text("port: 27015\noverlays: [a,b]\nconfig: ['sv_consistency 1']\n") initialize_instance("alpha", spec, root=tmp_path) assert (tmp_path / "instances/alpha/instance.env").exists() assert (tmp_path / "instances/alpha/server.cfg").exists() def test_empty_config_writes_empty_server_cfg(tmp_path: Path) -> None: spec = tmp_path / "spec.yaml" spec.write_text("port: 27015\n") initialize_instance("alpha", spec, root=tmp_path) assert (tmp_path / "instances/alpha/server.cfg").read_text() == "" def test_initialize_uses_configured_left4me_root(tmp_path: Path, monkeypatch) -> None: monkeypatch.setenv("LEFT4ME_ROOT", str(tmp_path)) spec = tmp_path / "spec.yaml" spec.write_text("port: 27015\noverlays: [standard]\n") initialize_instance("alpha", spec) env = (tmp_path / "instances/alpha/instance.env").read_text() assert f"L4D2_LOWERDIRS={tmp_path}/overlays/standard:{tmp_path}/installation" in env def test_emit_step_uses_callback() -> None: calls: list[str] = [] _emit_step("test step", on_stdout=calls.append, passthrough=False) assert calls == ["Step: test step"] def test_emit_step_uses_passthrough_stdout(monkeypatch) -> None: fake_out = StringIO() monkeypatch.setattr(sys, "stdout", fake_out) _emit_step("passthrough step", on_stdout=None, passthrough=True) assert fake_out.getvalue() == "Step: passthrough step\n" def test_emit_step_does_nothing_if_no_target() -> None: _emit_step("silent step", on_stdout=None, passthrough=False)