diff --git a/components/l4d2-host-lib/src/l4d2host/steam_install.py b/components/l4d2-host-lib/src/l4d2host/steam_install.py new file mode 100644 index 0000000..5282dbc --- /dev/null +++ b/components/l4d2-host-lib/src/l4d2host/steam_install.py @@ -0,0 +1,37 @@ +from pathlib import Path +from typing import Callable + +from l4d2host.process import run_command + + +class SteamInstaller: + def __init__(self, install_dir: Path = Path("/opt/l4d2/installation"), steamcmd: str = "steamcmd"): + self.install_dir = install_dir + self.steamcmd = steamcmd + + def install_or_update( + self, + *, + on_stdout: Callable[[str], None] | None = None, + on_stderr: Callable[[str], None] | None = None, + passthrough: bool = False, + ) -> None: + for platform in ("windows", "linux"): + run_command( + [ + self.steamcmd, + "+force_install_dir", + str(self.install_dir), + "+login", + "anonymous", + "+@sSteamCmdForcePlatformType", + platform, + "+app_update", + "222860", + "validate", + "+quit", + ], + on_stdout=on_stdout, + on_stderr=on_stderr, + passthrough=passthrough, + ) diff --git a/components/l4d2-host-lib/tests/test_install.py b/components/l4d2-host-lib/tests/test_install.py new file mode 100644 index 0000000..5ef73c8 --- /dev/null +++ b/components/l4d2-host-lib/tests/test_install.py @@ -0,0 +1,35 @@ +import subprocess + +import pytest + +from l4d2host.steam_install import SteamInstaller + + +def test_windows_then_linux(monkeypatch: pytest.MonkeyPatch) -> None: + calls: list[list[str]] = [] + + def fake_run_command(cmd, **kwargs): + del kwargs + calls.append(list(cmd)) + + monkeypatch.setattr("l4d2host.steam_install.run_command", fake_run_command) + SteamInstaller().install_or_update() + assert "windows" in calls[0] + assert "linux" in calls[1] + + +def test_fail_fast_on_first_failure(monkeypatch: pytest.MonkeyPatch) -> None: + calls: list[list[str]] = [] + + def fake_run_command(cmd, **kwargs): + del kwargs + calls.append(list(cmd)) + if len(calls) == 1: + raise subprocess.CalledProcessError(returncode=1, cmd=cmd) + + monkeypatch.setattr("l4d2host.steam_install.run_command", fake_run_command) + + with pytest.raises(subprocess.CalledProcessError): + SteamInstaller().install_or_update() + + assert len(calls) == 1