From a76e726e36e0289736445c625b2c56bd8c7f8a8d Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 22 Nov 2026 20:25:39 +0100 Subject: [PATCH] Merge steam-game-installer into the repo The installer ships as the steam_installer package beside steam_backlog_enforcer, with its tests, docs and Proton lock. The enforcer's fast install now starts the installer from this checkout. Both appinfo.vdf readers share one key-value decoder (steam_installer.appinfo_kv), and the duplicated test fixtures and helpers are folded into conftest and _cli_helpers so the whole-tree duplicate-code check is clean. check_requirements_sync treats sibling packages as first-party. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w --- .github/workflows/python-tests.yml | 3 +- .pre-commit-config.yaml | 32 ++- CLAUDE.md | 1 + .../README.md => DOCS-steam-installer.md | 44 ++-- README.md | 11 + .../.github/workflows/file-length.yml | 35 --- .../.github/workflows/repo-contract.yml | 25 -- _installer_import/.gitignore | 8 - _installer_import/.pre-commit-config.yaml | 84 ------- _installer_import/CLAUDE.md | 9 - _installer_import/pyproject.toml | 34 --- .../scripts/check_dependency_freshness.sh | 89 ------- .../scripts/check_file_length.sh | 31 --- _installer_import/scripts/check_md_naming.sh | 32 --- .../scripts/check_repo_contract.sh | 26 -- _installer_import/scripts/deploy.sh | 38 --- _installer_import/scripts/test_changed.sh | 50 ---- _installer_import/scripts/todo_hooks.sh | 12 - _installer_import/steam_installer/__init__.py | 0 _installer_import/tests/_cli_helpers.py | 9 - _installer_import/tests/test_cli.py | 97 -------- _installer_import/tests/test_cli_finish.py | 228 ------------------ _installer_import/tests/test_cli_steam.py | 205 ---------------- _installer_import/tests/test_deploy.py | 94 -------- _installer_import/tests/test_depotdl.py | 80 ------ _installer_import/tests/test_dlc.py | 199 --------------- _installer_import/tests/test_install_sh.py | 45 ---- _installer_import/tests/test_map_installed.py | 197 --------------- _installer_import/tests/test_proton_native.py | 211 ---------------- .../tests/test_proton_native_cli.py | 81 ------- install.sh | 15 ++ pyproject.toml | 31 ++- requirements.txt | 3 +- scripts/check_dependency_freshness.sh | 67 ++++- scripts/check_requirements_sync.py | 13 +- steam_backlog_enforcer/_appinfo_cache.py | 51 +--- steam_backlog_enforcer/_fast_install.py | 4 +- steam_backlog_enforcer/_fast_install_cmd.py | 41 ++-- .../_steam_restart_guard.py | 9 +- .../tests/_no_fast_install.py | 5 +- .../tests/_no_real_effects.py | 2 +- .../tests/test_fast_install_guards.py | 7 +- .../tests/test_fast_install_helpers.py | 53 +--- .../tests/test_fast_install_launcher.py | 87 +++++++ .../tests/test_no_real_effects.py | 1 + steam_installer/__init__.py | 1 + steam_installer/_http.py | 26 ++ .../anticheat.py | 16 +- .../appinfo.py | 51 ++-- .../appinfo_cache.py | 80 +++--- steam_installer/appinfo_kv.py | 68 ++++++ .../cli.py | 181 +++++++------- steam_installer/cli_args.py | 70 ++++++ .../compat_apply.py | 40 ++- .../compat_decide.py | 38 +-- .../compat_map.py | 71 +++--- .../config.py | 16 +- .../data/cachyos_signing_key.asc | 0 .../data/compat_overrides.json | 0 .../data/proton-native.lock | 0 .../depotdl.py | 72 +++--- .../deps.py | 18 +- .../dlc.py | 20 +- steam_installer/errors.py | 45 ++++ .../finalize.py | 33 +-- .../finish_check.py | 12 +- .../flow.py | 128 +++++----- .../ghost.py | 91 ++++--- .../install_deps.sh | 74 +++--- steam_installer/job.py | 24 ++ .../library.py | 13 +- steam_installer/manifest_depots.py | 58 +++++ .../manifest_write.py | 88 +++---- .../map_installed.py | 41 ++-- .../proton_native.py | 39 +-- .../proton_pkg.py | 63 +++-- .../recognize.py | 37 +-- .../resolve.py | 39 +-- {_installer_import => steam_installer}/run.sh | 46 ++-- .../runlog.py | 48 ++-- .../steamctl.py | 38 ++- .../takeover.py | 15 +- .../target.py | 35 +-- steam_installer/tests/__init__.py | 1 + steam_installer/tests/_acf_helpers.py | 79 ++++++ .../tests/_appinfo_blob.py | 5 +- steam_installer/tests/_cli_helpers.py | 43 ++++ steam_installer/tests/_map_data.py | 31 +++ .../tests/_proton_fixture.py | 56 ++++- .../tests/_recognize_helpers.py | 9 +- steam_installer/tests/_run_helpers.py | 19 ++ .../tests/conftest.py | 169 +++++++------ .../tests/test_appinfo.py | 154 ++++++------ .../tests/test_appinfo_cache.py | 72 ++++-- .../tests/test_appinfo_cache_backed.py | 96 ++++++++ .../tests/test_appinfo_public_manifests.py | 20 +- steam_installer/tests/test_cli.py | 168 +++++++++++++ .../tests/test_cli_credit.py | 8 +- steam_installer/tests/test_cli_finish.py | 173 +++++++++++++ steam_installer/tests/test_cli_finish_stub.py | 106 ++++++++ .../tests/test_cli_modes.py | 48 +++- steam_installer/tests/test_cli_steam.py | 178 ++++++++++++++ .../tests/test_cli_steam_client.py | 105 ++++++++ .../tests/test_compat_decide.py | 117 ++++++--- .../tests/test_compat_map.py | 94 +++++--- .../tests/test_config.py | 54 +++-- steam_installer/tests/test_depotdl.py | 88 +++++++ .../tests/test_deps.py | 17 +- steam_installer/tests/test_dlc.py | 190 +++++++++++++++ steam_installer/tests/test_dlc_finish.py | 88 +++++++ .../tests/test_finish_check.py | 93 ++++--- .../tests/test_freshness_wrapper.py | 49 ++-- .../tests/test_ghost.py | 64 +++-- .../tests/test_ghost_repair.py | 25 +- steam_installer/tests/test_http.py | 53 ++++ steam_installer/tests/test_install_sh.py | 75 ++++++ .../tests/test_library.py | 49 +++- .../tests/test_manifest_finish.py | 41 +++- .../tests/test_manifest_write.py | 54 +++-- .../tests/test_map_after_install.py | 114 +++++++++ steam_installer/tests/test_map_installed.py | 189 +++++++++++++++ steam_installer/tests/test_proton_native.py | 142 +++++++++++ .../tests/test_proton_native_cli.py | 134 ++++++++++ .../tests/test_proton_native_copy.py | 100 ++++++++ .../tests/test_proton_native_in_use.py | 35 +++ .../tests/test_proton_pkg.py | 62 +++-- .../tests/test_recognize.py | 66 +++-- .../tests/test_resolve.py | 134 +++++----- steam_installer/tests/test_resolve_prompt.py | 60 +++++ .../tests/test_runlog.py | 89 +++---- .../tests/test_steamctl.py | 28 ++- .../tests/test_takeover.py | 51 ++-- .../tests/test_target.py | 108 +++++++-- .../tests/test_vdf.py | 46 ++-- .../vdf.py | 36 ++- 135 files changed, 4847 insertions(+), 3372 deletions(-) rename _installer_import/README.md => DOCS-steam-installer.md (87%) delete mode 100644 _installer_import/.github/workflows/file-length.yml delete mode 100644 _installer_import/.github/workflows/repo-contract.yml delete mode 100644 _installer_import/.gitignore delete mode 100644 _installer_import/.pre-commit-config.yaml delete mode 100644 _installer_import/CLAUDE.md delete mode 100644 _installer_import/pyproject.toml delete mode 100755 _installer_import/scripts/check_dependency_freshness.sh delete mode 100755 _installer_import/scripts/check_file_length.sh delete mode 100755 _installer_import/scripts/check_md_naming.sh delete mode 100755 _installer_import/scripts/check_repo_contract.sh delete mode 100755 _installer_import/scripts/deploy.sh delete mode 100755 _installer_import/scripts/test_changed.sh delete mode 100755 _installer_import/scripts/todo_hooks.sh delete mode 100644 _installer_import/steam_installer/__init__.py delete mode 100644 _installer_import/tests/_cli_helpers.py delete mode 100644 _installer_import/tests/test_cli.py delete mode 100644 _installer_import/tests/test_cli_finish.py delete mode 100644 _installer_import/tests/test_cli_steam.py delete mode 100644 _installer_import/tests/test_deploy.py delete mode 100644 _installer_import/tests/test_depotdl.py delete mode 100644 _installer_import/tests/test_dlc.py delete mode 100644 _installer_import/tests/test_install_sh.py delete mode 100644 _installer_import/tests/test_map_installed.py delete mode 100644 _installer_import/tests/test_proton_native.py delete mode 100644 _installer_import/tests/test_proton_native_cli.py create mode 100644 steam_backlog_enforcer/tests/test_fast_install_launcher.py create mode 100644 steam_installer/__init__.py create mode 100644 steam_installer/_http.py rename {_installer_import/steam_installer => steam_installer}/anticheat.py (81%) rename {_installer_import/steam_installer => steam_installer}/appinfo.py (84%) rename {_installer_import/steam_installer => steam_installer}/appinfo_cache.py (52%) create mode 100644 steam_installer/appinfo_kv.py rename {_installer_import/steam_installer => steam_installer}/cli.py (57%) create mode 100644 steam_installer/cli_args.py rename {_installer_import/steam_installer => steam_installer}/compat_apply.py (68%) rename {_installer_import/steam_installer => steam_installer}/compat_decide.py (80%) rename {_installer_import/steam_installer => steam_installer}/compat_map.py (77%) rename {_installer_import/steam_installer => steam_installer}/config.py (83%) rename {_installer_import/steam_installer => steam_installer}/data/cachyos_signing_key.asc (100%) rename {_installer_import/steam_installer => steam_installer}/data/compat_overrides.json (100%) rename {_installer_import/steam_installer => steam_installer}/data/proton-native.lock (100%) rename {_installer_import/steam_installer => steam_installer}/depotdl.py (56%) rename {_installer_import/steam_installer => steam_installer}/deps.py (73%) rename {_installer_import/steam_installer => steam_installer}/dlc.py (81%) create mode 100644 steam_installer/errors.py rename {_installer_import/steam_installer => steam_installer}/finalize.py (82%) rename {_installer_import/steam_installer => steam_installer}/finish_check.py (95%) rename {_installer_import/steam_installer => steam_installer}/flow.py (61%) rename {_installer_import/steam_installer => steam_installer}/ghost.py (76%) rename _installer_import/install.sh => steam_installer/install_deps.sh (70%) create mode 100644 steam_installer/job.py rename {_installer_import/steam_installer => steam_installer}/library.py (83%) create mode 100644 steam_installer/manifest_depots.py rename {_installer_import/steam_installer => steam_installer}/manifest_write.py (71%) rename {_installer_import/steam_installer => steam_installer}/map_installed.py (74%) rename {_installer_import/steam_installer => steam_installer}/proton_native.py (85%) rename {_installer_import/steam_installer => steam_installer}/proton_pkg.py (78%) rename {_installer_import/steam_installer => steam_installer}/recognize.py (82%) rename {_installer_import/steam_installer => steam_installer}/resolve.py (83%) rename {_installer_import => steam_installer}/run.sh (61%) rename {_installer_import/steam_installer => steam_installer}/runlog.py (91%) rename {_installer_import/steam_installer => steam_installer}/steamctl.py (70%) rename {_installer_import/steam_installer => steam_installer}/takeover.py (94%) rename {_installer_import/steam_installer => steam_installer}/target.py (78%) create mode 100644 steam_installer/tests/__init__.py create mode 100644 steam_installer/tests/_acf_helpers.py rename {_installer_import => steam_installer}/tests/_appinfo_blob.py (93%) create mode 100644 steam_installer/tests/_cli_helpers.py create mode 100644 steam_installer/tests/_map_data.py rename {_installer_import => steam_installer}/tests/_proton_fixture.py (73%) rename {_installer_import => steam_installer}/tests/_recognize_helpers.py (82%) create mode 100644 steam_installer/tests/_run_helpers.py rename {_installer_import => steam_installer}/tests/conftest.py (62%) rename {_installer_import => steam_installer}/tests/test_appinfo.py (52%) rename {_installer_import => steam_installer}/tests/test_appinfo_cache.py (54%) create mode 100644 steam_installer/tests/test_appinfo_cache_backed.py rename {_installer_import => steam_installer}/tests/test_appinfo_public_manifests.py (71%) create mode 100644 steam_installer/tests/test_cli.py rename {_installer_import => steam_installer}/tests/test_cli_credit.py (69%) create mode 100644 steam_installer/tests/test_cli_finish.py create mode 100644 steam_installer/tests/test_cli_finish_stub.py rename {_installer_import => steam_installer}/tests/test_cli_modes.py (51%) create mode 100644 steam_installer/tests/test_cli_steam.py create mode 100644 steam_installer/tests/test_cli_steam_client.py rename {_installer_import => steam_installer}/tests/test_compat_decide.py (53%) rename {_installer_import => steam_installer}/tests/test_compat_map.py (66%) rename {_installer_import => steam_installer}/tests/test_config.py (59%) create mode 100644 steam_installer/tests/test_depotdl.py rename {_installer_import => steam_installer}/tests/test_deps.py (55%) create mode 100644 steam_installer/tests/test_dlc.py create mode 100644 steam_installer/tests/test_dlc_finish.py rename {_installer_import => steam_installer}/tests/test_finish_check.py (76%) rename {_installer_import => steam_installer}/tests/test_freshness_wrapper.py (69%) rename {_installer_import => steam_installer}/tests/test_ghost.py (68%) rename {_installer_import => steam_installer}/tests/test_ghost_repair.py (72%) create mode 100644 steam_installer/tests/test_http.py create mode 100644 steam_installer/tests/test_install_sh.py rename {_installer_import => steam_installer}/tests/test_library.py (76%) rename {_installer_import => steam_installer}/tests/test_manifest_finish.py (87%) rename {_installer_import => steam_installer}/tests/test_manifest_write.py (79%) create mode 100644 steam_installer/tests/test_map_after_install.py create mode 100644 steam_installer/tests/test_map_installed.py create mode 100644 steam_installer/tests/test_proton_native.py create mode 100644 steam_installer/tests/test_proton_native_cli.py create mode 100644 steam_installer/tests/test_proton_native_copy.py create mode 100644 steam_installer/tests/test_proton_native_in_use.py rename {_installer_import => steam_installer}/tests/test_proton_pkg.py (57%) rename {_installer_import => steam_installer}/tests/test_recognize.py (78%) rename {_installer_import => steam_installer}/tests/test_resolve.py (62%) create mode 100644 steam_installer/tests/test_resolve_prompt.py rename {_installer_import => steam_installer}/tests/test_runlog.py (68%) rename {_installer_import => steam_installer}/tests/test_steamctl.py (62%) rename {_installer_import => steam_installer}/tests/test_takeover.py (72%) rename {_installer_import => steam_installer}/tests/test_target.py (62%) rename {_installer_import => steam_installer}/tests/test_vdf.py (71%) rename {_installer_import/steam_installer => steam_installer}/vdf.py (79%) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index e79dfd8..bd29cc1 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -32,10 +32,11 @@ jobs: # requirements.txt); pytest-cov merges the workers' coverage. - name: Run tests with coverage run: | - python -m pytest steam_backlog_enforcer/tests/ \ + python -m pytest steam_backlog_enforcer/tests/ steam_installer/tests/ \ -n auto \ -p no:sugar \ --cov=steam_backlog_enforcer \ + --cov=steam_installer \ --cov-branch \ --cov-fail-under=100 \ -q diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 822d053..901a622 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,6 +18,8 @@ repos: hooks: - id: trailing-whitespace args: [--markdown-linebreak-ext=md] + # The pinned signing key is byte-for-byte what its source served. + exclude: ^steam_installer/data/ - id: end-of-file-fixer - id: check-yaml # pnpm 12 writes pnpm-lock.yaml as two YAML documents (the pinned @@ -33,7 +35,7 @@ repos: - id: debug-statements - id: name-tests-test args: [--pytest-test-first] - exclude: ^steam_backlog_enforcer/tests/_[a-z_]+\.py$ + exclude: ^steam_(backlog_enforcer|installer)/tests/_[a-z_]+\.py$ - id: check-ast - id: check-builtin-literals - id: check-docstring-first @@ -63,10 +65,16 @@ repos: types: [text] - id: requirements-sync name: requirements.txt covers all package imports - entry: python3 scripts/check_requirements_sync.py + entry: python3 scripts/check_requirements_sync.py --package steam_backlog_enforcer language: system pass_filenames: false files: ^(steam_backlog_enforcer/.*\.py|requirements\.txt)$ + - id: requirements-sync-installer + name: requirements.txt covers the installer's imports + entry: python3 scripts/check_requirements_sync.py --package steam_installer + language: system + pass_filenames: false + files: ^(steam_installer/.*\.py|requirements\.txt)$ # =========================================================================== # RUFF - Linter + formatter @@ -114,6 +122,7 @@ repos: - --disable-error-code=list-item additional_dependencies: - types-requests + - kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit - earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time - leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9 @@ -145,6 +154,7 @@ repos: # Python 3.15 (removed co_lnotab). Same reason as jobs = 1 in pyproject. - --jobs=1 - steam_backlog_enforcer + - steam_installer - scripts/check_requirements_sync.py - scripts/pylint_symilar_cache.py # pylint resolves imports in its OWN isolated env, so anything the @@ -157,6 +167,7 @@ repos: - earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time - leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9 - howlongtobeatpy==1.0.24 + - kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit - mcp==2.3.0 - pytest - requests @@ -219,7 +230,7 @@ repos: types: [python] require_serial: true stages: [manual] - files: ^(steam_backlog_enforcer/(?!tests/).*|scripts/(check_requirements_sync|pylint_symilar_cache))\.py$ + files: ^((steam_backlog_enforcer|steam_installer)/(?!tests/).*|scripts/(check_requirements_sync|pylint_symilar_cache))\.py$ args: &pylint_per_file - --rcfile=pyproject.toml - --fail-under=10.0 @@ -228,15 +239,15 @@ repos: - <<: *pylint_shard id: ci-mirror-pylint-tests-1 name: CI mirror pylint (tests helpers + test_[a-h], per-file checks) - files: ^steam_backlog_enforcer/tests/(?!test_[i-z]).*\.py$ + files: ^steam_(backlog_enforcer|installer)/tests/(?!test_[i-z]).*\.py$ - <<: *pylint_shard id: ci-mirror-pylint-tests-2 name: CI mirror pylint (test_[i-r], per-file checks) - files: ^steam_backlog_enforcer/tests/test_[i-r][^/]*\.py$ + files: ^steam_(backlog_enforcer|installer)/tests/test_[i-r][^/]*\.py$ - <<: *pylint_shard id: ci-mirror-pylint-tests-3 name: CI mirror pylint (test_[s-z], per-file checks) - files: ^steam_backlog_enforcer/tests/test_[s-z][^/]*\.py$ + files: ^steam_(backlog_enforcer|installer)/tests/test_[s-z][^/]*\.py$ - &pylint_whole_tree <<: *pylint_shard id: ci-mirror-pylint-dup @@ -253,6 +264,7 @@ repos: # duplicate-code speed-up) loud instead of silently slow. - --enable=duplicate-code,bad-plugin-value - steam_backlog_enforcer + - steam_installer - scripts/check_requirements_sync.py - scripts/pylint_symilar_cache.py - <<: *pylint_whole_tree @@ -265,11 +277,12 @@ repos: - --disable=all - --enable=cyclic-import - steam_backlog_enforcer + - steam_installer - scripts/check_requirements_sync.py - scripts/pylint_symilar_cache.py - id: ci-mirror-pytest name: CI mirror (clean-venv pytest, xdist, 100% coverage) - entry: bash scripts/ci_mirror.sh pytest steam_backlog_enforcer/tests/ + entry: bash scripts/ci_mirror.sh pytest steam_backlog_enforcer/tests/ steam_installer/tests/ language: system pass_filenames: false always_run: true @@ -302,7 +315,7 @@ repos: # sha512 read as a misspelling of "view". - --skip=*.json,*.lock,*-lock.yaml,.git,__pycache__,.venv,node_modules # `lazer` is osu!lazer's own name, not a misspelling of laser. - - --ignore-words-list=lazer,als,ans,ect,nd,som,sur,te,nam,numer,lew,sie,wil,postion,clen,ther,folow,derrive,ony,tje,noe,theses,crate,doubleclick,wile,tabel,pary,blok,bloc,proces,serwer,parametr,adres,hart,dout,metod,tekst,synonim,grup,mosty,lokal,skalar,milion,nowe,tre,hel,alph + - --ignore-words-list=fpr,lazer,als,ans,ect,nd,som,sur,te,nam,numer,lew,sie,wil,postion,clen,ther,folow,derrive,ony,tje,noe,theses,crate,doubleclick,wile,tabel,pary,blok,bloc,proces,serwer,parametr,adres,hart,dout,metod,tekst,synonim,grup,mosty,lokal,skalar,milion,nowe,tre,hel,alph # =========================================================================== # SHELLCHECK - Shell script linting @@ -370,7 +383,8 @@ repos: |package\.json|package-lock\.json|pnpm-lock\.yaml |Cargo\.(toml|lock)|go\.(mod|sum) |\.fvmrc|\.nvmrc|\.python-version - |\.github/workflows/.*\.ya?ml)$ + |\.github/workflows/.*\.ya?ml + |steam_installer/data/proton-native\.lock)$ - repo: local hooks: diff --git a/CLAUDE.md b/CLAUDE.md index a485b9a..ac1d27c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -24,4 +24,5 @@ After running tests fix all coverage gaps and issues, do not ignore unless speci - coverage: `python3 -m pytest -q` - coverage-gaps: `coverage-gaps coverage.lcov` - deploy: `scripts/deploy.sh` +- install-game: `steam_installer/run.sh ` (the daemon itself runs `python3 -m steam_installer.cli`; docs in `DOCS-steam-installer.md`) - push: `scripts/check_gate_stamp.sh full && git push` (every manual-stage gate hook, ~3 min, then push). Agents (60 s cap): one call per id of `scripts/check_gate_stamp.sh full --list`: `CAP_MEM=4G CAP_CPU_PCT=20 ~/.claude/scripts/capped.sh timeout 55 scripts/check_gate_stamp.sh step ` (each < 55 s; the last stamps the tree), then `git push`. diff --git a/_installer_import/README.md b/DOCS-steam-installer.md similarity index 87% rename from _installer_import/README.md rename to DOCS-steam-installer.md index e7b6bd2..414363c 100644 --- a/_installer_import/README.md +++ b/DOCS-steam-installer.md @@ -1,27 +1,27 @@ -# steam-game-installer +# Steam game installer Download an owned Steam game with `depotdownloader` (which saturates a link far better than the Steam client), then make the Steam client accept the result as a real install so **Play** works. ```bash -./run.sh BeamNG.drive +steam_installer/run.sh BeamNG.drive ``` -That is the whole thing. `run.sh` installs its own dependencies (`libibus` via -pacman, DepotDownloader from the SteamRE release, a local `.venv` and the native +That is the whole thing. `steam_installer/run.sh` installs its own dependencies (`libibus` via +pacman, DepotDownloader from the SteamRE release and the native Proton described below) on first use, then works out what you meant. It never runs `steamcmd`: app info comes from Steam's own cache and the tool writes the manifest itself. All four of these install the same game: ```bash -./run.sh 284160 # an AppID -./run.sh https://steamcommunity.com/app/284160/guides/ # any Steam URL -./run.sh BeamNG.drive # the exact name -./run.sh BeamNG # part of the name +steam_installer/run.sh 284160 # an AppID +steam_installer/run.sh https://steamcommunity.com/app/284160/guides/ # any Steam URL +steam_installer/run.sh BeamNG.drive # the exact name +steam_installer/run.sh BeamNG # part of the name ``` -An exact title always wins, so `./run.sh Portal` installs Portal and not +An exact title always wins, so `steam_installer/run.sh Portal` installs Portal and not Portal 2 — even though Steam's own search ranks the sequel first. An ambiguous name shows a numbered picker, and in a non-interactive run it lists the candidates and exits 2 rather than guessing. @@ -40,7 +40,7 @@ it with no Steam Linux Runtime container, and Proton Experimental, Steam Linux Runtime 4.0 and the Steamworks Redistributables are not needed while only such games are installed (steam-backlog-enforcer removes them then). -- **The tool.** `install.sh` downloads the exact build pinned in +- **The tool.** `steam_installer/install_deps.sh` downloads the exact build pinned in `steam_installer/data/proton-native.lock` from the CachyOS mirror. It checks the package's `.sig` against the CachyOS key pinned in `steam_installer/data/cachyos_signing_key.asc` (fingerprint and sources in @@ -61,7 +61,7 @@ games are installed (steam-backlog-enforcer removes them then). entry in `config/config.vdf` (`"name" "proton-cachyos-native"`, priority 250). The file is edited as text, and none of its other lines is ever logged. An entry Valve chose (priority 100) is replaced; one you picked is kept. -- **Games already installed.** `./run.sh --map-installed` prints the decision +- **Games already installed.** `steam_installer/run.sh --map-installed` prints the decision for every installed game and writes nothing; add `--apply` to close Steam and write the safe ones. `--appids 730,1201270` decides any cached app instead. @@ -110,7 +110,7 @@ Steam records it. ## Which depotdownloader -Always `~/.local/bin/depotdownloader` (the copy `install.sh` manages) when it +Always `~/.local/bin/depotdownloader` (the copy `steam_installer/install_deps.sh` manages) when it exists, never "whatever is first on PATH". Each depotdownloader binary keeps its remembered login in its own storage slot, keyed by its path; with an AUR copy in `/usr/bin` too, a terminal run and a `sudo`'d run (whose PATH lacks @@ -129,7 +129,7 @@ That manifest is not harmless: a running Steam client reads it, concludes the app is installed but out of date, and downloads the entire game by itself. ```bash -./run.sh --repair-ghosts +steam_installer/run.sh --repair-ghosts ``` finds them across every library and clears them away — but only the *cold* ones. @@ -219,16 +219,14 @@ not game content and overstate the total by gigabytes. ## Development +The installer shares this repo's gate (ruff, mypy, pylint, bandit, 250-line cap) +and its test run, which enforces **100% of statements and branches** for both +packages (`fail_under = 100`): + ```bash -./install.sh # also prepares the test env -.venv/bin/python -m pytest tests/ -q -ruff check steam_installer/ tests/ -ruff format --check steam_installer/ tests/ +python3 -m pytest -q # both suites, coverage gated +scripts/test_changed.sh # only the tests of changed files ``` -Coverage is enforced at **100% of statements and branches** -(`fail_under = 100`), so an uncovered line fails the suite rather than quietly -lowering a number. One test runs `python -m steam_installer.cli` in a -subprocess to prove the `__main__` entry point works; recording coverage from -a child process needs a `.pth` that calls `coverage.process_startup()`, which -`install.sh` writes into the venv. Skip `install.sh` and coverage reports 99%. +The steam-backlog-enforcer daemon runs the installer from this checkout as +`python3 -m steam_installer.cli`; it needs no virtualenv. diff --git a/README.md b/README.md index da4dd2f..243920c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Steam Backlog Enforcer +This repo is also an alternative Steam frontend: it installs games faster than the +Steam client (`steam_installer`, see [DOCS-steam-installer.md](DOCS-steam-installer.md)). + Forces you to play one Steam game at a time: earn at least one new achievement in the assigned game before moving on to the next. @@ -84,3 +87,11 @@ Claude Code spawns: Registration lives in the checked-in [`.mcp.json`](./.mcp.json) (project scope). Restart Claude Code in this repo and approve the project MCP-server prompt for it to load. Verify with `claude mcp list`. + +## Steam game installer + +`steam_installer` (merged in from steam-game-installer) downloads an owned game +with depotdownloader and makes Steam accept it as installed. The daemon runs it +as `python3 -m steam_installer.cli`; by hand use `steam_installer/run.sh `. +Dependencies: `steam_installer/install_deps.sh` (run by `install.sh`). Details in +[DOCS-steam-installer.md](DOCS-steam-installer.md). diff --git a/_installer_import/.github/workflows/file-length.yml b/_installer_import/.github/workflows/file-length.yml deleted file mode 100644 index 38f5df1..0000000 --- a/_installer_import/.github/workflows/file-length.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: file length - -# The shared 250-line cap, enforced on push as well as at commit time: the -# pre-commit hook only sees staged files and a fresh clone may never have run -# `pre-commit install`. This checks the whole tree. - -on: - push: - branches: [main] - pull_request: - workflow_dispatch: - -jobs: - file-length: - name: files at most 250 lines - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - # The gate and its exemption list live in the shared utils repo. - - name: Check out the shared gate - uses: actions/checkout@v4 - with: - repository: kuhyx/utils - path: .utils - - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: "3.x" - - # Tracked files only: --all would also walk the .utils checkout above, - # which is another repo's code and not this one's to split. - - name: Every file is at most 250 lines - run: git ls-files -z | xargs -0 bash .utils/scripts/check_file_length.sh diff --git a/_installer_import/.github/workflows/repo-contract.yml b/_installer_import/.github/workflows/repo-contract.yml deleted file mode 100644 index 89d5597..0000000 --- a/_installer_import/.github/workflows/repo-contract.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: repo-contract - -# The local pre-commit hook can be skipped (a fresh clone has no hooks), so the -# contract is enforced on push too. Mirrors md-naming.yml. -on: - push: - branches: [main] - pull_request: - workflow_dispatch: - -jobs: - repo-contract: - name: repo contract - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Check out the shared gate - uses: actions/checkout@v4 - with: - repository: kuhyx/utils - path: .utils - - - name: Enforce the repo contract - run: bash .utils/scripts/check_repo_contract.sh diff --git a/_installer_import/.gitignore b/_installer_import/.gitignore deleted file mode 100644 index f1efa20..0000000 --- a/_installer_import/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -.venv/ -__pycache__/ -*.pyc -.pytest_cache/ -*.egg-info/ -.coverage -.coverage.* -coverage.xml diff --git a/_installer_import/.pre-commit-config.yaml b/_installer_import/.pre-commit-config.yaml deleted file mode 100644 index 4cf791d..0000000 --- a/_installer_import/.pre-commit-config.yaml +++ /dev/null @@ -1,84 +0,0 @@ -# Pre-commit hooks for this repo. -# -# Created when the shared markdown-naming gate was rolled out. The checks -# are `language: system` because they are plain bash already on this -# machine; pre-commit must not build an isolated env for them. -default_install_hook_types: [pre-commit, post-commit, post-merge, post-rewrite] -repos: - - repo: local - hooks: - # Four markdown namespaces (README / CLAUDE* / DOCS* / TODO*), plus the - # rule that a TODO carries "REMOVE ME AFTER FINISH" and nothing else may. - # Shared gate in ~/src/utils owns the namespaces and the exemption list, so - # one repo's idea of a valid name cannot drift from every other repo's. - - id: md-naming - name: markdown naming convention - entry: scripts/check_md_naming.sh - language: system - files: \.(md|markdown)$ - - - id: todo-closer - name: close Todo trailer tasks - entry: scripts/todo_hooks.sh closer - language: system - stages: [post-commit] - always_run: true - pass_filenames: false - - - id: todo-import-pr-merge - name: import PR Todo sections (pull) - entry: scripts/todo_hooks.sh import-pr post-merge - language: system - stages: [post-merge] - always_run: true - pass_filenames: false - - - id: todo-import-pr-rewrite - name: import PR Todo sections (pull --rebase) - entry: scripts/todo_hooks.sh import-pr post-rewrite - language: system - stages: [post-rewrite] - always_run: true - pass_filenames: false - - # Shared 250-line cap on every file in the commit, code and prose alike. - # Checks only what is staged, so an unrelated commit never goes red, and - # it fails rather than warns: no baseline, no allowlist. - - id: file-length - name: files at most 250 lines - entry: scripts/check_file_length.sh - language: system - types: [text] - - # Every dependency on its ecosystem's newest STABLE release, exact-pinned - # so the question is mechanically answerable at all. That includes the - # native Proton build, pinned in proton-native.lock and checked against - # cachyos.db by the shared gate's pacman-repo adapter. - # - # `files:` is what keeps this cheap: a commit touching only source makes - # ZERO network requests, and manifest commits mostly hit a 6h disk cache. - # - # Runs WITHOUT --strict on purpose. Offline it degrades to the cached - # answer and passes with a loud banner; `--no-verify` is banned here, so - # a hook that hard-failed on a flaky connection would leave no way to - # commit at all. The strict run is local only (CI does not check - # freshness): scripts/check_dependency_freshness.sh --all --strict - - id: dependency-freshness - name: dependencies on newest stable - entry: scripts/check_dependency_freshness.sh - language: system - pass_filenames: true - files: >- - (pubspec\.(yaml|lock)|pyproject\.toml|requirements.*\.txt|uv\.lock - |package\.json|package-lock\.json|pnpm-lock\.yaml - |Cargo\.(toml|lock)|go\.(mod|sum) - |\.fvmrc|\.nvmrc|\.python-version - |\.github/workflows/.*\.ya?ml - |steam_installer/data/proton-native\.lock)$ - - - id: repo-contract - name: repo contract (CLAUDE.md Commands + test_changed) - entry: scripts/check_repo_contract.sh - language: system - pass_filenames: false - files: ^(CLAUDE\.md|AGENTS\.md|scripts/test_changed\.sh)$ diff --git a/_installer_import/CLAUDE.md b/_installer_import/CLAUDE.md deleted file mode 100644 index 6949597..0000000 --- a/_installer_import/CLAUDE.md +++ /dev/null @@ -1,9 +0,0 @@ -## Commands - -- run: `./run.sh ` -- test: `python3 -m pytest -q` -- test-changed: `scripts/test_changed.sh` -- lint: `pre-commit run --all-files` -- coverage: `python3 -m pytest -q --cov --cov-branch --cov-report=xml` -- coverage-gaps: `coverage-gaps coverage.xml` -- deploy: `scripts/deploy.sh` (fast-forwards kuhy's checkout `~/src/steam-game-installer` to origin/main, refusing a dirty tree, reruns `./run.sh --deps-only`, then checks the CLI starts; idempotent, non-zero unless at origin/main and healthy; test: `tests/test_deploy.py`) diff --git a/_installer_import/pyproject.toml b/_installer_import/pyproject.toml deleted file mode 100644 index 697f19a..0000000 --- a/_installer_import/pyproject.toml +++ /dev/null @@ -1,34 +0,0 @@ -[project] -name = "steam-game-installer" -version = "0.1.0" -description = "Fast Steam game installs: depotdownloader for speed, a hand-written manifest for recognition, a FOSS Proton to run them." -requires-python = ">=3.14" -# kuhy's shared "Created by kuhy" credit, shown at the end of --help. -dependencies = [ - "kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit", -] - -[project.scripts] -steam-game-installer = "steam_installer.cli:main" - -[build-system] -requires = ["setuptools>=68"] -build-backend = "setuptools.build_meta" - -[tool.pytest.ini_options] -testpaths = ["tests"] - -[tool.coverage.run] -source = ["steam_installer"] -branch = true -# The `__main__` guard is exercised by running the module as a script in a -# subprocess; without this its coverage would be recorded in a child process -# and thrown away. -parallel = true -concurrency = ["multiprocessing"] - -[tool.coverage.report] -# The suite covers every line and branch. Anything less is a regression, so -# make it fail rather than merely reporting a lower number. -fail_under = 100 -show_missing = true diff --git a/_installer_import/scripts/check_dependency_freshness.sh b/_installer_import/scripts/check_dependency_freshness.sh deleted file mode 100755 index 934c6a5..0000000 --- a/_installer_import/scripts/check_dependency_freshness.sh +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Fail if any dependency in the commit is behind its ecosystem's newest stable. -# -# Thin delegate to the shared gate in ~/src/utils, which owns the registry -# adapters, the pre-release rules and the allowlist semantics. Copying that -# logic here is what lets one repo's idea of "current" drift from every other -# repo's -- so this script only locates the shared checker and forwards its -# arguments. -# -# Usage: -# scripts/check_dependency_freshness.sh [ ...] # pre-commit -# scripts/check_dependency_freshness.sh --all --strict # full local run -# -# Exit: 0 current | 1 behind | 2 allowlist wrong | 3 undeterminable (offline) -# ============================================================================ - -set -euo pipefail - -readonly UTILS="${UTILS_ROOT:-$HOME/src/utils}" -readonly SHARED_GATE="$UTILS/scripts/check_dependency_freshness.sh" -readonly PACMAN_PARSER="$UTILS/dep_freshness/parsers/pacman_lock.py" - -# True when this run must judge the native-Proton lock (a full run, or the lock staged). -needs_pacman_adapter() { - local arg - for arg in "$@"; do - [[ "$arg" == "--all" || "$arg" == *proton-native.lock ]] && return 0 - done - return 1 -} - -# Print a gate directory that can read the Proton lock, or nothing. -# -# kuhy's ~/src/utils can lag origin/main (local commits not yet pushed, so it -# cannot fast-forward). Then the gate runs a read-only snapshot of utils' -# origin/main from the cache instead: the checkout itself is never touched. -gate_root_for_lock() { - if [[ -f "$PACMAN_PARSER" ]]; then - echo "$UTILS" - return - fi - local sha snapshot - local -a paths=(scripts/check_dependency_freshness.sh dep_freshness) - git -C "$UTILS" cat-file -e origin/main:dep_freshness/parsers/pacman_lock.py 2>/dev/null || - return 0 - sha="$(git -C "$UTILS" rev-parse origin/main)" - snapshot="${XDG_CACHE_HOME:-$HOME/.cache}/steam-game-installer/utils-gate/$sha" - if [[ ! -f "$snapshot/dep_freshness/parsers/pacman_lock.py" ]]; then - rm -rf "$snapshot.tmp" - mkdir -p "$snapshot.tmp" - # The fleet allowlist lives beside the gate, when utils has one. - if git -C "$UTILS" cat-file -e origin/main:dependency-freshness.allowlist.yaml 2>/dev/null; then - paths+=(dependency-freshness.allowlist.yaml) - fi - git -C "$UTILS" archive origin/main "${paths[@]}" | tar -x -C "$snapshot.tmp" - rm -rf "$snapshot" - mv "$snapshot.tmp" "$snapshot" - fi - echo "dependency-freshness: $UTILS lacks the pacman adapter; using its origin/main ${sha:0:12}" >&2 - echo "$snapshot" -} - -main() { - if [[ ! -x "$SHARED_GATE" ]]; then - echo "Error: shared dependency-freshness gate not found at $SHARED_GATE" >&2 - echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2 - echo " UTILS_ROOT to where it lives." >&2 - exit 1 - fi - - local root="$UTILS" - # An older utils checkout skips a manifest it has no parser for and exits - # 0, which would wave any Proton pin through. Fail closed instead. - if needs_pacman_adapter "$@"; then - root="$(gate_root_for_lock)" - if [[ -z "$root" ]]; then - echo "Error: $UTILS has no dep_freshness/parsers/pacman_lock.py, so it" >&2 - echo " cannot check steam_installer/data/proton-native.lock." >&2 - echo " Update it: git -C $UTILS pull --ff-only" >&2 - exit 1 - fi - fi - - exec bash "$root/scripts/check_dependency_freshness.sh" "$@" -} - -main "$@" diff --git a/_installer_import/scripts/check_file_length.sh b/_installer_import/scripts/check_file_length.sh deleted file mode 100755 index 915b47c..0000000 --- a/_installer_import/scripts/check_file_length.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Fail if any file in the commit exceeds the shared 250-line cap. -# -# Thin delegate to the shared gate in ~/src/utils, which owns the cap and the -# exemption list (generated files, markup, data). Copying that logic here is -# how one repo's idea of "too long" drifts from every other repo's -- so this -# script only locates the shared checker and forwards its arguments. -# -# Usage: -# scripts/check_file_length.sh [ ...] # pre-commit passes these -# scripts/check_file_length.sh --all # whole tree, from cwd -# ============================================================================ - -set -euo pipefail - -readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_file_length.sh" - -main() { - if [[ ! -x "$SHARED_GATE" ]]; then - echo "Error: shared file-length gate not found at $SHARED_GATE" >&2 - echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2 - echo " UTILS_ROOT to where it lives." >&2 - exit 1 - fi - - exec bash "$SHARED_GATE" "$@" -} - -main "$@" diff --git a/_installer_import/scripts/check_md_naming.sh b/_installer_import/scripts/check_md_naming.sh deleted file mode 100755 index 7847c56..0000000 --- a/_installer_import/scripts/check_md_naming.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Fail if any markdown file breaks the shared naming convention. -# -# Thin delegate to the shared gate in ~/src/utils, which owns the four namespaces -# (README / CLAUDE* / DOCS* / TODO*), the "REMOVE ME AFTER FINISH" marker rule -# and the exemption list. Copying that logic here is what lets one repo's idea -# of a valid name drift from every other repo's -- so this script only locates -# the shared checker and forwards its arguments. -# -# Usage: -# scripts/check_md_naming.sh [ ...] # pre-commit passes these -# scripts/check_md_naming.sh --all # whole tree, from cwd -# ============================================================================ - -set -euo pipefail - -readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_md_naming.sh" - -main() { - if [[ ! -x "$SHARED_GATE" ]]; then - echo "Error: shared markdown-naming gate not found at $SHARED_GATE" >&2 - echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2 - echo " UTILS_ROOT to where it lives." >&2 - exit 1 - fi - - exec bash "$SHARED_GATE" "$@" -} - -main "$@" diff --git a/_installer_import/scripts/check_repo_contract.sh b/_installer_import/scripts/check_repo_contract.sh deleted file mode 100755 index 8488858..0000000 --- a/_installer_import/scripts/check_repo_contract.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Fail if this repo breaks the shared repo contract. -# -# Thin delegate to the shared gate in ~/src/utils, which owns the contract -# text. Copy this file to scripts/check_repo_contract.sh in a repo; never copy -# the shared gate itself, or the shim would exec itself forever. -# ============================================================================ - -set -euo pipefail - -readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_repo_contract.sh" - -main() { - if [[ ! -x "$SHARED_GATE" ]]; then - echo "Error: shared repo-contract gate not found at $SHARED_GATE" >&2 - echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2 - echo " UTILS_ROOT to where it lives." >&2 - exit 1 - fi - - exec bash "$SHARED_GATE" "$@" -} - -main "$@" diff --git a/_installer_import/scripts/deploy.sh b/_installer_import/scripts/deploy.sh deleted file mode 100755 index ed075f1..0000000 --- a/_installer_import/scripts/deploy.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Deploy origin/main to where this tool runs: kuhy's checkout -# (~/src/steam-game-installer, used through ./run.sh). Refuses a dirty tree, -# fast-forwards it, reruns ./run.sh --deps-only (libibus, depotdownloader, the native Proton, -# editable .venv), then starts the CLI as the health check. Nothing is -# published elsewhere: no package, no service, no release. -# Idempotent; non-zero unless the checkout is at origin/main and the CLI starts. -# scripts/deploy.sh -# SGI_DEPLOY_DIR=/path another checkout (the test uses a fixture) -# ============================================================================ - -set -euo pipefail - -readonly DIR="${SGI_DEPLOY_DIR:-$HOME/src/steam-game-installer}" - -die() { - echo "deploy: FAILED ($1)" >&2 - exit 1 -} - -main() { - [[ -d "$DIR/.git" ]] || die "no checkout at $DIR" - [[ -z "$(git -C "$DIR" status --porcelain)" ]] || die "$DIR has local changes" - git -C "$DIR" checkout -q main || die "cannot switch $DIR to main" - git -C "$DIR" pull -q --ff-only origin main || die "cannot fast-forward $DIR" - local head want - head="$(git -C "$DIR" rev-parse HEAD)" - want="$(git -C "$DIR" rev-parse origin/main)" - [[ "$head" == "$want" ]] || die "$DIR is at ${head:0:7}, origin/main is ${want:0:7}" - (cd "$DIR" && ./run.sh --deps-only) || die "dependency install failed" - "$DIR/.venv/bin/python" -m steam_installer.cli --help >/dev/null || - die "health check: the CLI does not start" - echo "deploy: $DIR at ${head:0:7}, deps ok, CLI starts" -} - -main "$@" diff --git a/_installer_import/scripts/test_changed.sh b/_installer_import/scripts/test_changed.sh deleted file mode 100755 index 65efb87..0000000 --- a/_installer_import/scripts/test_changed.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash - -# ============================================================================ -# Run only the tests related to files changed vs HEAD (staged, unstaged and -# untracked). Quiet: failures plus a one-line summary. Falls back to the full -# suite when a change cannot be mapped to tests. Generated by repo_contract. -# ============================================================================ - -set -euo pipefail - -cd "$(git rev-parse --show-toplevel)" -CHANGED=() -while IFS= read -r f; do - [[ -n "$f" && -e "$f" ]] && CHANGED+=("$f") -done < <({ git diff --name-only HEAD 2>/dev/null || true; git ls-files --others --exclude-standard; } | sort -u) - -pytest_run() { - if [[ -f uv.lock ]]; then uv run pytest "$@" - elif [[ -f poetry.lock ]]; then poetry run pytest "$@" - else python3 -m pytest "$@"; fi -} - -if grep -qs testmon pyproject.toml requirements*.txt; then - pytest_run --testmon -q --tb=short; exit $? -fi - -full=0 -tests=() -for f in "${CHANGED[@]}"; do - case "$f" in - */conftest.py | conftest.py | pyproject.toml | requirements*.txt | pytest.ini | setup.cfg) full=1 ;; - *.py) - base="$(basename "$f" .py)" - if [[ "$base" == test_* || "$base" == *_test ]]; then - tests+=("$f") - else - while IFS= read -r t; do tests+=("$t"); done \ - < <(git ls-files "*test_${base}.py" "*${base}_test.py") - fi - ;; - esac -done - -if [[ $full -eq 0 && ${#CHANGED[@]} -eq 0 ]]; then echo "no changes vs HEAD: nothing to test"; exit 0; fi -if [[ $full -eq 0 && ${#tests[@]} -gt 0 ]]; then pytest_run -q --tb=short "${tests[@]}"; exit $?; fi -if [[ $full -eq 0 ]] && ! printf '%s\n' "${CHANGED[@]}" | grep -q '\.py$'; then - echo "no python changes: nothing to test"; exit 0 -fi -echo "no mapped tests: running full suite" -pytest_run -q --tb=short diff --git a/_installer_import/scripts/todo_hooks.sh b/_installer_import/scripts/todo_hooks.sh deleted file mode 100755 index 168dc7b..0000000 --- a/_installer_import/scripts/todo_hooks.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# Shim: forward to the shared todo git hooks in ~/src/utils (silent if absent). -set -euo pipefail - -readonly SHARED="${UTILS_ROOT:-$HOME/src/utils}/scripts/todo_hooks.sh" - -main() { - [[ -f "$SHARED" ]] || exit 0 - exec bash "$SHARED" "$@" -} - -main "$@" diff --git a/_installer_import/steam_installer/__init__.py b/_installer_import/steam_installer/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/_installer_import/tests/_cli_helpers.py b/_installer_import/tests/_cli_helpers.py deleted file mode 100644 index a063350..0000000 --- a/_installer_import/tests/_cli_helpers.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Stubs shared by the CLI flow tests (plain functions, not fixtures).""" - -from steam_installer import depotdl - -ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"] - - -def _stub_download(monkeypatch, returncode=0): - monkeypatch.setattr(depotdl, "download", lambda **kw: returncode) diff --git a/_installer_import/tests/test_cli.py b/_installer_import/tests/test_cli.py deleted file mode 100644 index 6f209ea..0000000 --- a/_installer_import/tests/test_cli.py +++ /dev/null @@ -1,97 +0,0 @@ -"""End-to-end-ish tests for the CLI's decision logic. - -The bug that motivated all of this lived here, not in the manifest writer: -a step died instantly having done nothing, and main() still wrote -StateFlags=4, printed "launch Steam and confirm Play works" and returned 0. -These tests pin the fail-closed behaviour. -""" - -import pytest -from steam_installer import ( - appinfo, - cli, - depotdl, - recognize, -) - -from _cli_helpers import ARGV, _stub_download - -pytestmark = pytest.mark.usefixtures("cli_logs") - - -def test_downloaded_content_finishes_the_manifest(steam, monkeypatch): - """The content is what matters, and the manifest must come out consistent, - not merely flagged installed.""" - library, install_dir = steam - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - - app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") - assert app_state["StateFlags"] == "4" - for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"): - assert app_state[key] == "0", key - assert recognize.download_counters_settled(app_state) - - -def test_refuses_when_a_required_depot_never_downloaded(steam, monkeypatch): - """The exact fail-open case: nothing was really downloaded, so declaring the - app installed would be a fabricated success.""" - library, install_dir = steam - _stub_download(monkeypatch) - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda appid, language: appinfo.DepotPlan({"1771303", "9999999"}, set()), - ) - - assert cli.main(ARGV) == 1 - - app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") - assert app_state["StateFlags"] == "1026" - - -def test_refuses_when_depotdownloader_fails(steam, monkeypatch): - library, _ = steam - _stub_download(monkeypatch, returncode=1) - - assert cli.main(ARGV) == 1 - app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") - assert app_state["StateFlags"] == "1026" - - -def test_declining_to_close_steam_aborts_before_downloading(steam, monkeypatch): - """Steam owns the manifest while it runs. The tool offers to close it, but a - "no" must stop the run rather than download for an hour and lose the write.""" - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [4242]) - monkeypatch.setattr( - "builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y" - ) - called = [] - monkeypatch.setattr(depotdl, "download", lambda **kw: called.append(1) or 0) - - assert cli.main(ARGV) == 1 - assert called == [] - - -def test_user_declining_confirmation_aborts(steam, monkeypatch): - monkeypatch.setattr("builtins.input", lambda *a: "n") - called = [] - monkeypatch.setattr(depotdl, "download", lambda **kw: called.append(1) or 0) - assert cli.main(ARGV) == 1 - assert called == [] - - -def test_unhandled_error_returns_nonzero_and_names_the_log(steam, monkeypatch, capsys): - def boom(**kwargs): - raise RuntimeError("depotdownloader exploded") - - monkeypatch.setattr(depotdl, "download", boom) - - assert cli.main(ARGV) == 1 - assert "Run log" in capsys.readouterr().out - - -def test_requires_a_name_or_appid(): - with pytest.raises(SystemExit): - cli.main(["--username", "u"]) diff --git a/_installer_import/tests/test_cli_finish.py b/_installer_import/tests/test_cli_finish.py deleted file mode 100644 index f252cf7..0000000 --- a/_installer_import/tests/test_cli_finish.py +++ /dev/null @@ -1,228 +0,0 @@ -"""CLI flow tests, part 2: the manifest-finishing end of a run.""" - -import subprocess -import sys - -import pytest -from _cli_helpers import ARGV, _stub_download - -from steam_installer import ( - appinfo, - cli, - recognize, - vdf, -) - -pytestmark = pytest.mark.usefixtures("cli_logs") - - -# The 15 keys Steam accepted on 2026-11-22 (A Space for the Unbound): it -# logged "Fully Installed,App Running", queued no download, and added its own -# LastOwner, UserConfig, MountedConfig, ... on exit. -FROM_SCRATCH_KEYS = [ - "appid", "universe", "name", "StateFlags", "installdir", "LastUpdated", - "SizeOnDisk", "StagingSize", "buildid", "UpdateResult", "BytesToDownload", - "BytesDownloaded", "BytesToStage", "BytesStaged", "InstalledDepots", -] - - -def _proven_plan(monkeypatch, install_dir, shared_owner=None): - (gid,) = recognize.discover_cached_depot_manifests(install_dir).values() - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda appid, language: appinfo.DepotPlan( - {"1771303"}, set(shared_owner or ()), manifests={"1771303": gid}, - shared_owner=dict(shared_owner or {}), - ), - ) - return gid - - -def test_missing_manifest_is_written_from_scratch(steam, monkeypatch): - """No steamcmd any more, so nothing else creates the manifest.""" - library, install_dir = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - manifest.unlink() - gid = _proven_plan(monkeypatch, install_dir) - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - - app_state = vdf.parse(manifest.read_text())["AppState"] - assert list(app_state) == FROM_SCRATCH_KEYS - assert app_state["appid"] == "1771300" - assert app_state["universe"] == "1" - assert app_state["name"] == "KCD2" - assert app_state["StateFlags"] == "4" - assert app_state["installdir"] == "KingdomComeDeliverance2" - assert app_state["SizeOnDisk"] == "1000" - assert app_state["buildid"] == "23914554" - for key in ("StagingSize", "UpdateResult", "BytesToDownload", - "BytesDownloaded", "BytesToStage", "BytesStaged"): - assert app_state[key] == "0", key - assert app_state["InstalledDepots"] == {"1771303": {"manifest": gid, "size": "0"}} - # Nothing to back up: there was no manifest before. - assert not manifest.with_suffix(".acf.bak").exists() - - -def test_a_shared_depot_is_recorded_under_its_owner_from_scratch(steam, monkeypatch): - """Fixture-only: no shared-depot game was tested live. Steam records a - depot installed under another app as SharedDepots {depot: owner}.""" - library, install_dir = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - manifest.unlink() - _proven_plan(monkeypatch, install_dir, shared_owner={"228989": "228980"}) - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - app_state = vdf.parse(manifest.read_text())["AppState"] - assert app_state["SharedDepots"] == {"228989": "228980"} - assert "228989" not in app_state["InstalledDepots"] - - -def test_unproven_content_writes_no_manifest_from_scratch(steam, monkeypatch, capsys): - """Fail closed: without a proven build there is no buildid to write, and a - half-true StateFlags=4 manifest is what made Steam redownload games.""" - library, _ = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - manifest.unlink() - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 1 - assert not manifest.exists() - assert "no usable buildid" in capsys.readouterr().out - - -def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch): - seen = {} - monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "") - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda appid, language: ( - seen.setdefault("language", language) - and None - or appinfo.DepotPlan({"1771303"}, set()) - ), - ) - _stub_download(monkeypatch) - - cli.main(["--appid", "1771300", "--username", "u"]) - - assert seen["language"] == "english" - - -def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys): - """Steam's staging tree is adopted into the install dir before downloading, - and only cleared once the install is finished -- never discarded up front.""" - library, install_dir = steam - staged = library.steamapps / "downloading" / "1771300" - staged.mkdir(parents=True) - (staged / "big.pak").write_bytes(b"x" * 2_000_000) - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - - out = capsys.readouterr().out - assert "Hardlinking it" in out - assert "now-redundant Steam staging data" in out - # Adopted, not copied: the file is one inode with two names. - assert (install_dir / "big.pak").stat().st_size == 2_000_000 - - -def test_fails_when_written_manifest_does_not_read_back_as_installed( - steam, monkeypatch, capsys -): - """Claiming success without re-reading the write is the same fail-open - mistake in a different place.""" - _stub_download(monkeypatch) - monkeypatch.setattr( - recognize, "manifest_reflects_real_install", lambda *a, **kw: False - ) - - assert cli.main(ARGV) == 1 - assert "does not read back as a real install" in capsys.readouterr().out - - -def test_module_is_runnable_as_a_script(coverage_subprocess_env): - """`python -m steam_installer.cli` is a real entry point (the __main__ - guard), so it must not be dead code.""" - result = subprocess.run( - [sys.executable, "-m", "steam_installer.cli", "--help"], - capture_output=True, - text=True, - timeout=60, - env=coverage_subprocess_env, - ) - assert result.returncode == 0 - assert "--skip-steamcmd" not in result.stdout - - -def test_a_stub_manifest_is_finished_with_the_proven_build(steam, monkeypatch): - """The enforcer's stub says build 0 and Steam never ran to fill it in. The - cached depot manifest matching the public branch proves the current build, - which is what the finished manifest must record (BG3, 2026-10-23).""" - library, install_dir = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - manifest.write_text( - vdf.dumps("AppState", {"appid": "1771300", "StateFlags": "1026", "buildid": "0"}) - ) - (gid,) = recognize.discover_cached_depot_manifests(install_dir).values() - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda appid, language: appinfo.DepotPlan( - {"1771303"}, {"228989"}, manifests={"1771303": gid} - ), - ) - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - app_state = vdf.parse(manifest.read_text())["AppState"] - assert (app_state["StateFlags"], app_state["buildid"]) == ("4", "23914554") - - -def test_the_install_folder_is_corrected_even_when_the_download_fails( - steam, monkeypatch -): - """The enforcer's stub names the folder after the display name ("Baldur's - Gate 3", not "Baldurs Gate 3"). Steam resumes onto whatever the manifest - says, so it is fixed as soon as Steam is closed, not only on success.""" - library, _ = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - state = recognize.read_app_state(manifest) - state["installdir"] = "Kingdom Come: Deliverance II" - manifest.write_text(vdf.dumps("AppState", state)) - _stub_download(monkeypatch, returncode=2) - - assert cli.main(ARGV) == 1 - assert recognize.read_app_state(manifest)["installdir"] == "KingdomComeDeliverance2" - - -def test_wrong_installdir_and_mismatched_counters_end_correct_and_zeroed( - steam, monkeypatch -): - """C18: the live repro (installdir "... WRONG", BytesToDownload 1000, - BytesDownloaded 5) once came out as 1000/1000. The written manifest must - name the real folder, keep its files, and carry every counter at 0.""" - library, install_dir = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - state = recognize.read_app_state(manifest) - state.update( - installdir="KingdomComeDeliverance2 WRONG", - BytesToDownload="1000", - BytesDownloaded="5", - BytesToStage="1000", - BytesStaged="5", - ) - manifest.write_text(vdf.dumps("AppState", state)) - _stub_download(monkeypatch) - - assert cli.main(ARGV) == 0 - - app_state = recognize.read_app_state(manifest) - assert app_state["installdir"] == "KingdomComeDeliverance2" - assert (install_dir / "game.pak").stat().st_size == 1000 - for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"): - assert app_state[key] == "0", key diff --git a/_installer_import/tests/test_cli_steam.py b/_installer_import/tests/test_cli_steam.py deleted file mode 100644 index 5ee916a..0000000 --- a/_installer_import/tests/test_cli_steam.py +++ /dev/null @@ -1,205 +0,0 @@ -"""What the CLI does around a real install: ghosts, usernames, the Steam client. - -Split from test_cli_modes.py to keep both files under the repo's 250-line cap. -""" - -import pytest - -from steam_installer import appinfo, cli, config, depotdl, ghost, recognize -from tests.conftest import GHOST_ACF - - -def test_a_cold_ghost_blocks_an_install_and_names_the_fix(cli_steam, capsys): - """Installing over a fabricated manifest is how Steam was tricked into - re-downloading in the first place; stop and say what to run.""" - (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) - - assert cli.main(["--yes", "284160", "--username", "u"]) == 1 - assert "./run.sh --repair-ghosts" in capsys.readouterr().out - - -def test_a_warm_ghost_does_not_block_an_install(cli_steam, monkeypatch): - """Staged bytes are the whole reason to run this tool, not a blocker.""" - (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) - staged = cli_steam.steamapps / "downloading" / "284160" - staged.mkdir(parents=True) - (staged / "part.pak").write_bytes(b"x" * 4096) - reached = [] - monkeypatch.setattr(depotdl, "download", lambda **kw: reached.append(1) or 1) - monkeypatch.setattr( - appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set()) - ) - monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") - - cli.main(["--yes", "284160", "--username", "u"]) - - assert reached == [1] - - -def test_repair_ghosts_clears_a_cold_ghost(cli_steam, capsys): - (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) - - assert cli.main(["--repair-ghosts"]) == 0 - - assert not (cli_steam.steamapps / "appmanifest_284160.acf").exists() - assert "Ghost: appid 284160" in capsys.readouterr().out - - -def test_repair_ghosts_refuses_to_discard_a_warm_ghosts_download(cli_steam, capsys): - (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) - staged = cli_steam.steamapps / "downloading" / "284160" - staged.mkdir(parents=True) - (staged / "part.pak").write_bytes(b"x" * 4096) - - assert cli.main(["--repair-ghosts"]) == 0 - - assert (cli_steam.steamapps / "appmanifest_284160.acf").exists() - assert (staged / "part.pak").exists() - assert "nothing is deleted" in capsys.readouterr().out - - -def test_repair_ghosts_says_so_when_there_is_nothing_to_do(cli_steam, capsys): - assert cli.main(["--repair-ghosts"]) == 0 - assert "No ghost installs found" in capsys.readouterr().out - - -def test_repair_ghosts_needs_no_target(cli_steam): - """It is the one mode that runs without naming a game.""" - assert cli.main(["--repair-ghosts"]) == 0 - - -def test_a_saved_username_removes_the_need_for_the_flag(cli_steam, monkeypatch): - config.save_username("saved-user") - seen = {} - monkeypatch.setattr( - depotdl, - "download", - lambda **kw: seen.setdefault("username", kw["username"]) or 1, - ) - monkeypatch.setattr( - appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set()) - ) - monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") - - cli.main(["--yes", "284160"]) - - assert seen["username"] == "saved-user" - - -def test_no_username_anywhere_fails_with_an_explanation(cli_steam, capsys): - assert cli.main(["--yes", "284160"]) == 1 - assert "No Steam username available" in capsys.readouterr().out - - -def test_a_non_interactive_run_without_yes_does_not_start_a_download(cli_steam, capsys): - """A piped invocation never asked for a 50 GB download; refuse rather than - assume.""" - assert cli.main(["284160", "--username", "u"]) == 1 - assert "use --yes" in capsys.readouterr().out - - -def test_language_defaults_to_english_when_not_interactive(cli_steam, monkeypatch): - seen = {} - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda a, language: ( - seen.setdefault("language", language) or appinfo.DepotPlan(set(), set()) - ), - ) - monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") - monkeypatch.setattr(depotdl, "download", lambda **kw: 1) - - cli.main(["--yes", "284160", "--username", "u"]) - - assert seen["language"] == "english" - - -def test_nothing_to_do_at_all_is_a_usage_error(): - with pytest.raises(SystemExit): - cli.main(["--username", "u"]) - - -def test_a_ghost_for_a_different_app_does_not_block_this_one(cli_steam, monkeypatch): - (cli_steam.steamapps / "appmanifest_999.acf").write_text( - GHOST_ACF.replace('"appid"\t\t"284160"', '"appid"\t\t"999"') - ) - monkeypatch.setattr(depotdl, "download", lambda **kw: 1) - monkeypatch.setattr( - appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set()) - ) - monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") - - assert cli.main(["--yes", "284160", "--username", "u"]) == 1 - - -def _installable(monkeypatch, library): - monkeypatch.setattr( - appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set()) - ) - monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") - monkeypatch.setattr(depotdl, "download", lambda **kw: 1) - - -def test_declining_the_shutdown_prompt_stops_the_run(cli_steam, monkeypatch): - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) - monkeypatch.setattr(cli, "_is_interactive", lambda args: True) - monkeypatch.setattr( - "builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y" - ) - monkeypatch.setattr( - depotdl, "download", lambda **kw: pytest.fail("must not download") - ) - - assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1 - - -def test_steam_is_closed_and_reopened_around_the_install(cli_steam, monkeypatch): - """It was running before, so it is running after -- whatever the outcome.""" - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) - monkeypatch.setattr(ghost.recognize, "steam_client_pids", lambda: [42]) - events = [] - monkeypatch.setattr( - cli.flow.steamctl, "stop", lambda logger: events.append("stop") or True - ) - monkeypatch.setattr( - cli.flow.steamctl, "start", lambda logger: events.append("start") - ) - _installable(monkeypatch, cli_steam) - - assert cli.main(["--yes", "284160", "--username", "u"]) == 1 - assert events == ["stop", "start"] - - -def test_a_steam_that_refuses_to_exit_aborts_the_run(cli_steam, monkeypatch, capsys): - """Never escalate to a signal: a hung client is the user's to deal with.""" - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) - - def refuse(logger): - raise cli.flow.steamctl.SteamStillRunning("Steam is still running after 60s") - - monkeypatch.setattr(cli.flow.steamctl, "stop", refuse) - monkeypatch.setattr( - depotdl, "download", lambda **kw: pytest.fail("must not download") - ) - - assert cli.main(["--yes", "284160", "--username", "u"]) == 1 - assert "still running" in capsys.readouterr().out - - -def test_accepting_the_shutdown_prompt_closes_and_reopens_steam(cli_steam, monkeypatch): - """The interactive path: asked, answered yes, Steam cycled around the run.""" - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) - monkeypatch.setattr(cli, "_is_interactive", lambda args: True) - monkeypatch.setattr("builtins.input", lambda *a: "y") - events = [] - monkeypatch.setattr( - cli.flow.steamctl, "stop", lambda logger: events.append("stop") or True - ) - monkeypatch.setattr( - cli.flow.steamctl, "start", lambda logger: events.append("start") - ) - _installable(monkeypatch, cli_steam) - - assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1 - assert events == ["stop", "start"] diff --git a/_installer_import/tests/test_deploy.py b/_installer_import/tests/test_deploy.py deleted file mode 100644 index 003210e..0000000 --- a/_installer_import/tests/test_deploy.py +++ /dev/null @@ -1,94 +0,0 @@ -"""scripts/deploy.sh against a fixture checkout instead of kuhy's real one.""" - -from __future__ import annotations - -import os -from pathlib import Path -import subprocess - -import pytest - -DEPLOY = Path(__file__).resolve().parent.parent / "scripts" / "deploy.sh" - -# Stubs stand in for the real run.sh / venv so the test installs nothing. -RUN_SH = '#!/bin/bash\n[[ "$1" == --deps-only ]] && touch .deps-ran\n' -VENV_PY = '#!/bin/bash\n[[ -f "$(dirname "$0")/healthy" ]]\n' - - -def _git(cwd: Path, *args: str) -> str: - """Run git quietly in cwd and return stdout.""" - return subprocess.run( - ["git", "-C", str(cwd), *args], check=True, capture_output=True, text=True - ).stdout.strip() - - -def _commit(repo: Path, name: str, body: str, mode: int = 0o644) -> None: - """Write one file into repo and commit it.""" - path = repo / name - path.write_text(body) - path.chmod(mode) - _git(repo, "add", name) - _git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", name) - - -@pytest.fixture -def checkout(tmp_path: Path) -> tuple[Path, Path]: - """An origin with run.sh and a clone of it that is one commit behind.""" - origin = tmp_path / "origin" - origin.mkdir() - _git(origin, "init", "-q", "-b", "main") - _commit(origin, ".gitignore", ".venv/\n.deps-ran\n") - _commit(origin, "run.sh", RUN_SH, 0o755) - clone = tmp_path / "clone" - subprocess.run(["git", "clone", "-q", str(origin), str(clone)], check=True) - _commit(origin, "new.txt", "new\n") - venv_bin = clone / ".venv" / "bin" - venv_bin.mkdir(parents=True) - (venv_bin / "python").write_text(VENV_PY) - (venv_bin / "python").chmod(0o755) - (venv_bin / "healthy").touch() - return origin, clone - - -def _deploy(clone: Path) -> subprocess.CompletedProcess[str]: - """Run deploy.sh pointed at the fixture clone.""" - env = {**os.environ, "SGI_DEPLOY_DIR": str(clone)} - return subprocess.run( - ["bash", str(DEPLOY)], env=env, capture_output=True, text=True, check=False - ) - - -def test_deploy_fast_forwards_installs_and_checks( - checkout: tuple[Path, Path], -) -> None: - """A clean checkout ends at origin/main with deps run and exit 0.""" - origin, clone = checkout - result = _deploy(clone) - assert result.returncode == 0, result.stderr - assert _git(clone, "rev-parse", "HEAD") == _git(origin, "rev-parse", "HEAD") - assert (clone / ".deps-ran").exists() - assert _deploy(clone).returncode == 0 # idempotent - - -def test_deploy_refuses_dirty_checkout(checkout: tuple[Path, Path]) -> None: - """Local edits in kuhy's checkout are never overwritten.""" - _, clone = checkout - (clone / "run.sh").write_text(RUN_SH + "# local edit\n") - result = _deploy(clone) - assert result.returncode != 0 - assert "local changes" in result.stderr - - -def test_deploy_fails_when_health_check_fails(checkout: tuple[Path, Path]) -> None: - """A CLI that cannot start makes deploy exit non-zero.""" - _, clone = checkout - (clone / ".venv" / "bin" / "healthy").unlink() - result = _deploy(clone) - assert result.returncode != 0 - assert "health" in result.stderr - - -def test_claude_md_declares_deploy() -> None: - """CLAUDE.md names scripts/deploy.sh as the deploy command.""" - claude = DEPLOY.parent.parent / "CLAUDE.md" - assert "- deploy: `scripts/deploy.sh`" in claude.read_text() diff --git a/_installer_import/tests/test_depotdl.py b/_installer_import/tests/test_depotdl.py deleted file mode 100644 index f43cd4e..0000000 --- a/_installer_import/tests/test_depotdl.py +++ /dev/null @@ -1,80 +0,0 @@ -import logging - -from steam_installer import depotdl - - -def _capture(monkeypatch): - """Intercept the subprocess call and hand back the argv it was given.""" - seen = {} - - def fake_run_logged(cmd, logger, timeout=None): - seen["cmd"] = cmd - return seen.get("rc", 0), "" - - monkeypatch.setattr(depotdl.runlog, "run_logged", fake_run_logged) - return seen - - -def test_download_builds_expected_argv(tmp_path, monkeypatch): - seen = _capture(monkeypatch) - - depotdl.download( - appid=1771300, - install_dir=tmp_path / "Game", - username="krzys2000", - logger=logging.getLogger("t"), - language="english", - max_downloads=32, - ) - - cmd = seen["cmd"] - assert cmd[0] == "depotdownloader" - pairs = dict(zip(cmd[1::2], cmd[2::2])) - assert pairs["-app"] == "1771300" - assert pairs["-language"] == "english" - assert pairs["-max-downloads"] == "32" - assert pairs["-username"] == "krzys2000" - - -def test_download_always_passes_os_windows(tmp_path, monkeypatch): - """Without -os, depotdownloader defaults to the host OS and can filter out - every real (Windows-tagged) depot -- the original "0 bytes from 0 depots".""" - seen = _capture(monkeypatch) - - depotdl.download( - appid=1771300, - install_dir=tmp_path / "Game", - username="u", - logger=logging.getLogger("t"), - ) - - cmd = seen["cmd"] - assert cmd[cmd.index("-os") + 1] == "windows" - assert "-remember-password" in cmd - - -def test_download_creates_the_install_dir(tmp_path, monkeypatch): - _capture(monkeypatch) - target = tmp_path / "nested" / "Game" - - depotdl.download( - appid=1, install_dir=target, username="u", logger=logging.getLogger("t") - ) - - assert target.is_dir() - - -def test_download_returns_the_exit_code_instead_of_raising(tmp_path, monkeypatch): - """The caller must distinguish "download failed" from "manifest step failed" - to decide whether touching the manifest is safe at all.""" - seen = _capture(monkeypatch) - seen["rc"] = 1 - - returncode = depotdl.download( - appid=1, - install_dir=tmp_path / "Game", - username="u", - logger=logging.getLogger("t"), - ) - - assert returncode == 1 diff --git a/_installer_import/tests/test_dlc.py b/_installer_import/tests/test_dlc.py deleted file mode 100644 index 342e440..0000000 --- a/_installer_import/tests/test_dlc.py +++ /dev/null @@ -1,199 +0,0 @@ -"""DLC that ships as its own app: discovery, download, and crediting it in the manifest. - -Shapes are taken from the real `app_info_print` of Cyberpunk 2077 (1091500) and -its DLC, 2025-09-25: Phantom Liberty (2138330) keeps its ~25 GB depot under its -own app id; 2060310 is listed in the game's own depots; 2441600 has none. -""" - -import logging - -import pytest -from _cli_helpers import ARGV, _stub_download - -from steam_installer import appinfo, cli, depotdl, deps, dlc, recognize, runlog, vdf - -pytestmark = pytest.mark.usefixtures("cli_logs") - -# Every real depot that has content ships a manifest on the public branch. -_PUB = {"manifests": {"public": {"gid": "1"}}} - -GAME = { - "extended": {"listofdlc": "2060310,2138330,2441600"}, - "depots": { - "1091501": {"config": {"oslist": "windows"}, **_PUB}, - "2060310": {"dlcappid": "2060310", "config": {"oslist": "windows"}, **_PUB}, - }, -} -DLC_BLOCKS = { - # Listed in the game's own depots: the base run already covers it. - 2060310: {"depots": {"2060310": {"dlcappid": "2060310"}}}, - # Its own depot, tagged with its own id -- exactly what it installs. - 2138330: { - "depots": { - "2138330": {"dlcappid": "2138330", "config": {"oslist": "windows"}, **_PUB}, - "2138331": {"config": {"oslist": "windows", "language": "german"}, **_PUB}, - } - }, - 2441600: {"common": {"name": "Pre-Order Bonus"}}, -} - - -def _app_info(monkeypatch, game=GAME, dlc_blocks=DLC_BLOCKS): - monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game) - monkeypatch.setattr( - appinfo, "_cached_blocks", lambda ids: {i: dlc_blocks[i] for i in ids} - ) - - -def test_only_dlc_with_depots_of_its_own_needs_a_run(monkeypatch): - _app_info(monkeypatch) - assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} - - -@pytest.mark.parametrize("game", [{}, {"extended": "scalar"}, {"extended": {}}]) -def test_a_game_without_dlc_needs_no_lookup(monkeypatch, game): - monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game) - monkeypatch.setattr(appinfo, "_cached_blocks", lambda ids: pytest.fail()) - assert appinfo.get_dlc_plans(1) == {} - - -def test_a_game_with_no_depot_list_still_finds_its_dlc(monkeypatch): - _app_info(monkeypatch, game={"extended": {"listofdlc": "2138330"}}) - assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} - - -def test_depot_plan_tags_the_games_own_dlc_depots(monkeypatch): - _app_info(monkeypatch) - plan = appinfo.get_depot_plan(1091500) - assert plan.required == {"1091501"} - assert plan.dlc == {"2060310": "2060310"} - - -def _logged(monkeypatch, returncode, output=""): - monkeypatch.setattr( - depotdl.runlog, "run_logged", lambda cmd, logger: (returncode, output) - ) - - -@pytest.mark.parametrize( - ("returncode", "output", "expected"), - [ - (0, "", True), - (1, "App 2441600 (Bonus) is not available from this account.", None), - (1, "Connection lost", False), - ], -) -def test_download_dlc_separates_not_owned_from_failed( - tmp_path, monkeypatch, returncode, output, expected -): - _logged(monkeypatch, returncode, output) - result = depotdl.download_dlc(2441600, tmp_path, "u", logging.getLogger("t")) - assert result is expected - - -def test_the_managed_depotdownloader_wins_over_path(tmp_path, monkeypatch): - managed = tmp_path / "depotdownloader" - managed.write_text("") - managed.chmod(0o755) - monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", managed) - assert deps.depotdownloader_bin() == str(managed) - - -@pytest.fixture -def run_log(tmp_path, monkeypatch): - monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state") - log = runlog.setup(1091500) - yield log - for handler in list(log.logger.handlers): - log.logger.removeHandler(handler) - handler.close() - - -def _download_owned(monkeypatch, run_log, tmp_path, outcomes): - monkeypatch.setattr( - appinfo, - "get_dlc_plans", - lambda appid, language: {7: {"70": "g70"}, 8: {"80": "g80"}}, - ) - monkeypatch.setattr( - depotdl, "download_dlc", lambda dlc_appid, **kw: outcomes[dlc_appid] - ) - return dlc.download_owned(1, tmp_path, "u", "english", 8, run_log) - - -def test_owned_dlc_is_returned_and_unowned_skipped(monkeypatch, run_log, tmp_path): - plan = _download_owned(monkeypatch, run_log, tmp_path, {7: True, 8: None}) - assert (plan.required, plan.dlc) == ({"70"}, {"70": "7"}) - # Its public gid too, so the finish check can prove the DLC's build. - assert plan.manifests == {"70": "g70"} - - -def test_a_failed_dlc_download_aborts(monkeypatch, run_log, tmp_path): - assert _download_owned(monkeypatch, run_log, tmp_path, {7: False, 8: True}) is None - - -def test_downloaded_dlc_is_required_and_credited_to_its_app(steam, monkeypatch): - library, install_dir = steam - (install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x") - manifest = library.steamapps / "appmanifest_1771300.acf" - state = recognize.read_app_state(manifest) - # Steam's own progress for a DLC download it had started itself. - state["DlcDownloads"] = { - "2138330": {"BytesDownloaded": "1", "BytesToDownload": "9"} - } - manifest.write_text(vdf.dumps("AppState", state)) - _stub_download(monkeypatch) - monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389")) - - assert cli.main(ARGV) == 0 - - state = recognize.read_app_state(manifest) - assert state["InstalledDepots"]["2138330"]["dlcappid"] == "2138330" - assert "dlcappid" not in state["InstalledDepots"]["1771303"] - assert "DlcDownloads" not in state - - -def _dlc_plan(gid): - return appinfo.DepotPlan( - {"2138330"}, set(), {"2138330": "2138330"}, {"2138330": gid} - ) - - -def test_a_stub_with_own_app_dlc_finishes_with_the_proven_build(steam, monkeypatch): - """Base game and its own-app DLC both match the public branch: a build-0 - stub is finished, the DLC's gid coming from the DLC's own app info.""" - library, install_dir = steam - (install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x") - manifest = library.steamapps / "appmanifest_1771300.acf" - manifest.write_text(vdf.dumps("AppState", {"appid": "1771300", "buildid": "0"})) - monkeypatch.setattr( - appinfo, - "get_depot_plan", - lambda appid, language: appinfo.DepotPlan( - {"1771303"}, set(), manifests={"1771303": "1999516201078544527"} - ), - ) - _stub_download(monkeypatch) - monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389")) - - assert cli.main(ARGV) == 0 - assert recognize.read_app_state(manifest)["buildid"] == "23914554" - - -def test_a_failed_dlc_download_leaves_the_manifest_alone(steam, monkeypatch): - library, _ = steam - manifest = library.steamapps / "appmanifest_1771300.acf" - before = recognize.read_app_state(manifest) - _stub_download(monkeypatch) - monkeypatch.setattr(dlc, "download_owned", lambda *a: None) - - assert cli.main(ARGV) == 1 - # Only the folder name is fixed up front; nothing claims an install. - after = recognize.read_app_state(manifest) - assert after.pop("installdir") == "KingdomComeDeliverance2" - assert after == before - - -def test_a_dlc_with_a_malformed_depot_section_is_skipped(monkeypatch): - _app_info(monkeypatch, dlc_blocks={**DLC_BLOCKS, 2441600: {"depots": "scalar"}}) - assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} diff --git a/_installer_import/tests/test_install_sh.py b/_installer_import/tests/test_install_sh.py deleted file mode 100644 index d431b1e..0000000 --- a/_installer_import/tests/test_install_sh.py +++ /dev/null @@ -1,45 +0,0 @@ -"""install.sh --check-only: what a run needs, with steamcmd no longer on the list.""" - -import os -import subprocess -from pathlib import Path - -ROOT = Path(__file__).resolve().parent.parent - - -def _stub(bin_dir: Path, name: str, script: str) -> None: - path = bin_dir / name - path.write_text(f"#!/bin/bash\n{script}\n") - path.chmod(0o755) - - -def _check_only(tmp_path, libibus_installed: bool): - bin_dir = tmp_path / "bin" - bin_dir.mkdir() - _stub(bin_dir, "depotdownloader", "exit 0") - _stub(bin_dir, "pacman", f'[[ "$*" == "-Q libibus" ]] && exit {0 if libibus_installed else 1}; exit 0') - env = {**os.environ, "PATH": f"{bin_dir}:/usr/bin:/bin", "HOME": str(tmp_path)} - return subprocess.run( - ["bash", str(ROOT / "install.sh"), "--check-only"], - capture_output=True, text=True, env=env, timeout=30, check=False, - ) - - -def test_missing_libibus_is_reported_and_steamcmd_is_not_needed(tmp_path): - result = _check_only(tmp_path, libibus_installed=False) - assert result.returncode == 1 - assert "missing: libibus" in result.stderr - assert "steamcmd" not in result.stdout + result.stderr - - -def test_all_present_without_steamcmd(tmp_path): - result = _check_only(tmp_path, libibus_installed=True) - assert result.returncode == 0, result.stderr - assert "All dependencies present." in result.stdout - - -def test_run_sh_rechecks_when_the_proton_pin_changes(): - """A stale stamp must not hide a lock bump or a deleted tool folder.""" - text = (ROOT / "run.sh").read_text() - assert "proton-native.lock" in text - assert "steam_installer.proton_native --check" in text diff --git a/_installer_import/tests/test_map_installed.py b/_installer_import/tests/test_map_installed.py deleted file mode 100644 index 6449a64..0000000 --- a/_installer_import/tests/test_map_installed.py +++ /dev/null @@ -1,197 +0,0 @@ -"""Mapping games to the native Proton: after an install, and for the whole library.""" - -import pytest -from _appinfo_blob import encode -from _cli_helpers import ARGV, _stub_download - -from steam_installer import ( - anticheat, - cli, - compat_apply, - compat_map, - proton_pkg, - recognize, - steamctl, - vdf, -) - -pytestmark = pytest.mark.usefixtures("cli_logs") - -LOCKED = proton_pkg.read_lock().version -CONFIG = '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t}\n\t\t}\n\t}\n}\n' -PLAIN = {"common": {"name": "Plain", "oslist": "windows", "type": "Game"}, - "depots": {"11": {"config": {"oslist": "windows"}}}} -NATIVE = {"common": {"name": "Native", "oslist": "windows,linux", "type": "Game"}} -SHARED = {"common": {"name": "Shared", "oslist": "windows", "type": "Game"}, - "depots": {"31": {"depotfromapp": "228980"}}} -CS = {"common": {"name": "CS", "oslist": "windows,linux", "type": "Game"}} -TOOL = {"common": {"name": "Proton Experimental", "type": "Tool"}} - - -@pytest.fixture -def home(tmp_path, monkeypatch): - """A Steam root with config.vdf, the stamped tool, an app cache and anti-cheat list.""" - root = tmp_path / "Steam" - (root / "config").mkdir(parents=True) - (root / "config" / "config.vdf").write_text(CONFIG) - tool = root / "compatibilitytools.d" / "proton-cachyos-native" - tool.mkdir(parents=True) - (tool / "toolmanifest.vdf").write_text("x") - (tool / "VERSION").write_text(LOCKED + "\n") - cache = tmp_path / "appinfo.vdf" - cache.write_bytes(encode({10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL})) - monkeypatch.setattr(compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache) - monkeypatch.setattr(anticheat, "_download", lambda: b'[{"storeIds": {"steam": "730"}}]') - steamapps = root / "steamapps" - steamapps.mkdir() - for appid in (10, 20, 1493710): - (steamapps / f"appmanifest_{appid}.acf").write_text( - vdf.dumps("AppState", {"appid": str(appid), "InstalledDepots": {}})) - monkeypatch.setattr(cli, "find_steam_root", lambda: root) - return root - - -def _mapped(root, appid): - return compat_map.current(root / "config" / "config.vdf", appid) - - -def test_dry_run_lists_decisions_and_writes_nothing(home, capsys): - before = (home / "config" / "config.vdf").read_bytes() - assert cli.main(["--map-installed"]) == 0 - out = capsys.readouterr().out - assert "10 Plain: map" in out - assert "20 Native: Valve default: native" in out - assert "1493710" not in out # a tool, not a game - assert "Dry run" in out - assert (home / "config" / "config.vdf").read_bytes() == before - - -def test_appids_decides_any_cached_app(home, capsys): - assert cli.main(["--map-installed", "--appids", "730,20,30,10,999"]) == 0 - out = capsys.readouterr().out - assert "730 CS: Valve default: anti-cheat, native" in out - assert "30 Shared: Valve default: shared depots" in out - assert "10 Plain: map" in out - assert "999: not in Steam's app cache" in out - - -def test_apply_maps_the_safe_ones_and_a_rerun_changes_nothing(home, capsys): - assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 - assert _mapped(home, 10) == ("proton-cachyos-native", "250") - assert _mapped(home, 20) is None - once = (home / "config" / "config.vdf").read_bytes() - capsys.readouterr() - assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 - assert "10 Plain: map -- already mapped" in capsys.readouterr().out - assert (home / "config" / "config.vdf").read_bytes() == once - - -def test_apply_closes_steam_first_and_restarts_it(home, monkeypatch): - events = [] - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [] if events else [5]) - monkeypatch.setattr(steamctl, "stop", lambda logger: events.append("stop") or True) - monkeypatch.setattr(steamctl, "start", lambda logger: events.append("start")) - assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 - assert events == ["stop", "start"] - assert _mapped(home, 10) is not None - - -def test_apply_declined_writes_nothing(home, monkeypatch): - monkeypatch.setattr(recognize, "steam_client_pids", lambda: [5]) - monkeypatch.setattr("builtins.input", lambda *a: "n") - assert cli.main(["--map-installed", "--apply"]) == 1 - assert _mapped(home, 10) is None - - -def test_a_kept_choice_and_a_write_error_are_reported(home, monkeypatch, capsys): - config = home / "config" / "config.vdf" - compat_map.map_app(config, 10) - config.write_text(config.read_text().replace("proton-cachyos-native", "GE-Proton10")) - assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 - assert "kept: already mapped to GE-Proton10" in capsys.readouterr().out - config.write_text("garbage {") - assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 - - -def test_owned_dlc_in_the_manifest_keeps_valves_default(home, capsys): - manifest = home / "steamapps" / "appmanifest_10.acf" - manifest.write_text(vdf.dumps("AppState", {"appid": "10", "InstalledDepots": { - "11": {"manifest": "1"}, "12": {"manifest": "2", "dlcappid": "13"}}})) - assert cli.main(["--map-installed"]) == 0 - assert "10 Plain: Valve default: owned DLC" in capsys.readouterr().out - - -def test_valve_proton_only_goes_with_an_install(home): - with pytest.raises(SystemExit): - cli.main(["--map-installed", "--valve-proton"]) - - -def test_an_install_maps_the_game_while_steam_is_closed(steam, monkeypatch, capsys, tmp_path): - library, install_dir = steam - _with_compat_home(library.path, monkeypatch, tmp_path) - _stub_download(monkeypatch) - monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) - _proven(monkeypatch, install_dir) - assert cli.main(ARGV) == 0 - assert _mapped(library.path, 1771300) == ("proton-cachyos-native", "250") - assert "mapped to proton-cachyos-native" in capsys.readouterr().out - - -def test_valve_proton_keeps_valves_default_for_one_run(steam, monkeypatch, capsys, tmp_path): - library, install_dir = steam - _with_compat_home(library.path, monkeypatch, tmp_path) - _stub_download(monkeypatch) - monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) - _proven(monkeypatch, install_dir) - assert cli.main([*ARGV, "--valve-proton"]) == 0 - assert _mapped(library.path, 1771300) is None - assert "Valve default: override: --valve-proton" in capsys.readouterr().out - - -def test_a_mapping_failure_leaves_the_install_on_valves_default(steam, monkeypatch, capsys, tmp_path): - """The install itself succeeded; the game still runs on Valve's Proton.""" - library, install_dir = steam - _with_compat_home(library.path, monkeypatch, tmp_path) - (library.path / "config" / "config.vdf").write_text("broken {") - _stub_download(monkeypatch) - monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) - _proven(monkeypatch, install_dir) - assert cli.main(ARGV) == 0 - assert "stays on Valve's default" in capsys.readouterr().out - - -def test_an_owned_dlc_download_keeps_valves_default(steam, monkeypatch, capsys, tmp_path): - library, install_dir = steam - _with_compat_home(library.path, monkeypatch, tmp_path) - _stub_download(monkeypatch) - monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) - _proven(monkeypatch, install_dir, dlc={"1771303": "9"}) - assert cli.main(ARGV) == 0 - assert "Valve default: owned DLC" in capsys.readouterr().out - - -def _with_compat_home(root, monkeypatch, tmp_path): - (root / "config").mkdir(parents=True, exist_ok=True) - (root / "config" / "config.vdf").write_text(CONFIG) - tool = root / "compatibilitytools.d" / "proton-cachyos-native" - tool.mkdir(parents=True) - (tool / "toolmanifest.vdf").write_text("x") - (tool / "VERSION").write_text(LOCKED + "\n") - monkeypatch.setattr(anticheat, "_download", lambda: b"[]") - - -def _proven(monkeypatch, install_dir, dlc=None): - (gid,) = recognize.discover_cached_depot_manifests(install_dir).values() - plan = compat_apply.appinfo.DepotPlan({"1771303"}, set(), manifests={"1771303": gid}, - dlc=dict(dlc or {})) - monkeypatch.setattr(compat_apply.appinfo, "get_depot_plan", lambda appid, language: plan) - - -def test_a_failed_write_stops_the_run(home, monkeypatch, capsys): - def boom(config, appid): - raise compat_map.CompatMapError("config.vdf line 3: unexpected layout") - - monkeypatch.setattr(compat_map, "map_app", boom) - (home / "steamapps" / "appmanifest_notanid.acf").write_text("x") - assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 - assert "Mapping stopped: config.vdf line 3" in capsys.readouterr().out diff --git a/_installer_import/tests/test_proton_native.py b/_installer_import/tests/test_proton_native.py deleted file mode 100644 index 6272089..0000000 --- a/_installer_import/tests/test_proton_native.py +++ /dev/null @@ -1,211 +0,0 @@ -"""Fetch, verify and install the native Proton, never running its .INSTALL.""" - -import os -import shutil - -import pytest -from _proton_fixture import ( - FILES, - VERSION, - build_db, - build_package, - build_repo, - gpg, - stop_agent, -) - -from steam_installer import proton_native, proton_pkg - - -@pytest.fixture(scope="module") -def repo(tmp_path_factory): - built = build_repo(tmp_path_factory.mktemp("repo")) - yield built - stop_agent(built.gpg_home) - - -@pytest.fixture -def mirror(repo, tmp_path): - """A private copy of the fake mirror a test may tamper with.""" - root = tmp_path / "mirror" - shutil.copytree(repo.root, root) - return root - - -@pytest.fixture -def setup(repo, mirror, tmp_path, monkeypatch): - tools = tmp_path / "compatibilitytools.d" - tools.mkdir() - lock = proton_pkg.Lock( - "proton-cachyos-native", (mirror / "cachyos.db").as_uri(), VERSION, repo.sha256 - ) - monkeypatch.setattr(proton_native, "in_use", lambda tool: []) - - def run(**kw): - return proton_native.ensure( - tools, lock, key=repo.key, fingerprint=repo.fingerprint, **kw - ) - - return tools, lock, run - - -def _tree(tool): - return sorted(str(p.relative_to(tool)) for p in tool.rglob("*")) - - -def test_a_fresh_install_extracts_only_the_tool_and_stamps_it(setup): - tools, _, run = setup - status = run() - tool = tools / "proton-cachyos-native" - assert status.startswith("installed") - assert "signature" in status and "sha256" in status - assert (tool / "VERSION").read_text() == VERSION + "\n" - assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] - assert os.readlink(tool / "files/bin/msidb") == "wine" - assert not (tools / ".INSTALL").exists() - assert not any(".INSTALL" in p or "stray" in p for p in _tree(tools)) - assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] - - -def test_a_current_stamp_needs_no_network(setup, monkeypatch): - _, _, run = setup - run() - monkeypatch.setattr(proton_native, "_download", lambda url, dest: pytest.fail()) - assert run() == f"current: {VERSION}" - - -def _assert_fails_untouched(run, tools, match): - old = tools / "proton-cachyos-native" - old.mkdir(exist_ok=True) - (old / "keep").write_text("old build") - with pytest.raises(proton_pkg.PkgError, match=match): - run() - assert (old / "keep").read_text() == "old build" - assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] - - -def test_a_tampered_signature_fails_closed(setup, mirror): - tools, _, run = setup - sig = next(mirror.glob("*.sig")) - data = bytearray(sig.read_bytes()) - data[-5] ^= 0xFF - sig.write_bytes(bytes(data)) - _assert_fails_untouched(run, tools, "signature") - - -def test_a_signature_from_another_key_fails_closed(setup, mirror, tmp_path): - tools, _, run = setup - other = tmp_path / "other" - other.mkdir(mode=0o700) - gpg(other, "--quick-gen-key", "Mallory ", "ed25519", "sign", "never") - package = next(mirror.glob("*.pkg.tar.zst")) - (mirror / f"{package.name}.sig").unlink() - gpg(other, "--output", f"{package}.sig", "--detach-sign", str(package)) - stop_agent(other) - _assert_fails_untouched(run, tools, "signature") - - -def test_a_package_that_differs_from_the_pinned_sha256_fails_closed(setup, mirror, repo): - tools, _, run = setup - package = next(mirror.glob("*.pkg.tar.zst")) - build_package(package, {**FILES, "proton": b"evil"}) - gpg(repo.gpg_home, "--yes", "--output", f"{package}.sig", "--detach-sign", str(package)) - _assert_fails_untouched(run, tools, "sha256") - - -def test_a_db_sha256_that_disagrees_with_the_lock_fails_closed(setup, mirror): - tools, _, run = setup - build_db(mirror / "cachyos.db", VERSION, "0" * 64) - _assert_fails_untouched(run, tools, "sha256") - - -def test_a_newer_package_in_the_repo_asks_for_a_lock_bump(setup, mirror, repo): - tools, _, run = setup - build_db(mirror / "cachyos.db", "1:11.0.20261101-1", repo.sha256) - _assert_fails_untouched(run, tools, "bump") - - -def test_a_repo_without_the_package_fails(setup, mirror, repo): - tools, _, run = setup - build_db(mirror / "cachyos.db", VERSION, repo.sha256, name="something-else") - _assert_fails_untouched(run, tools, "no proton-cachyos-native") - - -def _existing_copy(tools): - tool = tools / "proton-cachyos-native" - for name, data in FILES.items(): - (tool / name).parent.mkdir(parents=True, exist_ok=True) - (tool / name).write_bytes(data) - os.symlink("wine", tool / "files/bin/msidb") - (tool / "__pycache__").mkdir() - (tool / "__pycache__" / "x.pyc").write_bytes(b"made by running it") - return tool - - -def test_an_identical_copy_is_adopted_without_extracting(setup, monkeypatch): - tools, _, run = setup - tool = _existing_copy(tools) - monkeypatch.setattr(proton_pkg, "extract", lambda *a: pytest.fail("extracted")) - status = run() - assert status.startswith(f"adopted: all {len(FILES) + 1} files match .MTREE (signature") - assert (tool / "VERSION").read_text() == VERSION + "\n" - - -@pytest.mark.parametrize( - ("change", "where"), - [ - (lambda t: (t / "files/bin/wine").write_bytes(b"patched"), "files/bin/wine"), - (lambda t: (t / "user_settings.py").write_text("x"), "user_settings.py"), - (lambda t: (t / "proton").unlink(), "proton"), - (lambda t: (t / "files/bin/msidb").unlink() or os.symlink("x", t / "files/bin/msidb"), "msidb"), - (lambda t: (t / "files/bin/msidb").unlink(), "msidb"), - ], -) -def test_a_differing_copy_is_replaced(setup, change, where): - tools, _, run = setup - tool = _existing_copy(tools) - change(tool) - status = run() - assert status.startswith("replaced: mismatch in") and where in status - assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] - assert not (tool / "user_settings.py").exists() - assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] - - -def test_replacing_is_deferred_while_steam_or_a_game_runs(setup, monkeypatch): - tools, _, run = setup - tool = _existing_copy(tools) - (tool / "proton").write_text("old") - monkeypatch.setattr(proton_native, "in_use", lambda t: [4242]) - status = run() - assert status.startswith("deferred") - assert (tool / "proton").read_text() == "old" - assert not (tool / "VERSION").exists() - - -def test_an_older_stamp_is_updated(setup): - tools, _, run = setup - tool = _existing_copy(tools) - (tool / "VERSION").write_text("1:10.0-1\n") - assert run().startswith("updated: 1:10.0-1 ->") - assert (tool / "VERSION").read_text() == VERSION + "\n" - - -def test_in_use_sees_steam_and_processes_running_from_the_tool(tmp_path, monkeypatch): - proc = tmp_path / "proc" - tool = tmp_path / "tool" - (tool / "files/bin").mkdir(parents=True) - (tool / "files/bin/wine").write_text("") - (tmp_path / "other").write_text("") - for pid, target in ( - ("10", tool / "files/bin/wine"), - ("11", tmp_path / "other"), - ("13", tmp_path / "gone"), - ): - (proc / pid).mkdir(parents=True) - os.symlink(target, proc / pid / "exe") - (proc / "self").mkdir() - (proc / "12").mkdir() - monkeypatch.setattr(proton_native.recognize, "PROC_DIR", proc) - monkeypatch.setattr(proton_native.recognize, "steam_client_pids", lambda: [99]) - assert proton_native.in_use(tool) == [99, 10] diff --git a/_installer_import/tests/test_proton_native_cli.py b/_installer_import/tests/test_proton_native_cli.py deleted file mode 100644 index e80dee7..0000000 --- a/_installer_import/tests/test_proton_native_cli.py +++ /dev/null @@ -1,81 +0,0 @@ -"""The proton_native entry point: what install.sh and run.sh call.""" - -import subprocess -import sys - -from steam_installer import proton_native, proton_pkg - - -def test_main_reports_and_exits(monkeypatch, capsys, tmp_path): - monkeypatch.setattr(proton_native, "ensure", lambda *a, **kw: "current: x") - monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) - assert proton_native.main([]) == 0 - assert "current: x" in capsys.readouterr().out - - def boom(*a, **kw): - raise proton_pkg.PkgError("bad signature") - - monkeypatch.setattr(proton_native, "ensure", boom) - assert proton_native.main([]) == 1 - assert "bad signature" in capsys.readouterr().err - - -def test_check_reports_whether_the_stamp_matches_the_lock(tmp_path, monkeypatch): - monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) - assert proton_native.main(["--check"]) == 1 - tool = tmp_path / "compatibilitytools.d" / "proton-cachyos-native" - tool.mkdir(parents=True) - (tool / "VERSION").write_text(proton_pkg.read_lock().version + "\n") - assert proton_native.main(["--check"]) == 0 - - -def test_no_steam_installation_means_nothing_to_install(monkeypatch, capsys): - def absent(): - raise FileNotFoundError("No Steam installation found") - - monkeypatch.setattr(proton_native, "find_steam_root", absent) - assert proton_native.main([]) == 0 - assert proton_native.main(["--check"]) == 0 - assert "skipped" in capsys.readouterr().out - - -def test_the_repo_can_be_pointed_at_a_test_mirror(monkeypatch, tmp_path, capsys): - seen = {} - monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) - monkeypatch.setenv("SGI_PROTON_REPO", "file:///mirror/cachyos.db") - monkeypatch.setattr(proton_native, "ensure", lambda d, lock: seen.setdefault("repo", lock.repo)) - assert proton_native.main([]) == 0 - assert seen["repo"] == "file:///mirror/cachyos.db" - - -def test_module_runs_as_a_script(tmp_path, coverage_subprocess_env): - env = {**coverage_subprocess_env, "HOME": str(tmp_path)} - result = subprocess.run( - [sys.executable, "-m", "steam_installer.proton_native", "--check"], - capture_output=True, text=True, env=env, timeout=30, check=False, - ) - assert result.returncode == 0 - assert "skipped" in result.stdout - - -def test_downloads_send_a_user_agent(monkeypatch, tmp_path): - """The CachyOS mirror answers 403 to urllib's default agent (2026-11-22).""" - import io - - seen = {} - - class _Response(io.BytesIO): - def __enter__(self): - return self - - def __exit__(self, *a): - return False - - def fake_urlopen(request, timeout): - seen["agent"] = request.get_header("User-agent") - return _Response(b"data") - - monkeypatch.setattr(proton_native.urllib.request, "urlopen", fake_urlopen) - proton_native._download("https://m/x", tmp_path / "x") - assert (tmp_path / "x").read_bytes() == b"data" - assert seen["agent"] == "steam-game-installer" diff --git a/install.sh b/install.sh index 6320397..b1a9faf 100755 --- a/install.sh +++ b/install.sh @@ -32,6 +32,21 @@ else echo "(The rest of this tool is unaffected.)" fi +# The Steam game installer (steam_installer, in this repo) fast-installs the +# games the daemon assigns: depotdownloader, kuhy-credit and the native Proton. +# Runs as the desktop user (it writes ~/.local and Steam's compatibilitytools.d) +# and is not fatal: without it the daemon falls back to Steam's own download. +echo +echo "Installing the Steam game installer's dependencies..." +INSTALLER_DEPS="$SCRIPT_DIR/steam_installer/install_deps.sh" +if [[ $EUID -eq 0 && -n ${SUDO_USER:-} ]]; then + sudo -u "$SUDO_USER" bash "$INSTALLER_DEPS" \ + || echo "Warning: installer dependencies incomplete (Steam's own download is used)." +else + bash "$INSTALLER_DEPS" \ + || echo "Warning: installer dependencies incomplete (Steam's own download is used)." +fi + # Install the web-UI user unit. No root needed: it serves read-only views and # needs no privilege. Automated rather than documented, because the whole # reason this unit exists is that a hand-started server outlived its own code. diff --git a/pyproject.toml b/pyproject.toml index 84c2575..9846747 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "steam-backlog-enforcer" version = "1.0.0" -description = "Enforce your Steam backlog: one game at a time, tracked with HowLongToBeat data" +description = "Enforce your Steam backlog: one game at a time, tracked with HowLongToBeat data. Also ships steam_installer, a fast Steam game installer." # 3.11 floor: websockets 17 (and its successors) drop 3.10, and this repo # tracks newest stable rather than the oldest interpreter still alive. requires-python = ">=3.14" @@ -21,8 +21,16 @@ dependencies = [ # contract C4); stdlib-only, imported by root from the user site like # earned-time. "leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9", + # kuhy's shared "Created by kuhy" credit, shown at the end of the + # installer's --help (steam_installer). + "kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit", ] +# The installer's console script; the daemon itself runs +# `python3 -m steam_installer.cli` from the checkout. +[project.scripts] +steam-game-installer = "steam_installer.cli:main" + # Optional extras. The `mcp` group is ONLY needed by the MCP server # (steam_backlog_enforcer._mcp), which Claude Code spawns via a dedicated # venv (see scripts/setup_mcp.sh / .mcp.json). It is deliberately NOT a core @@ -37,7 +45,12 @@ mcp = ["mcp==2.3.0"] # to guess. Scoping discovery to the Python package fixes `pip install -e .` # for the CLI, systemd, AND the dedicated MCP venv. [tool.setuptools.packages.find] -include = ["steam_backlog_enforcer*"] +include = ["steam_backlog_enforcer*", "steam_installer*"] + +[tool.setuptools.package-data] +# The pinned Proton lock, its signing key and the compat overrides are read +# at run time through `Path(__file__).parent / "data"`. +steam_installer = ["data/*"] # ============================================================================ # RUFF - Fast Python linter and formatter @@ -79,6 +92,13 @@ unfixable = [] # libX11 puts local display sockets in /tmp/.X11-unix; the path is not ours to # move to a private temp dir (and `# noqa` is banned by pre-commit). "steam_backlog_enforcer/_desktop_env.py" = ["S108"] +# open_https() allows only https URLs: the scheme is checked just above the +# urlopen call, so the S310 "file:/custom scheme" worry cannot occur there. +"steam_installer/_http.py" = ["S310"] +# The pinned Proton package is fetched from lock.repo or SGI_PROTON_REPO (an +# operator-set local mirror, file:// or http://); the db is sha256-pinned and +# the package sha256- and gpg-verified, so the transport needs no restriction. +"steam_installer/proton_native.py" = ["S310"] [tool.ruff.lint.pydocstyle] convention = "google" @@ -86,7 +106,7 @@ convention = "google" [tool.ruff.lint.isort] force-single-line = false force-sort-within-sections = true -known-first-party = ["steam_backlog_enforcer"] +known-first-party = ["steam_backlog_enforcer", "steam_installer"] [tool.ruff.lint.flake8-quotes] docstring-quotes = "double" @@ -205,7 +225,7 @@ exclude_dirs = ["tests", ".venv"] # PYTEST # ============================================================================ [tool.pytest.ini_options] -testpaths = ["steam_backlog_enforcer/tests"] +testpaths = ["steam_backlog_enforcer/tests", "steam_installer/tests"] python_files = ["test_*.py", "*_test.py"] python_classes = ["Test*"] python_functions = ["test_*"] @@ -215,6 +235,7 @@ addopts = [ "--strict-config", "-ra", "--cov=steam_backlog_enforcer", + "--cov=steam_installer", "--cov-branch", "--cov-report=term-missing", "--cov-report=lcov", @@ -229,7 +250,7 @@ filterwarnings = [ # COVERAGE # ============================================================================ [tool.coverage.run] -source = ["steam_backlog_enforcer"] +source = ["steam_backlog_enforcer", "steam_installer"] branch = true omit = ["*/__pycache__/*", "*/tests/*", "*/.venv/*"] diff --git a/requirements.txt b/requirements.txt index 026ea1a..41b8f64 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -# Steam Backlog Enforcer — runtime + development dependencies +# Steam Backlog Enforcer (and steam_installer) — runtime + development dependencies # Install with: pip install -r requirements.txt aiohttp==3.14.5 @@ -7,6 +7,7 @@ codespell==2.4.3 coverage==7.17.4 earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time howlongtobeatpy==1.0.24 +kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9 mcp==2.3.0 mypy==2.4.1 diff --git a/scripts/check_dependency_freshness.sh b/scripts/check_dependency_freshness.sh index 49d6e29..71c197f 100755 --- a/scripts/check_dependency_freshness.sh +++ b/scripts/check_dependency_freshness.sh @@ -18,7 +18,57 @@ set -euo pipefail -readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_dependency_freshness.sh" +readonly UTILS="${UTILS_ROOT:-$HOME/src/utils}" +readonly SHARED_GATE="$UTILS/scripts/check_dependency_freshness.sh" +readonly PACMAN_PARSER="$UTILS/dep_freshness/parsers/pacman_lock.py" + +# True when this run must judge the native-Proton lock (a full run, or the lock staged). +needs_pacman_adapter() { + local arg + for arg in "$@"; do + [[ "$arg" == "--all" || "$arg" == *proton-native.lock ]] && return 0 + done + return 1 +} + +# git against the utils checkout. A pre-commit run inherits the committing +# repo's git location variables, which would point these commands at the wrong +# repository, so they are cleared first. +utils_git() { + env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE -u GIT_COMMON_DIR \ + -u GIT_PREFIX git -C "$UTILS" "$@" +} + +# Print a gate directory that can read the Proton lock, or nothing. +# +# kuhy's ~/src/utils can lag origin/main (local commits not yet pushed, so it +# cannot fast-forward). Then the gate runs a read-only snapshot of utils' +# origin/main from the cache instead: the checkout itself is never touched. +gate_root_for_lock() { + if [[ -f "$PACMAN_PARSER" ]]; then + echo "$UTILS" + return + fi + local sha snapshot + local -a paths=(scripts/check_dependency_freshness.sh dep_freshness) + utils_git cat-file -e origin/main:dep_freshness/parsers/pacman_lock.py 2>/dev/null || + return 0 + sha="$(utils_git rev-parse origin/main)" + snapshot="${XDG_CACHE_HOME:-$HOME/.cache}/steam-game-installer/utils-gate/$sha" + if [[ ! -f "$snapshot/dep_freshness/parsers/pacman_lock.py" ]]; then + rm -rf "$snapshot.tmp" + mkdir -p "$snapshot.tmp" + # The fleet allowlist lives beside the gate, when utils has one. + if utils_git cat-file -e origin/main:dependency-freshness.allowlist.yaml 2>/dev/null; then + paths+=(dependency-freshness.allowlist.yaml) + fi + utils_git archive origin/main "${paths[@]}" | tar -x -C "$snapshot.tmp" + rm -rf "$snapshot" + mv "$snapshot.tmp" "$snapshot" + fi + echo "dependency-freshness: $UTILS lacks the pacman adapter; using its origin/main ${sha:0:12}" >&2 + echo "$snapshot" +} main() { if [[ ! -x "$SHARED_GATE" ]]; then @@ -28,7 +78,20 @@ main() { exit 1 fi - exec bash "$SHARED_GATE" "$@" + local root="$UTILS" + # An older utils checkout skips a manifest it has no parser for and exits + # 0, which would wave any Proton pin through. Fail closed instead. + if needs_pacman_adapter "$@"; then + root="$(gate_root_for_lock)" + if [[ -z "$root" ]]; then + echo "Error: $UTILS has no dep_freshness/parsers/pacman_lock.py, so it" >&2 + echo " cannot check steam_installer/data/proton-native.lock." >&2 + echo " Update it: git -C $UTILS pull --ff-only" >&2 + exit 1 + fi + fi + + exec bash "$root/scripts/check_dependency_freshness.sh" "$@" } main "$@" diff --git a/scripts/check_requirements_sync.py b/scripts/check_requirements_sync.py index b768844..0132cec 100755 --- a/scripts/check_requirements_sync.py +++ b/scripts/check_requirements_sync.py @@ -96,14 +96,21 @@ def _top_level_imports(module: Path) -> set[str]: def _third_party_imports(package_dir: Path, package_name: str) -> set[str]: """Return third-party top-level imports across the whole package. - Standard-library modules (via ``sys.stdlib_module_names``) and the package's - own name are filtered out, leaving only external dependencies. + Standard-library modules (via ``sys.stdlib_module_names``), the package's own + name and the repo's sibling packages (``steam_backlog_enforcer`` imports + ``steam_installer`` from the same checkout) are filtered out, leaving only + external dependencies. """ stdlib = sys.stdlib_module_names + first_party = { + sibling.name + for sibling in package_dir.parent.iterdir() + if (sibling / "__init__.py").is_file() + } | {package_name} external: set[str] = set() for module in _iter_package_modules(package_dir): for name in _top_level_imports(module): - if name != package_name and name not in stdlib: + if name not in first_party and name not in stdlib: external.add(name) return external diff --git a/steam_backlog_enforcer/_appinfo_cache.py b/steam_backlog_enforcer/_appinfo_cache.py index 9224cde..1748214 100644 --- a/steam_backlog_enforcer/_appinfo_cache.py +++ b/steam_backlog_enforcer/_appinfo_cache.py @@ -16,23 +16,17 @@ from __future__ import annotations import struct from typing import TYPE_CHECKING, Final +from steam_installer.appinfo_kv import HEADER, MAGIC, read_block, string_table + if TYPE_CHECKING: from collections.abc import Iterable from pathlib import Path from steam_backlog_enforcer._vdf_text import VdfNode -MAGIC: Final = 0x07564429 # "SteamPlay 2.0 Manifests": its extended/app_mappings is Valve's per-game # tool pick (e.g. a broken native port sent to Proton). STEAM_PLAY_APP: Final = 891390 -_HEADER: Final = 60 -# KV type bytes: a nested block, a NUL-terminated string, end of block. -_BLOCK: Final = 0 -_STRING: Final = 1 -_END: Final = 8 -# Fixed-width value types: KV type byte -> struct format. -_SCALARS: Final = {2: " Path: @@ -60,7 +54,7 @@ def _parse(buf: bytes, wanted: set[int]) -> dict[int, dict[str, VdfNode]]: if magic != MAGIC: msg = f"unsupported appinfo.vdf format {magic:#010x}" raise ValueError(msg) - strings = _string_table(buf, table_offset) + strings = string_table(buf, table_offset) apps: dict[int, dict[str, VdfNode]] = {} pos = 16 while True: @@ -68,44 +62,7 @@ def _parse(buf: bytes, wanted: set[int]) -> dict[int, dict[str, VdfNode]]: if app_id == 0: return apps if app_id in wanted: - data, _ = _kv(buf, pos + 8 + _HEADER, strings) + data, _ = read_block(buf, pos + 8 + HEADER, strings) inner = data.get("appinfo") apps[app_id] = inner if isinstance(inner, dict) else data pos += 8 + size - - -def _string_table(buf: bytes, offset: int) -> list[str]: - (count,) = struct.unpack_from(" tuple[dict[str, VdfNode], int]: - out: dict[str, VdfNode] = {} - while True: - kind = buf[pos] - pos += 1 - if kind == _END: - return out, pos - (index,) = struct.unpack_from(" Path | None: - """steam-game-installer's ``run.sh`` in the desktop user's home, if present. +def installer_path() -> Path | None: + """The installer's ``cli.py`` inside this checkout, if present. - Resolved from the passwd entry rather than ``Path.home()``: the daemon runs - as root, and the installer, its venv and its credentials are per-user. + A checkout is the same for every user, so unlike the old external clone + there is no per-user lookup. """ - try: - home = Path(pwd.getpwnam(user).pw_dir) if user else Path.home() - except KeyError: - return None - path = home / _INSTALLER - return path if path.is_file() else None + return _INSTALLER if _INSTALLER.is_file() else None def steam_account_name() -> str | None: @@ -51,11 +52,17 @@ def steam_account_name() -> str | None: def build_cmd(installer: Path, app_id: int, user: str | None) -> list[str]: - """The installer invocation, dropped to the desktop user when root.""" + """The installer invocation, dropped to the desktop user when root. + + Launched as ``python3 -m steam_installer.cli`` with the checkout on + ``PYTHONPATH``: ``sudo`` resets the environment, so it is named in the argv. + """ cmd = [ - str(installer), - # install.sh reaches for yay; dependencies are installed once, by hand. - "--no-deps", + "env", + f"PYTHONPATH={installer.parents[1]}", + _PYTHON, + "-m", + "steam_installer.cli", "--appid", str(app_id), "--yes", diff --git a/steam_backlog_enforcer/_steam_restart_guard.py b/steam_backlog_enforcer/_steam_restart_guard.py index 49ba6f3..5543f69 100644 --- a/steam_backlog_enforcer/_steam_restart_guard.py +++ b/steam_backlog_enforcer/_steam_restart_guard.py @@ -70,10 +70,11 @@ def assert_safe_to_restart() -> None: # Matched against /proc cmdlines, so a run started by hand counts exactly like -# one the enforcer spawned. run.sh as well as the module it execs: the sudo -# and bash wrappers exist the moment Popen returns, the Python process only a -# moment later, and a Steam launch in that gap would race the shutdown. -_INSTALLER_MARKERS = (b"steam-game-installer/run.sh", b"steam_installer.cli") +# one the enforcer spawned. The module name is in the argv of every wrapper +# too (``sudo``, ``env``), which exist the moment Popen returns while the +# Python process follows a moment later, so a Steam launch cannot slip into +# that gap. ``run.sh`` is the by-hand launcher that execs the same module. +_INSTALLER_MARKERS = (b"steam_installer/run.sh", b"steam_installer.cli") def fast_install_running() -> bool: diff --git a/steam_backlog_enforcer/tests/_no_fast_install.py b/steam_backlog_enforcer/tests/_no_fast_install.py index 94f2053..9351b19 100644 --- a/steam_backlog_enforcer/tests/_no_fast_install.py +++ b/steam_backlog_enforcer/tests/_no_fast_install.py @@ -46,9 +46,10 @@ def _no_fast_install(tmp_path: Path) -> Iterator[None]: _FAILURES.clear() patches = [patch(name, return_value=False) for name in _RUNNING_BINDINGS] patches += [ - # Absolute, so `home / _INSTALLER` resolves here and never under ~. + # The installer is in this repo now, so it always exists: point the + # constant at a file that does not, or a test could close real Steam. patch.object( - _fast_install_cmd, "_INSTALLER", tmp_path / "no-installer" / "run.sh" + _fast_install_cmd, "_INSTALLER", tmp_path / "no-installer" / "cli.py" ), patch.object(_fast_install_cmd, "STEAMAPPS_PATH", fake_steamapps), ] diff --git a/steam_backlog_enforcer/tests/_no_real_effects.py b/steam_backlog_enforcer/tests/_no_real_effects.py index d199152..3147994 100644 --- a/steam_backlog_enforcer/tests/_no_real_effects.py +++ b/steam_backlog_enforcer/tests/_no_real_effects.py @@ -101,7 +101,7 @@ def _drives_steam(argv: Sequence[str]) -> bool: for arg in argv: if PurePath(arg).name in _DENIED_TOOLS: return True - if arg.startswith("steam://") or arg == "-shutdown": + if arg.startswith("steam://") or arg in {"-shutdown", "steam_installer.cli"}: return True return False diff --git a/steam_backlog_enforcer/tests/test_fast_install_guards.py b/steam_backlog_enforcer/tests/test_fast_install_guards.py index 0af249d..6b2bf83 100644 --- a/steam_backlog_enforcer/tests/test_fast_install_guards.py +++ b/steam_backlog_enforcer/tests/test_fast_install_guards.py @@ -60,12 +60,15 @@ class TestFastInstallRunning: @pytest.mark.parametrize( "cmdline", [ - b"bash\0/home/u/src/steam-game-installer/run.sh\0--appid\0001", + ( + b"sudo\0-u\0u\0env\0PYTHONPATH=/r\0/usr/bin/python3\0" + b"-m\0steam_installer.cli\0--appid\0001" + ), b"python\0-m\0steam_installer.cli\0--appid\0001", ], ) def test_detects_installer(self, tmp_path: Path, cmdline: bytes) -> None: - """Both the run.sh wrapper and the Python module count as the installer.""" + """The sudo wrapper and the Python module both count as the installer.""" proc = self._proc(tmp_path, {"self": b"", "10": None, "11": cmdline}) with patch(f"{GUARD}.Path", return_value=proc): assert fast_install_running() is True diff --git a/steam_backlog_enforcer/tests/test_fast_install_helpers.py b/steam_backlog_enforcer/tests/test_fast_install_helpers.py index cb53026..ccc011f 100644 --- a/steam_backlog_enforcer/tests/test_fast_install_helpers.py +++ b/steam_backlog_enforcer/tests/test_fast_install_helpers.py @@ -7,7 +7,6 @@ to keep both files under the 250-line cap. from __future__ import annotations from pathlib import Path -from types import SimpleNamespace from unittest.mock import MagicMock, patch from steam_backlog_enforcer import _fast_install, _fast_install_cmd, _steam_state @@ -17,41 +16,6 @@ PKG = "steam_backlog_enforcer._fast_install" CMD = "steam_backlog_enforcer._fast_install_cmd" -class TestInstallerPath: - """run.sh is resolved from the desktop user's passwd home.""" - - def test_found_in_user_home(self, tmp_path: Path) -> None: - """run.sh is found under the desktop user's passwd home.""" - installer = tmp_path / "src" / "steam-game-installer" / "run.sh" - installer.parent.mkdir(parents=True) - installer.write_text("#!/bin/bash\n") - # The autouse guard points _INSTALLER at a dead absolute path; put the - # real relative one back so the home join itself is what is tested. - relative = Path("src", "steam-game-installer", "run.sh") - with ( - patch.object(_fast_install_cmd, "_INSTALLER", relative), - patch(f"{CMD}.pwd.getpwnam", return_value=SimpleNamespace(pw_dir=tmp_path)), - ): - assert _fast_install_cmd.installer_path("kuhy") == installer - - def test_missing_file_is_none(self, tmp_path: Path) -> None: - """A home without run.sh yields no installer.""" - with patch( - f"{CMD}.pwd.getpwnam", return_value=SimpleNamespace(pw_dir=tmp_path) - ): - assert _fast_install_cmd.installer_path("kuhy") is None - - def test_unknown_user_is_none(self) -> None: - """A user unknown to passwd yields no installer.""" - with patch(f"{CMD}.pwd.getpwnam", side_effect=KeyError("nobody")): - assert _fast_install_cmd.installer_path("nobody") is None - - def test_no_user_uses_own_home(self, tmp_path: Path) -> None: - """With no user the process's own home is searched.""" - with patch(f"{CMD}.Path.home", return_value=tmp_path): - assert _fast_install_cmd.installer_path(None) is None - - class TestSteamAccountName: """The username comes from Steam's own loginusers.vdf.""" @@ -105,10 +69,15 @@ class TestBuildCmd: patch(f"{CMD}.steam_account_name", return_value="krzys"), patch(f"{CMD}.desktop_user_cmd", side_effect=lambda cmd, _u: cmd), ): - cmd = _fast_install_cmd.build_cmd(Path("/i/run.sh"), 42, "kuhy") + cmd = _fast_install_cmd.build_cmd( + Path("/r/steam_installer/cli.py"), 42, "kuhy" + ) assert cmd == [ - "/i/run.sh", - "--no-deps", + "env", + "PYTHONPATH=/r", + "/usr/bin/python3", + "-m", + "steam_installer.cli", "--appid", "42", "--yes", @@ -124,9 +93,9 @@ class TestBuildCmd: patch(f"{CMD}.steam_account_name", return_value=None), patch(f"{CMD}.desktop_user_cmd", return_value=["wrapped"]) as wrap, ): - assert _fast_install_cmd.build_cmd(Path("/i/run.sh"), 42, "kuhy") == [ - "wrapped" - ] + assert _fast_install_cmd.build_cmd( + Path("/r/steam_installer/cli.py"), 42, "kuhy" + ) == ["wrapped"] argv, user = wrap.call_args.args assert "--username" not in argv assert user == "kuhy" diff --git a/steam_backlog_enforcer/tests/test_fast_install_launcher.py b/steam_backlog_enforcer/tests/test_fast_install_launcher.py new file mode 100644 index 0000000..8b6a238 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_fast_install_launcher.py @@ -0,0 +1,87 @@ +"""The installer ships in this repo: where it is, and the argv that starts it. + +The daemon is root and the installer runs as the desktop user, so every piece +of the command line must survive ``sudo``: the interpreter, ``PYTHONPATH`` and +a flag set the installer's own parser accepts. A flag the parser rejects makes +argparse exit 2 and every fast install silently fall back to Steam's download. +""" + +from __future__ import annotations + +from pathlib import Path +from unittest.mock import patch + +import pytest + +from steam_backlog_enforcer import _fast_install_cmd +from steam_backlog_enforcer._fast_install_cmd import build_cmd, installer_path +from steam_backlog_enforcer._steam_restart_guard import _INSTALLER_MARKERS +from steam_installer import cli_args + +CMD = "steam_backlog_enforcer._fast_install_cmd" +# Read at import: the autouse guard swaps the attribute out before each test. +REAL_INSTALLER: Path = vars(_fast_install_cmd)["_INSTALLER"] +REPO = Path(__file__).resolve().parents[2] + + +def _argv_after_launcher(cmd: list[str]) -> list[str]: + """The installer's own arguments: everything after ``-m steam_installer.cli``.""" + return cmd[cmd.index("steam_installer.cli") + 1 :] + + +class TestInstallerLocation: + """The installer is found in this checkout, never under ~/src.""" + + def test_real_path_is_inside_this_repo(self) -> None: + """Unpatched, the installer is this repo's own cli module, and exists.""" + assert REAL_INSTALLER == REPO / "steam_installer" / "cli.py" + assert REAL_INSTALLER.is_file() + assert "steam-game-installer" not in REAL_INSTALLER.parts + + def test_found_when_present(self) -> None: + """installer_path returns the repo's cli module.""" + with patch.object(_fast_install_cmd, "_INSTALLER", REAL_INSTALLER): + assert installer_path() == REAL_INSTALLER + + def test_missing_is_none(self, tmp_path: Path) -> None: + """A checkout without the installer falls back to Steam's download.""" + with patch.object(_fast_install_cmd, "_INSTALLER", tmp_path / "gone.py"): + assert installer_path() is None + + +class TestBuiltArgv: + """The exact argv the daemon hands to the desktop user.""" + + @pytest.fixture(name="argv") + def _argv(self) -> list[str]: + with patch(f"{CMD}.steam_account_name", return_value="krzys"): + return build_cmd(REAL_INSTALLER, 284160, None) + + def test_installer_parser_accepts_it(self, argv: list[str]) -> None: + """The installer's argparse takes the argv as built, with no exit 2.""" + args = cli_args.build_parser().parse_args(_argv_after_launcher(argv)) + assert args.appid == 284160 + assert args.yes is True + assert args.language == "english" + assert args.username == "krzys" + + def test_parser_accepts_it_without_account(self) -> None: + """No signed-in account means no ``--username``, still parseable.""" + with patch(f"{CMD}.steam_account_name", return_value=None): + argv = build_cmd(REAL_INSTALLER, 7, None) + cli_args.build_parser().parse_args(_argv_after_launcher(argv)) + assert "--username" not in argv + + def test_no_run_sh_only_flag(self, argv: list[str]) -> None: + """``--no-deps`` belongs to run.sh; the module's parser has no such flag.""" + assert "--no-deps" not in argv + + def test_imports_from_this_checkout(self, argv: list[str]) -> None: + """sudo resets the environment, so PYTHONPATH is named in the argv.""" + assert f"PYTHONPATH={REPO}" in argv + assert argv[argv.index("steam_installer.cli") - 1] == "-m" + + def test_scan_marker_matches_the_real_cmdline(self, argv: list[str]) -> None: + """The /proc guard sees the process this argv starts, sudo wrapper too.""" + cmdline = b"\0".join(arg.encode() for arg in argv) + assert any(marker in cmdline for marker in _INSTALLER_MARKERS) diff --git a/steam_backlog_enforcer/tests/test_no_real_effects.py b/steam_backlog_enforcer/tests/test_no_real_effects.py index 1c354dd..7174ba0 100644 --- a/steam_backlog_enforcer/tests/test_no_real_effects.py +++ b/steam_backlog_enforcer/tests/test_no_real_effects.py @@ -63,6 +63,7 @@ class TestDrivesSteam: ["sudo", "-u", "kuhy", "steam.sh"], ["xdg-open", "https://example.com"], ["runuser", "steam-game-installer", "620"], + ["python3", "-m", "steam_installer.cli", "--appid", "620"], ["pkill", "-f", "x"], ["killall", "x"], ["/bin/kill", "-TERM", "42"], diff --git a/steam_installer/__init__.py b/steam_installer/__init__.py new file mode 100644 index 0000000..3139f76 --- /dev/null +++ b/steam_installer/__init__.py @@ -0,0 +1 @@ +"""Fast Steam game installer (depotdownloader plus a hand-written manifest).""" diff --git a/steam_installer/_http.py b/steam_installer/_http.py new file mode 100644 index 0000000..d9204b4 --- /dev/null +++ b/steam_installer/_http.py @@ -0,0 +1,26 @@ +"""One https-only way to open a URL, shared by the network callers.""" + +from __future__ import annotations + +import contextlib +from typing import TYPE_CHECKING +import urllib.request + +if TYPE_CHECKING: + from collections.abc import Iterator + from http.client import HTTPResponse + + +@contextlib.contextmanager +def open_https( + url: str, *, timeout: float, user_agent: str | None = None +) -> Iterator[HTTPResponse]: + """Open `url` (https only) with `user_agent`; a context manager.""" + if not url.startswith("https://"): + msg = f"refusing a non-https URL: {url}" + raise ValueError(msg) + # No header when none is given: urllib then sends its own default agent. + headers = {} if user_agent is None else {"User-Agent": user_agent} + request = urllib.request.Request(url, headers=headers) + with urllib.request.urlopen(request, timeout=timeout) as response: + yield response diff --git a/_installer_import/steam_installer/anticheat.py b/steam_installer/anticheat.py similarity index 81% rename from _installer_import/steam_installer/anticheat.py rename to steam_installer/anticheat.py index dc4eb1a..0efa71c 100644 --- a/_installer_import/steam_installer/anticheat.py +++ b/steam_installer/anticheat.py @@ -11,8 +11,10 @@ from __future__ import annotations import json import os -import urllib.request from pathlib import Path +import urllib.error + +from steam_installer import _http URL = ( "https://raw.githubusercontent.com/AreWeAntiCheatYet/AreWeAntiCheatYet/" @@ -22,7 +24,7 @@ CACHE = Path.home() / ".cache" / "steam-game-installer" / "anticheat-games.json" def _download() -> bytes: - with urllib.request.urlopen(URL, timeout=20) as response: + with _http.open_https(URL, timeout=20) as response: return response.read() @@ -30,7 +32,8 @@ def _ids(raw: bytes | str) -> set[int]: """Steam ids in a games.json; ValueError/TypeError when it is not one.""" games = json.loads(raw) if not isinstance(games, list): - raise TypeError("games.json is not a list") + msg = "games.json is not a list" + raise TypeError(msg) ids: set[int] = set() for game in games: stores = game.get("storeIds") if isinstance(game, dict) else None @@ -45,13 +48,14 @@ def steam_ids(cache: Path = CACHE) -> set[int] | None: try: raw = _download() ids = _ids(raw) - except (OSError, ValueError, TypeError): + # URLError is an OSError; named so the handler reads as network failure. + except urllib.error.URLError, OSError, ValueError, TypeError: try: return _ids(cache.read_bytes()) - except (OSError, ValueError, TypeError): + except OSError, ValueError, TypeError: return None cache.parent.mkdir(parents=True, exist_ok=True) tmp = cache.with_name(f"{cache.name}.tmp{os.getpid()}") tmp.write_bytes(raw) - os.replace(tmp, cache) + tmp.replace(cache) return ids diff --git a/_installer_import/steam_installer/appinfo.py b/steam_installer/appinfo.py similarity index 84% rename from _installer_import/steam_installer/appinfo.py rename to steam_installer/appinfo.py index bd7f014..a2ae5c0 100644 --- a/_installer_import/steam_installer/appinfo.py +++ b/steam_installer/appinfo.py @@ -3,19 +3,22 @@ from __future__ import annotations from dataclasses import dataclass, field +from typing import Any -from steam_installer import appinfo_cache +from steam_installer import appinfo_cache, errors # depotdownloader is never given -branch, so it always fetches this one. PUBLIC_BRANCH = "public" -class AppInfoError(Exception): - pass +class AppInfoError(errors.InstallerError): + """Steam's app info is missing or unreadable.""" @dataclass class AppMetadata: + """What the installer needs to know about one app, from Steam's cache.""" + appid: int name: str installdir: str @@ -44,10 +47,11 @@ class DepotPlan: @property def all_expected(self) -> set[str]: + """Every depot the install should end up with.""" return self.required | self.shared -def _cached_blocks(appids: list[int]) -> dict[int, dict]: +def cached_blocks(appids: list[int]) -> dict[int, dict[str, Any]]: """App info for those of `appids` Steam's local cache holds. Read from `appcache/appinfo.vdf` (see :mod:`appinfo_cache`), not from @@ -60,46 +64,51 @@ def _cached_blocks(appids: list[int]) -> dict[int, dict]: return {appid: apps[appid] for appid in appids if appid in apps} -def _fetch_app_blocks(appids: list[int]) -> dict[int, dict]: +def _fetch_app_blocks(appids: list[int]) -> dict[int, dict[str, Any]]: """App info for every id in `appids`; a missing one is an error with the fix.""" - blocks = _cached_blocks(appids) + blocks = cached_blocks(appids) missing = [str(appid) for appid in appids if appid not in blocks] if missing: - raise AppInfoError( + msg = ( f"app {', '.join(missing)} is not in Steam's local app cache: " f"{appinfo_cache.FIX}" ) + raise AppInfoError(msg) return blocks -def _fetch_app_block(appid: int) -> dict: +def fetch_app_block(appid: int) -> dict[str, Any]: + """App info for one id; a missing one is an error with the fix.""" return _fetch_app_blocks([appid])[appid] def get_metadata(appid: int) -> AppMetadata: """Fetch the fields this project actually needs: display name and installdir.""" - app_block = _fetch_app_block(appid) + app_block = fetch_app_block(appid) common = app_block.get("common", {}) if not isinstance(common, dict): - raise AppInfoError(f"no 'common' section in app info for {appid}") + msg = f"no 'common' section in app info for {appid}" + raise AppInfoError(msg) name = common.get("name") if not isinstance(name, str): - raise AppInfoError(f"no name field found for app {appid}") + msg = f"no name field found for app {appid}" + raise AppInfoError(msg) installdir = common.get("installdir") if not isinstance(installdir, str): config = app_block.get("config", {}) installdir = config.get("installdir") if isinstance(config, dict) else None if not isinstance(installdir, str): - raise AppInfoError(f"no installdir field found for app {appid}") + msg = f"no installdir field found for app {appid}" + raise AppInfoError(msg) return AppMetadata(appid=appid, name=name, installdir=installdir) def get_build_id(appid: int, branch: str = "public") -> str | None: """Current buildid for `branch`, used to sanity-check the manifest we write.""" - app_block = _fetch_app_block(appid) + app_block = fetch_app_block(appid) depots = app_block.get("depots", {}) if not isinstance(depots, dict): return None @@ -122,10 +131,11 @@ def get_depot_plan( or marked `optional` -- none of those are fetched by a default install, so demanding them would make a healthy download look incomplete. """ - app_block = _fetch_app_block(appid) + app_block = fetch_app_block(appid) depots = app_block.get("depots", {}) if not isinstance(depots, dict): - raise AppInfoError(f"no 'depots' section in app info for {appid}") + msg = f"no 'depots' section in app info for {appid}" + raise AppInfoError(msg) plan = _plan_from_depots(appid, depots, language, os_name) # Tag the game's own list's DLC depots too: depotdownloader fetches the # owned ones in the base run, and Steam records each with its dlcappid. @@ -149,15 +159,16 @@ def _public_gid(entry: object) -> str | None: def get_dlc_plans( appid: int, language: str = "english", os_name: str = "windows" ) -> dict[int, dict[str, str]]: - """DLC app id -> {depot: public gid} it installs itself, for DLC with depots - of its own. + """Map each DLC app id to the {depot: public gid} it installs itself. + + Only DLC with depots of its own appear. Some DLC (Cyberpunk 2077's Phantom Liberty, app 2138330, ~25 GB) keeps its content under its own app id rather than in the game's depot list, so a depotdownloader run for the game never sees it. DLC with no depots of its own, or whose depots the game already lists, needs no run of its own. """ - app_block = _fetch_app_block(appid) + app_block = fetch_app_block(appid) extended = app_block.get("extended", {}) listed = extended.get("listofdlc", "") if isinstance(extended, dict) else "" dlc_ids = [int(x) for x in str(listed).split(",") if x.strip().isdigit()] @@ -169,7 +180,7 @@ def get_dlc_plans( # A DLC the cache lacks is not owned: Steam caches app info for every app # the account owns (1828 of 7102 listed DLC ids on 2026-11-22), and # depotdownloader would refuse an unowned one anyway. - for dlc_id, dlc_block in _cached_blocks(dlc_ids).items(): + for dlc_id, dlc_block in cached_blocks(dlc_ids).items(): depots = dlc_block.get("depots", {}) if not isinstance(depots, dict): continue @@ -182,7 +193,7 @@ def get_dlc_plans( def _plan_from_depots( - appid: int, depots: dict, language: str, os_name: str + appid: int, depots: dict[str, Any], language: str, os_name: str ) -> DepotPlan: """The depots of one app's `depots` section that an (os, language) install gets.""" required: set[str] = set() diff --git a/_installer_import/steam_installer/appinfo_cache.py b/steam_installer/appinfo_cache.py similarity index 52% rename from _installer_import/steam_installer/appinfo_cache.py rename to steam_installer/appinfo_cache.py index ac5eec6..2858e51 100644 --- a/_installer_import/steam_installer/appinfo_cache.py +++ b/steam_installer/appinfo_cache.py @@ -20,29 +20,36 @@ would silently change which depots count. from __future__ import annotations import struct -from pathlib import Path +from typing import TYPE_CHECKING, Any +from steam_installer import errors +from steam_installer.appinfo_kv import ( + HEADER, + MAGIC, + AppinfoFormatError, + read_block, + string_table, +) from steam_installer.library import find_steam_root -MAGIC = 0x07564429 -_HEADER = 60 +if TYPE_CHECKING: + from pathlib import Path + FIX = "start Steam once, let it sync, rerun" -# Fixed-width value types: KV type byte -> struct format. -_SCALARS = {2: " Path: + """Where Steam keeps its binary appinfo cache.""" return find_steam_root() / "appcache" / "appinfo.vdf" -def load(path: Path | None = None) -> dict[int, dict]: +def load(path: Path | None = None) -> dict[int, dict[str, Any]]: """Every app in the cache, appid -> its `appinfo` block. Parsed once per file version: four lookups in one run would otherwise @@ -52,7 +59,8 @@ def load(path: Path | None = None) -> dict[int, dict]: try: stat = path.stat() except OSError as exc: - raise CacheError(f"cannot read Steam's app cache {path}: {FIX}") from exc + msg = f"cannot read Steam's app cache {path}: {FIX}" + raise CacheError(msg) from exc key = (stat.st_mtime_ns, stat.st_size) cached = _memo.get(path) if cached is not None and cached[0] == key: @@ -62,54 +70,22 @@ def load(path: Path | None = None) -> dict[int, dict]: return apps -def _parse(buf: bytes) -> dict[int, dict]: +def _parse(buf: bytes) -> dict[int, dict[str, Any]]: magic, _universe, table_offset = struct.unpack_from(" list[str]: - (count,) = struct.unpack_from(" tuple[dict, int]: - out: dict = {} - while True: - kind = buf[pos] - pos += 1 - if kind == 8: - return out, pos - (index,) = struct.unpack_from(" struct format. +_SCALARS: Final = {2: " list[str]: + """The NUL-terminated strings (u32 count first) that keys index into.""" + (count,) = struct.unpack_from(" tuple[dict[str, Any], int]: + """Decode one key-value block at *pos*; return it and the next position. + + Every scalar comes back as a string, as Steam's text VDF prints it. + """ + out: dict[str, Any] = {} + while True: + kind = buf[pos] + pos += 1 + if kind == _END: + return out, pos + (index,) = struct.unpack_from(" confirm -> adopt what exists -> fast download -> recognize.""" +"""Entry point: resolve, confirm, adopt what exists, download, recognize.""" from __future__ import annotations -import argparse +import logging import sys - -from kuhy_credit import credit_line +from typing import TYPE_CHECKING from steam_installer import ( + cli_args, config, - depotdl, deps, + errors, flow, ghost, map_installed, @@ -19,98 +19,49 @@ from steam_installer import ( takeover, target, ) +from steam_installer.job import Job from steam_installer.library import find_steam_root, list_libraries, pick_install_target - -def _build_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser( - description="Download an owned Steam game fast, then make Steam recognize it.", - # kuhy's shared credit line (kuhy_credit, kuhyx/utils). - epilog=credit_line(), - ) - parser.add_argument( - "target", - nargs="?", - default=None, - metavar="TARGET", - help=( - "AppID (284160), a Steam URL " - "(https://steamcommunity.com/app/284160/guides/), or a game name " - "(BeamNG.drive, or just BeamNG)" - ), - ) - parser.add_argument( - "--appid", type=int, default=None, help="Steam AppID (skips detection)" - ) - parser.add_argument( - "--username", - default=None, - help="Steam account username (default: $STEAM_USERNAME, then the saved one)", - ) - parser.add_argument( - "--language", default=None, help="Language depot to fetch (default: prompts)" - ) - parser.add_argument( - "--max-downloads", type=int, default=depotdl.DEFAULT_MAX_DOWNLOADS - ) - parser.add_argument( - "--yes", - action="store_true", - help="Assume yes for every prompt. Ambiguous names still fail rather than guess.", - ) - parser.add_argument( - "--dry-run", - action="store_true", - help="Resolve the target and report what would happen. Changes nothing.", - ) - parser.add_argument( - "--repair-ghosts", - action="store_true", - help=( - "Scan every library for the fake 'installed' manifests anonymous " - "steamcmd leaves behind, and clear them away." - ), - ) - map_installed.add_arguments(parser) - parser.add_argument( - "--verbose", - action="store_true", - help="Show on the console everything that goes to the run log.", - ) - return parser +if TYPE_CHECKING: + import argparse + from pathlib import Path def main(argv: list[str] | None = None) -> int: - parser = _build_parser() + """Run the installer; return the process exit code.""" + parser = cli_args.build_parser() args = parser.parse_args(argv) - if not (args.repair_ghosts or args.map_installed) and not (args.appid or args.target): + if not (args.repair_ghosts or args.map_installed) and not ( + args.appid or args.target + ): parser.error( - "provide an AppID, a Steam URL, a game name, --repair-ghosts or --map-installed" + "provide an AppID, a Steam URL, a game name, " + "--repair-ghosts or --map-installed" ) map_installed.check_arguments(parser, args) literal = target.parse_literal(args.target) if args.target else None - log = runlog.setup(args.appid or (literal[0] if literal else None), args.verbose) + log = runlog.setup( + args.appid or (literal[0] if literal else None), verbose=args.verbose + ) logger = log.logger # Printed up front as well as at the end: if the run dies badly, the path # still has to be on screen to copy-paste. logger.info("Run log: %s", log.path) logger.debug("argv: %s", sys.argv) - try: + # Any Exception ends in "log it, exit 1" plus the run-log path, as before. + with errors.CrashGuard(logger, log.path): return _run(args, log) - except Exception: - logger.exception("Run failed with an unhandled error") - logger.info("Run log (send this): %s", log.path) - return 1 + return 1 def _is_interactive(args: argparse.Namespace) -> bool: return sys.stdin.isatty() and not args.yes -def _ask(args: argparse.Namespace, interactive: bool, question: str) -> bool: +def _ask(args: argparse.Namespace, question: str, *, interactive: bool) -> bool: if args.yes: return True if not interactive: @@ -143,7 +94,12 @@ def _repair_ghosts(log: runlog.RunLog) -> int: return 0 -def _describe(logger, metadata, install_target, report, staged) -> int: +def _describe( + logger: logging.Logger, + install_target: Path, + report: ghost.GhostReport, + staged: takeover.Staged | None, +) -> int: """--dry-run output: everything the real run would act on, and nothing done.""" logger.info("Install target: %s", install_target) if report.is_ghost: @@ -162,26 +118,51 @@ def _describe(logger, metadata, install_target, report, staged) -> int: return 0 -def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: - logger = log.logger - interactive = _is_interactive(args) +def _log_error(logger: logging.Logger, exc: Exception) -> None: + """Log the message alone, with no traceback: these are user errors.""" + logger.log(logging.ERROR, "%s", exc) + +def _resolve( + args: argparse.Namespace, logger: logging.Logger, *, interactive: bool +) -> target.Resolved | int: + """Resolve the target, or return the exit code for a failure already logged.""" + try: + deps.require() + return target.resolve_target(args.target, args.appid, interactive=interactive) + except deps.DependencyError as exc: + _log_error(logger, exc) + return 1 + except (target.TargetError, resolve.ResolveError) as exc: + _log_error(logger, exc) + return 2 + + +def _pick_username( + args: argparse.Namespace, logger: logging.Logger, *, interactive: bool +) -> str | None: + """Return the Steam username, or None after logging why there is none.""" + try: + return config.resolve_username(args.username, interactive=interactive) + except config.ConfigError as exc: + _log_error(logger, exc) + return None + + +def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: if args.repair_ghosts: return _repair_ghosts(log) if args.map_installed: return map_installed.run(args, log, find_steam_root()) + return _run_install(args, log) - try: - deps.require() - resolved = target.resolve_target( - args.target, args.appid, interactive=interactive - ) - except deps.DependencyError as exc: - logger.error("%s", exc) - return 1 - except (target.TargetError, resolve.ResolveError) as exc: - logger.error("%s", exc) - return 2 + +def _run_install(args: argparse.Namespace, log: runlog.RunLog) -> int: + logger = log.logger + interactive = _is_interactive(args) + resolved = _resolve(args, logger, interactive=interactive) + if isinstance(resolved, int): + return resolved metadata = resolved.metadata logger.info( @@ -199,24 +180,22 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: staged = takeover.find_staged(library, metadata.appid, install_target) if args.dry_run: - return _describe(logger, metadata, install_target, report, staged) + return _describe(logger, install_target, report, staged) if report.is_ghost and not report.is_warm: logger.error( "appid %s has a ghost manifest (a fake 'installed' state with nothing " - "behind it). Clear it first: ./run.sh --repair-ghosts", + "behind it). Clear it first: steam_installer/run.sh --repair-ghosts", metadata.appid, ) return 1 - try: - username = config.resolve_username(args.username, interactive=interactive) - except config.ConfigError as exc: - logger.error("%s", exc) + username = _pick_username(args, logger, interactive=interactive) + if username is None: return 1 logger.info("Install target: %s", install_target) - if not _ask(args, interactive, "Proceed? [y/N]: "): + if not _ask(args, "Proceed? [y/N]: ", interactive=interactive): logger.info("Cancelled (use --yes for a non-interactive run).") return 1 @@ -232,12 +211,14 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: args=args, log=log, steam_root=steam_root, - library=library, - metadata=metadata, - install_target=install_target, - staged=staged, - username=username, - language=language, + job=Job( + library=library, + metadata=metadata, + install_target=install_target, + staged=staged, + username=username, + language=language, + ), ) diff --git a/steam_installer/cli_args.py b/steam_installer/cli_args.py new file mode 100644 index 0000000..06f7866 --- /dev/null +++ b/steam_installer/cli_args.py @@ -0,0 +1,70 @@ +"""Command-line argument definitions for the installer.""" + +from __future__ import annotations + +import argparse + +from kuhy_credit import credit_line + +from steam_installer import depotdl, map_installed + + +def build_parser() -> argparse.ArgumentParser: + """Build the installer's argument parser.""" + parser = argparse.ArgumentParser( + description="Download an owned Steam game fast, then make Steam recognize it.", + # kuhy's shared credit line (kuhy_credit, kuhyx/utils). + epilog=credit_line(), + ) + parser.add_argument( + "target", + nargs="?", + default=None, + metavar="TARGET", + help=( + "AppID (284160), a Steam URL " + "(https://steamcommunity.com/app/284160/guides/), or a game name " + "(BeamNG.drive, or just BeamNG)" + ), + ) + parser.add_argument( + "--appid", type=int, default=None, help="Steam AppID (skips detection)" + ) + parser.add_argument( + "--username", + default=None, + help="Steam account username (default: $STEAM_USERNAME, then the saved one)", + ) + parser.add_argument( + "--language", default=None, help="Language depot to fetch (default: prompts)" + ) + parser.add_argument( + "--max-downloads", type=int, default=depotdl.DEFAULT_MAX_DOWNLOADS + ) + parser.add_argument( + "--yes", + action="store_true", + help=( + "Assume yes for every prompt. Ambiguous names still fail rather than guess." + ), + ) + parser.add_argument( + "--dry-run", + action="store_true", + help="Resolve the target and report what would happen. Changes nothing.", + ) + parser.add_argument( + "--repair-ghosts", + action="store_true", + help=( + "Scan every library for the fake 'installed' manifests anonymous " + "steamcmd leaves behind, and clear them away." + ), + ) + map_installed.add_arguments(parser) + parser.add_argument( + "--verbose", + action="store_true", + help="Show on the console everything that goes to the run log.", + ) + return parser diff --git a/_installer_import/steam_installer/compat_apply.py b/steam_installer/compat_apply.py similarity index 68% rename from _installer_import/steam_installer/compat_apply.py rename to steam_installer/compat_apply.py index 9e271a2..4cda5ba 100644 --- a/_installer_import/steam_installer/compat_apply.py +++ b/steam_installer/compat_apply.py @@ -7,11 +7,14 @@ is still closed) and by `--map-installed`. The decision rules live in from __future__ import annotations -import logging -from pathlib import Path +from typing import TYPE_CHECKING, Any from steam_installer import anticheat, appinfo, compat_decide, compat_map, proton_pkg +if TYPE_CHECKING: + import logging + from pathlib import Path + VALVE_PROTON = "--valve-proton" TOOL_DIR = "compatibilitytools.d/proton-cachyos-native" @@ -19,7 +22,10 @@ TOOL_DIR = "compatibilitytools.d/proton-cachyos-native" class Context: """What every decision in one run shares: fetched once, not per game.""" - def __init__(self, steam_root: Path, extra_overrides: dict[str, str] | None = None): + def __init__( + self, steam_root: Path, extra_overrides: dict[str, str] | None = None + ) -> None: + """Remember what every decision in this run shares.""" self.steam_root = steam_root self.config = compat_map.config_path(steam_root) self.anticheat = anticheat.steam_ids() @@ -28,20 +34,22 @@ class Context: steam_root / TOOL_DIR, proton_pkg.read_lock().version ) - def decide(self, appid: int, block: dict, owned_dlc: bool) -> compat_decide.Decision: - return compat_decide.decide( - appid, - block, + def decide( + self, appid: int, block: dict[str, Any], *, owned_dlc: bool + ) -> compat_decide.Decision: + """Decide whether *appid* maps to the native Proton.""" + facts = compat_decide.Facts( anticheat=self.anticheat, overrides=self.overrides, - owned_dlc=owned_dlc, tool_ready=self.tool_ready, ) + return compat_decide.decide(appid, block, facts, owned_dlc=owned_dlc) def after_install( steam_root: Path, appid: int, + *, owned_dlc: bool, valve_proton: bool, logger: logging.Logger, @@ -54,18 +62,28 @@ def after_install( extra = {str(appid): VALVE_PROTON} if valve_proton else {} try: context = Context(steam_root, extra) - decision = context.decide(appid, appinfo._fetch_app_block(appid), owned_dlc) + decision = context.decide( + appid, appinfo.fetch_app_block(appid), owned_dlc=owned_dlc + ) if not decision.map: logger.info("Compat tool: %s", decision.describe()) return outcome = compat_map.map_app(context.config, appid) - except (appinfo.AppInfoError, compat_map.CompatMapError, OSError, ValueError) as exc: - logger.warning("Compat tool not set (%s); the game stays on Valve's default.", exc) + except ( + appinfo.AppInfoError, + compat_map.CompatMapError, + OSError, + ValueError, + ) as exc: + logger.warning( + "Compat tool not set (%s); the game stays on Valve's default.", exc + ) return report(logger, appid, outcome) def report(logger: logging.Logger, appid: int, outcome: compat_map.Outcome) -> None: + """Log what happened to *appid*'s compat mapping.""" if outcome.action == "kept": logger.info("Compat tool for %s kept: %s", appid, outcome.detail) elif outcome.action == "already": diff --git a/_installer_import/steam_installer/compat_decide.py b/steam_installer/compat_decide.py similarity index 80% rename from _installer_import/steam_installer/compat_decide.py rename to steam_installer/compat_decide.py index d366c05..b1fe0d1 100644 --- a/_installer_import/steam_installer/compat_decide.py +++ b/steam_installer/compat_decide.py @@ -9,8 +9,8 @@ playable. When unsure, the answer is no. from __future__ import annotations -import json from dataclasses import dataclass, field +import json from pathlib import Path OVERRIDES = Path(__file__).parent / "data" / "compat_overrides.json" @@ -18,14 +18,18 @@ OVERRIDES = Path(__file__).parent / "data" / "compat_overrides.json" @dataclass class Decision: + """Whether one game goes on the native Proton, and why.""" + appid: int reasons: list[str] = field(default_factory=list) @property def map(self) -> bool: + """Whether the game gets the native Proton.""" return not self.reasons def describe(self) -> str: + """One line naming the verdict and its reasons.""" return "map" if self.map else f"Valve default: {', '.join(self.reasons)}" @@ -35,7 +39,8 @@ def load_overrides(path: Path = OVERRIDES) -> dict[str, str]: if not isinstance(data, dict) or not all( key.isdigit() and isinstance(reason, str) for key, reason in data.items() ): - raise ValueError(f"{path.name}: compat_overrides must map app ids to reasons") + msg = f"{path.name}: compat_overrides must map app ids to reasons" + raise ValueError(msg) return data @@ -83,26 +88,27 @@ def _platform_reasons(block: dict) -> list[str]: return reasons -def decide( - appid: int, - block: dict, - *, - anticheat: set[int] | None, - overrides: dict[str, str], - owned_dlc: bool, - tool_ready: bool, -) -> Decision: +@dataclass(frozen=True) +class Facts: + """What every decision in one run shares: fetched once, not per game.""" + + anticheat: set[int] | None + overrides: dict[str, str] + tool_ready: bool + + +def decide(appid: int, block: dict, facts: Facts, *, owned_dlc: bool) -> Decision: """Every reason `appid` must keep Valve's default; none means map it.""" reasons: list[str] = [] - if anticheat is None: + if facts.anticheat is None: reasons.append("anti-cheat list unavailable") - elif appid in anticheat: + elif appid in facts.anticheat: reasons.append("anti-cheat") reasons += _platform_reasons(block) if owned_dlc: reasons.append("owned DLC") - if str(appid) in overrides: - reasons.append(f"override: {overrides[str(appid)]}") - if not tool_ready: + if str(appid) in facts.overrides: + reasons.append(f"override: {facts.overrides[str(appid)]}") + if not facts.tool_ready: reasons.append("native Proton not installed") return Decision(appid, reasons) diff --git a/_installer_import/steam_installer/compat_map.py b/steam_installer/compat_map.py similarity index 77% rename from _installer_import/steam_installer/compat_map.py rename to steam_installer/compat_map.py index f87c351..6e0dd07 100644 --- a/_installer_import/steam_installer/compat_map.py +++ b/steam_installer/compat_map.py @@ -18,13 +18,13 @@ write while it runs would be lost; it is refused. from __future__ import annotations +from dataclasses import dataclass, field import os +from pathlib import Path import re import tempfile -from dataclasses import dataclass, field -from pathlib import Path -from steam_installer import recognize +from steam_installer import errors, recognize TOOL = "proton-cachyos-native" PRIORITY = "250" @@ -35,12 +35,14 @@ _MAPPING = (*_STEAM, "compattoolmapping") _TOKEN = re.compile(r'"(?:[^"\\]|\\.)*"|[{}]|[^\s"{}]+') -class CompatMapError(Exception): - pass +class CompatMapError(errors.InstallerError): + """Steam's config.vdf cannot be edited safely.""" @dataclass class Outcome: + """What editing the compat mapping did.""" + action: str # "mapped", "already" or "kept" detail: str = "" @@ -61,6 +63,7 @@ class _Layout: def config_path(steam_root: Path) -> Path: + """Path of Steam's ``config.vdf`` under *steam_root*.""" return steam_root / "config" / "config.vdf" @@ -76,9 +79,10 @@ def map_app(config: Path, appid: int) -> Outcome: """Point `appid` at the native Proton unless kuhy chose another tool.""" pids = recognize.steam_client_pids() if pids: - raise CompatMapError( + msg = ( "Steam is running and would overwrite config.vdf on exit -- close it first" ) + raise CompatMapError(msg) layout = _scan(_read(config), str(appid)) lines = layout.lines entry = layout.entry @@ -90,8 +94,12 @@ def map_app(config: Path, appid: int) -> Outcome: if name and priority != VALVE_PRIORITY: return Outcome("kept", f"already mapped to {name} (priority {priority})") key_line = entry.open_line - 1 - if lines[key_line].strip() != f'"{appid}"' or lines[entry.close_line].strip() != "}": - raise CompatMapError(f"config.vdf line {key_line + 1}: unexpected layout") + if ( + lines[key_line].strip() != f'"{appid}"' + or lines[entry.close_line].strip() != "}" + ): + msg = f"config.vdf line {key_line + 1}: unexpected layout" + raise CompatMapError(msg) indent = _indent(lines[entry.open_line]) lines[key_line : entry.close_line + 1] = _entry(appid, indent) _write(config, lines) @@ -112,7 +120,8 @@ def _read(config: Path) -> list[str]: try: return config.read_text(encoding="utf-8").splitlines(keepends=True) except OSError as exc: - raise CompatMapError(f"cannot read {config}") from exc + msg = f"cannot read {config}" + raise CompatMapError(msg) from exc def _tokens(lines: list[str]) -> list[tuple[str, int, bool]]: @@ -126,13 +135,14 @@ def _tokens(lines: list[str]) -> list[tuple[str, int, bool]]: for match in _TOKEN.finditer(text): while starts[line_no + 1] <= match.start(): line_no += 1 - token = match.group(0) - if token[0] == '"': - out.append((token[1:-1], line_no, True)) - elif token in "{}": - out.append((token, line_no, False)) + piece = match.group(0) + if piece[0] == '"': + out.append((piece[1:-1], line_no, True)) + elif piece in "{}": + out.append((piece, line_no, False)) else: - raise CompatMapError(f"config.vdf line {line_no + 1}: unexpected layout") + msg = f"config.vdf line {line_no + 1}: unexpected layout" + raise CompatMapError(msg) return out @@ -142,34 +152,41 @@ def _scan(lines: list[str], appid: str) -> _Layout: stack: list[str] = [] pending: str | None = None for text, index, quoted in _tokens(lines): - in_entry = layout.entry is not None and tuple(stack) == (*_MAPPING, appid) + entry = layout.entry if tuple(stack) == (*_MAPPING, appid) else None if quoted: if pending is None: pending = text continue - if in_entry: - layout.entry.values[pending.lower()] = text + if entry is not None: + entry.values[pending.lower()] = text pending = None elif text == "{": if pending is None: - raise CompatMapError(f"config.vdf line {index + 1}: block without a key") + msg = f"config.vdf line {index + 1}: block without a key" + raise CompatMapError(msg) stack.append(pending.lower()) pending = None _note_open(layout, tuple(stack), appid, index) else: if not stack or pending is not None: - raise CompatMapError(f"config.vdf line {index + 1}: unbalanced brace") - if in_entry: - layout.entry.close_line = index + msg = f"config.vdf line {index + 1}: unbalanced brace" + raise CompatMapError(msg) + if entry is not None: + entry.close_line = index stack.pop() if layout.steam_open < 0: - raise CompatMapError("config.vdf has no InstallConfigStore/Software/Valve/Steam") + msg = "config.vdf has no InstallConfigStore/Software/Valve/Steam" + raise CompatMapError(msg) return layout def _note_open(layout: _Layout, path: tuple[str, ...], appid: str, index: int) -> None: - if path in (_STEAM, _MAPPING, (*_MAPPING, appid)) and layout.lines[index].strip() != "{": - raise CompatMapError(f"config.vdf line {index + 1}: unexpected layout") + if ( + path in (_STEAM, _MAPPING, (*_MAPPING, appid)) + and layout.lines[index].strip() != "{" + ): + msg = f"config.vdf line {index + 1}: unexpected layout" + raise CompatMapError(msg) if path == _STEAM: layout.steam_open = index elif path == _MAPPING: @@ -201,8 +218,8 @@ def _write(config: Path, lines: list[str]) -> None: try: with os.fdopen(fd, "w", encoding="utf-8") as handle: handle.write("".join(lines)) - os.chmod(tmp, mode) - os.replace(tmp, config) + Path(tmp).chmod(mode) + Path(tmp).replace(config) except BaseException: Path(tmp).unlink(missing_ok=True) raise diff --git a/_installer_import/steam_installer/config.py b/steam_installer/config.py similarity index 83% rename from _installer_import/steam_installer/config.py rename to steam_installer/config.py index 03b6233..cfa0428 100644 --- a/_installer_import/steam_installer/config.py +++ b/steam_installer/config.py @@ -2,8 +2,8 @@ Only the username is ever persisted. Passwords and Steam Guard codes stay where they belong: with depotdownloader's `-remember-password` login token -(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile would be a downgrade in security over -what Steam's own tooling already does. +(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile +would be a downgrade in security over what Steam's own tooling already does. """ from __future__ import annotations @@ -11,14 +11,16 @@ from __future__ import annotations import os from pathlib import Path +from steam_installer import errors + CONFIG_DIR = Path.home() / ".config" / "steam-game-installer" CONFIG_PATH = CONFIG_DIR / "config" USERNAME_KEY = "username" ENV_VAR = "STEAM_USERNAME" -class ConfigError(Exception): - pass +class ConfigError(errors.InstallerError): + """The saved config is unusable or a username is missing.""" def load_username() -> str | None: @@ -51,12 +53,14 @@ def resolve_username(cli_value: str | None, *, interactive: bool) -> str: if saved: return saved if not interactive: - raise ConfigError( + msg = ( "No Steam username available. Pass --username, set " f"${ENV_VAR}, or run once interactively to save one." ) + raise ConfigError(msg) entered = input("Steam username: ").strip() if not entered: - raise ConfigError("No Steam username entered.") + msg = "No Steam username entered." + raise ConfigError(msg) save_username(entered) return entered diff --git a/_installer_import/steam_installer/data/cachyos_signing_key.asc b/steam_installer/data/cachyos_signing_key.asc similarity index 100% rename from _installer_import/steam_installer/data/cachyos_signing_key.asc rename to steam_installer/data/cachyos_signing_key.asc diff --git a/_installer_import/steam_installer/data/compat_overrides.json b/steam_installer/data/compat_overrides.json similarity index 100% rename from _installer_import/steam_installer/data/compat_overrides.json rename to steam_installer/data/compat_overrides.json diff --git a/_installer_import/steam_installer/data/proton-native.lock b/steam_installer/data/proton-native.lock similarity index 100% rename from _installer_import/steam_installer/data/proton-native.lock rename to steam_installer/data/proton-native.lock diff --git a/_installer_import/steam_installer/depotdl.py b/steam_installer/depotdl.py similarity index 56% rename from _installer_import/steam_installer/depotdl.py rename to steam_installer/depotdl.py index 9349bf0..54676f6 100644 --- a/_installer_import/steam_installer/depotdl.py +++ b/steam_installer/depotdl.py @@ -11,82 +11,74 @@ built to avoid. from __future__ import annotations -import logging -from pathlib import Path +from dataclasses import dataclass +from typing import TYPE_CHECKING from steam_installer import deps, runlog +if TYPE_CHECKING: + import logging + from pathlib import Path + DEFAULT_MAX_DOWNLOADS = 32 +# Windows depots only: a game this tool can map to Proton ships no other kind. +OS_NAME = "windows" + # depotdownloader's licence check refusing a DLC app. The one failure that is # expected: most games list DLC the account never bought. NOT_OWNED_MARKER = "is not available from this account" -def _command( - appid: int, - install_dir: Path, - username: str, - language: str, - os_name: str, - max_downloads: int, -) -> list[str]: +@dataclass(frozen=True) +class Pull: + """Where and as whom depotdownloader pulls content, and in which language.""" + + install_dir: Path + username: str + language: str = "english" + max_downloads: int = DEFAULT_MAX_DOWNLOADS + + +def _command(appid: int, pull: Pull) -> list[str]: return [ deps.depotdownloader_bin(), "-app", str(appid), "-os", - os_name, + OS_NAME, "-language", - language, + pull.language, "-max-downloads", - str(max_downloads), + str(pull.max_downloads), "-username", - username, + pull.username, "-remember-password", "-dir", - str(install_dir), + str(pull.install_dir), ] -def download( - appid: int, - install_dir: Path, - username: str, - logger: logging.Logger, - language: str = "english", - os_name: str = "windows", - max_downloads: int = DEFAULT_MAX_DOWNLOADS, -) -> int: - """Run depotdownloader against `install_dir`, streaming its own progress output live. +def download(appid: int, pull: Pull, logger: logging.Logger) -> int: + """Run depotdownloader against the install dir, streaming its progress live. Returns the exit code rather than raising: the caller has to distinguish "download failed" from "download fine, manifest step failed" to decide whether touching the manifest is safe at all. """ - install_dir.mkdir(parents=True, exist_ok=True) - cmd = _command(appid, install_dir, username, language, os_name, max_downloads) - returncode, _ = runlog.run_logged(cmd, logger) + pull.install_dir.mkdir(parents=True, exist_ok=True) + returncode, _ = runlog.run_logged(_command(appid, pull), logger) return returncode -def download_dlc( - dlc_appid: int, - install_dir: Path, - username: str, - logger: logging.Logger, - language: str = "english", - os_name: str = "windows", - max_downloads: int = DEFAULT_MAX_DOWNLOADS, -) -> bool | None: - """Fetch one DLC app into the game's own `install_dir`. +def download_dlc(dlc_appid: int, pull: Pull, logger: logging.Logger) -> bool | None: + """Fetch one DLC app into the game's own install dir. Returns True when downloaded, None when the account does not own it, and False for any other failure -- which the caller must treat as fatal. """ - cmd = _command(dlc_appid, install_dir, username, language, os_name, max_downloads) - returncode, output = runlog.run_logged(cmd, logger) + returncode, output = runlog.run_logged(_command(dlc_appid, pull), logger) if returncode == 0: return True return None if NOT_OWNED_MARKER in output else False diff --git a/_installer_import/steam_installer/deps.py b/steam_installer/deps.py similarity index 73% rename from _installer_import/steam_installer/deps.py rename to steam_installer/deps.py index f9cb3f0..d0fd4e4 100644 --- a/_installer_import/steam_installer/deps.py +++ b/steam_installer/deps.py @@ -3,15 +3,20 @@ from __future__ import annotations import os -import shutil from pathlib import Path +import shutil + +from steam_installer import errors TOOL_HINTS = { - "depotdownloader": "Run ./install.sh, which fetches it from the SteamRE release.", + "depotdownloader": ( + "Run steam_installer/install_deps.sh, " + "which fetches it from the SteamRE release." + ), } -# The copy install.sh manages. Preferred over whatever PATH finds first: each +# The copy install_deps.sh manages. Preferred over whatever PATH finds first: each # depotdownloader binary keeps its remembered login in its own IsolatedStorage # slot, keyed by its path. With an AUR copy in /usr/bin as well, a terminal run # (PATH has ~/.local/bin) and a sudo'd run (secure_path does not) used two @@ -20,12 +25,12 @@ TOOL_HINTS = { MANAGED_DEPOTDOWNLOADER = Path.home() / ".local" / "bin" / "depotdownloader" -class DependencyError(Exception): - pass +class DependencyError(errors.InstallerError): + """A tool this installer shells out to is missing.""" def depotdownloader_bin() -> str: - """The depotdownloader to run: install.sh's own copy, else PATH's.""" + """The depotdownloader to run: install_deps.sh's own copy, else PATH's.""" if os.access(MANAGED_DEPOTDOWNLOADER, os.X_OK): return str(MANAGED_DEPOTDOWNLOADER) return "depotdownloader" @@ -43,6 +48,7 @@ def missing_tools() -> list[str]: def require() -> None: + """Fail with the fix when a required tool is missing.""" missing = missing_tools() if missing: details = "; ".join( diff --git a/_installer_import/steam_installer/dlc.py b/steam_installer/dlc.py similarity index 81% rename from _installer_import/steam_installer/dlc.py rename to steam_installer/dlc.py index bd621f2..29eb847 100644 --- a/_installer_import/steam_installer/dlc.py +++ b/steam_installer/dlc.py @@ -14,18 +14,11 @@ the install before the manifest is touched, like a failed base download. from __future__ import annotations -from pathlib import Path - from steam_installer import appinfo, depotdl, runlog def download_owned( - appid: int, - install_dir: Path, - username: str, - language: str, - max_downloads: int, - log: runlog.RunLog, + appid: int, pull: depotdl.Pull, log: runlog.RunLog ) -> appinfo.DepotPlan | None: """Download every owned DLC of `appid` that has depots of its own. @@ -35,18 +28,11 @@ def download_owned( than not owning it. """ logger = log.logger - plans = appinfo.get_dlc_plans(appid, language) + plans = appinfo.get_dlc_plans(appid, pull.language) downloaded = appinfo.DepotPlan(required=set(), shared=set()) for dlc_appid, depots in sorted(plans.items()): log.section(f"depotdownloader DLC {dlc_appid}") - outcome = depotdl.download_dlc( - dlc_appid=dlc_appid, - install_dir=install_dir, - username=username, - language=language, - max_downloads=max_downloads, - logger=logger, - ) + outcome = depotdl.download_dlc(dlc_appid=dlc_appid, pull=pull, logger=logger) if outcome is None: logger.info("DLC %d is not owned by this account -- skipped.", dlc_appid) continue diff --git a/steam_installer/errors.py b/steam_installer/errors.py new file mode 100644 index 0000000..a729fae --- /dev/null +++ b/steam_installer/errors.py @@ -0,0 +1,45 @@ +"""Base class of every error this package raises on purpose, and the crash guard.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import logging + from pathlib import Path + from types import TracebackType + + +class InstallerError(Exception): + """A failure the installer reports and exits on, as opposed to a bug.""" + + +class CrashGuard: + """Context manager: log any ``Exception`` with the run-log path, swallow it. + + ``try/except Exception`` would say the same, but pylint's broad-exception + check rejects it. ``KeyboardInterrupt`` and other ``BaseException`` pass. + """ + + def __init__(self, logger: logging.Logger, log_path: Path) -> None: + """Remember where a crash is reported.""" + self.logger = logger + self.log_path = log_path + + def __enter__(self) -> None: + """Nothing to set up.""" + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc: BaseException | None, + tb: TracebackType | None, + ) -> bool: + """Report an ``Exception`` and swallow it; let everything else pass.""" + if not isinstance(exc, Exception): + return False + self.logger.error( + "Run failed with an unhandled error", exc_info=(exc_type, exc, tb) + ) + self.logger.info("Run log (send this): %s", self.log_path) + return True diff --git a/_installer_import/steam_installer/finalize.py b/steam_installer/finalize.py similarity index 82% rename from _installer_import/steam_installer/finalize.py rename to steam_installer/finalize.py index d957f83..ad13507 100644 --- a/_installer_import/steam_installer/finalize.py +++ b/steam_installer/finalize.py @@ -9,10 +9,15 @@ it, is no longer run. Either way every precondition is checked first. from __future__ import annotations import time -from pathlib import Path +from typing import TYPE_CHECKING from steam_installer import appinfo, finish_check, manifest_write, recognize, runlog -from steam_installer.library import SteamLibrary +from steam_installer.manifest_depots import Proven + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary def finish( @@ -21,9 +26,10 @@ def finish( metadata: appinfo.AppMetadata, target: Path, depot_plan: appinfo.DepotPlan, - expected_build_id: str | None, ) -> int: + """Finish the manifest and close out the run.""" logger = log.logger + expected_build_id = appinfo.get_build_id(metadata.appid) manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf" seed = None @@ -62,22 +68,21 @@ def finish( if freed: logger.info("Cleared %.1f GB of now-redundant Steam staging data.", freed / 1e9) + build_proven = finish_check.build_proven( + recognize.discover_cached_depot_manifests(target), + depot_plan.required, + depot_plan.manifests, + ) written = manifest_write.finish_manifest_manually( library=library, appid=metadata.appid, install_dir=target, now_unix=int(time.time()), - dlc_depots=depot_plan.dlc, - seed=seed, - # Proven depot by depot, so it outranks whatever the manifest says. - build_id=( - expected_build_id - if finish_check.build_proven( - recognize.discover_cached_depot_manifests(target), - depot_plan.required, - depot_plan.manifests, - ) - else None + proven=Proven( + dlc_depots=depot_plan.dlc, + seed=seed, + # Proven depot by depot, so it outranks whatever the manifest says. + build_id=expected_build_id if build_proven else None, ), ) log.file_contents("Manifest after manual finish", written) diff --git a/_installer_import/steam_installer/finish_check.py b/steam_installer/finish_check.py similarity index 95% rename from _installer_import/steam_installer/finish_check.py rename to steam_installer/finish_check.py index fbcb644..e5a6130 100644 --- a/_installer_import/steam_installer/finish_check.py +++ b/steam_installer/finish_check.py @@ -6,14 +6,17 @@ write it gates lives in :mod:`manifest_write`. from __future__ import annotations -from pathlib import Path +from typing import TYPE_CHECKING, Any from steam_installer import manifest_write, recognize +if TYPE_CHECKING: + from pathlib import Path + def check_ready_to_finish( install_dir: Path, - app_state: dict, + app_state: dict[str, Any], required_depots: set[str], expected_build_id: str | None, expected_manifests: dict[str, str] | None = None, @@ -45,7 +48,7 @@ def check_ready_to_finish( # refusing is still free. shared = app_state.get("SharedDepots") shared_ids = set(shared) if isinstance(shared, dict) else set() - if cached and not (set(cached) - shared_ids): + if cached and set(cached) <= shared_ids: problems.append( "every cached depot is listed under SharedDepots in the existing " "manifest, so there would be nothing to record as installed" @@ -55,7 +58,8 @@ def check_ready_to_finish( if missing: problems.append( f"depot(s) {', '.join(missing)} have no cached manifest under " - f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR} -- the download did not finish" + f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR} -- " + "the download did not finish" ) expected_manifests = expected_manifests or {} diff --git a/_installer_import/steam_installer/flow.py b/steam_installer/flow.py similarity index 61% rename from _installer_import/steam_installer/flow.py rename to steam_installer/flow.py index 848b6fd..efcc58f 100644 --- a/_installer_import/steam_installer/flow.py +++ b/steam_installer/flow.py @@ -6,7 +6,8 @@ The manifest-finishing half lives in `finalize.py`. from __future__ import annotations -from pathlib import Path +import logging +from typing import TYPE_CHECKING from steam_installer import ( appinfo, @@ -20,10 +21,15 @@ from steam_installer import ( takeover, ) from steam_installer.finalize import finish -from steam_installer.library import SteamLibrary + +if TYPE_CHECKING: + import argparse + from pathlib import Path + + from steam_installer.job import Job -def _prepare_steam(args, logger) -> bool | None: +def prepare_steam(args: argparse.Namespace, logger: logging.Logger) -> bool | None: """Close Steam if it's running. Returns whether it was, or None to abort. Steam owns the manifest and is actively writing the staging tree, so it has @@ -42,38 +48,32 @@ def _prepare_steam(args, logger) -> bool | None: return None try: return steamctl.stop(logger) - except steamctl.SteamStillRunning as exc: - logger.error("%s", exc) + except steamctl.SteamStillRunningError as exc: + logger.log(logging.ERROR, "%s", exc) return None def install( *, - args, + args: argparse.Namespace, log: runlog.RunLog, steam_root: Path, - library: SteamLibrary, - metadata: appinfo.AppMetadata, - install_target: Path, - staged: takeover.Staged | None, - username: str, - language: str, + job: Job, ) -> int: """Adopt anything Steam already fetched, download the rest, make it count.""" logger = log.logger + metadata = job.metadata - steam_was_running = _prepare_steam(args, logger) + steam_was_running = prepare_steam(args, logger) if steam_was_running is None: return 1 try: - returncode = _download_and_recognize( - args, log, library, metadata, install_target, staged, username, language - ) + returncode = _download_and_recognize(args, log, job) if returncode == 0: # Still inside the window where Steam is closed: config.vdf written # under a running client is lost when it exits. - cached = recognize.discover_cached_depot_manifests(install_target) - dlc_depots = appinfo.get_depot_plan(metadata.appid, language).dlc + cached = recognize.discover_cached_depot_manifests(job.install_target) + dlc_depots = appinfo.get_depot_plan(metadata.appid, job.language).dlc compat_apply.after_install( steam_root, metadata.appid, @@ -87,55 +87,62 @@ def install( steamctl.start(logger) +def _adopt_staged(log: runlog.RunLog, job: Job) -> None: + """Hardlink whatever Steam already staged into the install dir.""" + staged = job.staged + if staged is None: + return + logger = log.logger + log.section("Adopting Steam's staged download") + logger.info( + "Steam already staged %.2f GB for this app (%d files). Hardlinking it " + "into place so depotdownloader verifies instead of refetching.", + staged.bytes_staged / 1e9, + staged.file_count, + ) + job.install_target.mkdir(parents=True, exist_ok=True) + takeover.adopt(staged, logger) + + +def _log_plan( + log: runlog.RunLog, job: Job, depot_plan: appinfo.DepotPlan, max_downloads: int +) -> None: + """Record what this run is about to fetch, for the run log.""" + logger = log.logger + log.section("Plan") + logger.debug("language=%s max_downloads=%s", job.language, max_downloads) + logger.debug("required depots: %s", sorted(depot_plan.required)) + logger.debug("shared depots: %s", sorted(depot_plan.shared)) + logger.debug("expected buildid: %s", appinfo.get_build_id(job.metadata.appid)) + manifest_path = job.library.steamapps / f"appmanifest_{job.metadata.appid}.acf" + log.file_contents("Manifest before run", manifest_path) + + def _download_and_recognize( - args, - log: runlog.RunLog, - library: SteamLibrary, - metadata: appinfo.AppMetadata, - install_target: Path, - staged: takeover.Staged | None, - username: str, - language: str, + args: argparse.Namespace, log: runlog.RunLog, job: Job ) -> int: logger = log.logger + metadata, install_target = job.metadata, job.install_target # First thing with Steam closed: whatever happens next, Steam resumes onto # this field, and a stub written from the display name ("Baldur's Gate 3") # would point it at a folder the download never fills. - manifest_write.ensure_installdir_correct(library, metadata.appid, install_target) - - if staged is not None: - log.section("Adopting Steam's staged download") - logger.info( - "Steam already staged %.2f GB for this app (%d files). Hardlinking it " - "into place so depotdownloader verifies instead of refetching.", - staged.bytes_staged / 1e9, - staged.file_count, - ) - install_target.mkdir(parents=True, exist_ok=True) - takeover.adopt(staged, logger) - - depot_plan = appinfo.get_depot_plan(metadata.appid, language) - expected_build_id = appinfo.get_build_id(metadata.appid) - log.section("Plan") - logger.debug("language=%s max_downloads=%s", language, args.max_downloads) - logger.debug("required depots: %s", sorted(depot_plan.required)) - logger.debug("shared depots: %s", sorted(depot_plan.shared)) - logger.debug("expected buildid: %s", expected_build_id) - - manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf" - log.file_contents("Manifest before run", manifest_path) + manifest_write.ensure_installdir_correct( + job.library, metadata.appid, install_target + ) + _adopt_staged(log, job) + depot_plan = appinfo.get_depot_plan(metadata.appid, job.language) + _log_plan(log, job, depot_plan, args.max_downloads) logger.info("Downloading via depotdownloader (fast path)...") log.section("depotdownloader") - returncode = depotdl.download( - appid=metadata.appid, + pull = depotdl.Pull( install_dir=install_target, - username=username, - language=language, + username=job.username, + language=job.language, max_downloads=args.max_downloads, - logger=logger, ) + returncode = depotdl.download(metadata.appid, pull, logger) if returncode != 0: logger.error( "depotdownloader failed (exit %d) -- not touching the manifest.", returncode @@ -144,9 +151,7 @@ def _download_and_recognize( return 1 logger.info("Checking for owned DLC with depots of its own...") - dlc_plan = dlc.download_owned( - metadata.appid, install_target, username, language, args.max_downloads, log - ) + dlc_plan = dlc.download_owned(metadata.appid, pull, log) if dlc_plan is None: logger.info("Run log (send this): %s", log.path) return 1 @@ -163,11 +168,4 @@ def _download_and_recognize( ): logger.debug("depot %s -> manifest %s", depot_id, mid) - return finish( - log, - library, - metadata, - install_target, - depot_plan, - expected_build_id, - ) + return finish(log, job.library, metadata, install_target, depot_plan) diff --git a/_installer_import/steam_installer/ghost.py b/steam_installer/ghost.py similarity index 76% rename from _installer_import/steam_installer/ghost.py rename to steam_installer/ghost.py index f861225..67d9efd 100644 --- a/_installer_import/steam_installer/ghost.py +++ b/steam_installer/ghost.py @@ -25,19 +25,25 @@ Two outcomes, and only one of them deletes anything: from __future__ import annotations -import logging -import time from dataclasses import dataclass -from pathlib import Path +import time +from typing import TYPE_CHECKING, Any from steam_installer import manifest_write, recognize, vdf -from steam_installer.library import SteamLibrary + +if TYPE_CHECKING: + import logging + from pathlib import Path + + from steam_installer.library import SteamLibrary INSTALLED_STATE_FLAG = 4 @dataclass(frozen=True) class GhostReport: + """What the scan found about one app's manifest.""" + appid: int manifest_path: Path install_dir: Path | None @@ -47,6 +53,7 @@ class GhostReport: @property def is_ghost(self) -> bool: + """Whether the manifest claims an install with nothing behind it.""" return bool(self.reasons) @property @@ -56,19 +63,52 @@ class GhostReport: def staging_size(library: SteamLibrary, appid: int) -> int: + """Bytes of staged content for *appid* in *library*.""" staged = library.steamapps / "downloading" / str(appid) if not staged.is_dir(): return 0 return sum(f.stat().st_size for f in staged.rglob("*") if f.is_file()) -def _install_dir(library: SteamLibrary, app_state: dict) -> Path | None: +def _install_dir(library: SteamLibrary, app_state: dict[str, Any]) -> Path | None: installdir = app_state.get("installdir") if not isinstance(installdir, str) or not installdir: return None return library.common / installdir +def _load_state(manifest_path: Path, appid: int) -> dict[str, Any] | None: + """The manifest's AppState, or None when it is absent, unparsable or foreign.""" + if not manifest_path.is_file(): + return None + try: + app_state = recognize.read_app_state(manifest_path) + except vdf.VdfParseError: + return None + if str(app_state.get("appid", "")) != str(appid): + return None + return app_state + + +def _looks_installed_but_empty( + app_state: dict[str, Any], content_bytes: int, install_dir: Path | None +) -> bool: + """Every ghost condition at once; one failing condition rules a ghost out.""" + state_flags = app_state.get("StateFlags", "") + if not (str(state_flags).isdigit() and int(state_flags) & INSTALLED_STATE_FLAG): + return False + if app_state.get("InstalledDepots"): + return False + if str(app_state.get("SizeOnDisk", "0")) != "0" or content_bytes: + return False + # Our own downloader's cache means a real run is in progress against this + # directory; an empty install dir mid-download is not a ghost. + return not ( + install_dir is not None + and recognize.discover_cached_depot_manifests(install_dir) + ) + + def inspect(library: SteamLibrary, appid: int) -> GhostReport: """Classify `appid` in `library`. An empty `reasons` means it is not a ghost. @@ -78,16 +118,9 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport: game. """ manifest_path = library.steamapps / f"appmanifest_{appid}.acf" - empty = GhostReport(appid, manifest_path, None, 0, 0, ()) - - if not manifest_path.is_file(): - return empty - try: - app_state = recognize.read_app_state(manifest_path) - except vdf.VdfParseError: - return empty - if str(app_state.get("appid", "")) != str(appid): - return empty + app_state = _load_state(manifest_path, appid) + if app_state is None: + return GhostReport(appid, manifest_path, None, 0, 0, ()) install_dir = _install_dir(library, app_state) content_bytes = ( @@ -96,24 +129,8 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport: else 0 ) staged = staging_size(library, appid) - known = GhostReport(appid, manifest_path, install_dir, content_bytes, staged, ()) - - state_flags = app_state.get("StateFlags", "") - if not (str(state_flags).isdigit() and int(state_flags) & INSTALLED_STATE_FLAG): - return known - depots = app_state.get("InstalledDepots") - if depots: - return known - if str(app_state.get("SizeOnDisk", "0")) != "0": - return known - if content_bytes: - return known - # Our own downloader's cache means a real run is in progress against this - # directory; an empty install dir mid-download is not a ghost. - if install_dir is not None and recognize.discover_cached_depot_manifests( - install_dir - ): - return known + if not _looks_installed_but_empty(app_state, content_bytes, install_dir): + return GhostReport(appid, manifest_path, install_dir, content_bytes, staged, ()) return GhostReport( appid, @@ -122,7 +139,7 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport: content_bytes, staged, ( - f"manifest claims StateFlags={state_flags} (installed)", + f"manifest claims StateFlags={app_state.get('StateFlags')} (installed)", "InstalledDepots is empty", "SizeOnDisk is 0", "no game content on disk", @@ -154,13 +171,15 @@ def repair( Refuses a warm ghost outright: those bytes belong to `takeover`. """ if not report.is_ghost: - raise ValueError(f"appid {report.appid} is not a ghost; refusing to touch it") + msg = f"appid {report.appid} is not a ghost; refusing to touch it" + raise ValueError(msg) if report.is_warm: - raise ValueError( + msg = ( f"appid {report.appid} has {report.staging_bytes / 1e9:.1f} GB of staged " "Steam content. That is a partial download, not a ghost to delete -- " "install it normally so the bytes are reused." ) + raise ValueError(msg) stamp = int(time.time()) if now_unix is None else now_unix touched: list[Path] = [] diff --git a/_installer_import/install.sh b/steam_installer/install_deps.sh similarity index 70% rename from _installer_import/install.sh rename to steam_installer/install_deps.sh index f9fc334..82bdee9 100755 --- a/_installer_import/install.sh +++ b/steam_installer/install_deps.sh @@ -1,12 +1,13 @@ #!/bin/bash # ============================================================================ -# Install everything this tool shells out to, then set up the dev environment. +# Install everything the Steam game installer shells out to. # # libibus the one runtime library of proton-cachyos-native's 63 # package dependencies a plain Arch desktop may lack (pacman) # depotdownloader official self-contained release from SteamRE/DepotDownloader -# .venv local virtualenv with the package installed editable +# kuhy-credit the installer's one third-party import (user site, pinned in +# requirements.txt) # native Proton proton-cachyos-native, pinned in # steam_installer/data/proton-native.lock, signature- and # sha256-checked, into Steam's compatibilitytools.d @@ -14,8 +15,8 @@ # steamcmd is not needed any more: app info comes from Steam's own cache and # the manifest is written by the tool itself. # -# Idempotent: every step no-ops when its target is already in place, so run.sh -# can call it on every run. Installing libibus runs `sudo -n pacman`; when sudo +# Idempotent: every step no-ops when its target is already in place, so +# steam_installer/run.sh can call it on every run. Installing libibus runs `sudo -n pacman`; when sudo # would prompt, it stops and prints the one command to type. # ============================================================================ @@ -23,8 +24,10 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly SCRIPT_DIR -readonly VENV="${SCRIPT_DIR}/.venv" -readonly STAMP="${VENV}/.deps-ok" +readonly REPO_DIR="${SCRIPT_DIR%/*}" +readonly PYTHON="${SGI_PYTHON:-/usr/bin/python3}" +readonly STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/steam-game-installer" +readonly STAMP="${STATE_DIR}/.deps-ok" readonly DD_REPO="SteamRE/DepotDownloader" readonly DD_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/steam-game-installer/depotdownloader" readonly BIN_DIR="${HOME}/.local/bin" @@ -67,17 +70,34 @@ ensure_libibus() { die "pacman could not install libibus (a stale package db 404s). Run: sudo pacman -Syu libibus" } +# The installer's one third-party import. The daemon launches it with the +# system interpreter as the desktop user, so it must live in that user's site. +ensure_kuhy_credit() { + if "$PYTHON" -c 'import kuhy_credit' >/dev/null 2>&1; then + return 0 + fi + if [[ "$CHECK_ONLY" -eq 1 ]]; then + echo "missing: kuhy-credit -- run steam_installer/install_deps.sh" >&2 + return 1 + fi + local pin + pin="$(grep -m1 '^kuhy-credit' "${REPO_DIR}/requirements.txt")" || + die "no kuhy-credit pin in ${REPO_DIR}/requirements.txt" + "$PYTHON" -m pip install --user --break-system-packages "$pin" || + die "could not install kuhy-credit" +} + # Never runs the package's .INSTALL; see steam_installer/proton_native.py. ensure_native_proton() { - local python="${VENV}/bin/python" + export PYTHONPATH="${REPO_DIR}${PYTHONPATH:+:$PYTHONPATH}" if [[ "$CHECK_ONLY" -eq 1 ]]; then - if [[ -x "$python" ]] && ! "$python" -m steam_installer.proton_native --check; then - echo "missing: proton-cachyos-native at the pinned version -- run ./install.sh" >&2 + if ! "$PYTHON" -m steam_installer.proton_native --check; then + echo "missing: proton-cachyos-native at the pinned version -- run steam_installer/install_deps.sh" >&2 return 1 fi return 0 fi - "$python" -m steam_installer.proton_native || die "native Proton install failed" + "$PYTHON" -m steam_installer.proton_native || die "native Proton install failed" } # Build from source only if the published binary can't be fetched. The upstream @@ -99,7 +119,7 @@ ensure_depotdownloader() { return 0 fi if [[ "$CHECK_ONLY" -eq 1 ]]; then - echo "missing: depotdownloader -- run ./install.sh" >&2 + echo "missing: depotdownloader -- run steam_installer/install_deps.sh" >&2 return 1 fi @@ -127,38 +147,11 @@ ensure_depotdownloader() { die "depotdownloader installed to ${BIN_DIR} but is not on PATH. Add ${BIN_DIR} to PATH." } -# The suite requires 100% coverage (pyproject: fail_under = 100), and one test -# runs `python -m steam_installer.cli` in a subprocess. Coverage in a child is -# only recorded if it calls coverage.process_startup() at interpreter start, -# which is what this .pth does. Without it a clean clone reports 99% and fails. -setup_dev_env() { - if [[ ! -x "${VENV}/bin/python" ]]; then - echo "Creating ${VENV}..." - python3 -m venv "$VENV" - fi - - local python="${VENV}/bin/python" - "$python" -m pip install -q --upgrade pip - # Named outright rather than via an extra: pyproject declares none, and pip - # only *warns* about a missing extra before exiting 0 -- so the old - # `[dev] || fallback` form silently installed no test tools at all on a - # fresh venv, and the .pth below then failed to import coverage. - "$python" -m pip install -q -e "$SCRIPT_DIR" pytest pytest-cov coverage - - local site_packages - site_packages="$("$python" -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')" - cat >"${site_packages}/steam_installer_coverage.pth" <<-'PTH' - import os, coverage; os.environ.get("COVERAGE_PROCESS_START") and coverage.process_startup() - PTH - "$python" -c 'import pytest, coverage' || - die "dev environment incomplete: pytest/coverage did not install" - echo "Dev environment ready (subprocess coverage enabled)." -} - main() { local missing=0 ensure_libibus || missing=1 ensure_depotdownloader || missing=1 + ensure_kuhy_credit || missing=1 if [[ "$CHECK_ONLY" -eq 1 ]]; then ensure_native_proton || missing=1 @@ -166,9 +159,10 @@ main() { exit "$missing" fi - setup_dev_env + ensure_kuhy_credit ensure_native_proton echo "All dependencies present." + mkdir -p "$STATE_DIR" : >"$STAMP" } diff --git a/steam_installer/job.py b/steam_installer/job.py new file mode 100644 index 0000000..38fdb06 --- /dev/null +++ b/steam_installer/job.py @@ -0,0 +1,24 @@ +"""Everything one install run is about, bundled so it travels as one argument.""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer import appinfo, takeover + from steam_installer.library import SteamLibrary + + +@dataclass(frozen=True) +class Job: + """The app, where it goes, what Steam already staged, and as whom.""" + + library: SteamLibrary + metadata: appinfo.AppMetadata + install_target: Path + staged: takeover.Staged | None + username: str + language: str diff --git a/_installer_import/steam_installer/library.py b/steam_installer/library.py similarity index 83% rename from _installer_import/steam_installer/library.py rename to steam_installer/library.py index 5694bd6..4ff41dc 100644 --- a/_installer_import/steam_installer/library.py +++ b/steam_installer/library.py @@ -12,14 +12,18 @@ DEFAULT_STEAM_ROOT = Path.home() / ".local/share/Steam" @dataclass class SteamLibrary: + """One Steam library folder.""" + path: Path @property def common(self) -> Path: + """Folder holding installed games.""" return self.path / "steamapps" / "common" @property def steamapps(self) -> Path: + """The library's ``steamapps`` folder.""" return self.path / "steamapps" @@ -28,13 +32,12 @@ def find_steam_root() -> Path: for candidate in (DEFAULT_STEAM_ROOT, Path.home() / ".steam/steam"): if (candidate / "steamapps").is_dir(): return candidate - raise FileNotFoundError( - "No Steam installation found at ~/.local/share/Steam or ~/.steam/steam" - ) + msg = "No Steam installation found at ~/.local/share/Steam or ~/.steam/steam" + raise FileNotFoundError(msg) def list_libraries(steam_root: Path) -> list[SteamLibrary]: - """Every Steam library folder, parsed from libraryfolders.vdf (plus steam_root itself).""" + """Every Steam library folder in libraryfolders.vdf, plus steam_root itself.""" libraries = [SteamLibrary(steam_root)] vdf_path = steam_root / "steamapps" / "libraryfolders.vdf" if not vdf_path.is_file(): @@ -69,7 +72,7 @@ def find_existing_install( def pick_install_target(libraries: list[SteamLibrary], installdir: str) -> Path: - """Where to install: an existing partial install if one exists, else the primary library.""" + """Where to install: an existing partial install, else the primary library.""" existing = find_existing_install(libraries, installdir) if existing is not None: return existing diff --git a/steam_installer/manifest_depots.py b/steam_installer/manifest_depots.py new file mode 100644 index 0000000..2b2204a --- /dev/null +++ b/steam_installer/manifest_depots.py @@ -0,0 +1,58 @@ +"""What a manifest finish records about depots and builds, apart from file I/O. + +Split out of manifest_write.py (250-line cap). +""" + +from __future__ import annotations + +from dataclasses import dataclass, field + + +@dataclass(frozen=True) +class Proven: + """The extras a finish may add beyond what the manifest on disk already says. + + ``dlc_depots`` maps a DLC depot to its DLC app id. Steam's own manifests + record each DLC depot with that ``dlcappid``, so it is written the same way. + ``build_id``, when given, is a build the caller proved from the cached depot + manifests; it replaces the one Steam recorded. ``seed`` (from + :func:`seed_app_state`) is written instead when there is no manifest. + """ + + dlc_depots: dict[str, str] = field(default_factory=dict) + build_id: str | None = None + seed: dict | None = None + + +def pick_buildid(app_state: dict, proven_build: str | None) -> str: + """The build to record: the proven one, else Steam's, else "0". + + "0" means "no real build installed yet" (Steam uses it as a sentinel) -- a + bare `or` treats that non-empty string as truthy and never falls through to + TargetBuildID, leaving a stale/wrong buildid. + """ + existing = app_state.get("buildid") + recorded = existing if existing and existing != "0" else None + return proven_build or recorded or app_state.get("TargetBuildID") or "0" + + +def installed_depots( + app_state: dict, depot_manifests: dict[str, str], dlc_depots: dict[str, str] +) -> dict: + """InstalledDepots for the cached depots, minus the shared ones. + + A shared depot (e.g. Steamworks Common Redistributables) is installed once + under its owning app and only referenced here. Listing it in + InstalledDepots as though this app owned a copy contradicts SharedDepots. + """ + shared = app_state.get("SharedDepots") + shared_ids = set(shared) if isinstance(shared, dict) else set() + installed: dict = {} + for depot_id, manifest_id in depot_manifests.items(): + if depot_id in shared_ids: + continue + entry = {"manifest": manifest_id, "size": "0"} + if depot_id in dlc_depots: + entry["dlcappid"] = dlc_depots[depot_id] + installed[depot_id] = entry + return installed diff --git a/_installer_import/steam_installer/manifest_write.py b/steam_installer/manifest_write.py similarity index 71% rename from _installer_import/steam_installer/manifest_write.py rename to steam_installer/manifest_write.py index b21b769..8441695 100644 --- a/_installer_import/steam_installer/manifest_write.py +++ b/steam_installer/manifest_write.py @@ -17,10 +17,15 @@ in the run that prompted this comment. from __future__ import annotations import os -from pathlib import Path +from typing import TYPE_CHECKING from steam_installer import recognize, vdf -from steam_installer.library import SteamLibrary +from steam_installer.manifest_depots import Proven, installed_depots, pick_buildid + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary def ensure_installdir_correct( @@ -103,75 +108,48 @@ def finish_manifest_manually( appid: int, install_dir: Path, now_unix: int, - dlc_depots: dict[str, str] | None = None, - build_id: str | None = None, - seed: dict | None = None, + proven: Proven | None = None, ) -> Path: """Patch appmanifest_.acf to StateFlags=4 using only verified-known values. - Patches the manifest on disk, or -- when there is none -- writes `seed` - (from :func:`seed_app_state`) instead. With neither it refuses. - - `dlc_depots` maps a DLC depot to its DLC app id. Steam's own manifests - record each DLC depot with that `dlcappid`, so it is written the same way. - `build_id`, when given, is a build the caller proved from the cached depot - manifests; it replaces the one Steam recorded. + Patches the manifest on disk, or -- when there is none -- writes + ``proven.seed`` instead. With neither it refuses. """ + proven = proven or Proven() manifest_path = library.steamapps / f"appmanifest_{appid}.acf" existed = manifest_path.is_file() - if not existed and seed is None: - raise recognize.RecognizeError( - f"No existing {manifest_path.name} to finish and no seed to write" - ) + if not existed and proven.seed is None: + msg = f"No existing {manifest_path.name} to finish and no seed to write" + raise recognize.RecognizeError(msg) app_state = ( - vdf.parse(manifest_path.read_text()).get("AppState", {}) if existed else seed + vdf.parse(manifest_path.read_text()).get("AppState", {}) + if existed + else proven.seed ) if not isinstance(app_state, dict): - raise recognize.RecognizeError( - f"{manifest_path} is not a valid AppState manifest" - ) + msg = f"{manifest_path} is not a valid AppState manifest" + raise recognize.RecognizeError(msg) depot_manifests = recognize.discover_cached_depot_manifests(install_dir) if not depot_manifests: - raise recognize.RecognizeError( - f"No cached depot manifests found under {install_dir}/{recognize.DEPOTDOWNLOADER_DIR}" - ) - - size_on_disk = measure_content_size(install_dir) - # "0" means "no real build installed yet" (Steam uses it as a - # sentinel) -- a bare `or` treats that non-empty string as truthy and - # never falls through to TargetBuildID, leaving a stale/wrong buildid. - existing_buildid = app_state.get("buildid") - buildid = existing_buildid if existing_buildid and existing_buildid != "0" else None - buildid = build_id or buildid or app_state.get("TargetBuildID") or "0" - - # A shared depot (e.g. Steamworks Common Redistributables) is installed once - # under its owning app and only referenced here. Listing it in - # InstalledDepots as though this app owned a copy contradicts SharedDepots. - shared = app_state.get("SharedDepots") - shared_ids = set(shared) if isinstance(shared, dict) else set() - dlc_depots = dlc_depots or {} - installed_depots: dict = {} - for depot_id, manifest_id in depot_manifests.items(): - if depot_id in shared_ids: - continue - entry = {"manifest": manifest_id, "size": "0"} - if depot_id in dlc_depots: - entry["dlcappid"] = dlc_depots[depot_id] - installed_depots[depot_id] = entry - if not installed_depots: - raise recognize.RecognizeError( - f"Every cached depot for {appid} is a shared depot -- nothing to install" + msg = ( + "No cached depot manifests found under " + f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR}" ) + raise recognize.RecognizeError(msg) + depots = installed_depots(app_state, depot_manifests, proven.dlc_depots) + if not depots: + msg = f"Every cached depot for {appid} is a shared depot -- nothing to install" + raise recognize.RecognizeError(msg) app_state["StateFlags"] = "4" app_state["installdir"] = install_dir.name app_state["LastUpdated"] = str(now_unix) - app_state["SizeOnDisk"] = str(size_on_disk) + app_state["SizeOnDisk"] = str(measure_content_size(install_dir)) app_state["StagingSize"] = "0" - app_state["buildid"] = buildid + app_state["buildid"] = pick_buildid(app_state, proven.build_id) app_state["UpdateResult"] = "0" - app_state["InstalledDepots"] = installed_depots + app_state["InstalledDepots"] = depots app_state.pop("StagedDepots", None) # Steam's own per-DLC progress for a download it started: left in place it # contradicts the settled counters and Steam resumes fetching the DLC. @@ -199,14 +177,14 @@ def backup_once(manifest_path: Path) -> None: def write_atomic(path: Path, text: str) -> None: """Write via a temp file + rename so a crash can't truncate the manifest. - A partial write leaves an unparseable manifest, which Steam and this tool - both then choke on. `os.replace` is atomic within a filesystem, and the + A partial write leaves an unparsable manifest, which Steam and this tool + both then choke on. `Path.replace` is atomic within a filesystem, and the temp file is created alongside the target to stay on the same one. """ tmp = path.with_name(f"{path.name}.tmp{os.getpid()}") try: tmp.write_text(text) - os.replace(tmp, path) + tmp.replace(path) except BaseException: tmp.unlink(missing_ok=True) raise diff --git a/_installer_import/steam_installer/map_installed.py b/steam_installer/map_installed.py similarity index 74% rename from _installer_import/steam_installer/map_installed.py rename to steam_installer/map_installed.py index 8ed5731..dddd81b 100644 --- a/_installer_import/steam_installer/map_installed.py +++ b/steam_installer/map_installed.py @@ -1,8 +1,8 @@ """`--map-installed`: run the compat decision over every installed game. - ./run.sh --map-installed # dry run: print, write nothing - ./run.sh --map-installed --apply # close Steam, map the safe ones - ./run.sh --map-installed --appids 730,10 # decide any cached app (read-only) + steam_installer/run.sh --map-installed # dry run: write nothing + steam_installer/run.sh --map-installed --apply # close Steam, map the safe ones + steam_installer/run.sh --map-installed --appids 730,10 # any cached app, read-only The dry run only reads, so it works with Steam open. `--apply` closes Steam first (it rewrites config.vdf on exit) and restarts it afterwards. @@ -10,8 +10,8 @@ first (it rewrites config.vdf on exit) and restarts it afterwards. from __future__ import annotations -import argparse -from pathlib import Path +import logging +from typing import TYPE_CHECKING, Any from steam_installer import ( appinfo, @@ -24,11 +24,16 @@ from steam_installer import ( ) from steam_installer.library import SteamLibrary, list_libraries +if TYPE_CHECKING: + import argparse + from pathlib import Path + # Steam's own compat tools and runtimes are apps too, but not games. TOOL_TYPES = {"tool", "config"} def add_arguments(parser: argparse.ArgumentParser) -> None: + """Register the ``--map-installed`` options on *parser*.""" parser.add_argument( "--valve-proton", action="store_true", @@ -50,13 +55,14 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: def check_arguments(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None: + """Reject option combinations that make no sense.""" if args.map_installed and args.valve_proton: parser.error("--valve-proton goes with an install, not --map-installed") -def _installed(libraries: list[SteamLibrary]) -> dict[int, dict]: - """appid -> AppState for every manifest in every library.""" - found: dict[int, dict] = {} +def _installed(libraries: list[SteamLibrary]) -> dict[int, dict[str, Any]]: + """Appid -> AppState for every manifest in every library.""" + found: dict[int, dict[str, Any]] = {} for library in libraries: for manifest in sorted(library.steamapps.glob("appmanifest_*.acf")): appid = manifest.stem.removeprefix("appmanifest_") @@ -65,19 +71,20 @@ def _installed(libraries: list[SteamLibrary]) -> dict[int, dict]: return found -def _owned_dlc(app_state: dict) -> bool: +def _owned_dlc(app_state: dict[str, Any]) -> bool: depots = app_state.get("InstalledDepots") entries = depots.values() if isinstance(depots, dict) else [] return any(isinstance(entry, dict) and "dlcappid" in entry for entry in entries) -def _is_tool(block: dict) -> bool: +def _is_tool(block: dict[str, Any]) -> bool: common = block.get("common") kind = common.get("type", "") if isinstance(common, dict) else "" return isinstance(kind, str) and kind.lower() in TOOL_TYPES def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: + """Decide, and with ``--apply`` write, mappings for installed games.""" logger = log.logger installed = _installed(list_libraries(steam_root)) if args.appids: @@ -85,7 +92,7 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: else: appids = sorted(installed) context = compat_apply.Context(steam_root) - blocks = appinfo._cached_blocks(appids) + blocks = appinfo.cached_blocks(appids) to_map: list[int] = [] for appid in appids: block = blocks.get(appid) @@ -95,7 +102,9 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: if _is_tool(block) and not args.appids: continue name = block.get("common", {}).get("name", "?") - decision = context.decide(appid, block, _owned_dlc(installed.get(appid, {}))) + decision = context.decide( + appid, block, owned_dlc=_owned_dlc(installed.get(appid, {})) + ) line = f"{appid} {name}: {decision.describe()}" if decision.map: to_map.append(appid) @@ -109,15 +118,17 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: return _apply(args, logger, context.config, to_map) -def _apply(args, logger, config, to_map: list[int]) -> int: - was_running = flow._prepare_steam(args, logger) +def _apply( + args: argparse.Namespace, logger: logging.Logger, config: Path, to_map: list[int] +) -> int: + was_running = flow.prepare_steam(args, logger) if was_running is None: return 1 try: for appid in to_map: compat_apply.report(logger, appid, compat_map.map_app(config, appid)) except compat_map.CompatMapError as exc: - logger.error("Mapping stopped: %s", exc) + logger.log(logging.ERROR, "Mapping stopped: %s", exc) return 1 finally: if was_running: diff --git a/_installer_import/steam_installer/proton_native.py b/steam_installer/proton_native.py similarity index 85% rename from _installer_import/steam_installer/proton_native.py rename to steam_installer/proton_native.py index 14529af..d74e27b 100644 --- a/_installer_import/steam_installer/proton_native.py +++ b/steam_installer/proton_native.py @@ -1,6 +1,6 @@ """Install the pinned proton-cachyos-native build into Steam's compatibilitytools.d. - python -m steam_installer.proton_native # what install.sh runs + python -m steam_installer.proton_native # what install_deps.sh runs python -m steam_installer.proton_native --check # exit 0 when in place The package comes straight from the CachyOS mirror: the `proton-cachyos-native` @@ -24,18 +24,22 @@ from __future__ import annotations import dataclasses import os +from pathlib import Path import shutil import subprocess import sys import tempfile import urllib.request -from pathlib import Path from steam_installer import proton_pkg, recognize from steam_installer.library import find_steam_root from steam_installer.proton_pkg import FINGERPRINT, KEY, Lock, PkgError TOOL_NAME = "proton-cachyos-native" +# On PATH, as ever (see steamctl.STEAM on why a name and not a literal). +GPGCONF = "gpgconf" +# A generic agent: the mirror answers 403 to urllib's default one. +USER_AGENT = "steam-game-installer" def in_use(tool: Path) -> list[int]: @@ -56,8 +60,11 @@ def in_use(tool: Path) -> list[int]: def _download(url: str, dest: Path) -> None: # A generic agent: the mirror answers 403 to urllib's default one. - request = urllib.request.Request(url, headers={"User-Agent": "steam-game-installer"}) - with urllib.request.urlopen(request, timeout=60) as response, dest.open("wb") as out: + request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) + with ( + urllib.request.urlopen(request, timeout=60) as response, + dest.open("wb") as out, + ): shutil.copyfileobj(response, out, 1 << 20) @@ -67,13 +74,15 @@ def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path _download(lock.repo, db) entry = proton_pkg.find_in_db(db, lock.name) if entry.version != lock.version: - raise PkgError( + msg = ( f"the repo serves {lock.name} {entry.version} but the lock pins " f"{lock.version}: bump steam_installer/data/proton-native.lock " "(version and sha256)" ) + raise PkgError(msg) if entry.sha256 != lock.sha256: - raise PkgError("the repo's sha256 for the pinned version differs from the lock") + msg = "the repo's sha256 for the pinned version differs from the lock" + raise PkgError(msg) package = work / entry.filename url = proton_pkg.package_url(lock, entry.filename) _download(url, package) @@ -85,11 +94,13 @@ def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path proton_pkg.verify_signature(package, sig, key, fingerprint, home) finally: subprocess.run( - ["gpgconf", "--homedir", str(home), "--kill", "all"], - check=False, capture_output=True, + [GPGCONF, "--homedir", str(home), "--kill", "all"], + check=False, + capture_output=True, ) if proton_pkg.sha256_file(package) != lock.sha256: - raise PkgError("the downloaded package's sha256 differs from the pinned sha256") + msg = "the downloaded package's sha256 differs from the pinned sha256" + raise PkgError(msg) return package @@ -140,8 +151,8 @@ def ensure( busy = in_use(tool) if tool.exists() else [] if busy: return ( - f"deferred: Steam or a game is running (pid {', '.join(map(str, busy))}); " - "rerun with Steam closed" + "deferred: Steam or a game is running " + f"(pid {', '.join(map(str, busy))}); rerun with Steam closed" ) _swap(package, tools_dir, tool, lock.version) return f"{verb} ({checks})" @@ -157,7 +168,7 @@ def main(argv: list[str] | None = None) -> int: try: tools_dir = find_steam_root() / "compatibilitytools.d" except FileNotFoundError: - print("native Proton skipped: no Steam installation") + sys.stdout.write("native Proton skipped: no Steam installation\n") return 0 if argv and argv[0] == "--check": stamp = tools_dir / TOOL_NAME / "VERSION" @@ -165,9 +176,9 @@ def main(argv: list[str] | None = None) -> int: try: status = ensure(tools_dir, lock) except (PkgError, OSError) as exc: - print(f"error: native Proton: {exc}", file=sys.stderr) + sys.stderr.write(f"error: native Proton: {exc}\n") return 1 - print(f"native Proton {status}") + sys.stdout.write(f"native Proton {status}\n") return 0 diff --git a/_installer_import/steam_installer/proton_pkg.py b/steam_installer/proton_pkg.py similarity index 78% rename from _installer_import/steam_installer/proton_pkg.py rename to steam_installer/proton_pkg.py index 74ab6e8..cdd760e 100644 --- a/_installer_import/steam_installer/proton_pkg.py +++ b/steam_installer/proton_pkg.py @@ -12,20 +12,25 @@ Pinned key: 882DCFE48E2051D48E2562ABF3B607488DB35A47 ("CachyOS for the pin. Taken on 2026-11-22 from two other sources instead: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x882DCFE48E2051D48E2562ABF3B607488DB35A47 (the key file) and the long id F3B607488DB35A47 that CachyOS's own installer -imports, https://github.com/CachyOS/cachyos-repo-add-script/blob/5b27dc1eff276e104077cfaf75798b4c290eaef4/cachyos-repo.sh#L156 +imports: +https://github.com/CachyOS/cachyos-repo-add-script/blob/5b27dc1eff276e104077cfaf75798b4c290eaef4/cachyos-repo.sh#L156 """ from __future__ import annotations +from dataclasses import dataclass import gzip import hashlib +from pathlib import Path import re import subprocess import tarfile -from dataclasses import dataclass -from pathlib import Path from urllib.parse import quote +from steam_installer import errors + +# On PATH, as ever (see steamctl.STEAM on why a name and not a literal). +GPG = "gpg" DATA = Path(__file__).parent / "data" LOCK = DATA / "proton-native.lock" KEY = DATA / "cachyos_signing_key.asc" @@ -33,12 +38,14 @@ FINGERPRINT = "882DCFE48E2051D48E2562ABF3B607488DB35A47" PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/" -class PkgError(Exception): - pass +class PkgError(errors.InstallerError): + """A package failed a check and must not be installed.""" @dataclass(frozen=True) class Lock: + """The pinned package: name, version and checksum.""" + name: str repo: str # URL of the .db version: str @@ -47,6 +54,8 @@ class Lock: @dataclass(frozen=True) class DbEntry: + """One package's entry in the repo database.""" + filename: str version: str sha256: str @@ -54,12 +63,15 @@ class DbEntry: @dataclass(frozen=True) class MtreeEntry: + """One file's entry in a package's ``.MTREE``.""" + kind: str # "file", "link" or "dir" sha256: str = "" link: str = "" def read_lock(path: Path = LOCK) -> Lock: + """Read the pinned package lock at *path*.""" fields = {} for line in path.read_text().splitlines(): if line.strip() and not line.lstrip().startswith("#"): @@ -68,7 +80,8 @@ def read_lock(path: Path = LOCK) -> Lock: try: return Lock(fields["name"], fields["repo"], fields["version"], fields["sha256"]) except KeyError as exc: - raise PkgError(f"{path.name} has no {exc.args[0]}=") from exc + msg = f"{path.name} has no {exc.args[0]}=" + raise PkgError(msg) from exc def package_url(lock: Lock, filename: str) -> str: @@ -84,8 +97,11 @@ def find_in_db(db: Path, name: str) -> DbEntry: continue fields = _desc(tar.extractfile(member).read().decode()) if fields.get("NAME") == name: - return DbEntry(fields["FILENAME"], fields["VERSION"], fields["SHA256SUM"]) - raise PkgError(f"the repo database lists no {name}") + return DbEntry( + fields["FILENAME"], fields["VERSION"], fields["SHA256SUM"] + ) + msg = f"the repo database lists no {name}" + raise PkgError(msg) def _desc(text: str) -> dict[str, str]: @@ -97,6 +113,7 @@ def _desc(text: str) -> dict[str, str]: def sha256_file(path: Path) -> str: + """Hex sha256 of the file at *path*.""" digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1 << 20), b""): @@ -104,10 +121,13 @@ def sha256_file(path: Path) -> str: return digest.hexdigest() -def _gpg(home: Path, *args: str) -> subprocess.CompletedProcess: +def _gpg(home: Path, *args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( - ["gpg", "--homedir", str(home), "--batch", "--no-tty", *args], - capture_output=True, text=True, check=False, timeout=60, + [GPG, "--homedir", str(home), "--batch", "--no-tty", *args], + capture_output=True, + text=True, + check=False, + timeout=60, ) @@ -125,10 +145,13 @@ def key_fingerprints(key: Path, home: Path) -> list[str]: return out -def verify_signature(package: Path, sig: Path, key: Path, fingerprint: str, home: Path) -> None: +def verify_signature( + package: Path, sig: Path, key: Path, fingerprint: str, home: Path +) -> None: """Fail unless `sig` is a good signature by the pinned primary key.""" if fingerprint not in key_fingerprints(key, home): - raise PkgError("the pinned key file does not hold the pinned fingerprint") + msg = "the pinned key file does not hold the pinned fingerprint" + raise PkgError(msg) result = _gpg(home, "--status-fd", "1", "--verify", str(sig), str(package)) signers = [ line.split()[-1] @@ -136,7 +159,8 @@ def verify_signature(package: Path, sig: Path, key: Path, fingerprint: str, home if line.startswith("[GNUPG:] VALIDSIG ") ] if result.returncode != 0 or signers != [fingerprint]: - raise PkgError(f"package signature does not verify against {fingerprint}") + msg = f"package signature does not verify against {fingerprint}" + raise PkgError(msg) def _unescape(path: str) -> str: @@ -151,7 +175,8 @@ def read_mtree(package: Path) -> dict[str, MtreeEntry]: text = gzip.decompress(tar.extractfile(member).read()).decode() break else: - raise PkgError("package has no .MTREE") + msg = "package has no .MTREE" + raise PkgError(msg) entries: dict[str, MtreeEntry] = {} defaults: dict[str, str] = {} for line in text.splitlines(): @@ -166,8 +191,10 @@ def read_mtree(package: Path) -> dict[str, MtreeEntry]: if not path.startswith(PREFIX) or path == PREFIX: continue values = {**defaults, **values} - entries[path[len(PREFIX):]] = MtreeEntry( - values.get("type", "file"), values.get("sha256digest", ""), values.get("link", "") + entries[path[len(PREFIX) :]] = MtreeEntry( + values.get("type", "file"), + values.get("sha256digest", ""), + values.get("link", ""), ) return entries @@ -203,6 +230,6 @@ def extract(package: Path, dest: Path) -> None: members = [] for member in tar.getmembers(): if member.name.startswith(PREFIX) and member.name != PREFIX: - member.name = member.name[len(PREFIX):] + member.name = member.name[len(PREFIX) :] members.append(member) tar.extractall(dest, members=members, filter="data") diff --git a/_installer_import/steam_installer/recognize.py b/steam_installer/recognize.py similarity index 82% rename from _installer_import/steam_installer/recognize.py rename to steam_installer/recognize.py index a2a9a51..3301834 100644 --- a/_installer_import/steam_installer/recognize.py +++ b/steam_installer/recognize.py @@ -9,12 +9,15 @@ and client process, and whether a manifest's bookkeeping says "done". from __future__ import annotations +from pathlib import Path import re import shutil -from pathlib import Path +from typing import TYPE_CHECKING, Any -from steam_installer import vdf -from steam_installer.library import SteamLibrary +from steam_installer import errors, vdf + +if TYPE_CHECKING: + from steam_installer.library import SteamLibrary MANIFEST_CACHE_RE = re.compile(r"^(\d+)_(\d+)\.manifest$") @@ -26,20 +29,22 @@ DEPOTDOWNLOADER_DIR = ".DepotDownloader" PROC_DIR = Path("/proc") -class RecognizeError(Exception): - pass +class RecognizeError(errors.InstallerError): + """The manifest does not match the install.""" def manifest_reflects_real_install( library: SteamLibrary, appid: int, install_dir: Path ) -> bool: - """Don't trust steamcmd's exit code alone -- verify the manifest actually matches - reality. Confirmed necessary the hard way: steamcmd exited 0 while leaving - `installdir` pointed at a stale, unrelated folder name from an earlier corrupted - manifest state, `InstalledDepots` empty, and `StateFlags` still "update needed" -- - a "clean" exit that hadn't actually recognized anything. Relaunching the Steam - client then treated the real, correctly-downloaded folder as orphaned and wiped - it while starting a fresh (slow, wrong-folder) download. + """Whether the manifest matches the real install, not just steamcmd's exit code. + + Don't trust steamcmd's exit code alone. Confirmed necessary the hard way: + steamcmd exited 0 while leaving `installdir` pointed at a stale, unrelated + folder name from an earlier corrupted manifest state, `InstalledDepots` + empty, and `StateFlags` still "update needed" -- a "clean" exit that hadn't + actually recognized anything. Relaunching the Steam client then treated the + real, correctly-downloaded folder as orphaned and wiped it while starting a + fresh (slow, wrong-folder) download. """ manifest_path = library.steamapps / f"appmanifest_{appid}.acf" if not manifest_path.is_file(): @@ -56,7 +61,7 @@ def manifest_reflects_real_install( ) -def download_counters_settled(app_state: dict) -> bool: +def download_counters_settled(app_state: dict[str, Any]) -> bool: """Whether the download bookkeeping agrees that nothing is left to fetch. This is the check that was missing when Steam threw away a StateFlags=4 @@ -73,11 +78,11 @@ def download_counters_settled(app_state: dict) -> bool: # ones are not. if total is not None and done is not None and total != done: return False - return app_state.get("StagingSize", "0") == "0" + return bool(app_state.get("StagingSize", "0") == "0") def discover_cached_depot_manifests(install_dir: Path) -> dict[str, str]: - """Depot ID -> manifest ID, read from depotdownloader's own local cache filenames.""" + """Depot ID -> manifest ID, from depotdownloader's local cache filenames.""" cache_dir = install_dir / DEPOTDOWNLOADER_DIR found: dict[str, str] = {} if not cache_dir.is_dir(): @@ -116,7 +121,7 @@ def clear_steam_staging(library: SteamLibrary, appid: int) -> int: return freed -def read_app_state(manifest_path: Path) -> dict: +def read_app_state(manifest_path: Path) -> dict[str, Any]: """The `AppState` block of a manifest, or an empty dict if unreadable.""" if not manifest_path.is_file(): return {} diff --git a/_installer_import/steam_installer/resolve.py b/steam_installer/resolve.py similarity index 83% rename from _installer_import/steam_installer/resolve.py rename to steam_installer/resolve.py index 42fed8e..f8149bb 100644 --- a/_installer_import/steam_installer/resolve.py +++ b/steam_installer/resolve.py @@ -20,12 +20,14 @@ asking for a numeric AppID rather than treating a search failure as fatal. from __future__ import annotations +from dataclasses import dataclass import json +import sys import time import urllib.error import urllib.parse -import urllib.request -from dataclasses import dataclass + +from steam_installer import _http, errors COMMUNITY_SEARCH_URL = "https://steamcommunity.com/actions/SearchApps/{term}" STORE_SEARCH_URL = "https://store.steampowered.com/api/storesearch/?term={term}&cc=us" @@ -45,24 +47,27 @@ RETRY_DELAY = 1.0 @dataclass class SearchResult: + """One candidate from a name search.""" + appid: int name: str -class ResolveError(Exception): - pass +class ResolveError(errors.InstallerError): + """The target could not be resolved to a single app.""" -class SearchUnavailable(Exception): +class SearchUnavailableError(errors.InstallerError): """Neither search endpoint could be reached (network/TLS block, not a code bug).""" def _fetch_json(url: str, timeout: float, *, attempts: int = ATTEMPTS) -> object: - request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) last: Exception = RuntimeError("no attempts made") for attempt in range(attempts): try: - with urllib.request.urlopen(request, timeout=timeout) as response: + with _http.open_https( + url, timeout=timeout, user_agent=USER_AGENT + ) as response: return json.loads(response.read()) except (urllib.error.URLError, TimeoutError, OSError) as exc: last = exc @@ -77,7 +82,8 @@ def _search_community(term: str, timeout: float) -> list[SearchResult]: """Community endpoint. Returns a bare JSON list; appids arrive as strings.""" payload = _fetch_json(COMMUNITY_SEARCH_URL.format(term=term), timeout) if not isinstance(payload, list): - raise TypeError(f"unexpected community search payload: {type(payload)}") + msg = f"unexpected community search payload: {type(payload)}" + raise TypeError(msg) return [ SearchResult(appid=int(item["appid"]), name=item["name"]) for item in payload @@ -89,7 +95,8 @@ def _search_store(term: str, timeout: float) -> list[SearchResult]: """Storefront endpoint. Returns {"items": [{"id": int, "name": str}, ...]}.""" payload = _fetch_json(STORE_SEARCH_URL.format(term=term), timeout) if not isinstance(payload, dict): - raise TypeError(f"unexpected store search payload: {type(payload)}") + msg = f"unexpected store search payload: {type(payload)}" + raise TypeError(msg) return [ SearchResult(appid=int(item["id"]), name=item["name"]) for item in payload.get("items", []) @@ -115,7 +122,7 @@ def search(name: str, *, timeout: float = DEFAULT_TIMEOUT) -> list[SearchResult] KeyError, ) as exc: failures.append(f"{label}: {exc}") - raise SearchUnavailable("; ".join(failures)) + raise SearchUnavailableError("; ".join(failures)) def exact_match(results: list[SearchResult], name: str) -> SearchResult | None: @@ -133,14 +140,16 @@ def exact_match(results: list[SearchResult], name: str) -> SearchResult | None: def prompt_confirm(results: list[SearchResult]) -> SearchResult: """Show numbered candidates and require an explicit choice. Never guesses.""" if not results: - raise ResolveError("No matches found on Steam.") - print("Matches found:") + msg = "No matches found on Steam." + raise ResolveError(msg) + sys.stdout.write("Matches found:\n") for i, result in enumerate(results, start=1): - print(f" {i}. {result.name} (appid {result.appid})") + sys.stdout.write(f" {i}. {result.name} (appid {result.appid})\n") while True: choice = input(f"Which one? [1-{len(results)}, or 0 to cancel]: ").strip() if choice == "0": - raise ResolveError("Cancelled by user.") + msg = "Cancelled by user." + raise ResolveError(msg) if choice.isdigit() and 1 <= int(choice) <= len(results): return results[int(choice) - 1] - print("Invalid choice, try again.") + sys.stdout.write("Invalid choice, try again.\n") diff --git a/_installer_import/run.sh b/steam_installer/run.sh similarity index 61% rename from _installer_import/run.sh rename to steam_installer/run.sh index 8d3aa73..c309d70 100755 --- a/_installer_import/run.sh +++ b/steam_installer/run.sh @@ -2,25 +2,31 @@ # ============================================================================ # Install a Steam game from whatever you happen to have: an AppID, a Steam URL, -# or the game's name. +# or the game's name. Part of steam-backlog-enforcer; the root daemon launches +# `python3 -m steam_installer.cli` itself, so this is the by-hand entry point. # -# ./run.sh 284160 -# ./run.sh https://steamcommunity.com/app/284160/guides/ -# ./run.sh BeamNG.drive -# ./run.sh BeamNG +# steam_installer/run.sh 284160 +# steam_installer/run.sh https://steamcommunity.com/app/284160/guides/ +# steam_installer/run.sh BeamNG.drive +# steam_installer/run.sh BeamNG # # Dependencies install themselves on first run. Every other argument is passed -# straight through to the CLI, so `./run.sh --help` lists the full set. +# straight through to the CLI, so `run.sh --help` lists the full set. # ============================================================================ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly SCRIPT_DIR -readonly VENV_PY="${SCRIPT_DIR}/.venv/bin/python" -readonly STAMP="${SCRIPT_DIR}/.venv/.deps-ok" -readonly INSTALLER="${SCRIPT_DIR}/install.sh" -readonly PROTON_LOCK="${SCRIPT_DIR}/steam_installer/data/proton-native.lock" +readonly REPO_DIR="${SCRIPT_DIR%/*}" +readonly PYTHON="${SGI_PYTHON:-/usr/bin/python3}" +readonly STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/steam-game-installer" +readonly STAMP="${STATE_DIR}/.deps-ok" +readonly INSTALLER="${SCRIPT_DIR}/install_deps.sh" +readonly PROTON_LOCK="${SCRIPT_DIR}/data/proton-native.lock" + +# The package lives in this checkout, not in site-packages. +export PYTHONPATH="${REPO_DIR}${PYTHONPATH:+:$PYTHONPATH}" SKIP_DEPS="${SGI_SKIP_DEPS:-0}" DEPS_ONLY=0 @@ -40,22 +46,19 @@ usage() { echo " --deps-only Install dependencies and exit" echo " -h, --help Show this help, then the installer's own options" echo - if [[ -x "$VENV_PY" ]]; then - "$VENV_PY" -m steam_installer.cli --help - fi + "$PYTHON" -m steam_installer.cli --help exit 0 } # Dependencies are only re-checked when the stamp is missing or older than the # files that decide what gets installed, so a normal run adds no startup cost. deps_are_current() { - [[ -x "$VENV_PY" ]] || return 1 [[ -f "$STAMP" ]] || return 1 [[ "$STAMP" -nt "$INSTALLER" ]] || return 1 - [[ "$STAMP" -nt "${SCRIPT_DIR}/pyproject.toml" ]] || return 1 + [[ "$STAMP" -nt "${REPO_DIR}/pyproject.toml" ]] || return 1 [[ "$STAMP" -nt "$PROTON_LOCK" ]] || return 1 # A bumped pin or a deleted tool folder: the stamp alone would miss both. - "$VENV_PY" -m steam_installer.proton_native --check >/dev/null 2>&1 + "$PYTHON" -m steam_installer.proton_native --check >/dev/null 2>&1 } main() { @@ -67,14 +70,9 @@ main() { exit 0 fi - [[ -x "$VENV_PY" ]] || { - echo "error: no virtualenv at ${VENV_PY}. Run ./install.sh" >&2 - exit 1 - } - - # `-m` rather than the console script, so a stale .venv shim can never - # shadow the working tree. - exec "$VENV_PY" -m steam_installer.cli "${ARGS[@]}" + # `-m` rather than a console script, so a stale install can never shadow + # the working tree. + exec "$PYTHON" -m steam_installer.cli "${ARGS[@]}" } while [[ $# -gt 0 ]]; do diff --git a/_installer_import/steam_installer/runlog.py b/steam_installer/runlog.py similarity index 91% rename from _installer_import/steam_installer/runlog.py rename to steam_installer/runlog.py index ebcd38d..11ce745 100644 --- a/_installer_import/steam_installer/runlog.py +++ b/steam_installer/runlog.py @@ -17,18 +17,18 @@ mirror every byte to both the terminal and the log. from __future__ import annotations +from dataclasses import dataclass import errno import io import logging import os +from pathlib import Path import pty import select import shlex import subprocess import sys import time -from dataclasses import dataclass -from pathlib import Path LOG_DIR = Path.home() / ".local/state/steam-game-installer" LOGGER_NAME = "steam_installer" @@ -63,7 +63,7 @@ class RunLog: self.logger.debug("(unreadable: %s)", exc) -def setup(appid: int | None, verbose: bool = False) -> RunLog: +def setup(appid: int | None, *, verbose: bool = False) -> RunLog: """Start a run log. Console stays terse; the file gets everything at DEBUG.""" LOG_DIR.mkdir(parents=True, exist_ok=True) stamp = time.strftime("%Y%m%d-%H%M%S") @@ -102,6 +102,24 @@ def _point_latest_at(path: Path) -> None: pass +def _spawn_on_pty( + cmd: list[str], master_fd: int, slave_fd: int +) -> subprocess.Popen[bytes]: + """Start `cmd` with its output on the pty; close both ends if it won't start.""" + try: + return subprocess.Popen( + cmd, + stdout=slave_fd, + stderr=slave_fd, + stdin=_stdin_for_child(slave_fd), + close_fds=True, + ) + except OSError: + os.close(master_fd) + os.close(slave_fd) + raise + + def run_logged( cmd: list[str], logger: logging.Logger, @@ -118,18 +136,7 @@ def run_logged( master_fd, slave_fd = pty.openpty() chunks: list[bytes] = [] - try: - process = subprocess.Popen( - cmd, - stdout=slave_fd, - stderr=slave_fd, - stdin=_stdin_for_child(slave_fd), - close_fds=True, - ) - except OSError: - os.close(master_fd) - os.close(slave_fd) - raise + process = _spawn_on_pty(cmd, master_fd, slave_fd) # The child owns the slave end now; holding it open here would keep the # master readable forever and hang the relay after the child exits. os.close(slave_fd) @@ -169,8 +176,11 @@ def _log_output( def _read_pty(fd: int, size: int) -> bytes: - """Indirection so tests can fault-inject the pty read without patching the - global `os.read`, which subprocess itself uses during spawn.""" + """Read from the pty; an indirection so tests can fault-inject it. + + Tests use it instead of patching the global `os.read`, which subprocess + itself uses during spawn. + """ return os.read(fd, size) @@ -184,14 +194,14 @@ def _stdin_for_child(slave_fd: int) -> int: """ try: return sys.stdin.fileno() - except (AttributeError, ValueError, OSError, io.UnsupportedOperation): + except AttributeError, ValueError, OSError, io.UnsupportedOperation: return slave_fd def _relay( master_fd: int, chunks: list[bytes], - process: subprocess.Popen, + process: subprocess.Popen[bytes], timeout: float | None, started: float, ) -> None: diff --git a/_installer_import/steam_installer/steamctl.py b/steam_installer/steamctl.py similarity index 70% rename from _installer_import/steam_installer/steamctl.py rename to steam_installer/steamctl.py index 4ee4572..80946e8 100644 --- a/_installer_import/steam_installer/steamctl.py +++ b/steam_installer/steamctl.py @@ -13,18 +13,24 @@ escalate. from __future__ import annotations -import logging import subprocess import time -from collections.abc import Callable +from typing import TYPE_CHECKING -from steam_installer import recognize +from steam_installer import errors, recognize +if TYPE_CHECKING: + from collections.abc import Callable + import logging + +# Looked up on PATH by the OS, as `steam` always was; a name, not a literal in +# the argv list, because ruff S607 objects to partial executable paths there. +STEAM = "steam" SHUTDOWN_TIMEOUT = 60.0 POLL_INTERVAL = 0.5 -class SteamStillRunning(Exception): +class SteamStillRunningError(errors.InstallerError): """Steam was asked to exit and did not.""" @@ -36,14 +42,14 @@ def stop( ) -> bool: """Shut Steam down. Returns True if it had been running, False if it wasn't. - Raises SteamStillRunning if it refuses to exit, so callers can abort + Raises SteamStillRunningError if it refuses to exit, so callers can abort without having touched anything. """ if not recognize.steam_client_pids(): return False logger.info("Shutting down the Steam client...") - subprocess.run(["steam", "-shutdown"], check=False, capture_output=True) + subprocess.run([STEAM, "-shutdown"], check=False, capture_output=True) waited = 0.0 while waited < timeout: @@ -54,19 +60,25 @@ def stop( waited += POLL_INTERVAL pids = recognize.steam_client_pids() - raise SteamStillRunning( + msg = ( f"Steam is still running after {timeout:.0f}s (pid " f"{', '.join(str(p) for p in pids)}). Close it by hand and re-run." ) + raise SteamStillRunningError(msg) + + +def _spawn_detached(cmd: list[str]) -> subprocess.Popen[bytes]: + """Start `cmd` in its own session with no output; the caller never waits.""" + return subprocess.Popen( + cmd, + start_new_session=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) def start(logger: logging.Logger) -> None: """Relaunch Steam, detached, so it outlives this process.""" logger.info("Restarting the Steam client...") # Detached on purpose: Steam outlives this process and is never waited on. - subprocess.Popen( - ["steam"], - start_new_session=True, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) + _spawn_detached([STEAM]) diff --git a/_installer_import/steam_installer/takeover.py b/steam_installer/takeover.py similarity index 94% rename from _installer_import/steam_installer/takeover.py rename to steam_installer/takeover.py index 9ea9bea..bd339a0 100644 --- a/_installer_import/steam_installer/takeover.py +++ b/steam_installer/takeover.py @@ -32,16 +32,21 @@ but they are not frozen either -- on ext4 there is no reflink to get both. from __future__ import annotations -import logging -import os from dataclasses import dataclass -from pathlib import Path +import os +from typing import TYPE_CHECKING -from steam_installer.library import SteamLibrary +if TYPE_CHECKING: + import logging + from pathlib import Path + + from steam_installer.library import SteamLibrary @dataclass(frozen=True) class Staged: + """Content Steam staged for a download in progress.""" + appid: int staging_dir: Path install_dir: Path @@ -51,6 +56,8 @@ class Staged: @dataclass(frozen=True) class Adoption: + """What taking over staged content achieved.""" + linked: int skipped: int failed: int diff --git a/_installer_import/steam_installer/target.py b/steam_installer/target.py similarity index 78% rename from _installer_import/steam_installer/target.py rename to steam_installer/target.py index 3bf6ec2..5b43ace 100644 --- a/_installer_import/steam_installer/target.py +++ b/steam_installer/target.py @@ -9,18 +9,18 @@ Four accepted forms, all resolving to the same app: BeamNG Whatever the input, the appid is always confirmed against Steam's own app -cache (`appcache/appinfo.vdf`) before anything acts on it, so the name and installdir used -downstream come from Steam rather than from a search index that ranks +cache (`appcache/appinfo.vdf`) before anything acts on it, so the name and +installdir used downstream come from Steam rather than from a search index that ranks "Portal 2" above "Portal". """ from __future__ import annotations +from dataclasses import dataclass import re import sys -from dataclasses import dataclass -from steam_installer import appinfo, resolve +from steam_installer import appinfo, errors, resolve # `app/` preceded by a delimiter: matches both store and community # URLs, any trailing path or query, and `steam://` links. Not anchored to a @@ -28,12 +28,14 @@ from steam_installer import appinfo, resolve APP_URL_RE = re.compile(r"(?:^|[/?&])app/(\d+)(?:[/?#]|$)") -class TargetError(Exception): +class TargetError(errors.InstallerError): """The input could not be resolved to exactly one Steam app.""" @dataclass(frozen=True) class Resolved: + """A target resolved to an app and its metadata.""" + metadata: appinfo.AppMetadata source: str # "flag" | "appid" | "url" | "exact" | "picked" @@ -53,19 +55,20 @@ def _confirm(appid: int, source: str) -> Resolved: try: return Resolved(appinfo.get_metadata(appid), source) except appinfo.AppInfoError as exc: - raise TargetError(f"appid {appid} is not a known Steam app ({exc})") from exc + msg = f"appid {appid} is not a known Steam app ({exc})" + raise TargetError(msg) from exc def _by_name(text: str, *, interactive: bool) -> Resolved: try: results = resolve.search(text) - except resolve.SearchUnavailable as exc: - raise TargetError( - f"Steam search is unavailable ({exc}). Pass the numeric AppID instead." - ) from exc + except resolve.SearchUnavailableError as exc: + msg = f"Steam search is unavailable ({exc}). Pass the numeric AppID instead." + raise TargetError(msg) from exc if not results: - raise TargetError(f"No Steam app matches {text!r}.") + msg = f"No Steam app matches {text!r}." + raise TargetError(msg) if len(results) == 1: return _confirm(results[0].appid, "only match") @@ -77,13 +80,14 @@ def _by_name(text: str, *, interactive: bool) -> Resolved: if not interactive: # Fail closed. Guessing here is how the wrong 90 GB gets downloaded. - print(f"{text!r} is ambiguous. Candidates:", file=sys.stderr) + sys.stderr.write(f"{text!r} is ambiguous. Candidates:\n") for result in results: - print(f" {result.appid} {result.name}", file=sys.stderr) - raise TargetError( + sys.stderr.write(f" {result.appid} {result.name}\n") + msg = ( "Ambiguous name in a non-interactive run. Re-run with the exact " "title or the numeric AppID." ) + raise TargetError(msg) return _confirm(resolve.prompt_confirm(results).appid, "picked") @@ -95,7 +99,8 @@ def resolve_target( if appid is not None: return _confirm(appid, "flag") if text is None: - raise TargetError("No target given.") + msg = "No target given." + raise TargetError(msg) literal = parse_literal(text) if literal is not None: diff --git a/steam_installer/tests/__init__.py b/steam_installer/tests/__init__.py new file mode 100644 index 0000000..7960209 --- /dev/null +++ b/steam_installer/tests/__init__.py @@ -0,0 +1 @@ +"""Installer tests.""" diff --git a/steam_installer/tests/_acf_helpers.py b/steam_installer/tests/_acf_helpers.py new file mode 100644 index 0000000..89a8bb2 --- /dev/null +++ b/steam_installer/tests/_acf_helpers.py @@ -0,0 +1,79 @@ +"""Manifest helpers for tests that need a Steam library on disk.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from steam_installer import vdf + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + + +GHOST_ACF = """"AppState" +{ +\t"appid"\t\t"284160" +\t"universe"\t\t"1" +\t"name"\t\t"BeamNG.drive" +\t"StateFlags"\t\t"4" +\t"installdir"\t\t"BeamNG.drive" +\t"LastUpdated"\t\t"0" +\t"SizeOnDisk"\t\t"0" +\t"StagingSize"\t\t"0" +\t"buildid"\t\t"24617469" +\t"DownloadType"\t\t"1" +\t"BytesToDownload"\t\t"0" +\t"BytesDownloaded"\t\t"0" +\t"TargetBuildID"\t\t"24617469" +\t"InstalledDepots" +\t{ +\t} +\t"UserConfig" +\t{ +\t\t"language"\t\t"english" +\t} +} +""" + + +def _write(library: SteamLibrary, appid: int, text: str) -> Path: + path = library.steamapps / f"appmanifest_{appid}.acf" + path.write_text(text) + return path + + +def _real_install( + library: SteamLibrary, + appid: int = 475150, + installdir: str = "RealGame", + size: str = "13908166765", +) -> Path: + """A manifest shaped like the genuinely installed apps in a live library.""" + _write( + library, + appid, + vdf.dumps( + "AppState", + { + "appid": str(appid), + "StateFlags": "4", + "installdir": installdir, + "SizeOnDisk": size, + "InstalledDepots": {"475151": {"manifest": "123"}}, + }, + ), + ) + content = library.common / installdir + content.mkdir(parents=True, exist_ok=True) + (content / "game.pak").write_bytes(b"x" * 4096) + return content + + +def stamp_native_tool(root: Path, version: str) -> None: + """Put a ready native Proton (manifest and version stamp) under *root*.""" + tool = root / "compatibilitytools.d" / "proton-cachyos-native" + tool.mkdir(parents=True) + (tool / "toolmanifest.vdf").write_text("x") + (tool / "VERSION").write_text(version + "\n") diff --git a/_installer_import/tests/_appinfo_blob.py b/steam_installer/tests/_appinfo_blob.py similarity index 93% rename from _installer_import/tests/_appinfo_blob.py rename to steam_installer/tests/_appinfo_blob.py index 118926f..86a78e0 100644 --- a/_installer_import/tests/_appinfo_blob.py +++ b/steam_installer/tests/_appinfo_blob.py @@ -37,8 +37,9 @@ def encode(apps: dict[int, dict], change_number: int = 41062472) -> bytes: body = bytearray() for appid, data in apps.items(): kv = _kv({"appinfo": data}, strings) - header = struct.pack(" None: + monkeypatch.setattr(depotdl, "download", lambda appid, pull, logger: returncode) + + +def proven_plan( + monkeypatch: pytest.MonkeyPatch, + install_dir: Path, + shared_owner: dict[str, str] | None = None, + shared: set[str] | None = None, +) -> str: + """Stub the depot plan so the cached manifest proves the public build. + + *shared* defaults to the depots *shared_owner* names. Returns the gid. + """ + gid = next(iter(recognize.discover_cached_depot_manifests(install_dir).values())) + shared_depots = set(shared_owner or ()) if shared is None else shared + monkeypatch.setattr( + appinfo, + "get_depot_plan", + lambda appid, language: appinfo.DepotPlan( + {"1771303"}, + shared_depots, + manifests={"1771303": gid}, + shared_owner=dict(shared_owner or {}), + ), + ) + return gid diff --git a/steam_installer/tests/_map_data.py b/steam_installer/tests/_map_data.py new file mode 100644 index 0000000..2df0312 --- /dev/null +++ b/steam_installer/tests/_map_data.py @@ -0,0 +1,31 @@ +"""Shared data for the map-installed tests.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from steam_installer import compat_map, proton_pkg + +if TYPE_CHECKING: + from pathlib import Path + +LOCKED = proton_pkg.read_lock().version +CONFIG = ( + '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n' + '\t\t\t"Steam"\n\t\t\t{\n\t\t\t}\n\t\t}\n\t}\n}\n' +) +PLAIN = { + "common": {"name": "Plain", "oslist": "windows", "type": "Game"}, + "depots": {"11": {"config": {"oslist": "windows"}}}, +} +NATIVE = {"common": {"name": "Native", "oslist": "windows,linux", "type": "Game"}} +SHARED = { + "common": {"name": "Shared", "oslist": "windows", "type": "Game"}, + "depots": {"31": {"depotfromapp": "228980"}}, +} +CS = {"common": {"name": "CS", "oslist": "windows,linux", "type": "Game"}} +TOOL = {"common": {"name": "Proton Experimental", "type": "Tool"}} + + +def _mapped(root: Path, appid: int) -> tuple[str, str] | None: + return compat_map.current(root / "config" / "config.vdf", appid) diff --git a/_installer_import/tests/_proton_fixture.py b/steam_installer/tests/_proton_fixture.py similarity index 73% rename from _installer_import/tests/_proton_fixture.py rename to steam_installer/tests/_proton_fixture.py index dec074c..6310a25 100644 --- a/_installer_import/tests/_proton_fixture.py +++ b/steam_installer/tests/_proton_fixture.py @@ -7,13 +7,22 @@ the real mirror serves. from __future__ import annotations +from dataclasses import dataclass import gzip import hashlib import io +import shutil import subprocess import tarfile -from dataclasses import dataclass -from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Callable + from pathlib import Path + + from steam_installer.proton_pkg import Lock + + type Setup = tuple[Path, Lock, Callable[..., str]] PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/" VERSION = "1:11.0.20261015-1" @@ -27,8 +36,14 @@ FILES = { LINKS = {"files/bin/msidb": "wine"} +GPG = shutil.which("gpg") or "gpg" +GPGCONF = shutil.which("gpgconf") or "gpgconf" + + @dataclass class Repo: + """Repo.""" + root: Path # served as the mirror: holds cachyos.db, the package, the .sig key: Path fingerprint: str @@ -37,10 +52,12 @@ class Repo: @property def db_url(self) -> str: + """Db url.""" return (self.root / "cachyos.db").as_uri() @property def package(self) -> Path: + """Package.""" return self.root / FILENAME @@ -63,7 +80,9 @@ def _add(tar: tarfile.TarFile, name: str, data: bytes) -> None: tar.addfile(info, io.BytesIO(data)) -def build_package(path: Path, files: dict[str, bytes] = FILES) -> None: +def build_package(path: Path, files: dict[str, bytes] | None = None) -> None: + """Build package.""" + files = FILES if files is None else files with tarfile.open(path, "w:zst") as tar: _add(tar, ".INSTALL", b"rm -rf /\n") # never run, never extracted _add(tar, ".MTREE", _mtree()) @@ -77,7 +96,10 @@ def build_package(path: Path, files: dict[str, bytes] = FILES) -> None: tar.addfile(info) -def build_db(path: Path, version: str, sha256: str, name: str = "proton-cachyos-native") -> None: +def build_db( + path: Path, version: str, sha256: str, name: str = "proton-cachyos-native" +) -> None: + """Build db.""" desc = ( f"%FILENAME%\n{name}-{version}-x86_64.pkg.tar.zst\n\n%NAME%\n{name}\n\n" f"%VERSION%\n{version}\n\n%SHA256SUM%\n{sha256}\n\n" @@ -89,24 +111,37 @@ def build_db(path: Path, version: str, sha256: str, name: str = "proton-cachyos- def gpg(home: Path, *args: str) -> None: + """Gpg.""" subprocess.run( - ["gpg", "--homedir", str(home), "--batch", "--passphrase", "", *args], + [GPG, "--homedir", str(home), "--batch", "--passphrase", "", *args], check=True, capture_output=True, ) def build_repo(tmp: Path) -> Repo: + """Build repo.""" root = tmp / "mirror" root.mkdir() home = tmp / "signer" home.mkdir(mode=0o700) - gpg(home, "--quick-gen-key", "Fake CachyOS ", "ed25519", "sign", "never") + gpg( + home, + "--quick-gen-key", + "Fake CachyOS ", + "ed25519", + "sign", + "never", + ) listing = subprocess.run( - ["gpg", "--homedir", str(home), "--with-colons", "--fingerprint"], - check=True, capture_output=True, text=True, + [GPG, "--homedir", str(home), "--with-colons", "--fingerprint"], + check=True, + capture_output=True, + text=True, ).stdout - fingerprint = next(line.split(":")[9] for line in listing.splitlines() if line.startswith("fpr")) + fingerprint = next( + line.split(":")[9] for line in listing.splitlines() if line.startswith("fpr") + ) key = tmp / "key.asc" gpg(home, "--armor", "--output", str(key), "--export", fingerprint) package = root / FILENAME @@ -118,4 +153,5 @@ def build_repo(tmp: Path) -> Repo: def stop_agent(home: Path) -> None: - subprocess.run(["gpgconf", "--homedir", str(home), "--kill", "all"], check=False) + """Stop agent.""" + subprocess.run([GPGCONF, "--homedir", str(home), "--kill", "all"], check=False) diff --git a/_installer_import/tests/_recognize_helpers.py b/steam_installer/tests/_recognize_helpers.py similarity index 82% rename from _installer_import/tests/_recognize_helpers.py rename to steam_installer/tests/_recognize_helpers.py index 7af8a1d..848a90b 100644 --- a/_installer_import/tests/_recognize_helpers.py +++ b/steam_installer/tests/_recognize_helpers.py @@ -1,9 +1,14 @@ """Fixtures-as-functions shared by the recognize/manifest test files.""" -from pathlib import Path +from __future__ import annotations + +from typing import TYPE_CHECKING from steam_installer import vdf +if TYPE_CHECKING: + from pathlib import Path + def _write_partial_manifest(steamapps: Path, appid: int) -> None: steamapps.mkdir(parents=True, exist_ok=True) @@ -16,7 +21,7 @@ def _write_partial_manifest(steamapps: Path, appid: int) -> None: (steamapps / f"appmanifest_{appid}.acf").write_text(vdf.dumps("AppState", data)) -def _fake_proc(tmp_path, entries): +def _fake_proc(tmp_path: Path, entries: dict[str, str | None]) -> Path: """Build a /proc-shaped tree: {pid: exe_target_path_or_None}.""" proc = tmp_path / "proc" proc.mkdir() diff --git a/steam_installer/tests/_run_helpers.py b/steam_installer/tests/_run_helpers.py new file mode 100644 index 0000000..71d6939 --- /dev/null +++ b/steam_installer/tests/_run_helpers.py @@ -0,0 +1,19 @@ +"""Run a command in a test and capture its text output.""" + +from __future__ import annotations + +import subprocess + + +def run_captured( + argv: list[str], env: dict[str, str], timeout: float = 30 +) -> subprocess.CompletedProcess[str]: + """Run *argv* under *env*, text output captured, never raising on exit code.""" + return subprocess.run( + argv, + capture_output=True, + text=True, + env=env, + timeout=timeout, + check=False, + ) diff --git a/_installer_import/tests/conftest.py b/steam_installer/tests/conftest.py similarity index 62% rename from _installer_import/tests/conftest.py rename to steam_installer/tests/conftest.py index 79abb43..4dd236d 100644 --- a/_installer_import/tests/conftest.py +++ b/steam_installer/tests/conftest.py @@ -9,113 +9,81 @@ several files need them, and importing a fixture across test modules confuses both pytest and the linter. """ +from __future__ import annotations + import logging import os import pathlib +from pathlib import Path +import shutil +from typing import TYPE_CHECKING, Never import pytest + from steam_installer import ( + anticheat, appinfo, + appinfo_cache, cli, config, depotdl, deps, + proton_native, + proton_pkg, recognize, runlog, + steamctl, vdf, ) from steam_installer.library import SteamLibrary +from steam_installer.tests._proton_fixture import VERSION, Repo, build_repo, stop_agent + +if TYPE_CHECKING: + from collections.abc import Iterator + + from steam_installer.tests._proton_fixture import Setup @pytest.fixture -def coverage_subprocess_env(): +def coverage_subprocess_env() -> dict[str, str]: """Env for a child process that should also record coverage.""" env = os.environ.copy() - root = pathlib.Path(__file__).resolve().parent.parent + root = pathlib.Path(__file__).resolve().parents[2] env["COVERAGE_PROCESS_START"] = str(root / "pyproject.toml") env["PYTHONPATH"] = str(root) + os.pathsep + env.get("PYTHONPATH", "") return env -GHOST_ACF = """"AppState" -{ -\t"appid"\t\t"284160" -\t"universe"\t\t"1" -\t"name"\t\t"BeamNG.drive" -\t"StateFlags"\t\t"4" -\t"installdir"\t\t"BeamNG.drive" -\t"LastUpdated"\t\t"0" -\t"SizeOnDisk"\t\t"0" -\t"StagingSize"\t\t"0" -\t"buildid"\t\t"24617469" -\t"DownloadType"\t\t"1" -\t"BytesToDownload"\t\t"0" -\t"BytesDownloaded"\t\t"0" -\t"TargetBuildID"\t\t"24617469" -\t"InstalledDepots" -\t{ -\t} -\t"UserConfig" -\t{ -\t\t"language"\t\t"english" -\t} -} -""" - - -@pytest.fixture -def library(tmp_path): +@pytest.fixture(name="library") +def _library(tmp_path: Path) -> SteamLibrary: + """The library fixture.""" lib = SteamLibrary(tmp_path / "Steam") lib.steamapps.mkdir(parents=True) lib.common.mkdir(parents=True) return lib -def _write(library, appid, text): - path = library.steamapps / f"appmanifest_{appid}.acf" - path.write_text(text) - return path - - -def _real_install(library, appid=475150, installdir="RealGame", size="13908166765"): - """A manifest shaped like the genuinely installed apps in a live library.""" - _write( - library, - appid, - vdf.dumps( - "AppState", - { - "appid": str(appid), - "StateFlags": "4", - "installdir": installdir, - "SizeOnDisk": size, - "InstalledDepots": {"475151": {"manifest": "123"}}, - }, - ), - ) - content = library.common / installdir - content.mkdir(parents=True, exist_ok=True) - (content / "game.pak").write_bytes(b"x" * 4096) - return content - - @pytest.fixture -def isolated_cli(tmp_path, monkeypatch): - monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state") +def isolated_cli( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest +) -> None: + """The isolated cli fixture.""" + # Requested by name: pylint rejects an argument that is only there for its effect. + request.getfixturevalue("cli_logs") monkeypatch.setattr(config, "CONFIG_DIR", tmp_path / "cfg") monkeypatch.setattr(config, "CONFIG_PATH", tmp_path / "cfg" / "config") monkeypatch.delenv(config.ENV_VAR, raising=False) monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}") monkeypatch.setattr(recognize, "steam_client_pids", list) - yield - logger = logging.getLogger(runlog.LOGGER_NAME) - for handler in list(logger.handlers): - logger.removeHandler(handler) - handler.close() @pytest.fixture -def cli_steam(tmp_path, monkeypatch, isolated_cli): +def cli_steam( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest +) -> SteamLibrary: + """A fake Steam library with the cli isolated from this machine's state.""" + # Requested by name: pylint rejects an argument that is only there for its effect. + request.getfixturevalue("isolated_cli") library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) library.common.mkdir(parents=True) @@ -127,19 +95,20 @@ def cli_steam(tmp_path, monkeypatch, isolated_cli): lambda appid: appinfo.AppMetadata(appid, "BeamNG.drive", "BeamNG.drive"), ) monkeypatch.setattr( - depotdl, "download", lambda **kw: pytest.fail("must not download") + depotdl, "download", lambda *a, **kw: pytest.fail("must not download") ) return library @pytest.fixture(autouse=True) -def _no_host_depotdownloader(tmp_path, monkeypatch): +def _no_host_depotdownloader(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """Never let this machine's ~/.local/bin decide which binary a test sees.""" monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", tmp_path / "absent") @pytest.fixture -def cli_logs(tmp_path, monkeypatch): +def cli_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: + """The cli logs fixture.""" monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state") yield logger = logging.getLogger(runlog.LOGGER_NAME) @@ -149,7 +118,7 @@ def cli_logs(tmp_path, monkeypatch): @pytest.fixture -def steam(tmp_path, monkeypatch): +def steam(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[SteamLibrary, Path]: """A Steam tree with KCD2 downloaded but its manifest not yet finished.""" root = tmp_path / "Steam" library = SteamLibrary(root) @@ -189,7 +158,7 @@ def steam(tmp_path, monkeypatch): monkeypatch.setattr(appinfo, "get_build_id", lambda appid: "23914554") # No DLC unless a test says so: the real lookup is a steamcmd call. monkeypatch.setattr(appinfo, "get_dlc_plans", lambda appid, language: {}) - monkeypatch.setattr(recognize, "steam_client_pids", lambda: []) + monkeypatch.setattr(recognize, "steam_client_pids", list) monkeypatch.setattr("builtins.input", lambda *a: "y") # Prompting is tty-gated now, and pytest's stdin is not a tty. monkeypatch.setattr(cli, "_is_interactive", lambda args: True) @@ -197,18 +166,20 @@ def steam(tmp_path, monkeypatch): @pytest.fixture(autouse=True) -def _no_live_compat_side_effects(request, tmp_path, monkeypatch): +def _no_live_compat_side_effects( + request: pytest.FixtureRequest, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """No test may fetch the anti-cheat list or read the real app cache. The modules that test those seams themselves (with their own fakes) are exempt from the part they cover. """ - from steam_installer import anticheat, appinfo_cache - def offline(): - raise OSError("tests are offline") + def offline() -> Never: + msg = "tests are offline" + raise OSError(msg) - module = request.node.module.__name__ + module = request.node.module.__name__.rsplit(".", 1)[-1] if not request.node.name.startswith("test_anticheat_download"): monkeypatch.setattr(anticheat, "_download", offline) monkeypatch.setattr(anticheat, "CACHE", tmp_path / "no-anticheat-cache.json") @@ -218,10 +189,48 @@ def _no_live_compat_side_effects(request, tmp_path, monkeypatch): ) # The live Steam client is never seen, stopped or started by a test: an # unstubbed --apply run once shut down and relaunched kuhy's real Steam. - from steam_installer import steamctl - if module in ("test_steamctl", "test_recognize"): return # these fake subprocess and /proc themselves monkeypatch.setattr(recognize, "steam_client_pids", list) - monkeypatch.setattr(steamctl, "stop", lambda *a, **k: pytest.fail("real steam stop")) - monkeypatch.setattr(steamctl, "start", lambda *a, **k: pytest.fail("real steam start")) + monkeypatch.setattr( + steamctl, "stop", lambda *a, **k: pytest.fail("real steam stop") + ) + monkeypatch.setattr( + steamctl, "start", lambda *a, **k: pytest.fail("real steam start") + ) + + +@pytest.fixture(name="repo", scope="module") +def _repo(tmp_path_factory: pytest.TempPathFactory) -> Iterator[Repo]: + """The repo fixture.""" + built = build_repo(tmp_path_factory.mktemp("repo")) + yield built + stop_agent(built.gpg_home) + + +@pytest.fixture(name="mirror") +def _mirror(repo: Repo, tmp_path: Path) -> Path: + """A private copy of the fake mirror a test may tamper with.""" + root = tmp_path / "mirror" + shutil.copytree(repo.root, root) + return root + + +@pytest.fixture +def setup( + repo: Repo, mirror: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> Setup: + """The setup fixture.""" + tools = tmp_path / "compatibilitytools.d" + tools.mkdir() + lock = proton_pkg.Lock( + "proton-cachyos-native", (mirror / "cachyos.db").as_uri(), VERSION, repo.sha256 + ) + monkeypatch.setattr(proton_native, "in_use", lambda tool: []) + + def run(**kw: object) -> str: + return proton_native.ensure( + tools, lock, key=repo.key, fingerprint=repo.fingerprint, **kw + ) + + return tools, lock, run diff --git a/_installer_import/tests/test_appinfo.py b/steam_installer/tests/test_appinfo.py similarity index 52% rename from _installer_import/tests/test_appinfo.py rename to steam_installer/tests/test_appinfo.py index e509ac9..52abb54 100644 --- a/_installer_import/tests/test_appinfo.py +++ b/steam_installer/tests/test_appinfo.py @@ -1,11 +1,14 @@ +"""Tests for appinfo.""" + +from __future__ import annotations + import pytest -from _appinfo_blob import encode from steam_installer import appinfo -def _app_block(monkeypatch, block): - monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: block) +def _app_block(monkeypatch: pytest.MonkeyPatch, block: dict[str, object]) -> None: + monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: block) # Every real depot that has content ships a manifest on the public branch. @@ -26,7 +29,10 @@ KCD2_DEPOTS = { } -def test_get_depot_plan_selects_only_this_language_and_os(monkeypatch): +def test_get_depot_plan_selects_only_this_language_and_os( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get depot plan selects only this language and os.""" _app_block(monkeypatch, {"depots": KCD2_DEPOTS}) plan = appinfo.get_depot_plan(1771300, language="english", os_name="windows") @@ -36,34 +42,47 @@ def test_get_depot_plan_selects_only_this_language_and_os(monkeypatch): assert plan.all_expected == {"1771302", "1771303", "1771306", "228989"} -def test_get_depot_plan_switches_language(monkeypatch): +def test_get_depot_plan_switches_language(monkeypatch: pytest.MonkeyPatch) -> None: + """Get depot plan switches language.""" _app_block(monkeypatch, {"depots": KCD2_DEPOTS}) plan = appinfo.get_depot_plan(1771300, language="czech") assert "1771305" in plan.required assert "1771306" not in plan.required -def test_get_depot_plan_excludes_other_os(monkeypatch): +def test_get_depot_plan_excludes_other_os(monkeypatch: pytest.MonkeyPatch) -> None: + """Get depot plan excludes other os.""" _app_block( monkeypatch, - {"depots": {"10": {"config": {"oslist": "linux"}, **_PUB}, "11": {"config": {}, **_PUB}}}, + { + "depots": { + "10": {"config": {"oslist": "linux"}, **_PUB}, + "11": {"config": {}, **_PUB}, + } + }, ) plan = appinfo.get_depot_plan(1771300, os_name="windows") assert plan.required == {"11"} -def test_get_depot_plan_skips_optional_depots(monkeypatch): +def test_get_depot_plan_skips_optional_depots(monkeypatch: pytest.MonkeyPatch) -> None: + """Get depot plan skips optional depots.""" _app_block(monkeypatch, {"depots": {"10": {"optional": "1", **_PUB}, "11": _PUB}}) assert appinfo.get_depot_plan(1771300).required == {"11"} -def test_get_depot_plan_rejects_malformed_depots_section(monkeypatch): +def test_get_depot_plan_rejects_malformed_depots_section( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get depot plan rejects malformed depots section.""" _app_block(monkeypatch, {"depots": "not-a-dict"}) with pytest.raises(appinfo.AppInfoError, match="depots"): appinfo.get_depot_plan(1771300) -def test_get_depot_plan_with_no_depots_is_empty(monkeypatch): +def test_get_depot_plan_with_no_depots_is_empty( + monkeypatch: pytest.MonkeyPatch, +) -> None: """An app with no depot list yields nothing required -- callers must not read that as "everything downloaded fine".""" _app_block(monkeypatch, {}) @@ -72,22 +91,32 @@ def test_get_depot_plan_with_no_depots_is_empty(monkeypatch): assert plan.shared == set() -def test_get_build_id_reads_public_branch(monkeypatch): +def test_get_build_id_reads_public_branch(monkeypatch: pytest.MonkeyPatch) -> None: + """Get build id reads public branch.""" _app_block(monkeypatch, {"depots": KCD2_DEPOTS}) assert appinfo.get_build_id(1771300) == "23914554" -def test_get_build_id_missing_branch_returns_none(monkeypatch): +def test_get_build_id_missing_branch_returns_none( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get build id missing branch returns none.""" _app_block(monkeypatch, {"depots": {"branches": {}}}) assert appinfo.get_build_id(1771300) is None -def test_get_build_id_without_depots_returns_none(monkeypatch): +def test_get_build_id_without_depots_returns_none( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get build id without depots returns none.""" _app_block(monkeypatch, {}) assert appinfo.get_build_id(1771300) is None -def test_get_metadata_reads_name_and_installdir(monkeypatch): +def test_get_metadata_reads_name_and_installdir( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get metadata reads name and installdir.""" _app_block( monkeypatch, { @@ -102,7 +131,10 @@ def test_get_metadata_reads_name_and_installdir(monkeypatch): assert metadata.installdir == "KingdomComeDeliverance2" -def test_get_metadata_falls_back_to_config_installdir(monkeypatch): +def test_get_metadata_falls_back_to_config_installdir( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get metadata falls back to config installdir.""" _app_block( monkeypatch, {"common": {"name": "Game"}, "config": {"installdir": "GameDir"}}, @@ -110,86 +142,33 @@ def test_get_metadata_falls_back_to_config_installdir(monkeypatch): assert appinfo.get_metadata(1).installdir == "GameDir" -def test_get_metadata_requires_name(monkeypatch): +def test_get_metadata_requires_name(monkeypatch: pytest.MonkeyPatch) -> None: + """Get metadata requires name.""" _app_block(monkeypatch, {"common": {"installdir": "GameDir"}}) with pytest.raises(appinfo.AppInfoError, match="no name field"): appinfo.get_metadata(1) -def test_get_metadata_requires_installdir(monkeypatch): +def test_get_metadata_requires_installdir(monkeypatch: pytest.MonkeyPatch) -> None: + """Get metadata requires installdir.""" _app_block(monkeypatch, {"common": {"name": "Game"}}) with pytest.raises(appinfo.AppInfoError, match="no installdir"): appinfo.get_metadata(1) -def test_get_metadata_rejects_malformed_common_section(monkeypatch): +def test_get_metadata_rejects_malformed_common_section( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get metadata rejects malformed common section.""" _app_block(monkeypatch, {"common": "not-a-dict"}) with pytest.raises(appinfo.AppInfoError, match="no 'common' section"): appinfo.get_metadata(1) -# The block steamcmd's app_info_print used to return for KCD2, now as Steam -# keeps it in its binary cache. The differential tests below pin that the -# cache gives the same answers the steamcmd text did. -KCD2 = { - "common": {"name": "Kingdom Come: Deliverance II", "installdir": "KingdomComeDeliverance2"}, - "depots": { - "1771303": {"config": {"oslist": "windows"}}, - "branches": {"public": {"buildid": 23914554}}, - }, -} - - -@pytest.fixture -def cache(tmp_path, monkeypatch): - """Point appinfo at a fixture cache; returns a writer for its apps.""" - path = tmp_path / "appinfo.vdf" - monkeypatch.setattr(appinfo.appinfo_cache, "default_path", lambda: path) - - def write(apps): - path.write_bytes(encode(apps)) - - return write - - -def test_metadata_comes_from_steams_cache(cache): - cache({1771300: KCD2}) - metadata = appinfo.get_metadata(1771300) - assert metadata.name == "Kingdom Come: Deliverance II" - assert metadata.installdir == "KingdomComeDeliverance2" - - -def test_get_build_id_through_the_real_parser(cache): - # Stored as an int32 in the binary cache; still a string like steamcmd's. - cache({1771300: KCD2}) - assert appinfo.get_build_id(1771300) == "23914554" - - -def test_app_missing_from_the_cache_fails_with_the_fix(cache): - cache({1771300: KCD2}) - with pytest.raises(appinfo.AppInfoError, match="start Steam once, let it sync, rerun"): - appinfo.get_metadata(999999999) - - -def test_unreadable_cache_fails_with_the_fix(tmp_path, monkeypatch): - monkeypatch.setattr( - appinfo.appinfo_cache, "default_path", lambda: tmp_path / "missing.vdf" - ) - with pytest.raises(appinfo.AppInfoError, match="start Steam once"): - appinfo.get_metadata(1) - - -def test_dlc_missing_from_the_cache_counts_as_not_owned(cache): - """Steam caches app info for what the account owns; on 2026-11-22 only 1828 - of 7102 listed DLC ids were in the cache. Refusing on a missing one would - block installs that worked with steamcmd.""" - game = {**KCD2, "extended": {"listofdlc": "500,600"}} - dlc = {"depots": {"501": {"manifests": {"public": {"gid": "9"}}}}} - cache({1771300: game, 500: dlc}) - assert appinfo.get_dlc_plans(1771300) == {500: {"501": "9"}} - - -def test_get_build_id_handles_non_dict_sections(monkeypatch): +def test_get_build_id_handles_non_dict_sections( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get build id handles non dict sections.""" for block in ( {"depots": "scalar"}, {"depots": {"branches": "scalar"}}, @@ -199,13 +178,22 @@ def test_get_build_id_handles_non_dict_sections(monkeypatch): assert appinfo.get_build_id(1) is None -def test_get_build_id_returns_none_when_buildid_not_a_string(monkeypatch): +def test_get_build_id_returns_none_when_buildid_not_a_string( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get build id returns none when buildid not a string.""" _app_block(monkeypatch, {"depots": {"branches": {"public": {"buildid": {}}}}}) assert appinfo.get_build_id(1) is None -def test_a_shared_install_without_an_owner_is_still_shared(monkeypatch): - _app_block(monkeypatch, {"depots": {"5": {"sharedinstall": "1"}, "6": {"depotfromapp": "228980"}}}) +def test_a_shared_install_without_an_owner_is_still_shared( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A shared install without an owner is still shared.""" + _app_block( + monkeypatch, + {"depots": {"5": {"sharedinstall": "1"}, "6": {"depotfromapp": "228980"}}}, + ) plan = appinfo.get_depot_plan(1) assert plan.shared == {"5", "6"} assert plan.shared_owner == {"6": "228980"} diff --git a/_installer_import/tests/test_appinfo_cache.py b/steam_installer/tests/test_appinfo_cache.py similarity index 54% rename from _installer_import/tests/test_appinfo_cache.py rename to steam_installer/tests/test_appinfo_cache.py index 7e80fc1..e3f1d03 100644 --- a/_installer_import/tests/test_appinfo_cache.py +++ b/steam_installer/tests/test_appinfo_cache.py @@ -1,42 +1,71 @@ """Steam's local binary app-info cache, read without steamcmd.""" +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + import pytest -from _appinfo_blob import encode from steam_installer import appinfo_cache +from steam_installer.appinfo_cache import _parse +from steam_installer.tests._appinfo_blob import encode + +if TYPE_CHECKING: + from pathlib import Path -def _write(tmp_path, apps): +def _write(tmp_path: Path, apps: dict[int, Any]) -> Path: path = tmp_path / "appinfo.vdf" path.write_bytes(encode(apps)) return path -def test_reads_every_app_and_unwraps_appinfo(tmp_path): +def test_reads_every_app_and_unwraps_appinfo(tmp_path: Path) -> None: + """Reads every app and unwraps appinfo.""" path = _write(tmp_path, {10: {"common": {"name": "A"}}, 20: {"common": {}}}) apps = appinfo_cache.load(path) assert set(apps) == {10, 20} assert apps[10] == {"common": {"name": "A"}} -def test_scalars_come_back_as_strings_like_app_info_print(tmp_path): +def test_scalars_come_back_as_strings_like_app_info_print(tmp_path: Path) -> None: """The rest of the tool compares `== "1"` and `!= str(appid)`; an int here would silently drop a DLC app's own depots.""" - data = {"depots": {"5": {"dlcappid": 7, "optional": 1, "f": 1.5, - "gid": (7, 2**63 + 5), "neg": (10, -3)}}} + data = { + "depots": { + "5": { + "dlcappid": 7, + "optional": 1, + "f": 1.5, + "gid": (7, 2**63 + 5), + "neg": (10, -3), + } + } + } apps = appinfo_cache.load(_write(tmp_path, {7: data})) assert apps[7]["depots"]["5"] == { - "dlcappid": "7", "optional": "1", "f": "1.5", - "gid": str(2**63 + 5), "neg": "-3", + "dlcappid": "7", + "optional": "1", + "f": "1.5", + "gid": str(2**63 + 5), + "neg": "-3", } -def test_parse_is_cached_until_the_file_changes(tmp_path, monkeypatch): +def test_parse_is_cached_until_the_file_changes( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Parse is cached until the file changes.""" path = _write(tmp_path, {1: {"common": {"name": "old"}}}) assert appinfo_cache.load(path)[1]["common"]["name"] == "old" calls = [] - real = appinfo_cache._parse - monkeypatch.setattr(appinfo_cache, "_parse", lambda b: calls.append(1) or real(b)) + real = _parse + + def counting(buf: bytes) -> dict[int, dict[str, Any]]: + calls.append(1) + return real(buf) + + monkeypatch.setattr(appinfo_cache, "_parse", counting) appinfo_cache.load(path) assert calls == [] path.write_bytes(encode({1: {"common": {"name": "newer"}}})) @@ -44,14 +73,16 @@ def test_parse_is_cached_until_the_file_changes(tmp_path, monkeypatch): assert calls == [1] -def test_wrong_magic_is_refused(tmp_path): +def test_wrong_magic_is_refused(tmp_path: Path) -> None: + """Wrong magic is refused.""" path = tmp_path / "appinfo.vdf" path.write_bytes(b"\x28\x44\x56\x07" + b"\0" * 20) with pytest.raises(appinfo_cache.CacheError, match="unsupported"): appinfo_cache.load(path) -def test_unknown_value_type_is_refused(tmp_path): +def test_unknown_value_type_is_refused(tmp_path: Path) -> None: + """Unknown value type is refused.""" blob = bytearray(encode({1: {"common": {"name": "A"}}})) blob[16 + 8 + 60] = 9 # first KV type byte path = tmp_path / "appinfo.vdf" @@ -60,15 +91,15 @@ def test_unknown_value_type_is_refused(tmp_path): appinfo_cache.load(path) -def test_missing_file_names_the_fix(tmp_path): +def test_missing_file_names_the_fix(tmp_path: Path) -> None: + """Missing file names the fix.""" with pytest.raises(appinfo_cache.CacheError, match="start Steam once"): appinfo_cache.load(tmp_path / "nope.vdf") -def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path): - import _appinfo_blob - - blob = _appinfo_blob.encode({3: {"x": "y"}}) +def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path: Path) -> None: + """Entry without appinfo wrapper is taken as is.""" + blob = encode({3: {"x": "y"}}) # Rename the "appinfo" wrapper key in the string table. blob = blob.replace(b"appinfo\x00", b"appinfX\x00") path = tmp_path / "appinfo.vdf" @@ -76,6 +107,9 @@ def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path): assert appinfo_cache.load(path)[3] == {"appinfX": {"x": "y"}} -def test_default_path_is_under_the_steam_root(monkeypatch, tmp_path): +def test_default_path_is_under_the_steam_root( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Default path is under the steam root.""" monkeypatch.setattr(appinfo_cache, "find_steam_root", lambda: tmp_path) assert appinfo_cache.default_path() == tmp_path / "appcache" / "appinfo.vdf" diff --git a/steam_installer/tests/test_appinfo_cache_backed.py b/steam_installer/tests/test_appinfo_cache_backed.py new file mode 100644 index 0000000..bf62186 --- /dev/null +++ b/steam_installer/tests/test_appinfo_cache_backed.py @@ -0,0 +1,96 @@ +"""get_metadata/get_build_id/get_dlc_plans answered from Steam's binary cache.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +import pytest + +from steam_installer import appinfo +from steam_installer.tests._appinfo_blob import encode + +if TYPE_CHECKING: + from collections.abc import Callable + from pathlib import Path + + +# The block steamcmd's app_info_print used to return for KCD2, now as Steam +# keeps it in its binary cache. The differential tests below pin that the +# cache gives the same answers the steamcmd text did. +KCD2: dict[str, Any] = { + "common": { + "name": "Kingdom Come: Deliverance II", + "installdir": "KingdomComeDeliverance2", + }, + "depots": { + "1771303": {"config": {"oslist": "windows"}}, + "branches": {"public": {"buildid": 23914554}}, + }, +} + + +@pytest.fixture(name="cache") +def _cache( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> Callable[[dict[int, Any]], None]: + """Point appinfo at a fixture cache; returns a writer for its apps.""" + path = tmp_path / "appinfo.vdf" + monkeypatch.setattr(appinfo.appinfo_cache, "default_path", lambda: path) + + def write(apps: dict[int, Any]) -> None: + path.write_bytes(encode(apps)) + + return write + + +def test_metadata_comes_from_steams_cache( + cache: Callable[[dict[int, Any]], None], +) -> None: + """Metadata comes from Steam's cache.""" + cache({1771300: KCD2}) + metadata = appinfo.get_metadata(1771300) + assert metadata.name == "Kingdom Come: Deliverance II" + assert metadata.installdir == "KingdomComeDeliverance2" + + +def test_get_build_id_through_the_real_parser( + cache: Callable[[dict[int, Any]], None], +) -> None: + # Stored as an int32 in the binary cache; still a string like steamcmd's. + """Get build id through the real parser.""" + cache({1771300: KCD2}) + assert appinfo.get_build_id(1771300) == "23914554" + + +def test_app_missing_from_the_cache_fails_with_the_fix( + cache: Callable[[dict[int, Any]], None], +) -> None: + """App missing from the cache fails with the fix.""" + cache({1771300: KCD2}) + with pytest.raises( + appinfo.AppInfoError, match="start Steam once, let it sync, rerun" + ): + appinfo.get_metadata(999999999) + + +def test_unreadable_cache_fails_with_the_fix( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Unreadable cache fails with the fix.""" + monkeypatch.setattr( + appinfo.appinfo_cache, "default_path", lambda: tmp_path / "missing.vdf" + ) + with pytest.raises(appinfo.AppInfoError, match="start Steam once"): + appinfo.get_metadata(1) + + +def test_dlc_missing_from_the_cache_counts_as_not_owned( + cache: Callable[[dict[int, Any]], None], +) -> None: + """Steam caches app info for what the account owns; on 2026-11-22 only 1828 + of 7102 listed DLC ids were in the cache. Refusing on a missing one would + block installs that worked with steamcmd.""" + game = {**KCD2, "extended": {"listofdlc": "500,600"}} + dlc = {"depots": {"501": {"manifests": {"public": {"gid": "9"}}}}} + cache({1771300: game, 500: dlc}) + assert appinfo.get_dlc_plans(1771300) == {500: {"501": "9"}} diff --git a/_installer_import/tests/test_appinfo_public_manifests.py b/steam_installer/tests/test_appinfo_public_manifests.py similarity index 71% rename from _installer_import/tests/test_appinfo_public_manifests.py rename to steam_installer/tests/test_appinfo_public_manifests.py index 0e7a1ac..5988bce 100644 --- a/_installer_import/tests/test_appinfo_public_manifests.py +++ b/steam_installer/tests/test_appinfo_public_manifests.py @@ -4,11 +4,18 @@ Split from test_appinfo.py (250-line cap): these are the depots-without-a- manifest cases that made a finished BG3 download read as incomplete. """ +from __future__ import annotations + +from typing import TYPE_CHECKING + from steam_installer import appinfo +if TYPE_CHECKING: + import pytest -def _app_block(monkeypatch, block): - monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: block) + +def _app_block(monkeypatch: pytest.MonkeyPatch, block: dict[str, object]) -> None: + monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: block) # Shape taken from a real `app_info_print 1086940` (Baldur's Gate 3, build @@ -30,12 +37,17 @@ BG3_DEPOTS = { } -def test_get_depot_plan_skips_depots_without_a_public_manifest(monkeypatch): +def test_get_depot_plan_skips_depots_without_a_public_manifest( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Get depot plan skips depots without a public manifest.""" _app_block(monkeypatch, {"depots": BG3_DEPOTS}) assert appinfo.get_depot_plan(1086940).required == {"1086941"} -def test_get_depot_plan_records_every_public_manifest(monkeypatch): +def test_get_depot_plan_records_every_public_manifest( + monkeypatch: pytest.MonkeyPatch, +) -> None: """DLC depots too: the finish check proves the build from these gids.""" _app_block(monkeypatch, {"depots": BG3_DEPOTS}) assert appinfo.get_depot_plan(1086940).manifests == { diff --git a/steam_installer/tests/test_cli.py b/steam_installer/tests/test_cli.py new file mode 100644 index 0000000..6e3c97c --- /dev/null +++ b/steam_installer/tests/test_cli.py @@ -0,0 +1,168 @@ +"""End-to-end-ish tests for the CLI's decision logic. + +The bug that motivated all of this lived here, not in the manifest writer: +a step died instantly having done nothing, and main() still wrote +StateFlags=4, printed "launch Steam and confirm Play works" and returned 0. +These tests pin the fail-closed behaviour. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + +import pytest + +from steam_installer import ( + appinfo, + cli, + depotdl, + recognize, +) +from steam_installer.tests._cli_helpers import ARGV, _stub_download + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +def test_downloaded_content_finishes_the_manifest( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """The content is what matters, and the manifest must come out consistent, + not merely flagged installed.""" + library, _install_dir = steam + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + + app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") + assert app_state["StateFlags"] == "4" + for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"): + assert app_state[key] == "0", key + assert recognize.download_counters_settled(app_state) + + +def test_refuses_when_a_required_depot_never_downloaded( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """The exact fail-open case: nothing was really downloaded, so declaring the + app installed would be a fabricated success.""" + library, _install_dir = steam + _stub_download(monkeypatch) + monkeypatch.setattr( + appinfo, + "get_depot_plan", + lambda appid, language: appinfo.DepotPlan({"1771303", "9999999"}, set()), + ) + + assert cli.main(ARGV) == 1 + + app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") + assert app_state["StateFlags"] == "1026" + + +def test_refuses_when_depotdownloader_fails( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """Refuses when depotdownloader fails.""" + library, _ = steam + _stub_download(monkeypatch, returncode=1) + + assert cli.main(ARGV) == 1 + app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf") + assert app_state["StateFlags"] == "1026" + + +@pytest.mark.usefixtures("steam") +def test_declining_to_close_steam_aborts_before_downloading( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Steam owns the manifest while it runs. The tool offers to close it, but a + "no" must stop the run rather than download for an hour and lose the write.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [4242]) + monkeypatch.setattr( + "builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y" + ) + called = [] + + def download(*_a: object, **_kw: object) -> int: + called.append(1) + return 0 + + monkeypatch.setattr(depotdl, "download", download) + + assert cli.main(ARGV) == 1 + assert called == [] + + +@pytest.mark.usefixtures("steam") +def test_user_declining_confirmation_aborts(monkeypatch: pytest.MonkeyPatch) -> None: + """User declining confirmation aborts.""" + monkeypatch.setattr("builtins.input", lambda *a: "n") + called = [] + + def download(*_a: object, **_kw: object) -> int: + called.append(1) + return 0 + + monkeypatch.setattr(depotdl, "download", download) + assert cli.main(ARGV) == 1 + assert called == [] + + +@pytest.mark.usefixtures("steam") +def test_unhandled_error_returns_nonzero_and_names_the_log( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Unhandled error returns nonzero and names the log.""" + + def boom(*_args: object, **_kwargs: object) -> Never: + msg = "depotdownloader exploded" + raise RuntimeError(msg) + + monkeypatch.setattr(depotdl, "download", boom) + + assert cli.main(ARGV) == 1 + assert "Run log" in capsys.readouterr().out + + +@pytest.mark.usefixtures("steam") +@pytest.mark.parametrize("error", [AttributeError, KeyError, ZeroDivisionError]) +def test_any_crash_is_logged_with_the_log_path( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], + error: type[Exception], +) -> None: + """A bug outside the expected errors is still logged, not left to a traceback.""" + + def boom(*_args: object, **_kwargs: object) -> Never: + msg = "bug" + raise error(msg) + + monkeypatch.setattr(depotdl, "download", boom) + + assert cli.main(ARGV) == 1 + assert "Run log (send this)" in capsys.readouterr().out + + +@pytest.mark.usefixtures("steam") +def test_ctrl_c_still_interrupts(monkeypatch: pytest.MonkeyPatch) -> None: + """Ctrl c still interrupts.""" + + def boom(*_args: object, **_kwargs: object) -> Never: + raise KeyboardInterrupt + + monkeypatch.setattr(depotdl, "download", boom) + + with pytest.raises(KeyboardInterrupt): + cli.main(ARGV) + + +def test_requires_a_name_or_appid() -> None: + """Requires a name or appid.""" + with pytest.raises(SystemExit): + cli.main(["--username", "u"]) diff --git a/_installer_import/tests/test_cli_credit.py b/steam_installer/tests/test_cli_credit.py similarity index 69% rename from _installer_import/tests/test_cli_credit.py rename to steam_installer/tests/test_cli_credit.py index f51c455..7f42b98 100644 --- a/_installer_import/tests/test_cli_credit.py +++ b/steam_installer/tests/test_cli_credit.py @@ -1,11 +1,15 @@ """``--help`` ends on kuhy's shared credit line (kuhy_credit, kuhyx/utils).""" -import pytest +from __future__ import annotations + from kuhy_credit import URL, credit_line +import pytest + from steam_installer import cli -def test_help_ends_with_the_kuhy_credit(capsys): +def test_help_ends_with_the_kuhy_credit(capsys: pytest.CaptureFixture[str]) -> None: + """Help ends with the kuhy credit.""" with pytest.raises(SystemExit) as exc: cli.main(["--help"]) assert exc.value.code == 0 diff --git a/steam_installer/tests/test_cli_finish.py b/steam_installer/tests/test_cli_finish.py new file mode 100644 index 0000000..a2bd134 --- /dev/null +++ b/steam_installer/tests/test_cli_finish.py @@ -0,0 +1,173 @@ +"""CLI flow tests, part 2: the manifest-finishing end of a run.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import ( + appinfo, + cli, + recognize, + vdf, +) +from steam_installer.tests._cli_helpers import ARGV, _stub_download, proven_plan + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +# The 15 keys Steam accepted on 2026-11-22 (A Space for the Unbound): it +# logged "Fully Installed,App Running", queued no download, and added its own +# LastOwner, UserConfig, MountedConfig, ... on exit. +FROM_SCRATCH_KEYS = [ + "appid", + "universe", + "name", + "StateFlags", + "installdir", + "LastUpdated", + "SizeOnDisk", + "StagingSize", + "buildid", + "UpdateResult", + "BytesToDownload", + "BytesDownloaded", + "BytesToStage", + "BytesStaged", + "InstalledDepots", +] + + +def test_missing_manifest_is_written_from_scratch( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """No steamcmd any more, so nothing else creates the manifest.""" + library, install_dir = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + manifest.unlink() + gid = proven_plan(monkeypatch, install_dir) + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + + app_state = vdf.parse(manifest.read_text())["AppState"] + assert list(app_state) == FROM_SCRATCH_KEYS + assert app_state["appid"] == "1771300" + assert app_state["universe"] == "1" + assert app_state["name"] == "KCD2" + assert app_state["StateFlags"] == "4" + assert app_state["installdir"] == "KingdomComeDeliverance2" + assert app_state["SizeOnDisk"] == "1000" + assert app_state["buildid"] == "23914554" + for key in ( + "StagingSize", + "UpdateResult", + "BytesToDownload", + "BytesDownloaded", + "BytesToStage", + "BytesStaged", + ): + assert app_state[key] == "0", key + assert app_state["InstalledDepots"] == {"1771303": {"manifest": gid, "size": "0"}} + # Nothing to back up: there was no manifest before. + assert not manifest.with_suffix(".acf.bak").exists() + + +def test_a_shared_depot_is_recorded_under_its_owner_from_scratch( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """Fixture-only: no shared-depot game was tested live. Steam records a + depot installed under another app as SharedDepots {depot: owner}.""" + library, install_dir = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + manifest.unlink() + proven_plan(monkeypatch, install_dir, shared_owner={"228989": "228980"}) + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + app_state = vdf.parse(manifest.read_text())["AppState"] + assert app_state["SharedDepots"] == {"228989": "228980"} + assert "228989" not in app_state["InstalledDepots"] + + +def test_unproven_content_writes_no_manifest_from_scratch( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Fail closed: without a proven build there is no buildid to write, and a + half-true StateFlags=4 manifest is what made Steam redownload games.""" + library, _ = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + manifest.unlink() + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 1 + assert not manifest.exists() + assert "no usable buildid" in capsys.readouterr().out + + +@pytest.mark.usefixtures("steam") +def test_language_defaults_to_english_when_prompt_is_empty( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Language defaults to english when prompt is empty.""" + seen = {} + monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "") + monkeypatch.setattr( + appinfo, + "get_depot_plan", + lambda appid, language: ( + (seen.setdefault("language", language) and None) + or appinfo.DepotPlan({"1771303"}, set()) + ), + ) + _stub_download(monkeypatch) + + cli.main(["--appid", "1771300", "--username", "u"]) + + assert seen["language"] == "english" + + +def test_adopts_staged_content_then_clears_it( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Steam's staging tree is adopted into the install dir before downloading, + and only cleared once the install is finished -- never discarded up front.""" + library, install_dir = steam + staged = library.steamapps / "downloading" / "1771300" + staged.mkdir(parents=True) + (staged / "big.pak").write_bytes(b"x" * 2_000_000) + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + + out = capsys.readouterr().out + assert "Hardlinking it" in out + assert "now-redundant Steam staging data" in out + # Adopted, not copied: the file is one inode with two names. + assert (install_dir / "big.pak").stat().st_size == 2_000_000 + + +@pytest.mark.usefixtures("steam") +def test_fails_when_written_manifest_does_not_read_back_as_installed( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Claiming success without re-reading the write is the same fail-open + mistake in a different place.""" + _stub_download(monkeypatch) + monkeypatch.setattr( + recognize, "manifest_reflects_real_install", lambda *a, **kw: False + ) + + assert cli.main(ARGV) == 1 + assert "does not read back as a real install" in capsys.readouterr().out diff --git a/steam_installer/tests/test_cli_finish_stub.py b/steam_installer/tests/test_cli_finish_stub.py new file mode 100644 index 0000000..6f0b1fa --- /dev/null +++ b/steam_installer/tests/test_cli_finish_stub.py @@ -0,0 +1,106 @@ +"""CLI flow tests, part 3: stub manifests and the script entry point.""" + +from __future__ import annotations + +import subprocess +import sys +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import ( + cli, + recognize, + vdf, +) +from steam_installer.tests._cli_helpers import ARGV, _stub_download, proven_plan + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +def test_module_is_runnable_as_a_script( + coverage_subprocess_env: dict[str, str], +) -> None: + """`python -m steam_installer.cli` is a real entry point (the __main__ + guard), so it must not be dead code.""" + result = subprocess.run( + [sys.executable, "-m", "steam_installer.cli", "--help"], + capture_output=True, + text=True, + timeout=60, + env=coverage_subprocess_env, + check=False, + ) + assert result.returncode == 0 + assert "--skip-steamcmd" not in result.stdout + + +def test_a_stub_manifest_is_finished_with_the_proven_build( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """The enforcer's stub says build 0 and Steam never ran to fill it in. The + cached depot manifest matching the public branch proves the current build, + which is what the finished manifest must record (BG3, 2026-10-23).""" + library, install_dir = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + manifest.write_text( + vdf.dumps( + "AppState", {"appid": "1771300", "StateFlags": "1026", "buildid": "0"} + ) + ) + proven_plan(monkeypatch, install_dir, shared={"228989"}) + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + app_state = vdf.parse(manifest.read_text())["AppState"] + assert (app_state["StateFlags"], app_state["buildid"]) == ("4", "23914554") + + +def test_the_install_folder_is_corrected_even_when_the_download_fails( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """The enforcer's stub names the folder after the display name ("Baldur's + Gate 3", not "Baldurs Gate 3"). Steam resumes onto whatever the manifest + says, so it is fixed as soon as Steam is closed, not only on success.""" + library, _ = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + state = recognize.read_app_state(manifest) + state["installdir"] = "Kingdom Come: Deliverance II" + manifest.write_text(vdf.dumps("AppState", state)) + _stub_download(monkeypatch, returncode=2) + + assert cli.main(ARGV) == 1 + assert recognize.read_app_state(manifest)["installdir"] == "KingdomComeDeliverance2" + + +def test_wrong_installdir_and_mismatched_counters_end_correct_and_zeroed( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """C18: the live repro (installdir "... WRONG", BytesToDownload 1000, + BytesDownloaded 5) once came out as 1000/1000. The written manifest must + name the real folder, keep its files, and carry every counter at 0.""" + library, install_dir = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + state = recognize.read_app_state(manifest) + state.update( + installdir="KingdomComeDeliverance2 WRONG", + BytesToDownload="1000", + BytesDownloaded="5", + BytesToStage="1000", + BytesStaged="5", + ) + manifest.write_text(vdf.dumps("AppState", state)) + _stub_download(monkeypatch) + + assert cli.main(ARGV) == 0 + + app_state = recognize.read_app_state(manifest) + assert app_state["installdir"] == "KingdomComeDeliverance2" + assert (install_dir / "game.pak").stat().st_size == 1000 + for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"): + assert app_state[key] == "0", key diff --git a/_installer_import/tests/test_cli_modes.py b/steam_installer/tests/test_cli_modes.py similarity index 51% rename from _installer_import/tests/test_cli_modes.py rename to steam_installer/tests/test_cli_modes.py index aecbb47..86960d4 100644 --- a/_installer_import/tests/test_cli_modes.py +++ b/steam_installer/tests/test_cli_modes.py @@ -5,23 +5,41 @@ covers ghosts, usernames and the Steam client. This file covers detection and the modes that deliberately change nothing. """ +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + +import pytest + from steam_installer import cli, deps, target -from tests.conftest import GHOST_ACF +from steam_installer.tests._acf_helpers import GHOST_ACF + +if TYPE_CHECKING: + from steam_installer.library import SteamLibrary -def test_a_bare_appid_resolves_and_reports_without_changing_anything(cli_steam, capsys): +@pytest.mark.usefixtures("cli_steam") +def test_a_bare_appid_resolves_and_reports_without_changing_anything( + capsys: pytest.CaptureFixture[str], +) -> None: + """A bare appid resolves and reports without changing anything.""" assert cli.main(["--dry-run", "284160"]) == 0 out = capsys.readouterr().out assert "BeamNG.drive (appid 284160, matched by appid)" in out assert "nothing was changed" in out -def test_a_steam_url_resolves(cli_steam, capsys): +@pytest.mark.usefixtures("cli_steam") +def test_a_steam_url_resolves(capsys: pytest.CaptureFixture[str]) -> None: + """A steam url resolves.""" assert cli.main(["--dry-run", "https://steamcommunity.com/app/284160/guides/"]) == 0 assert "matched by url" in capsys.readouterr().out -def test_a_dry_run_reports_staged_bytes_it_would_adopt(cli_steam, capsys): +def test_a_dry_run_reports_staged_bytes_it_would_adopt( + cli_steam: SteamLibrary, capsys: pytest.CaptureFixture[str] +) -> None: + """A dry run reports staged bytes it would adopt.""" staged = cli_steam.steamapps / "downloading" / "284160" staged.mkdir(parents=True) (staged / "part.pak").write_bytes(b"x" * 4096) @@ -30,22 +48,34 @@ def test_a_dry_run_reports_staged_bytes_it_would_adopt(cli_steam, capsys): assert "would be adopted, not re-downloaded" in capsys.readouterr().out -def test_a_dry_run_names_a_ghost_manifest(cli_steam, capsys): +def test_a_dry_run_names_a_ghost_manifest( + cli_steam: SteamLibrary, capsys: pytest.CaptureFixture[str] +) -> None: + """A dry run names a ghost manifest.""" (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) assert cli.main(["--dry-run", "284160"]) == 0 assert "GHOST manifest" in capsys.readouterr().out -def test_an_ambiguous_name_exits_two(cli_steam, monkeypatch): - def boom(text, appid, *, interactive): - raise target.TargetError("Ambiguous name") +@pytest.mark.usefixtures("cli_steam") +def test_an_ambiguous_name_exits_two(monkeypatch: pytest.MonkeyPatch) -> None: + """An ambiguous name exits two.""" + + def boom(_text: str | None, _appid: int | None, **_kw: bool) -> Never: + msg = "Ambiguous name" + raise target.TargetError(msg) monkeypatch.setattr(target, "resolve_target", boom) assert cli.main(["--dry-run", "witcher"]) == 2 -def test_a_missing_dependency_exits_one(cli_steam, monkeypatch, capsys): +@pytest.mark.usefixtures("cli_steam") +def test_a_missing_dependency_exits_one( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """A missing dependency exits one.""" monkeypatch.setattr(deps.shutil, "which", lambda name: None) assert cli.main(["--dry-run", "284160"]) == 1 assert "depotdownloader" in capsys.readouterr().out diff --git a/steam_installer/tests/test_cli_steam.py b/steam_installer/tests/test_cli_steam.py new file mode 100644 index 0000000..925b749 --- /dev/null +++ b/steam_installer/tests/test_cli_steam.py @@ -0,0 +1,178 @@ +"""What the CLI does around a real install: ghosts, usernames, the Steam client. + +Split from test_cli_modes.py to keep both files under the repo's 250-line cap. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import appinfo, cli, config, depotdl +from steam_installer.tests._acf_helpers import GHOST_ACF + +if TYPE_CHECKING: + from steam_installer.library import SteamLibrary + + +def test_a_cold_ghost_blocks_an_install_and_names_the_fix( + cli_steam: SteamLibrary, capsys: pytest.CaptureFixture[str] +) -> None: + """Installing over a fabricated manifest is how Steam was tricked into + re-downloading in the first place; stop and say what to run.""" + (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) + + assert cli.main(["--yes", "284160", "--username", "u"]) == 1 + assert "steam_installer/run.sh --repair-ghosts" in capsys.readouterr().out + + +def test_a_warm_ghost_does_not_block_an_install( + cli_steam: SteamLibrary, monkeypatch: pytest.MonkeyPatch +) -> None: + """Staged bytes are the whole reason to run this tool, not a blocker.""" + (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) + staged = cli_steam.steamapps / "downloading" / "284160" + staged.mkdir(parents=True) + (staged / "part.pak").write_bytes(b"x" * 4096) + reached = [] + + def download(_appid: int, _pull: object, _logger: object) -> int: + reached.append(1) + return 1 + + monkeypatch.setattr(depotdl, "download", download) + monkeypatch.setattr( + appinfo, "get_depot_plan", lambda a, language: appinfo.DepotPlan(set(), set()) + ) + monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") + + cli.main(["--yes", "284160", "--username", "u"]) + + assert reached == [1] + + +def test_repair_ghosts_clears_a_cold_ghost( + cli_steam: SteamLibrary, capsys: pytest.CaptureFixture[str] +) -> None: + """Repair ghosts clears a cold ghost.""" + (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) + + assert cli.main(["--repair-ghosts"]) == 0 + + assert not (cli_steam.steamapps / "appmanifest_284160.acf").exists() + assert "Ghost: appid 284160" in capsys.readouterr().out + + +def test_repair_ghosts_refuses_to_discard_a_warm_ghosts_download( + cli_steam: SteamLibrary, capsys: pytest.CaptureFixture[str] +) -> None: + """Repair ghosts refuses to discard a warm ghosts download.""" + (cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF) + staged = cli_steam.steamapps / "downloading" / "284160" + staged.mkdir(parents=True) + (staged / "part.pak").write_bytes(b"x" * 4096) + + assert cli.main(["--repair-ghosts"]) == 0 + + assert (cli_steam.steamapps / "appmanifest_284160.acf").exists() + assert (staged / "part.pak").exists() + assert "nothing is deleted" in capsys.readouterr().out + + +@pytest.mark.usefixtures("cli_steam") +def test_repair_ghosts_says_so_when_there_is_nothing_to_do( + capsys: pytest.CaptureFixture[str], +) -> None: + """Repair ghosts says so when there is nothing to do.""" + assert cli.main(["--repair-ghosts"]) == 0 + assert "No ghost installs found" in capsys.readouterr().out + + +@pytest.mark.usefixtures("cli_steam") +def test_repair_ghosts_needs_no_target() -> None: + """It is the one mode that runs without naming a game.""" + assert cli.main(["--repair-ghosts"]) == 0 + + +@pytest.mark.usefixtures("cli_steam") +def test_a_saved_username_removes_the_need_for_the_flag( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A saved username removes the need for the flag.""" + config.save_username("saved-user") + seen = {} + monkeypatch.setattr( + depotdl, + "download", + lambda appid, pull, logger: seen.setdefault("username", pull.username) and 1, + ) + monkeypatch.setattr( + appinfo, "get_depot_plan", lambda a, language: appinfo.DepotPlan(set(), set()) + ) + monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") + + cli.main(["--yes", "284160"]) + + assert seen["username"] == "saved-user" + + +@pytest.mark.usefixtures("cli_steam") +def test_no_username_anywhere_fails_with_an_explanation( + capsys: pytest.CaptureFixture[str], +) -> None: + """No username anywhere fails with an explanation.""" + assert cli.main(["--yes", "284160"]) == 1 + assert "No Steam username available" in capsys.readouterr().out + + +@pytest.mark.usefixtures("cli_steam") +def test_a_non_interactive_run_without_yes_does_not_start_a_download( + capsys: pytest.CaptureFixture[str], +) -> None: + """A piped invocation never asked for a 50 GB download; refuse rather than + assume.""" + assert cli.main(["284160", "--username", "u"]) == 1 + assert "use --yes" in capsys.readouterr().out + + +@pytest.mark.usefixtures("cli_steam") +def test_language_defaults_to_english_when_not_interactive( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Language defaults to english when not interactive.""" + seen = {} + + def get_depot_plan(_appid: int, language: str) -> appinfo.DepotPlan: + seen["language"] = language + return appinfo.DepotPlan(set(), set()) + + monkeypatch.setattr(appinfo, "get_depot_plan", get_depot_plan) + monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") + monkeypatch.setattr(depotdl, "download", lambda appid, pull, logger: 1) + + cli.main(["--yes", "284160", "--username", "u"]) + + assert seen["language"] == "english" + + +def test_nothing_to_do_at_all_is_a_usage_error() -> None: + """Nothing to do at all is a usage error.""" + with pytest.raises(SystemExit): + cli.main(["--username", "u"]) + + +def test_a_ghost_for_a_different_app_does_not_block_this_one( + cli_steam: SteamLibrary, monkeypatch: pytest.MonkeyPatch +) -> None: + """A ghost for a different app does not block this one.""" + (cli_steam.steamapps / "appmanifest_999.acf").write_text( + GHOST_ACF.replace('"appid"\t\t"284160"', '"appid"\t\t"999"') + ) + monkeypatch.setattr(depotdl, "download", lambda *a, **kw: 1) + monkeypatch.setattr( + appinfo, "get_depot_plan", lambda a, language: appinfo.DepotPlan(set(), set()) + ) + monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") + + assert cli.main(["--yes", "284160", "--username", "u"]) == 1 diff --git a/steam_installer/tests/test_cli_steam_client.py b/steam_installer/tests/test_cli_steam_client.py new file mode 100644 index 0000000..e8e6c18 --- /dev/null +++ b/steam_installer/tests/test_cli_steam_client.py @@ -0,0 +1,105 @@ +"""The CLI and the Steam client: closed around an install, reopened after.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + +import pytest + +from steam_installer import appinfo, cli, depotdl, ghost, recognize + +if TYPE_CHECKING: + import logging + + +def _installable(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr( + appinfo, "get_depot_plan", lambda a, language: appinfo.DepotPlan(set(), set()) + ) + monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1") + monkeypatch.setattr(depotdl, "download", lambda *a, **kw: 1) + + +@pytest.mark.usefixtures("cli_steam") +def test_declining_the_shutdown_prompt_stops_the_run( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Declining the shutdown prompt stops the run.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) + monkeypatch.setattr(cli, "_is_interactive", lambda args: True) + monkeypatch.setattr( + "builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y" + ) + monkeypatch.setattr( + depotdl, "download", lambda *a, **kw: pytest.fail("must not download") + ) + + assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1 + + +@pytest.mark.usefixtures("cli_steam") +def test_steam_is_closed_and_reopened_around_the_install( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """It was running before, so it is running after -- whatever the outcome.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) + monkeypatch.setattr(ghost.recognize, "steam_client_pids", lambda: [42]) + events = [] + + def stop(_logger: object) -> bool: + events.append("stop") + return True + + monkeypatch.setattr(cli.flow.steamctl, "stop", stop) + monkeypatch.setattr( + cli.flow.steamctl, "start", lambda logger: events.append("start") + ) + _installable(monkeypatch) + + assert cli.main(["--yes", "284160", "--username", "u"]) == 1 + assert events == ["stop", "start"] + + +@pytest.mark.usefixtures("cli_steam") +def test_a_steam_that_refuses_to_exit_aborts_the_run( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Never escalate to a signal: a hung client is the user's to deal with.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) + + def refuse(_logger: logging.Logger) -> Never: + msg = "Steam is still running after 60s" + raise cli.flow.steamctl.SteamStillRunningError(msg) + + monkeypatch.setattr(cli.flow.steamctl, "stop", refuse) + monkeypatch.setattr( + depotdl, "download", lambda *a, **kw: pytest.fail("must not download") + ) + + assert cli.main(["--yes", "284160", "--username", "u"]) == 1 + assert "still running" in capsys.readouterr().out + + +@pytest.mark.usefixtures("cli_steam") +def test_accepting_the_shutdown_prompt_closes_and_reopens_steam( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """The interactive path: asked, answered yes, Steam cycled around the run.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) + monkeypatch.setattr(cli, "_is_interactive", lambda args: True) + monkeypatch.setattr("builtins.input", lambda *a: "y") + events = [] + + def stop(_logger: object) -> bool: + events.append("stop") + return True + + monkeypatch.setattr(cli.flow.steamctl, "stop", stop) + monkeypatch.setattr( + cli.flow.steamctl, "start", lambda logger: events.append("start") + ) + _installable(monkeypatch) + + assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1 + assert events == ["stop", "start"] diff --git a/_installer_import/tests/test_compat_decide.py b/steam_installer/tests/test_compat_decide.py similarity index 53% rename from _installer_import/tests/test_compat_decide.py rename to steam_installer/tests/test_compat_decide.py index 1639dc9..9634f3a 100644 --- a/_installer_import/tests/test_compat_decide.py +++ b/steam_installer/tests/test_compat_decide.py @@ -1,10 +1,18 @@ """Which games get the native Proton, and why the others keep Valve's default.""" +from __future__ import annotations + import json +from typing import TYPE_CHECKING, Never, Self import pytest -from steam_installer import anticheat, compat_decide +from steam_installer import _http, anticheat, compat_decide +from steam_installer.anticheat import _download as _download_anticheat + +if TYPE_CHECKING: + from pathlib import Path + import urllib.request WINDOWS_ONLY = { "common": {"oslist": "windows"}, @@ -13,13 +21,20 @@ WINDOWS_ONLY = { } -def _decide(block=WINDOWS_ONLY, **kw): - args = {"anticheat": set(), "overrides": {}, "owned_dlc": False, "tool_ready": True} - args.update(kw) - return compat_decide.decide(10, block, **args) +def _decide( + block: dict[str, object] | None = None, + *, + owned_dlc: bool = False, + **facts: object, +) -> compat_decide.Decision: + block = WINDOWS_ONLY if block is None else block + base = {"anticheat": set(), "overrides": {}, "tool_ready": True} + shared = compat_decide.Facts(**{**base, **facts}) + return compat_decide.decide(10, block, shared, owned_dlc=owned_dlc) -def test_a_plain_windows_game_is_mapped(): +def test_a_plain_windows_game_is_mapped() -> None: + """A plain windows game is mapped.""" decision = _decide() assert decision.map assert decision.describe() == "map" @@ -31,63 +46,82 @@ def test_a_plain_windows_game_is_mapped(): ({**WINDOWS_ONLY, "common": {"oslist": "windows,linux"}}, "native"), ({**WINDOWS_ONLY, "depots": {"11": {"config": {"oslist": "linux"}}}}, "native"), ( - {**WINDOWS_ONLY, "config": {"launch": {"0": {"config": {"oslist": "linux"}}}}}, + { + **WINDOWS_ONLY, + "config": {"launch": {"0": {"config": {"oslist": "linux"}}}}, + }, "native", ), ({**WINDOWS_ONLY, "common": {"oslist": "windows,macos"}}, "not Windows-only"), ({**WINDOWS_ONLY, "common": {}}, "no OS list"), ({"common": "x"}, "no OS list"), - ({**WINDOWS_ONLY, "depots": {"11": {"depotfromapp": "228980"}}}, "shared depots"), + ( + {**WINDOWS_ONLY, "depots": {"11": {"depotfromapp": "228980"}}}, + "shared depots", + ), ({**WINDOWS_ONLY, "depots": {"11": {"sharedinstall": "1"}}}, "shared depots"), ], ) -def test_unsafe_games_keep_valves_default(block, reason): +def test_unsafe_games_keep_valves_default( + block: dict[str, object], reason: str +) -> None: + """Unsafe games keep valves default.""" decision = _decide(block) assert not decision.map assert reason in decision.reasons -def test_odd_shapes_are_ignored_not_crashed_on(): +def test_odd_shapes_are_ignored_not_crashed_on() -> None: + """Odd shapes are ignored not crashed on.""" block = { "common": {"oslist": "windows"}, "depots": {"branches": {"public": {}}, "11": "scalar", "12": {"config": "x"}}, "config": {"launch": "scalar"}, } assert _decide(block).map - assert _decide({**block, "config": {"launch": {"0": "x", "1": {"config": "y"}}}}).map + assert _decide( + {**block, "config": {"launch": {"0": "x", "1": {"config": "y"}}}} + ).map assert _decide({**block, "depots": "scalar", "config": "scalar"}).map -def test_anti_cheat_whatever_its_status(): +def test_anti_cheat_whatever_its_status() -> None: + """Anti cheat whatever its status.""" assert _decide(anticheat={10}).reasons == ["anti-cheat"] -def test_no_anti_cheat_list_means_no_mapping(): +def test_no_anti_cheat_list_means_no_mapping() -> None: + """No anti cheat list means no mapping.""" assert _decide(anticheat=None).reasons == ["anti-cheat list unavailable"] -def test_owned_dlc_is_left_on_valves_default_until_tested_live(): +def test_owned_dlc_is_left_on_valves_default_until_tested_live() -> None: + """Owned dlc is left on valves default until tested live.""" assert _decide(owned_dlc=True).reasons == ["owned DLC"] -def test_override_and_missing_tool(): +def test_override_and_missing_tool() -> None: + """Override and missing tool.""" assert _decide(overrides={"10": "crashes at intro"}).reasons == [ "override: crashes at intro" ] assert _decide(tool_ready=False).reasons == ["native Proton not installed"] -def test_every_reason_is_reported(): +def test_every_reason_is_reported() -> None: + """Every reason is reported.""" block = {**WINDOWS_ONLY, "common": {"oslist": "windows,linux"}} decision = _decide(block, anticheat={10}) assert decision.describe() == "Valve default: anti-cheat, native" -def test_overrides_file_is_tracked_and_valid(): +def test_overrides_file_is_tracked_and_valid() -> None: + """Overrides file is tracked and valid.""" assert compat_decide.load_overrides() == {} -def test_overrides_file_must_map_ids_to_reasons(tmp_path): +def test_overrides_file_must_map_ids_to_reasons(tmp_path: Path) -> None: + """Overrides file must map ids to reasons.""" path = tmp_path / "o.json" path.write_text(json.dumps({"10": "why"})) assert compat_decide.load_overrides(path) == {"10": "why"} @@ -97,7 +131,8 @@ def test_overrides_file_must_map_ids_to_reasons(tmp_path): compat_decide.load_overrides(path) -def test_tool_ready_needs_the_manifest_and_the_pinned_stamp(tmp_path): +def test_tool_ready_needs_the_manifest_and_the_pinned_stamp(tmp_path: Path) -> None: + """Tool ready needs the manifest and the pinned stamp.""" tool = tmp_path / "proton-cachyos-native" assert not compat_decide.tool_ready(tool, "1:2-3") tool.mkdir() @@ -117,48 +152,64 @@ GAMES = [ ] -def test_anticheat_list_is_fetched_and_cached(tmp_path, monkeypatch): +def test_anticheat_list_is_fetched_and_cached( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Anticheat list is fetched and cached.""" cache = tmp_path / "ac.json" monkeypatch.setattr(anticheat, "_download", lambda: json.dumps(GAMES).encode()) assert anticheat.steam_ids(cache) == {730} assert json.loads(cache.read_text()) == GAMES -def test_anticheat_falls_back_to_the_last_good_copy(tmp_path, monkeypatch): +def test_anticheat_falls_back_to_the_last_good_copy( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Anticheat falls back to the last good copy.""" cache = tmp_path / "ac.json" cache.write_text(json.dumps(GAMES)) - def offline(): - raise OSError("no network") + def offline() -> Never: + msg = "no network" + raise OSError(msg) monkeypatch.setattr(anticheat, "_download", offline) assert anticheat.steam_ids(cache) == {730} -def test_anticheat_with_no_fetch_and_no_cache_is_unknown(tmp_path, monkeypatch): +def test_anticheat_with_no_fetch_and_no_cache_is_unknown( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Anticheat with no fetch and no cache is unknown.""" monkeypatch.setattr(anticheat, "_download", lambda: b"{not json") assert anticheat.steam_ids(tmp_path / "ac.json") is None (tmp_path / "ac.json").write_text('{"a": 1}') assert anticheat.steam_ids(tmp_path / "ac.json") is None -def test_anticheat_download_reads_the_upstream_url(monkeypatch): +def test_anticheat_download_reads_the_upstream_url( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Anticheat download reads the upstream url.""" seen = {} class _Response: - def __enter__(self): + def __enter__(self) -> Self: return self - def __exit__(self, *a): - return False + def __exit__(self, *a: object) -> None: + return None - def read(self): + def read(self) -> bytes: + """The body the fake mirror serves.""" return b"[]" - def fake_urlopen(url, timeout): - seen["url"] = url + def fake_urlopen(request: urllib.request.Request, **_kw: float) -> _Response: + seen["url"] = request.full_url + seen["agent"] = request.get_header("User-agent") return _Response() - monkeypatch.setattr(anticheat.urllib.request, "urlopen", fake_urlopen) - assert anticheat._download() == b"[]" + monkeypatch.setattr(_http.urllib.request, "urlopen", fake_urlopen) + assert _download_anticheat() == b"[]" assert "AreWeAntiCheatYet" in seen["url"] + assert seen["agent"] is None # urllib sends its own default agent diff --git a/_installer_import/tests/test_compat_map.py b/steam_installer/tests/test_compat_map.py similarity index 66% rename from _installer_import/tests/test_compat_map.py rename to steam_installer/tests/test_compat_map.py index 27638cd..593a733 100644 --- a/_installer_import/tests/test_compat_map.py +++ b/steam_installer/tests/test_compat_map.py @@ -1,10 +1,17 @@ """Per-game CompatToolMapping entries, inserted into config.vdf as text.""" +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + import pytest from steam_installer import compat_map -HEAD = '''"InstallConfigStore" +if TYPE_CHECKING: + from pathlib import Path + +HEAD = """"InstallConfigStore" { \t"Software" \t{ @@ -13,8 +20,8 @@ HEAD = '''"InstallConfigStore" \t\t\t"Steam" \t\t\t{ \t\t\t\t"SecretToken"\t\t"do-not-print-me" -''' -TAIL = '''\t\t\t} +""" +TAIL = """\t\t\t} \t\t} \t} \t"Music" @@ -22,10 +29,10 @@ TAIL = '''\t\t\t} \t\t"CompatToolMapping"\t\t"decoy outside the Steam block" \t} } -''' +""" -def _entry(appid, name, priority, indent="\t\t\t\t\t"): +def _entry(appid: int, name: str, priority: str, indent: str = "\t\t\t\t\t") -> str: return ( f'{indent}"{appid}"\n{indent}{{\n' f'{indent}\t"name"\t\t"{name}"\n{indent}\t"config"\t\t""\n' @@ -33,7 +40,7 @@ def _entry(appid, name, priority, indent="\t\t\t\t\t"): ) -def _config(tmp_path, mapping=None): +def _config(tmp_path: Path, mapping: str | None = None) -> Path: body = HEAD if mapping is not None: body += '\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n' + mapping + "\t\t\t\t}\n" @@ -44,11 +51,12 @@ def _config(tmp_path, mapping=None): @pytest.fixture(autouse=True) -def _steam_closed(monkeypatch): +def _steam_closed(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(compat_map.recognize, "steam_client_pids", list) -def test_adds_the_block_when_there_is_none(tmp_path): +def test_adds_the_block_when_there_is_none(tmp_path: Path) -> None: + """Adds the block when there is none.""" path = _config(tmp_path) before = path.read_text() @@ -62,7 +70,7 @@ def test_adds_the_block_when_there_is_none(tmp_path): assert path.stat().st_mode & 0o777 == 0o600 -def _mapping_block(appid): +def _mapping_block(appid: int) -> str: return ( '\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n' + _entry(appid, "proton-cachyos-native", "250") @@ -70,14 +78,16 @@ def _mapping_block(appid): ) -def test_adds_an_entry_to_an_existing_block(tmp_path): +def test_adds_an_entry_to_an_existing_block(tmp_path: Path) -> None: + """Adds an entry to an existing block.""" path = _config(tmp_path, _entry(10, "proton_9", "250")) assert compat_map.map_app(path, 20).action == "mapped" assert compat_map.current(path, 10) == ("proton_9", "250") assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") -def test_replaces_valves_own_choice(tmp_path): +def test_replaces_valves_own_choice(tmp_path: Path) -> None: + """Replaces valves own choice.""" path = _config(tmp_path, _entry(20, "proton_experimental", "100")) outcome = compat_map.map_app(path, 20) assert outcome.action == "mapped" @@ -86,13 +96,15 @@ def test_replaces_valves_own_choice(tmp_path): assert path.read_text().count('"20"') == 1 -def test_replaces_an_empty_entry(tmp_path): +def test_replaces_an_empty_entry(tmp_path: Path) -> None: + """Replaces an empty entry.""" path = _config(tmp_path, _entry(20, "", "250")) assert compat_map.map_app(path, 20).action == "mapped" assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") -def test_keeps_a_tool_kuhy_picked_himself(tmp_path): +def test_keeps_a_tool_kuhy_picked_himself(tmp_path: Path) -> None: + """Keeps a tool kuhy picked himself.""" path = _config(tmp_path, _entry(20, "GE-Proton10", "250")) before = path.read_text() outcome = compat_map.map_app(path, 20) @@ -101,7 +113,8 @@ def test_keeps_a_tool_kuhy_picked_himself(tmp_path): assert path.read_text() == before -def test_a_second_run_changes_nothing(tmp_path): +def test_a_second_run_changes_nothing(tmp_path: Path) -> None: + """A second run changes nothing.""" path = _config(tmp_path) compat_map.map_app(path, 20) once = path.read_text() @@ -109,7 +122,10 @@ def test_a_second_run_changes_nothing(tmp_path): assert path.read_text() == once -def test_refuses_while_steam_runs(tmp_path, monkeypatch): +def test_refuses_while_steam_runs( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Refuses while steam runs.""" path = _config(tmp_path) before = path.read_text() monkeypatch.setattr(compat_map.recognize, "steam_client_pids", lambda: [7]) @@ -124,10 +140,13 @@ def test_refuses_while_steam_runs(tmp_path, monkeypatch): '"InstallConfigStore"\n{\n}\n', '"InstallConfigStore"\n{\n\t"Software" { }\n}\n', '"InstallConfigStore"\n{\n\t}\n\t}\n', - '{\n}\n', + "{\n}\n", ], ) -def test_an_unexpected_layout_is_refused_without_echoing_lines(tmp_path, text): +def test_an_unexpected_layout_is_refused_without_echoing_lines( + tmp_path: Path, text: str +) -> None: + """An unexpected layout is refused without echoing lines.""" path = tmp_path / "config.vdf" path.write_text(text + '"x"\t"do-not-print-me"\n') with pytest.raises(compat_map.CompatMapError) as excinfo: @@ -135,61 +154,76 @@ def test_an_unexpected_layout_is_refused_without_echoing_lines(tmp_path, text): assert "do-not-print-me" not in str(excinfo.value) -def test_missing_config_is_an_error(tmp_path): +def test_missing_config_is_an_error(tmp_path: Path) -> None: + """Missing config is an error.""" with pytest.raises(compat_map.CompatMapError, match="cannot read"): compat_map.current(tmp_path / "nope.vdf", 1) -def test_current_is_none_for_an_unmapped_app(tmp_path): +def test_current_is_none_for_an_unmapped_app(tmp_path: Path) -> None: + """Current is none for an unmapped app.""" assert compat_map.current(_config(tmp_path), 20) is None assert compat_map.current(_config(tmp_path, _entry(10, "x", "1")), 20) is None -def test_keys_match_case_insensitively(tmp_path): +def test_keys_match_case_insensitively(tmp_path: Path) -> None: + """Keys match case insensitively.""" path = _config(tmp_path) path.write_text(path.read_text().replace('"Steam"', '"steam"')) assert compat_map.map_app(path, 20).action == "mapped" assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") -def test_config_path_is_under_the_steam_root(tmp_path): +def test_config_path_is_under_the_steam_root(tmp_path: Path) -> None: + """Config path is under the steam root.""" assert compat_map.config_path(tmp_path) == tmp_path / "config" / "config.vdf" -def test_a_value_spanning_lines_is_read_as_one_value(tmp_path): +def test_a_value_spanning_lines_is_read_as_one_value(tmp_path: Path) -> None: """Steam stores some values with embedded newlines (seen in the real file).""" path = _config(tmp_path) - path.write_text(path.read_text().replace( - '"SecretToken"\t\t"do-not-print-me"', '"SecretToken"\t\t"line one\nline {two}"')) + path.write_text( + path.read_text().replace( + '"SecretToken"\t\t"do-not-print-me"', + '"SecretToken"\t\t"line one\nline {two}"', + ) + ) assert compat_map.map_app(path, 20).action == "mapped" assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") -def test_an_entry_sharing_a_line_with_its_key_is_refused(tmp_path): +def test_an_entry_sharing_a_line_with_its_key_is_refused(tmp_path: Path) -> None: + """An entry sharing a line with its key is refused.""" path = _config(tmp_path, '\t\t\t\t\t"20" {\n\t\t\t\t\t\t"name" "x"\n\t\t\t\t\t}\n') with pytest.raises(compat_map.CompatMapError, match="unexpected layout"): compat_map.map_app(path, 20) -def test_an_entry_closing_on_a_shared_line_is_refused(tmp_path): +def test_an_entry_closing_on_a_shared_line_is_refused(tmp_path: Path) -> None: + """An entry closing on a shared line is refused.""" path = _config(tmp_path, '\t\t\t\t\t"20"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name" "" }\n') with pytest.raises(compat_map.CompatMapError, match="unexpected layout"): compat_map.map_app(path, 20) -def test_unquoted_tokens_are_refused(tmp_path): +def test_unquoted_tokens_are_refused(tmp_path: Path) -> None: + """Unquoted tokens are refused.""" path = _config(tmp_path) path.write_text(path.read_text().replace('"SecretToken"', "SecretToken")) with pytest.raises(compat_map.CompatMapError, match="line 9: unexpected layout"): compat_map.current(path, 20) -def test_a_failed_replace_leaves_the_file_and_no_temp(tmp_path, monkeypatch): +def test_a_failed_replace_leaves_the_file_and_no_temp( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A failed replace leaves the file and no temp.""" path = _config(tmp_path) before = path.read_text() - def fail(src, dst): - raise OSError("disk full") + def fail(_src: object, _dst: object) -> Never: + msg = "disk full" + raise OSError(msg) monkeypatch.setattr(compat_map.os, "replace", fail) with pytest.raises(OSError, match="disk full"): diff --git a/_installer_import/tests/test_config.py b/steam_installer/tests/test_config.py similarity index 59% rename from _installer_import/tests/test_config.py rename to steam_installer/tests/test_config.py index 2fd0417..9a7e529 100644 --- a/_installer_import/tests/test_config.py +++ b/steam_installer/tests/test_config.py @@ -1,87 +1,110 @@ """Username persistence and its precedence order.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + import pytest from steam_installer import config +if TYPE_CHECKING: + from pathlib import Path + @pytest.fixture(autouse=True) -def isolated_config(tmp_path, monkeypatch): +def isolated_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """Never read or write the real ~/.config during a test run.""" monkeypatch.setattr(config, "CONFIG_DIR", tmp_path / "cfg") monkeypatch.setattr(config, "CONFIG_PATH", tmp_path / "cfg" / "config") monkeypatch.delenv(config.ENV_VAR, raising=False) -def test_explicit_username_wins_over_everything(monkeypatch): +def test_explicit_username_wins_over_everything( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Explicit username wins over everything.""" monkeypatch.setenv(config.ENV_VAR, "from_env") config.save_username("from_file") assert config.resolve_username("explicit", interactive=False) == "explicit" -def test_env_wins_over_the_saved_file(monkeypatch): +def test_env_wins_over_the_saved_file(monkeypatch: pytest.MonkeyPatch) -> None: + """Env wins over the saved file.""" monkeypatch.setenv(config.ENV_VAR, "from_env") config.save_username("from_file") assert config.resolve_username(None, interactive=False) == "from_env" -def test_a_blank_env_var_does_not_shadow_the_saved_file(monkeypatch): +def test_a_blank_env_var_does_not_shadow_the_saved_file( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A blank env var does not shadow the saved file.""" monkeypatch.setenv(config.ENV_VAR, " ") config.save_username("from_file") assert config.resolve_username(None, interactive=False) == "from_file" -def test_the_saved_file_is_used_when_nothing_else_is_given(): +def test_the_saved_file_is_used_when_nothing_else_is_given() -> None: + """The saved file is used when nothing else is given.""" config.save_username("saved") assert config.resolve_username(None, interactive=False) == "saved" -def test_saving_creates_the_directory_and_locks_the_file_down(): +def test_saving_creates_the_directory_and_locks_the_file_down() -> None: + """Saving creates the directory and locks the file down.""" config.save_username("someone") assert config.CONFIG_PATH.read_text() == "username=someone\n" assert config.CONFIG_PATH.stat().st_mode & 0o777 == 0o600 -def test_saving_twice_replaces_rather_than_appends(): +def test_saving_twice_replaces_rather_than_appends() -> None: + """Saving twice replaces rather than appends.""" config.save_username("first") config.save_username("second") assert config.load_username() == "second" -def test_load_returns_none_when_nothing_is_saved(): +def test_load_returns_none_when_nothing_is_saved() -> None: + """Load returns none when nothing is saved.""" assert config.load_username() is None -def test_load_ignores_other_keys_and_malformed_lines(): +def test_load_ignores_other_keys_and_malformed_lines() -> None: + """Load ignores other keys and malformed lines.""" config.CONFIG_DIR.mkdir(parents=True) config.CONFIG_PATH.write_text("# a comment\nnot-a-pair\nother=x\nusername=real\n") assert config.load_username() == "real" -def test_an_empty_username_value_reads_as_unset(): +def test_an_empty_username_value_reads_as_unset() -> None: + """An empty username value reads as unset.""" config.CONFIG_DIR.mkdir(parents=True) config.CONFIG_PATH.write_text("username=\n") assert config.load_username() is None -def test_prompting_saves_the_answer(monkeypatch): +def test_prompting_saves_the_answer(monkeypatch: pytest.MonkeyPatch) -> None: + """Prompting saves the answer.""" monkeypatch.setattr("builtins.input", lambda *a: " typed ") assert config.resolve_username(None, interactive=True) == "typed" assert config.load_username() == "typed" -def test_an_empty_answer_is_refused(monkeypatch): +def test_an_empty_answer_is_refused(monkeypatch: pytest.MonkeyPatch) -> None: + """An empty answer is refused.""" monkeypatch.setattr("builtins.input", lambda *a: "") with pytest.raises(config.ConfigError, match="No Steam username entered"): config.resolve_username(None, interactive=True) -def test_non_interactive_with_nothing_available_explains_the_options(): +def test_non_interactive_with_nothing_available_explains_the_options() -> None: + """Non interactive with nothing available explains the options.""" with pytest.raises(config.ConfigError, match="--username"): config.resolve_username(None, interactive=False) -def test_a_line_with_no_separator_is_skipped_before_a_valid_one(): +def test_a_line_with_no_separator_is_skipped_before_a_valid_one() -> None: """`partition` returns an empty separator for a bare line; that must not be mistaken for a key with an empty value.""" config.CONFIG_DIR.mkdir(parents=True) @@ -89,7 +112,8 @@ def test_a_line_with_no_separator_is_skipped_before_a_valid_one(): assert config.load_username() == "real" -def test_a_config_file_with_no_username_key_reads_as_unset(): +def test_a_config_file_with_no_username_key_reads_as_unset() -> None: + """A config file with no username key reads as unset.""" config.CONFIG_DIR.mkdir(parents=True) config.CONFIG_PATH.write_text("other=x\n# nothing relevant here\n") assert config.load_username() is None diff --git a/steam_installer/tests/test_depotdl.py b/steam_installer/tests/test_depotdl.py new file mode 100644 index 0000000..46e7bee --- /dev/null +++ b/steam_installer/tests/test_depotdl.py @@ -0,0 +1,88 @@ +"""Tests for depotdl.""" + +from __future__ import annotations + +import logging +from typing import TYPE_CHECKING, Any + +from steam_installer import depotdl + +if TYPE_CHECKING: + from pathlib import Path + + import pytest + + +def _capture(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]: + """Intercept the subprocess call and hand back the argv it was given.""" + seen = {} + + def fake_run_logged( + cmd: list[str], _logger: logging.Logger, **_kw: float | None + ) -> tuple[int, str]: + seen["cmd"] = cmd + return seen.get("rc", 0), "" + + monkeypatch.setattr(depotdl.runlog, "run_logged", fake_run_logged) + return seen + + +def test_download_builds_expected_argv( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Download builds expected argv.""" + seen = _capture(monkeypatch) + + pull = depotdl.Pull(tmp_path / "Game", "krzys2000", "english", 32) + depotdl.download(1771300, pull, logging.getLogger("t")) + + cmd = seen["cmd"] + assert cmd[0] == "depotdownloader" + pairs = dict(zip(cmd[1::2], cmd[2::2], strict=False)) + assert pairs["-app"] == "1771300" + assert pairs["-language"] == "english" + assert pairs["-max-downloads"] == "32" + assert pairs["-username"] == "krzys2000" + + +def test_download_always_passes_os_windows( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Without -os, depotdownloader defaults to the host OS and can filter out + every real (Windows-tagged) depot -- the original "0 bytes from 0 depots".""" + seen = _capture(monkeypatch) + + depotdl.download( + 1771300, depotdl.Pull(tmp_path / "Game", "u"), logging.getLogger("t") + ) + + cmd = seen["cmd"] + assert cmd[cmd.index("-os") + 1] == "windows" + assert "-remember-password" in cmd + + +def test_download_creates_the_install_dir( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Download creates the install dir.""" + _capture(monkeypatch) + target = tmp_path / "nested" / "Game" + + depotdl.download(1, depotdl.Pull(target, "u"), logging.getLogger("t")) + + assert target.is_dir() + + +def test_download_returns_the_exit_code_instead_of_raising( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The caller must distinguish "download failed" from "manifest step failed" + to decide whether touching the manifest is safe at all.""" + seen = _capture(monkeypatch) + seen["rc"] = 1 + + returncode = depotdl.download( + 1, depotdl.Pull(tmp_path / "Game", "u"), logging.getLogger("t") + ) + + assert returncode == 1 diff --git a/_installer_import/tests/test_deps.py b/steam_installer/tests/test_deps.py similarity index 55% rename from _installer_import/tests/test_deps.py rename to steam_installer/tests/test_deps.py index c048f02..b048d62 100644 --- a/_installer_import/tests/test_deps.py +++ b/steam_installer/tests/test_deps.py @@ -1,24 +1,33 @@ """The external-binary check. Moved here from test_cli.py with the code it covers.""" +from __future__ import annotations + import pytest from steam_installer import deps -def test_nothing_missing_when_depotdownloader_is_on_path(monkeypatch): +def test_nothing_missing_when_depotdownloader_is_on_path( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Nothing missing when depotdownloader is on path.""" monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}") assert deps.missing_tools() == [] deps.require() -def test_steamcmd_is_never_required(monkeypatch): +def test_steamcmd_is_never_required(monkeypatch: pytest.MonkeyPatch) -> None: + """Steamcmd is never required.""" monkeypatch.setattr( deps.shutil, "which", lambda name: None if name == "steamcmd" else "/usr/bin/dd" ) assert deps.missing_tools() == [] -def test_missing_tool_raises_with_the_command_that_fixes_it(monkeypatch): +def test_missing_tool_raises_with_the_command_that_fixes_it( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Missing tool raises with the command that fixes it.""" monkeypatch.setattr(deps.shutil, "which", lambda name: None) with pytest.raises(deps.DependencyError) as excinfo: @@ -27,4 +36,4 @@ def test_missing_tool_raises_with_the_command_that_fixes_it(monkeypatch): message = str(excinfo.value) assert "depotdownloader" in message assert "steamcmd" not in message - assert "./install.sh" in message + assert "steam_installer/install_deps.sh" in message diff --git a/steam_installer/tests/test_dlc.py b/steam_installer/tests/test_dlc.py new file mode 100644 index 0000000..eca84f3 --- /dev/null +++ b/steam_installer/tests/test_dlc.py @@ -0,0 +1,190 @@ +"""DLC that ships as its own app: discovery, download, and crediting it in the manifest. + +Shapes are taken from the real `app_info_print` of Cyberpunk 2077 (1091500) and +its DLC, 2025-09-25: Phantom Liberty (2138330) keeps its ~25 GB depot under its +own app id; 2060310 is listed in the game's own depots; 2441600 has none. +""" + +from __future__ import annotations + +import logging +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import appinfo, depotdl, deps, dlc, runlog + +if TYPE_CHECKING: + from collections.abc import Iterator + from pathlib import Path + + from steam_installer.runlog import RunLog + +pytestmark = pytest.mark.usefixtures("cli_logs") + +# Every real depot that has content ships a manifest on the public branch. +_PUB = {"manifests": {"public": {"gid": "1"}}} + +GAME = { + "extended": {"listofdlc": "2060310,2138330,2441600"}, + "depots": { + "1091501": {"config": {"oslist": "windows"}, **_PUB}, + "2060310": {"dlcappid": "2060310", "config": {"oslist": "windows"}, **_PUB}, + }, +} +DLC_BLOCKS: dict[int, dict[str, object]] = { + # Listed in the game's own depots: the base run already covers it. + 2060310: {"depots": {"2060310": {"dlcappid": "2060310"}}}, + # Its own depot, tagged with its own id -- exactly what it installs. + 2138330: { + "depots": { + "2138330": {"dlcappid": "2138330", "config": {"oslist": "windows"}, **_PUB}, + "2138331": {"config": {"oslist": "windows", "language": "german"}, **_PUB}, + } + }, + 2441600: {"common": {"name": "Pre-Order Bonus"}}, +} + + +def _app_info( + monkeypatch: pytest.MonkeyPatch, + game: dict[str, object] | None = None, + dlc_blocks: dict[int, dict[str, object]] | None = None, +) -> None: + block = GAME if game is None else game + blocks = DLC_BLOCKS if dlc_blocks is None else dlc_blocks + monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: block) + monkeypatch.setattr( + appinfo, "cached_blocks", lambda ids: {i: blocks[i] for i in ids} + ) + + +def test_only_dlc_with_depots_of_its_own_needs_a_run( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Only dlc with depots of its own needs a run.""" + _app_info(monkeypatch) + assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} + + +@pytest.mark.parametrize("game", [{}, {"extended": "scalar"}, {"extended": {}}]) +def test_a_game_without_dlc_needs_no_lookup( + monkeypatch: pytest.MonkeyPatch, game: dict[str, object] +) -> None: + """A game without dlc needs no lookup.""" + monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: game) + monkeypatch.setattr( + appinfo, "cached_blocks", lambda ids: pytest.fail("no lookup expected") + ) + assert appinfo.get_dlc_plans(1) == {} + + +def test_a_game_with_no_depot_list_still_finds_its_dlc( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A game with no depot list still finds its dlc.""" + _app_info(monkeypatch, game={"extended": {"listofdlc": "2138330"}}) + assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} + + +def test_depot_plan_tags_the_games_own_dlc_depots( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Depot plan tags the games own dlc depots.""" + _app_info(monkeypatch) + plan = appinfo.get_depot_plan(1091500) + assert plan.required == {"1091501"} + assert plan.dlc == {"2060310": "2060310"} + + +def _logged(monkeypatch: pytest.MonkeyPatch, returncode: int, output: str = "") -> None: + monkeypatch.setattr( + depotdl.runlog, "run_logged", lambda cmd, logger: (returncode, output) + ) + + +@pytest.mark.parametrize( + ("returncode", "output", "expected"), + [ + (0, "", True), + (1, "App 2441600 (Bonus) is not available from this account.", None), + (1, "Connection lost", False), + ], +) +def test_download_dlc_separates_not_owned_from_failed( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + returncode: int, + output: str, + *, + expected: bool | None, +) -> None: + """Download dlc separates not owned from failed.""" + _logged(monkeypatch, returncode, output) + pull = depotdl.Pull(tmp_path, "u") + result = depotdl.download_dlc(2441600, pull, logging.getLogger("t")) + assert result is expected + + +def test_the_managed_depotdownloader_wins_over_path( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The managed depotdownloader wins over path.""" + managed = tmp_path / "depotdownloader" + managed.write_text("") + managed.chmod(0o755) + monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", managed) + assert deps.depotdownloader_bin() == str(managed) + + +@pytest.fixture(name="run_log") +def _run_log(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[RunLog]: + """The run log fixture.""" + monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state") + log = runlog.setup(1091500) + yield log + for handler in list(log.logger.handlers): + log.logger.removeHandler(handler) + handler.close() + + +def _download_owned( + monkeypatch: pytest.MonkeyPatch, + run_log: RunLog, + tmp_path: Path, + outcomes: dict[int, bool | None], +) -> appinfo.DepotPlan | None: + monkeypatch.setattr( + appinfo, + "get_dlc_plans", + lambda appid, language: {7: {"70": "g70"}, 8: {"80": "g80"}}, + ) + monkeypatch.setattr( + depotdl, "download_dlc", lambda dlc_appid, pull, logger: outcomes[dlc_appid] + ) + return dlc.download_owned(1, depotdl.Pull(tmp_path, "u", max_downloads=8), run_log) + + +def test_owned_dlc_is_returned_and_unowned_skipped( + monkeypatch: pytest.MonkeyPatch, run_log: RunLog, tmp_path: Path +) -> None: + """Owned dlc is returned and unowned skipped.""" + plan = _download_owned(monkeypatch, run_log, tmp_path, {7: True, 8: None}) + assert (plan.required, plan.dlc) == ({"70"}, {"70": "7"}) + # Its public gid too, so the finish check can prove the DLC's build. + assert plan.manifests == {"70": "g70"} + + +def test_a_failed_dlc_download_aborts( + monkeypatch: pytest.MonkeyPatch, run_log: RunLog, tmp_path: Path +) -> None: + """A failed dlc download aborts.""" + assert _download_owned(monkeypatch, run_log, tmp_path, {7: False, 8: True}) is None + + +def test_a_dlc_with_a_malformed_depot_section_is_skipped( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A dlc with a malformed depot section is skipped.""" + _app_info(monkeypatch, dlc_blocks={**DLC_BLOCKS, 2441600: {"depots": "scalar"}}) + assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}} diff --git a/steam_installer/tests/test_dlc_finish.py b/steam_installer/tests/test_dlc_finish.py new file mode 100644 index 0000000..dcd750c --- /dev/null +++ b/steam_installer/tests/test_dlc_finish.py @@ -0,0 +1,88 @@ +"""A stub manifest finished with DLC that ships as its own app.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import appinfo, cli, dlc, recognize, vdf +from steam_installer.tests._cli_helpers import ARGV, _stub_download + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +def _dlc_plan(gid: str) -> appinfo.DepotPlan: + return appinfo.DepotPlan( + {"2138330"}, set(), {"2138330": "2138330"}, {"2138330": gid} + ) + + +def test_a_stub_with_own_app_dlc_finishes_with_the_proven_build( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """Base game and its own-app DLC both match the public branch: a build-0 + stub is finished, the DLC's gid coming from the DLC's own app info.""" + library, install_dir = steam + (install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x") + manifest = library.steamapps / "appmanifest_1771300.acf" + manifest.write_text(vdf.dumps("AppState", {"appid": "1771300", "buildid": "0"})) + monkeypatch.setattr( + appinfo, + "get_depot_plan", + lambda appid, language: appinfo.DepotPlan( + {"1771303"}, set(), manifests={"1771303": "1999516201078544527"} + ), + ) + _stub_download(monkeypatch) + monkeypatch.setattr(dlc, "download_owned", lambda *_: _dlc_plan("100389")) + + assert cli.main(ARGV) == 0 + assert recognize.read_app_state(manifest)["buildid"] == "23914554" + + +def test_a_failed_dlc_download_leaves_the_manifest_alone( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """A failed dlc download leaves the manifest alone.""" + library, _ = steam + manifest = library.steamapps / "appmanifest_1771300.acf" + before = recognize.read_app_state(manifest) + _stub_download(monkeypatch) + monkeypatch.setattr(dlc, "download_owned", lambda *_: None) + + assert cli.main(ARGV) == 1 + # Only the folder name is fixed up front; nothing claims an install. + after = recognize.read_app_state(manifest) + assert after.pop("installdir") == "KingdomComeDeliverance2" + assert after == before + + +def test_downloaded_dlc_is_required_and_credited_to_its_app( + steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + """Downloaded dlc is required and credited to its app.""" + library, install_dir = steam + (install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x") + manifest = library.steamapps / "appmanifest_1771300.acf" + state = recognize.read_app_state(manifest) + # Steam's own progress for a DLC download it had started itself. + state["DlcDownloads"] = { + "2138330": {"BytesDownloaded": "1", "BytesToDownload": "9"} + } + manifest.write_text(vdf.dumps("AppState", state)) + _stub_download(monkeypatch) + monkeypatch.setattr(dlc, "download_owned", lambda *_: _dlc_plan("100389")) + + assert cli.main(ARGV) == 0 + + state = recognize.read_app_state(manifest) + assert state["InstalledDepots"]["2138330"]["dlcappid"] == "2138330" + assert "dlcappid" not in state["InstalledDepots"]["1771303"] + assert "DlcDownloads" not in state diff --git a/_installer_import/tests/test_finish_check.py b/steam_installer/tests/test_finish_check.py similarity index 76% rename from _installer_import/tests/test_finish_check.py rename to steam_installer/tests/test_finish_check.py index cf36e38..e259ff3 100644 --- a/_installer_import/tests/test_finish_check.py +++ b/steam_installer/tests/test_finish_check.py @@ -1,8 +1,22 @@ +"""Tests for finish check.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + from steam_installer.finish_check import check_ready_to_finish +if TYPE_CHECKING: + from pathlib import Path -def test_check_ready_to_finish_blocks_missing_depot(tmp_path, monkeypatch): - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + import pytest + + +def test_check_ready_to_finish_blocks_missing_depot( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Check ready to finish blocks missing depot.""" + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -17,8 +31,11 @@ def test_check_ready_to_finish_blocks_missing_depot(tmp_path, monkeypatch): assert any("1771303" in p for p in problems) -def test_check_ready_to_finish_blocks_zero_buildid(tmp_path, monkeypatch): - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) +def test_check_ready_to_finish_blocks_zero_buildid( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Check ready to finish blocks zero buildid.""" + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -33,8 +50,11 @@ def test_check_ready_to_finish_blocks_zero_buildid(tmp_path, monkeypatch): assert any("buildid" in p for p in problems) -def test_check_ready_to_finish_blocks_build_mismatch(tmp_path, monkeypatch): - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) +def test_check_ready_to_finish_blocks_build_mismatch( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Check ready to finish blocks build mismatch.""" + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -49,7 +69,9 @@ def test_check_ready_to_finish_blocks_build_mismatch(tmp_path, monkeypatch): assert any("different build" in p for p in problems) -def test_check_ready_to_finish_blocks_running_steam(tmp_path, monkeypatch): +def test_check_ready_to_finish_blocks_running_steam( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Steam owns the manifest while it runs and rewrites it from its own state.""" monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [4242]) install_dir = tmp_path / "Game" @@ -66,8 +88,11 @@ def test_check_ready_to_finish_blocks_running_steam(tmp_path, monkeypatch): assert any("4242" in p for p in problems) -def test_check_ready_to_finish_passes_when_everything_holds(tmp_path, monkeypatch): - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) +def test_check_ready_to_finish_passes_when_everything_holds( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Check ready to finish passes when everything holds.""" + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -85,10 +110,12 @@ def test_check_ready_to_finish_passes_when_everything_holds(tmp_path, monkeypatc ) -def test_check_ready_to_finish_blocks_empty_required_depot_set(tmp_path, monkeypatch): +def test_check_ready_to_finish_blocks_empty_required_depot_set( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """An empty required set would make the depot check vacuously true and wave through a download that fetched nothing.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" (install_dir / ".DepotDownloader").mkdir(parents=True) @@ -101,10 +128,12 @@ def test_check_ready_to_finish_blocks_empty_required_depot_set(tmp_path, monkeyp assert any("no depots match" in p for p in problems) -def test_check_ready_to_finish_blocks_when_no_game_content(tmp_path, monkeypatch): +def test_check_ready_to_finish_blocks_when_no_game_content( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Cache filenames are not content: a dir holding only .DepotDownloader/_.manifest must not be written as installed.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -119,10 +148,12 @@ def test_check_ready_to_finish_blocks_when_no_game_content(tmp_path, monkeypatch assert any("no game content" in p for p in problems) -def test_check_ready_to_finish_blocks_unknown_public_build(tmp_path, monkeypatch): +def test_check_ready_to_finish_blocks_unknown_public_build( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """A None expected build means we cannot tell current from stale -- refuse rather than silently skipping the comparison.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -138,12 +169,14 @@ def test_check_ready_to_finish_blocks_unknown_public_build(tmp_path, monkeypatch assert any("current public build" in p for p in problems) -def test_gate_blocks_before_staging_is_destroyed(tmp_path, monkeypatch): +def test_gate_blocks_before_staging_is_destroyed( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """finish_manifest_manually raises when every cached depot is shared. The CLI deletes Steam's staging tree first, so the gate must catch this while refusing is still free -- otherwise a failed run destroys tens of GB of resumable download state.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" cache_dir.mkdir(parents=True) @@ -159,20 +192,21 @@ def test_gate_blocks_before_staging_is_destroyed(tmp_path, monkeypatch): assert any("nothing to record as installed" in p for p in problems) -def _bg3_dir(tmp_path, gid="8253106406752983615"): +def _bg3_dir(tmp_path: Path, gid: str = "8253106406752983615") -> Path: install_dir = tmp_path / "Baldurs Gate 3" - cache_dir = install_dir / ".DepotDownloader" - cache_dir.mkdir(parents=True) - (cache_dir / f"1086941_{gid}.manifest").write_bytes(b"x") + (install_dir / ".DepotDownloader").mkdir(parents=True) + (install_dir / ".DepotDownloader" / f"1086941_{gid}.manifest").write_bytes(b"x") (install_dir / "Gustav.pak").write_bytes(b"x" * 1000) return install_dir -def test_cached_public_manifests_prove_the_build(tmp_path, monkeypatch): +def test_cached_public_manifests_prove_the_build( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """The enforcer's stub manifest says build 0, and Steam never ran to fill in TargetBuildID. Every depot holding the public branch's manifest *is* the current build, so that is no reason to refuse.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) problems = check_ready_to_finish( install_dir=_bg3_dir(tmp_path), app_state={"buildid": "0", "TargetBuildID": "0"}, @@ -183,8 +217,11 @@ def test_cached_public_manifests_prove_the_build(tmp_path, monkeypatch): assert problems == [] -def test_a_stale_depot_manifest_is_refused(tmp_path, monkeypatch): - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) +def test_a_stale_depot_manifest_is_refused( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A stale depot manifest is refused.""" + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) problems = check_ready_to_finish( install_dir=_bg3_dir(tmp_path, gid="111"), app_state={"buildid": "25605617"}, @@ -198,9 +235,11 @@ def test_a_stale_depot_manifest_is_refused(tmp_path, monkeypatch): assert any("start Steam once, let it sync, rerun" in p for p in problems) -def test_a_proven_build_still_needs_a_buildid_to_record(tmp_path, monkeypatch): +def test_a_proven_build_still_needs_a_buildid_to_record( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Matching gids prove the content, but the manifest needs a number.""" - monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: []) + monkeypatch.setattr("steam_installer.recognize.steam_client_pids", list) problems = check_ready_to_finish( install_dir=_bg3_dir(tmp_path), app_state={"buildid": "0"}, diff --git a/_installer_import/tests/test_freshness_wrapper.py b/steam_installer/tests/test_freshness_wrapper.py similarity index 69% rename from _installer_import/tests/test_freshness_wrapper.py rename to steam_installer/tests/test_freshness_wrapper.py index 755906e..59f0c9e 100644 --- a/_installer_import/tests/test_freshness_wrapper.py +++ b/steam_installer/tests/test_freshness_wrapper.py @@ -1,12 +1,21 @@ """The freshness wrapper fails closed when ~/src/utils cannot read the Proton lock.""" -import subprocess -from pathlib import Path +from __future__ import annotations -WRAPPER = Path(__file__).resolve().parent.parent / "scripts" / "check_dependency_freshness.sh" +from pathlib import Path +import shutil +import subprocess + +WRAPPER = ( + Path(__file__).resolve().parents[2] / "scripts" / "check_dependency_freshness.sh" +) LOCK = "steam_installer/data/proton-native.lock" +BASH = shutil.which("bash") or "bash" +GIT = shutil.which("git") or "git" + + def _utils(tmp_path: Path, *, adapter: bool) -> Path: root = tmp_path / "utils" gate = root / "scripts" / "check_dependency_freshness.sh" @@ -22,7 +31,7 @@ def _utils(tmp_path: Path, *, adapter: bool) -> Path: def _run(root: Path, *args: str) -> subprocess.CompletedProcess: return subprocess.run( - ["bash", str(WRAPPER), *args], + [BASH, str(WRAPPER), *args], env={"UTILS_ROOT": str(root), "PATH": "/usr/bin:/bin"}, capture_output=True, text=True, @@ -30,31 +39,35 @@ def _run(root: Path, *args: str) -> subprocess.CompletedProcess: ) -def test_lock_without_the_pacman_adapter_fails(tmp_path): +def test_lock_without_the_pacman_adapter_fails(tmp_path: Path) -> None: + """Lock without the pacman adapter fails.""" result = _run(_utils(tmp_path, adapter=False), LOCK) assert result.returncode == 1 assert "pacman_lock" in result.stderr assert "shared-gate-ran" not in result.stdout -def test_full_run_without_the_pacman_adapter_fails(tmp_path): +def test_full_run_without_the_pacman_adapter_fails(tmp_path: Path) -> None: + """Full run without the pacman adapter fails.""" assert _run(_utils(tmp_path, adapter=False), "--all", "--strict").returncode == 1 -def test_lock_with_the_adapter_reaches_the_shared_gate(tmp_path): +def test_lock_with_the_adapter_reaches_the_shared_gate(tmp_path: Path) -> None: + """Lock with the adapter reaches the shared gate.""" result = _run(_utils(tmp_path, adapter=True), LOCK) assert result.returncode == 0 assert "shared-gate-ran" in result.stdout -def test_other_manifests_skip_the_adapter_check(tmp_path): +def test_other_manifests_skip_the_adapter_check(tmp_path: Path) -> None: + """Other manifests skip the adapter check.""" result = _run(_utils(tmp_path, adapter=False), "requirements.txt") assert result.returncode == 0 def _git(repo: Path, *args: str) -> str: return subprocess.run( - ["git", "-C", str(repo), *args], capture_output=True, text=True, check=True + [GIT, "-C", str(repo), *args], capture_output=True, text=True, check=True ).stdout.strip() @@ -73,22 +86,29 @@ def _diverged_utils(tmp_path: Path, *, origin_has_adapter: bool) -> Path: parser.write_text("") _git(root, "-c", "user.name=t", "-c", "user.email=t@t", "add", "-A") _git(root, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "new") - _git(root, "update-ref", "refs/remotes/origin/main", _git(root, "rev-parse", "HEAD")) + _git( + root, "update-ref", "refs/remotes/origin/main", _git(root, "rev-parse", "HEAD") + ) _git(root, "reset", "-q", "--hard", old) return root def _run_cached(root: Path, cache: Path, *args: str) -> subprocess.CompletedProcess: return subprocess.run( - ["bash", str(WRAPPER), *args], - env={"UTILS_ROOT": str(root), "PATH": "/usr/bin:/bin", "XDG_CACHE_HOME": str(cache)}, + [BASH, str(WRAPPER), *args], + env={ + "UTILS_ROOT": str(root), + "PATH": "/usr/bin:/bin", + "XDG_CACHE_HOME": str(cache), + }, capture_output=True, text=True, check=False, ) -def test_old_checkout_runs_origin_main_snapshot(tmp_path): +def test_old_checkout_runs_origin_main_snapshot(tmp_path: Path) -> None: + """Old checkout runs origin main snapshot.""" root = _diverged_utils(tmp_path, origin_has_adapter=True) result = _run_cached(root, tmp_path / "cache", LOCK) assert result.returncode == 0, result.stderr @@ -98,7 +118,8 @@ def test_old_checkout_runs_origin_main_snapshot(tmp_path): assert not (root / "dep_freshness" / "parsers" / "pacman_lock.py").exists() -def test_old_checkout_and_old_origin_fail_closed(tmp_path): +def test_old_checkout_and_old_origin_fail_closed(tmp_path: Path) -> None: + """Old checkout and old origin fail closed.""" root = _diverged_utils(tmp_path, origin_has_adapter=False) result = _run_cached(root, tmp_path / "cache", LOCK) assert result.returncode == 1 diff --git a/_installer_import/tests/test_ghost.py b/steam_installer/tests/test_ghost.py similarity index 68% rename from _installer_import/tests/test_ghost.py rename to steam_installer/tests/test_ghost.py index dbad5e0..5b2904e 100644 --- a/_installer_import/tests/test_ghost.py +++ b/steam_installer/tests/test_ghost.py @@ -10,14 +10,24 @@ Shared fixtures (`library`, `GHOST_ACF`, `_write`, `_real_install`) live in conftest.py; the repair side lives in test_ghost_repair.py. """ +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + import pytest from steam_installer import ghost, vdf from steam_installer.library import SteamLibrary -from tests.conftest import GHOST_ACF, _real_install, _write +from steam_installer.tests._acf_helpers import GHOST_ACF, _real_install, _write + +if TYPE_CHECKING: + from pathlib import Path -def test_the_real_anonymous_steamcmd_manifest_is_detected(library): +def test_the_real_anonymous_steamcmd_manifest_is_detected( + library: SteamLibrary, +) -> None: + """The real anonymous steamcmd manifest is detected.""" _write(library, 284160, GHOST_ACF) (library.common / "BeamNG.drive").mkdir() @@ -29,20 +39,21 @@ def test_the_real_anonymous_steamcmd_manifest_is_detected(library): assert "no game content on disk" in report.reasons -def test_a_missing_content_directory_is_still_a_ghost(library): +def test_a_missing_content_directory_is_still_a_ghost(library: SteamLibrary) -> None: """The directory need not exist at all -- only the lie has to.""" _write(library, 284160, GHOST_ACF) assert ghost.inspect(library, 284160).is_ghost -def test_a_real_install_is_never_a_ghost(library): +def test_a_real_install_is_never_a_ghost(library: SteamLibrary) -> None: + """A real install is never a ghost.""" _real_install(library) report = ghost.inspect(library, 475150) assert not report.is_ghost assert report.content_bytes == 4096 -def test_content_on_disk_alone_disqualifies_a_ghost(library): +def test_content_on_disk_alone_disqualifies_a_ghost(library: SteamLibrary) -> None: """The interlock that matters: even a manifest that looks exactly like the fake one is not a ghost if there are real bytes behind it.""" _write(library, 284160, GHOST_ACF) @@ -53,14 +64,16 @@ def test_content_on_disk_alone_disqualifies_a_ghost(library): assert not ghost.inspect(library, 284160).is_ghost -def test_a_nonzero_size_on_disk_disqualifies_a_ghost(library): +def test_a_nonzero_size_on_disk_disqualifies_a_ghost(library: SteamLibrary) -> None: + """A nonzero size on disk disqualifies a ghost.""" _write( library, 284160, GHOST_ACF.replace('"SizeOnDisk"\t\t"0"', '"SizeOnDisk"\t\t"9"') ) assert not ghost.inspect(library, 284160).is_ghost -def test_listed_depots_disqualify_a_ghost(library): +def test_listed_depots_disqualify_a_ghost(library: SteamLibrary) -> None: + """Listed depots disqualify a ghost.""" _write( library, 284160, @@ -73,7 +86,9 @@ def test_listed_depots_disqualify_a_ghost(library): @pytest.mark.parametrize("flags", ["1026", "2", "0", "notanumber", ""]) -def test_a_manifest_not_claiming_installed_is_not_a_ghost(library, flags): +def test_a_manifest_not_claiming_installed_is_not_a_ghost( + library: SteamLibrary, flags: str +) -> None: """Without bit 4 the manifest is not claiming anything is installed, so there is no lie to clean up -- 1026 is a normal mid-download state.""" _write( @@ -84,7 +99,9 @@ def test_a_manifest_not_claiming_installed_is_not_a_ghost(library, flags): assert not ghost.inspect(library, 284160).is_ghost -def test_state_flags_six_still_counts_as_claiming_installed(library): +def test_state_flags_six_still_counts_as_claiming_installed( + library: SteamLibrary, +) -> None: """6 is 4|2 -- installed, update required. Every genuinely installed app in a real library carries it, so the bit test must not be an equality test.""" _write( @@ -93,25 +110,28 @@ def test_state_flags_six_still_counts_as_claiming_installed(library): assert ghost.inspect(library, 284160).is_ghost -def test_a_missing_manifest_is_not_a_ghost(library): +def test_a_missing_manifest_is_not_a_ghost(library: SteamLibrary) -> None: + """A missing manifest is not a ghost.""" assert not ghost.inspect(library, 284160).is_ghost -def test_a_manifest_for_a_different_appid_is_ignored(library): +def test_a_manifest_for_a_different_appid_is_ignored(library: SteamLibrary) -> None: + """A manifest for a different appid is ignored.""" _write( library, 284160, GHOST_ACF.replace('"appid"\t\t"284160"', '"appid"\t\t"999"') ) assert not ghost.inspect(library, 284160).is_ghost -def test_a_manifest_without_an_installdir_is_not_a_ghost(library): +def test_a_manifest_without_an_installdir_is_not_a_ghost(library: SteamLibrary) -> None: + """A manifest without an installdir is not a ghost.""" _write(library, 284160, GHOST_ACF.replace('\t"installdir"\t\t"BeamNG.drive"\n', "")) report = ghost.inspect(library, 284160) assert report.install_dir is None assert report.is_ghost -def test_our_own_downloader_state_disqualifies_a_ghost(library): +def test_our_own_downloader_state_disqualifies_a_ghost(library: SteamLibrary) -> None: """An empty install dir mid-run is not a ghost; .DepotDownloader proves a real download is in progress against it.""" _write(library, 284160, GHOST_ACF) @@ -122,7 +142,8 @@ def test_our_own_downloader_state_disqualifies_a_ghost(library): assert not ghost.inspect(library, 284160).is_ghost -def test_staged_content_makes_a_ghost_warm(library): +def test_staged_content_makes_a_ghost_warm(library: SteamLibrary) -> None: + """Staged content makes a ghost warm.""" _write(library, 284160, GHOST_ACF) staged = library.steamapps / "downloading" / "284160" staged.mkdir(parents=True) @@ -135,7 +156,8 @@ def test_staged_content_makes_a_ghost_warm(library): assert report.staging_bytes == 2048 -def test_scan_finds_only_the_ghost(library): +def test_scan_finds_only_the_ghost(library: SteamLibrary) -> None: + """Scan finds only the ghost.""" _write(library, 284160, GHOST_ACF) _real_install(library) (library.steamapps / "appmanifest_notanumber.acf").write_text(GHOST_ACF) @@ -143,16 +165,20 @@ def test_scan_finds_only_the_ghost(library): assert [r.appid for r in ghost.scan([library])] == [284160] -def test_scan_skips_a_library_with_no_steamapps(tmp_path): +def test_scan_skips_a_library_with_no_steamapps(tmp_path: Path) -> None: + """Scan skips a library with no steamapps.""" assert ghost.scan([SteamLibrary(tmp_path / "nope")]) == [] -def test_an_unparseable_manifest_is_not_a_ghost(library, monkeypatch): +def test_an_unparseable_manifest_is_not_a_ghost( + library: SteamLibrary, monkeypatch: pytest.MonkeyPatch +) -> None: """A manifest we cannot read is not a manifest we may act on.""" _write(library, 284160, "this is not vdf {{{") - def boom(path): - raise vdf.VdfParseError("unbalanced braces", 0) + def boom(_path: Path) -> Never: + msg = "unbalanced braces" + raise vdf.VdfParseError(msg, 0) monkeypatch.setattr(ghost.recognize, "read_app_state", boom) diff --git a/_installer_import/tests/test_ghost_repair.py b/steam_installer/tests/test_ghost_repair.py similarity index 72% rename from _installer_import/tests/test_ghost_repair.py rename to steam_installer/tests/test_ghost_repair.py index 34c4196..24e1ccd 100644 --- a/_installer_import/tests/test_ghost_repair.py +++ b/steam_installer/tests/test_ghost_repair.py @@ -3,17 +3,26 @@ Split from test_ghost.py to keep both files under the repo's 250-line cap. """ +from __future__ import annotations + import logging +from typing import TYPE_CHECKING import pytest from steam_installer import ghost -from tests.conftest import GHOST_ACF, _real_install, _write +from steam_installer.tests._acf_helpers import GHOST_ACF, _real_install, _write + +if TYPE_CHECKING: + from steam_installer.library import SteamLibrary LOGGER = logging.getLogger("test-ghost-repair") -def test_repair_moves_the_manifest_aside_and_removes_the_empty_directory(library): +def test_repair_moves_the_manifest_aside_and_removes_the_empty_directory( + library: SteamLibrary, +) -> None: + """Repair moves the manifest aside and removes the empty directory.""" _write(library, 284160, GHOST_ACF) content = library.common / "BeamNG.drive" content.mkdir() @@ -28,7 +37,9 @@ def test_repair_moves_the_manifest_aside_and_removes_the_empty_directory(library assert touched == [backup, content] -def test_repair_refuses_to_delete_a_non_empty_directory(library, caplog): +def test_repair_refuses_to_delete_a_non_empty_directory( + library: SteamLibrary, caplog: pytest.LogCaptureFixture +) -> None: """rmdir, never rmtree: the kernel itself is the last line of defence.""" _write(library, 284160, GHOST_ACF) content = library.common / "BeamNG.drive" @@ -45,7 +56,7 @@ def test_repair_refuses_to_delete_a_non_empty_directory(library, caplog): assert content not in touched -def test_repair_refuses_a_warm_ghost(library): +def test_repair_refuses_a_warm_ghost(library: SteamLibrary) -> None: """Staged bytes are a real partial download, not a lie to clean up.""" _write(library, 284160, GHOST_ACF) staged = library.steamapps / "downloading" / "284160" @@ -60,7 +71,8 @@ def test_repair_refuses_a_warm_ghost(library): assert (staged / "part.pak").exists() -def test_repair_refuses_anything_that_is_not_a_ghost(library): +def test_repair_refuses_anything_that_is_not_a_ghost(library: SteamLibrary) -> None: + """Repair refuses anything that is not a ghost.""" _real_install(library) report = ghost.inspect(library, 475150) @@ -68,7 +80,8 @@ def test_repair_refuses_anything_that_is_not_a_ghost(library): ghost.repair(report, LOGGER) -def test_repair_defaults_its_timestamp_to_now(library): +def test_repair_defaults_its_timestamp_to_now(library: SteamLibrary) -> None: + """Repair defaults its timestamp to now.""" _write(library, 284160, GHOST_ACF) touched = ghost.repair(ghost.inspect(library, 284160), LOGGER) assert touched[0].name.startswith("appmanifest_284160.acf.ghost-") diff --git a/steam_installer/tests/test_http.py b/steam_installer/tests/test_http.py new file mode 100644 index 0000000..b1cb459 --- /dev/null +++ b/steam_installer/tests/test_http.py @@ -0,0 +1,53 @@ +"""The one https-only way the package opens a URL.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Self + +import pytest + +from steam_installer import _http + +if TYPE_CHECKING: + import urllib.request + + +class _Response: + def __enter__(self) -> Self: + return self + + def __exit__(self, *exc: object) -> None: + return None + + +def test_a_non_https_url_is_refused_before_any_request( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Not even file:// or plain http gets as far as urlopen.""" + monkeypatch.setattr( + _http.urllib.request, "urlopen", lambda *a, **kw: pytest.fail("no request") + ) + for url in ("http://example.invalid/x", "file:///etc/passwd", "ftp://x/y"): + with ( + pytest.raises(ValueError, match="non-https"), + _http.open_https(url, timeout=1, user_agent="ua"), + ): + pass + + +def test_an_https_url_is_opened_with_the_agent_and_timeout( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """The request carries the caller's User-Agent, and the timeout is passed on.""" + seen: dict[str, object] = {} + + def fake_urlopen(request: urllib.request.Request, timeout: float) -> _Response: + seen["url"] = request.full_url + seen["agent"] = request.get_header("User-agent") + seen["timeout"] = timeout + return _Response() + + monkeypatch.setattr(_http.urllib.request, "urlopen", fake_urlopen) + with _http.open_https("https://example.invalid/x", timeout=7, user_agent="ua"): + pass + assert seen == {"url": "https://example.invalid/x", "agent": "ua", "timeout": 7} diff --git a/steam_installer/tests/test_install_sh.py b/steam_installer/tests/test_install_sh.py new file mode 100644 index 0000000..636214f --- /dev/null +++ b/steam_installer/tests/test_install_sh.py @@ -0,0 +1,75 @@ +"""install_deps.sh --check-only: what a run needs; steamcmd is gone from it.""" + +from __future__ import annotations + +import os +from pathlib import Path +import shutil +from typing import TYPE_CHECKING + +from steam_installer.tests._run_helpers import run_captured + +if TYPE_CHECKING: + import subprocess + +PKG = Path(__file__).resolve().parents[1] + + +BASH = shutil.which("bash") or "bash" + + +def _stub(bin_dir: Path, name: str, script: str) -> None: + path = bin_dir / name + path.write_text(f"#!/bin/bash\n{script}\n") + path.chmod(0o755) + + +def _check_only( + tmp_path: Path, *, libibus_installed: bool, proton_ok: bool = True +) -> subprocess.CompletedProcess[str]: + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + _stub(bin_dir, "depotdownloader", "exit 0") + _stub( + bin_dir, + "pacman", + f'[[ "$*" == "-Q libibus" ]] && exit {0 if libibus_installed else 1}; exit 0', + ) + # The real `--check` would look at the developer's own Steam tree. + _stub(bin_dir, "python3", f"exit {0 if proton_ok else 1}") + env = { + **os.environ, + "PATH": f"{bin_dir}:/usr/bin:/bin", + "HOME": str(tmp_path), + "SGI_PYTHON": str(bin_dir / "python3"), + } + return run_captured([BASH, str(PKG / "install_deps.sh"), "--check-only"], env) + + +def test_missing_libibus_is_reported_and_steamcmd_is_not_needed(tmp_path: Path) -> None: + """Missing libibus is reported and steamcmd is not needed.""" + result = _check_only(tmp_path, libibus_installed=False) + assert result.returncode == 1 + assert "missing: libibus" in result.stderr + assert "steamcmd" not in result.stdout + result.stderr + + +def test_all_present_without_steamcmd(tmp_path: Path) -> None: + """All present without steamcmd.""" + result = _check_only(tmp_path, libibus_installed=True) + assert result.returncode == 0, result.stderr + assert "All dependencies present." in result.stdout + + +def test_run_sh_rechecks_when_the_proton_pin_changes() -> None: + """A stale stamp must not hide a lock bump or a deleted tool folder.""" + text = (PKG / "run.sh").read_text() + assert "proton-native.lock" in text + assert "steam_installer.proton_native --check" in text + + +def test_missing_native_proton_is_reported(tmp_path: Path) -> None: + """Missing native proton is reported.""" + result = _check_only(tmp_path, libibus_installed=True, proton_ok=False) + assert result.returncode == 1 + assert "missing: proton-cachyos-native" in result.stderr diff --git a/_installer_import/tests/test_library.py b/steam_installer/tests/test_library.py similarity index 76% rename from _installer_import/tests/test_library.py rename to steam_installer/tests/test_library.py index a6f2d15..11f85da 100644 --- a/_installer_import/tests/test_library.py +++ b/steam_installer/tests/test_library.py @@ -1,3 +1,7 @@ +"""Tests for library.""" + +from __future__ import annotations + from pathlib import Path import pytest @@ -29,20 +33,23 @@ def _make_steam_root(tmp_path: Path, extra_library: Path | None = None) -> Path: return root -def test_list_libraries_includes_root_when_no_vdf(tmp_path): +def test_list_libraries_includes_root_when_no_vdf(tmp_path: Path) -> None: + """List libraries includes root when no vdf.""" root = _make_steam_root(tmp_path) libraries = list_libraries(root) assert [lib.path for lib in libraries] == [root] -def test_list_libraries_includes_extra_library_from_vdf(tmp_path): +def test_list_libraries_includes_extra_library_from_vdf(tmp_path: Path) -> None: + """List libraries includes extra library from vdf.""" extra = tmp_path / "SecondDrive" root = _make_steam_root(tmp_path, extra_library=extra) libraries = list_libraries(root) assert {lib.path for lib in libraries} == {root, extra.resolve()} -def test_find_existing_install_locates_game_in_any_library(tmp_path): +def test_find_existing_install_locates_game_in_any_library(tmp_path: Path) -> None: + """Find existing install locates game in any library.""" root = _make_steam_root(tmp_path) other = tmp_path / "Other" (other / "steamapps" / "common" / "MyGame").mkdir(parents=True) @@ -51,13 +58,15 @@ def test_find_existing_install_locates_game_in_any_library(tmp_path): assert found == other / "steamapps" / "common" / "MyGame" -def test_find_existing_install_returns_none_when_absent(tmp_path): +def test_find_existing_install_returns_none_when_absent(tmp_path: Path) -> None: + """Find existing install returns none when absent.""" root = _make_steam_root(tmp_path) libraries = [SteamLibrary(root)] assert find_existing_install(libraries, "NoSuchGame") is None -def test_pick_install_target_prefers_existing_install(tmp_path): +def test_pick_install_target_prefers_existing_install(tmp_path: Path) -> None: + """Pick install target prefers existing install.""" root = _make_steam_root(tmp_path) other = tmp_path / "Other" (other / "steamapps" / "common" / "MyGame").mkdir(parents=True) @@ -66,14 +75,18 @@ def test_pick_install_target_prefers_existing_install(tmp_path): assert target == other / "steamapps" / "common" / "MyGame" -def test_pick_install_target_defaults_to_primary_library(tmp_path): +def test_pick_install_target_defaults_to_primary_library(tmp_path: Path) -> None: + """Pick install target defaults to primary library.""" root = _make_steam_root(tmp_path) libraries = [SteamLibrary(root)] target = pick_install_target(libraries, "NewGame") assert target == root / "steamapps" / "common" / "NewGame" -def test_find_steam_root_prefers_default_location(tmp_path, monkeypatch): +def test_find_steam_root_prefers_default_location( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Find steam root prefers default location.""" root = tmp_path / ".local/share/Steam" (root / "steamapps").mkdir(parents=True) monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", root) @@ -82,7 +95,10 @@ def test_find_steam_root_prefers_default_location(tmp_path, monkeypatch): assert library.find_steam_root() == root -def test_find_steam_root_falls_back_to_dot_steam(tmp_path, monkeypatch): +def test_find_steam_root_falls_back_to_dot_steam( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Find steam root falls back to dot steam.""" fallback = tmp_path / ".steam/steam" (fallback / "steamapps").mkdir(parents=True) monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", tmp_path / "absent") @@ -91,7 +107,10 @@ def test_find_steam_root_falls_back_to_dot_steam(tmp_path, monkeypatch): assert library.find_steam_root() == fallback -def test_find_steam_root_raises_when_no_steam_install(tmp_path, monkeypatch): +def test_find_steam_root_raises_when_no_steam_install( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Find steam root raises when no steam install.""" monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", tmp_path / "absent") monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path)) @@ -99,7 +118,7 @@ def test_find_steam_root_raises_when_no_steam_install(tmp_path, monkeypatch): library.find_steam_root() -def test_list_libraries_ignores_malformed_libraryfolders(tmp_path): +def test_list_libraries_ignores_malformed_libraryfolders(tmp_path: Path) -> None: """A malformed or partial libraryfolders.vdf must degrade to "just the primary library", never crash the run.""" root = tmp_path / "Steam" @@ -111,7 +130,8 @@ def test_list_libraries_ignores_malformed_libraryfolders(tmp_path): assert [lib.path for lib in library.list_libraries(root)] == [root] -def test_list_libraries_skips_entries_without_a_path(tmp_path): +def test_list_libraries_skips_entries_without_a_path(tmp_path: Path) -> None: + """List libraries skips entries without a path.""" root = tmp_path / "Steam" (root / "steamapps").mkdir(parents=True) other = tmp_path / "hdd" @@ -131,7 +151,8 @@ def test_list_libraries_skips_entries_without_a_path(tmp_path): assert paths == [root, other.resolve()] -def test_list_libraries_ignores_non_dict_folder_entries(tmp_path): +def test_list_libraries_ignores_non_dict_folder_entries(tmp_path: Path) -> None: + """List libraries ignores non dict folder entries.""" root = tmp_path / "Steam" (root / "steamapps").mkdir(parents=True) (root / "steamapps" / "libraryfolders.vdf").write_text( @@ -141,7 +162,9 @@ def test_list_libraries_ignores_non_dict_folder_entries(tmp_path): assert [lib.path for lib in library.list_libraries(root)] == [root] -def test_list_libraries_ignores_non_dict_libraryfolders_root(tmp_path, monkeypatch): +def test_list_libraries_ignores_non_dict_libraryfolders_root( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Defensive guard: vdf.parse always yields a dict for a block today, so inject a parser that does not, keeping the guard honest if vdf changes.""" root = tmp_path / "Steam" diff --git a/_installer_import/tests/test_manifest_finish.py b/steam_installer/tests/test_manifest_finish.py similarity index 87% rename from _installer_import/tests/test_manifest_finish.py rename to steam_installer/tests/test_manifest_finish.py index 8d5774c..b47208a 100644 --- a/_installer_import/tests/test_manifest_finish.py +++ b/steam_installer/tests/test_manifest_finish.py @@ -1,5 +1,11 @@ +"""Tests for manifest finish.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + import pytest -from _recognize_helpers import _write_partial_manifest + from steam_installer import vdf from steam_installer.library import SteamLibrary from steam_installer.manifest_write import ( @@ -9,9 +15,14 @@ from steam_installer.recognize import ( RecognizeError, download_counters_settled, ) +from steam_installer.tests._recognize_helpers import _write_partial_manifest + +if TYPE_CHECKING: + from pathlib import Path -def test_finish_manifest_manually_requires_existing_manifest(tmp_path): +def test_finish_manifest_manually_requires_existing_manifest(tmp_path: Path) -> None: + """Finish manifest manually requires existing manifest.""" library = SteamLibrary(tmp_path / "Steam") install_dir = tmp_path / "Steam" / "steamapps" / "common" / "Game" install_dir.mkdir(parents=True) @@ -19,7 +30,10 @@ def test_finish_manifest_manually_requires_existing_manifest(tmp_path): finish_manifest_manually(library, 1771300, install_dir, now_unix=1000) -def test_finish_manifest_manually_requires_cached_depot_manifests(tmp_path): +def test_finish_manifest_manually_requires_cached_depot_manifests( + tmp_path: Path, +) -> None: + """Finish manifest manually requires cached depot manifests.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) install_dir = library.common / "Game" @@ -28,7 +42,8 @@ def test_finish_manifest_manually_requires_cached_depot_manifests(tmp_path): finish_manifest_manually(library, 1771300, install_dir, now_unix=1000) -def test_finish_manifest_manually_sets_state_flags_and_depots(tmp_path): +def test_finish_manifest_manually_sets_state_flags_and_depots(tmp_path: Path) -> None: + """Finish manifest manually sets state flags and depots.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) install_dir = library.common / "Game" @@ -51,7 +66,9 @@ def test_finish_manifest_manually_sets_state_flags_and_depots(tmp_path): assert manifest_path.with_suffix(".acf.bak").is_file() -def test_finish_manifest_manually_falls_back_to_target_buildid_when_zero(tmp_path): +def test_finish_manifest_manually_falls_back_to_target_buildid_when_zero( + tmp_path: Path, +) -> None: """buildid "0" is a sentinel for "no real build installed yet", not a real value -- a naive `or` chain treats it as truthy and never falls through.""" library = SteamLibrary(tmp_path / "Steam") @@ -78,7 +95,7 @@ def test_finish_manifest_manually_falls_back_to_target_buildid_when_zero(tmp_pat assert app_state["buildid"] == "23914554" -def test_finish_manifest_manually_corrects_installdir(tmp_path): +def test_finish_manifest_manually_corrects_installdir(tmp_path: Path) -> None: """The exact bug that caused real data loss: a stale installdir field pointing at a different folder than where content was actually downloaded.""" library = SteamLibrary(tmp_path / "Steam") @@ -102,7 +119,7 @@ def test_finish_manifest_manually_corrects_installdir(tmp_path): assert app_state["installdir"] == "KingdomComeDeliverance2" -def test_finish_manifest_settles_download_counters(tmp_path): +def test_finish_manifest_settles_download_counters(tmp_path: Path) -> None: """The regression that started all this: a manifest claiming StateFlags=4 while still saying 22 GB of 92 GB downloaded. Steam discarded the "installed" claim and resumed a 90 GB download.""" @@ -142,7 +159,7 @@ def test_finish_manifest_settles_download_counters(tmp_path): assert download_counters_settled(app_state) -def test_finish_manifest_excludes_shared_depots_from_installed(tmp_path): +def test_finish_manifest_excludes_shared_depots_from_installed(tmp_path: Path) -> None: """228989 is a SharedDepots entry owned by app 228980; claiming this app installed its own copy contradicts SharedDepots.""" library = SteamLibrary(tmp_path / "Steam") @@ -162,7 +179,8 @@ def test_finish_manifest_excludes_shared_depots_from_installed(tmp_path): assert app_state["SharedDepots"] == {"228989": "228980"} -def test_finish_manifest_rejects_only_shared_depots(tmp_path): +def test_finish_manifest_rejects_only_shared_depots(tmp_path: Path) -> None: + """Finish manifest rejects only shared depots.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) install_dir = library.common / "Game" @@ -174,7 +192,10 @@ def test_finish_manifest_rejects_only_shared_depots(tmp_path): finish_manifest_manually(library, 1771300, install_dir, now_unix=1000) -def test_finish_manifest_rejects_malformed_appstate(tmp_path, monkeypatch): +def test_finish_manifest_rejects_malformed_appstate( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Finish manifest rejects malformed appstate.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) install_dir = library.common / "Game" diff --git a/_installer_import/tests/test_manifest_write.py b/steam_installer/tests/test_manifest_write.py similarity index 79% rename from _installer_import/tests/test_manifest_write.py rename to steam_installer/tests/test_manifest_write.py index 0be9ad3..25cc507 100644 --- a/_installer_import/tests/test_manifest_write.py +++ b/steam_installer/tests/test_manifest_write.py @@ -1,5 +1,11 @@ +"""Tests for manifest write.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + import pytest -from _recognize_helpers import _write_partial_manifest + from steam_installer import manifest_write, vdf from steam_installer.library import SteamLibrary from steam_installer.manifest_write import ( @@ -7,9 +13,14 @@ from steam_installer.manifest_write import ( finish_manifest_manually, measure_content_size, ) +from steam_installer.tests._recognize_helpers import _write_partial_manifest + +if TYPE_CHECKING: + from pathlib import Path -def test_ensure_installdir_correct_patches_stale_field(tmp_path): +def test_ensure_installdir_correct_patches_stale_field(tmp_path: Path) -> None: + """Ensure installdir correct patches stale field.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) data = vdf.parse((library.steamapps / "appmanifest_1771300.acf").read_text()) @@ -27,7 +38,8 @@ def test_ensure_installdir_correct_patches_stale_field(tmp_path): assert app_state["installdir"] == "KingdomComeDeliverance2" -def test_ensure_installdir_correct_noop_when_already_correct(tmp_path): +def test_ensure_installdir_correct_noop_when_already_correct(tmp_path: Path) -> None: + """Ensure installdir correct noop when already correct.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) manifest_path = library.steamapps / "appmanifest_1771300.acf" @@ -43,7 +55,8 @@ def test_ensure_installdir_correct_noop_when_already_correct(tmp_path): assert manifest_path.read_text() == before -def test_ensure_installdir_correct_noop_when_no_manifest(tmp_path): +def test_ensure_installdir_correct_noop_when_no_manifest(tmp_path: Path) -> None: + """Ensure installdir correct noop when no manifest.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) # Should not raise, and should not create a manifest out of nothing. @@ -51,7 +64,8 @@ def test_ensure_installdir_correct_noop_when_no_manifest(tmp_path): assert not (library.steamapps / "appmanifest_1771300.acf").exists() -def test_measure_content_size_excludes_depotdownloader_cache(tmp_path): +def test_measure_content_size_excludes_depotdownloader_cache(tmp_path: Path) -> None: + """Measure content size excludes depotdownloader cache.""" install_dir = tmp_path / "Game" cache_dir = install_dir / ".DepotDownloader" / "staging" / "Data" cache_dir.mkdir(parents=True) @@ -62,7 +76,7 @@ def test_measure_content_size_excludes_depotdownloader_cache(tmp_path): assert measure_content_size(install_dir) == 100 -def test_backup_keeps_the_pristine_manifest_across_reruns(tmp_path): +def test_backup_keeps_the_pristine_manifest_across_reruns(tmp_path: Path) -> None: """A second run must not overwrite the backup with run 1's patched output -- that turns the recovery artifact for a 90+ GB install into a copy of the thing it is supposed to protect against.""" @@ -81,8 +95,10 @@ def test_backup_keeps_the_pristine_manifest_across_reruns(tmp_path): assert manifest.with_suffix(".acf.bak").read_text() == pristine -def test_manifest_write_is_atomic(tmp_path, monkeypatch): - """A crash mid-write must not leave a truncated, unparseable manifest.""" +def test_manifest_write_is_atomic( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A crash mid-write must not leave a truncated, unparsable manifest.""" library = SteamLibrary(tmp_path / "Steam") _write_partial_manifest(library.steamapps, 1771300) manifest = library.steamapps / "appmanifest_1771300.acf" @@ -92,11 +108,11 @@ def test_manifest_write_is_atomic(tmp_path, monkeypatch): cache.mkdir(parents=True) (cache / "1771303_1999516201078544527.manifest").write_bytes(b"x") - def explode(self, *args, **kwargs): + def explode(self: Path, *args: object, **kwargs: object) -> Never: raise OSError(28, "No space left on device") monkeypatch.setattr("pathlib.Path.write_text", explode) - with pytest.raises(OSError): + with pytest.raises(OSError, match="No space left"): finish_manifest_manually(library, 1771300, install_dir, now_unix=1000) # Original intact, and no temp file left behind. @@ -104,7 +120,9 @@ def test_manifest_write_is_atomic(tmp_path, monkeypatch): assert list(library.steamapps.glob("*.tmp*")) == [] -def test_ensure_installdir_correct_ignores_malformed_manifest(tmp_path, monkeypatch): +def test_ensure_installdir_correct_ignores_malformed_manifest( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Defensive guard: vdf.parse always yields a dict today, so inject a parser that does not, to keep the guard honest if vdf ever changes.""" library = SteamLibrary(tmp_path / "Steam") @@ -121,7 +139,8 @@ def test_ensure_installdir_correct_ignores_malformed_manifest(tmp_path, monkeypa assert manifest.read_text() == before -def test_ensure_installdir_correct_is_a_noop_when_already_right(tmp_path): +def test_ensure_installdir_correct_is_a_noop_when_already_right(tmp_path: Path) -> None: + """Ensure installdir correct is a noop when already right.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) manifest = library.steamapps / "appmanifest_1771300.acf" @@ -132,21 +151,24 @@ def test_ensure_installdir_correct_is_a_noop_when_already_right(tmp_path): assert not manifest.with_suffix(".acf.bak").exists() -def test_write_atomic_cleans_up_its_temp_file_on_failure(tmp_path, monkeypatch): +def test_write_atomic_cleans_up_its_temp_file_on_failure( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Write atomic cleans up its temp file on failure.""" target = tmp_path / "appmanifest_1.acf" target.write_text("original") - def explode(self, *args, **kwargs): + def explode(self: Path, *args: object, **kwargs: object) -> Never: raise OSError(28, "No space left on device") monkeypatch.setattr("pathlib.Path.write_text", explode) - with pytest.raises(OSError): + with pytest.raises(OSError, match="No space left"): manifest_write.write_atomic(target, "new content") assert list(tmp_path.glob("*.tmp*")) == [] -def test_measure_content_size_skips_directories_and_symlinks(tmp_path): +def test_measure_content_size_skips_directories_and_symlinks(tmp_path: Path) -> None: """Directories have a size of their own, and a symlink would double-count its target -- neither is game content Steam should see in SizeOnDisk.""" install_dir = tmp_path / "Game" diff --git a/steam_installer/tests/test_map_after_install.py b/steam_installer/tests/test_map_after_install.py new file mode 100644 index 0000000..00eb992 --- /dev/null +++ b/steam_installer/tests/test_map_after_install.py @@ -0,0 +1,114 @@ +"""Mapping the native Proton right after an install, and when it must not.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import ( + anticheat, + cli, + compat_apply, + recognize, +) +from steam_installer.tests._acf_helpers import stamp_native_tool +from steam_installer.tests._cli_helpers import ARGV, _stub_download +from steam_installer.tests._map_data import ( + CONFIG, + LOCKED, + PLAIN, + _mapped, +) + +if TYPE_CHECKING: + from pathlib import Path + + from steam_installer.library import SteamLibrary + + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +def test_an_install_maps_the_game_while_steam_is_closed( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """An install maps the game while steam is closed.""" + library, install_dir = steam + _with_compat_home(library.path, monkeypatch) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main(ARGV) == 0 + assert _mapped(library.path, 1771300) == ("proton-cachyos-native", "250") + assert "mapped to proton-cachyos-native" in capsys.readouterr().out + + +def test_valve_proton_keeps_valves_default_for_one_run( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """Valve proton keeps valves default for one run.""" + library, install_dir = steam + _with_compat_home(library.path, monkeypatch) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main([*ARGV, "--valve-proton"]) == 0 + assert _mapped(library.path, 1771300) is None + assert "Valve default: override: --valve-proton" in capsys.readouterr().out + + +def test_a_mapping_failure_leaves_the_install_on_valves_default( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """The install itself succeeded; the game still runs on Valve's Proton.""" + library, install_dir = steam + _with_compat_home(library.path, monkeypatch) + (library.path / "config" / "config.vdf").write_text("broken {") + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main(ARGV) == 0 + assert "stays on Valve's default" in capsys.readouterr().out + + +def test_an_owned_dlc_download_keeps_valves_default( + steam: tuple[SteamLibrary, Path], + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """An owned dlc download keeps valves default.""" + library, install_dir = steam + _with_compat_home(library.path, monkeypatch) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir, dlc={"1771303": "9"}) + assert cli.main(ARGV) == 0 + assert "Valve default: owned DLC" in capsys.readouterr().out + + +def _with_compat_home(root: Path, monkeypatch: pytest.MonkeyPatch) -> None: + (root / "config").mkdir(parents=True, exist_ok=True) + (root / "config" / "config.vdf").write_text(CONFIG) + stamp_native_tool(root, LOCKED) + monkeypatch.setattr(anticheat, "_download", lambda: b"[]") + + +def _proven( + monkeypatch: pytest.MonkeyPatch, + install_dir: Path, + dlc: dict[str, str] | None = None, +) -> None: + gid = next(iter(recognize.discover_cached_depot_manifests(install_dir).values())) + plan = compat_apply.appinfo.DepotPlan( + {"1771303"}, set(), manifests={"1771303": gid}, dlc=dict(dlc or {}) + ) + monkeypatch.setattr( + compat_apply.appinfo, "get_depot_plan", lambda appid, language: plan + ) diff --git a/steam_installer/tests/test_map_installed.py b/steam_installer/tests/test_map_installed.py new file mode 100644 index 0000000..d8b35b4 --- /dev/null +++ b/steam_installer/tests/test_map_installed.py @@ -0,0 +1,189 @@ +"""Mapping games to the native Proton: after an install, and for the whole library.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Never + +import pytest + +from steam_installer import ( + anticheat, + cli, + compat_apply, + compat_map, + recognize, + steamctl, + vdf, +) +from steam_installer.tests._acf_helpers import stamp_native_tool +from steam_installer.tests._appinfo_blob import encode +from steam_installer.tests._map_data import ( + CONFIG, + CS, + LOCKED, + NATIVE, + PLAIN, + SHARED, + TOOL, + _mapped, +) + +if TYPE_CHECKING: + from pathlib import Path + + +pytestmark = pytest.mark.usefixtures("cli_logs") + + +@pytest.fixture(name="home") +def _home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A Steam root: config.vdf, the stamped tool, an app cache, anti-cheat list.""" + root = tmp_path / "Steam" + (root / "config").mkdir(parents=True) + (root / "config" / "config.vdf").write_text(CONFIG) + stamp_native_tool(root, LOCKED) + cache = tmp_path / "appinfo.vdf" + cache.write_bytes( + encode({10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL}) + ) + monkeypatch.setattr( + compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache + ) + monkeypatch.setattr( + anticheat, "_download", lambda: b'[{"storeIds": {"steam": "730"}}]' + ) + steamapps = root / "steamapps" + steamapps.mkdir() + for appid in (10, 20, 1493710): + (steamapps / f"appmanifest_{appid}.acf").write_text( + vdf.dumps("AppState", {"appid": str(appid), "InstalledDepots": {}}) + ) + monkeypatch.setattr(cli, "find_steam_root", lambda: root) + return root + + +def test_dry_run_lists_decisions_and_writes_nothing( + home: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """Dry run lists decisions and writes nothing.""" + before = (home / "config" / "config.vdf").read_bytes() + assert cli.main(["--map-installed"]) == 0 + out = capsys.readouterr().out + assert "10 Plain: map" in out + assert "20 Native: Valve default: native" in out + assert "1493710" not in out # a tool, not a game + assert "Dry run" in out + assert (home / "config" / "config.vdf").read_bytes() == before + + +@pytest.mark.usefixtures("home") +def test_appids_decides_any_cached_app(capsys: pytest.CaptureFixture[str]) -> None: + """Appids decides any cached app.""" + assert cli.main(["--map-installed", "--appids", "730,20,30,10,999"]) == 0 + out = capsys.readouterr().out + assert "730 CS: Valve default: anti-cheat, native" in out + assert "30 Shared: Valve default: shared depots" in out + assert "10 Plain: map" in out + assert "999: not in Steam's app cache" in out + + +def test_apply_maps_the_safe_ones_and_a_rerun_changes_nothing( + home: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """Apply maps the safe ones and a rerun changes nothing.""" + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert _mapped(home, 10) == ("proton-cachyos-native", "250") + assert _mapped(home, 20) is None + once = (home / "config" / "config.vdf").read_bytes() + capsys.readouterr() + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert "10 Plain: map -- already mapped" in capsys.readouterr().out + assert (home / "config" / "config.vdf").read_bytes() == once + + +def test_apply_closes_steam_first_and_restarts_it( + home: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Apply closes steam first and restarts it.""" + events = [] + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [] if events else [5]) + + def stop(_logger: object) -> bool: + events.append("stop") + return True + + monkeypatch.setattr(steamctl, "stop", stop) + monkeypatch.setattr(steamctl, "start", lambda logger: events.append("start")) + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert events == ["stop", "start"] + assert _mapped(home, 10) is not None + + +def test_apply_declined_writes_nothing( + home: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Apply declined writes nothing.""" + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [5]) + monkeypatch.setattr("builtins.input", lambda *a: "n") + assert cli.main(["--map-installed", "--apply"]) == 1 + assert _mapped(home, 10) is None + + +def test_a_kept_choice_and_a_write_error_are_reported( + home: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """A kept choice and a write error are reported.""" + config = home / "config" / "config.vdf" + compat_map.map_app(config, 10) + config.write_text( + config.read_text().replace("proton-cachyos-native", "GE-Proton10") + ) + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert "kept: already mapped to GE-Proton10" in capsys.readouterr().out + config.write_text("garbage {") + assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 + + +def test_owned_dlc_in_the_manifest_keeps_valves_default( + home: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """Owned dlc in the manifest keeps valves default.""" + manifest = home / "steamapps" / "appmanifest_10.acf" + manifest.write_text( + vdf.dumps( + "AppState", + { + "appid": "10", + "InstalledDepots": { + "11": {"manifest": "1"}, + "12": {"manifest": "2", "dlcappid": "13"}, + }, + }, + ) + ) + assert cli.main(["--map-installed"]) == 0 + assert "10 Plain: Valve default: owned DLC" in capsys.readouterr().out + + +@pytest.mark.usefixtures("home") +def test_valve_proton_only_goes_with_an_install() -> None: + """Valve proton only goes with an install.""" + with pytest.raises(SystemExit): + cli.main(["--map-installed", "--valve-proton"]) + + +def test_a_failed_write_stops_the_run( + home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + """A failed write stops the run.""" + + def boom(_config: Path, _appid: int) -> Never: + msg = "config.vdf line 3: unexpected layout" + raise compat_map.CompatMapError(msg) + + monkeypatch.setattr(compat_map, "map_app", boom) + (home / "steamapps" / "appmanifest_notanid.acf").write_text("x") + assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 + out = capsys.readouterr().out + assert "Mapping stopped" in out + assert "config.vdf line 3" in out diff --git a/steam_installer/tests/test_proton_native.py b/steam_installer/tests/test_proton_native.py new file mode 100644 index 0000000..87529b1 --- /dev/null +++ b/steam_installer/tests/test_proton_native.py @@ -0,0 +1,142 @@ +"""Fetch, verify and install the native Proton, never running its .INSTALL.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import proton_native, proton_pkg +from steam_installer.tests._proton_fixture import ( + FILES, + VERSION, + Repo, + build_db, + build_package, + gpg, + stop_agent, +) + +if TYPE_CHECKING: + from collections.abc import Callable + from pathlib import Path + + from steam_installer.tests._proton_fixture import Setup + + +def test_a_fresh_install_extracts_only_the_tool_and_stamps_it(setup: Setup) -> None: + """A fresh install extracts only the tool and stamps it.""" + tools, _, run = setup + status = run() + tool = tools / "proton-cachyos-native" + assert status.startswith("installed") + assert "signature" in status + assert "sha256" in status + assert (tool / "VERSION").read_text() == VERSION + "\n" + assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] + assert str((tool / "files/bin/msidb").readlink()) == "wine" + assert not (tools / ".INSTALL").exists() + paths = [str(p.relative_to(tools)) for p in tools.rglob("*")] + assert not any(".INSTALL" in p or "stray" in p for p in paths) + assert sorted(p.name for p in tools.iterdir()) == ["proton-cachyos-native"] + + +def test_a_current_stamp_needs_no_network( + setup: Setup, monkeypatch: pytest.MonkeyPatch +) -> None: + """A current stamp needs no network.""" + _, _, run = setup + run() + monkeypatch.setattr( + proton_native, "_download", lambda url, dest: pytest.fail("no network expected") + ) + assert run() == f"current: {VERSION}" + + +def _assert_fails_untouched(run: Callable[[], str], tools: Path, match: str) -> None: + old = tools / "proton-cachyos-native" + old.mkdir(exist_ok=True) + (old / "keep").write_text("old build") + with pytest.raises(proton_pkg.PkgError, match=match): + run() + assert (old / "keep").read_text() == "old build" + assert sorted(p.name for p in tools.iterdir()) == ["proton-cachyos-native"] + + +def test_a_tampered_signature_fails_closed(setup: Setup, mirror: Path) -> None: + """A tampered signature fails closed.""" + tools, _, run = setup + sig = next(mirror.glob("*.sig")) + data = bytearray(sig.read_bytes()) + data[-5] ^= 0xFF + sig.write_bytes(bytes(data)) + _assert_fails_untouched(run, tools, "signature") + + +def test_a_signature_from_another_key_fails_closed( + setup: Setup, + mirror: Path, + tmp_path: Path, +) -> None: + """A signature from another key fails closed.""" + tools, _, run = setup + other = tmp_path / "other" + other.mkdir(mode=0o700) + gpg( + other, + "--quick-gen-key", + "Mallory ", + "ed25519", + "sign", + "never", + ) + package = next(mirror.glob("*.pkg.tar.zst")) + (mirror / f"{package.name}.sig").unlink() + gpg(other, "--output", f"{package}.sig", "--detach-sign", str(package)) + stop_agent(other) + _assert_fails_untouched(run, tools, "signature") + + +def test_a_package_that_differs_from_the_pinned_sha256_fails_closed( + setup: Setup, mirror: Path, repo: Repo +) -> None: + """A package that differs from the pinned sha256 fails closed.""" + tools, _, run = setup + package = next(mirror.glob("*.pkg.tar.zst")) + build_package(package, {**FILES, "proton": b"evil"}) + gpg( + repo.gpg_home, + "--yes", + "--output", + f"{package}.sig", + "--detach-sign", + str(package), + ) + _assert_fails_untouched(run, tools, "sha256") + + +def test_a_db_sha256_that_disagrees_with_the_lock_fails_closed( + setup: Setup, mirror: Path +) -> None: + """A db sha256 that disagrees with the lock fails closed.""" + tools, _, run = setup + build_db(mirror / "cachyos.db", VERSION, "0" * 64) + _assert_fails_untouched(run, tools, "sha256") + + +def test_a_newer_package_in_the_repo_asks_for_a_lock_bump( + setup: Setup, mirror: Path, repo: Repo +) -> None: + """A newer package in the repo asks for a lock bump.""" + tools, _, run = setup + build_db(mirror / "cachyos.db", "1:11.0.20261101-1", repo.sha256) + _assert_fails_untouched(run, tools, "bump") + + +def test_a_repo_without_the_package_fails( + setup: Setup, mirror: Path, repo: Repo +) -> None: + """A repo without the package fails.""" + tools, _, run = setup + build_db(mirror / "cachyos.db", VERSION, repo.sha256, name="something-else") + _assert_fails_untouched(run, tools, "no proton-cachyos-native") diff --git a/steam_installer/tests/test_proton_native_cli.py b/steam_installer/tests/test_proton_native_cli.py new file mode 100644 index 0000000..9548e60 --- /dev/null +++ b/steam_installer/tests/test_proton_native_cli.py @@ -0,0 +1,134 @@ +"""The proton_native entry point: what install_deps.sh and run.sh call.""" + +from __future__ import annotations + +import io +import sys +from typing import TYPE_CHECKING, Never + +from steam_installer import proton_native, proton_pkg +from steam_installer.proton_native import _download +from steam_installer.tests._run_helpers import run_captured + +if TYPE_CHECKING: + from pathlib import Path + from urllib.request import Request + + import pytest + + +def _boom(*_a: object, **_kw: object) -> Never: + msg = "bad signature" + raise proton_pkg.PkgError(msg) + + +def test_main_exits_zero_on_success_and_one_on_failure( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Main exits zero on success and one on failure.""" + monkeypatch.setattr(proton_native, "ensure", lambda *a, **kw: "current: x") + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + assert proton_native.main([]) == 0 + monkeypatch.setattr(proton_native, "ensure", _boom) + assert proton_native.main([]) == 1 + + +def test_main_reports_what_happened( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path +) -> None: + """Main reports what happened.""" + monkeypatch.setattr(proton_native, "ensure", lambda *a, **kw: "current: x") + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + assert proton_native.main([]) == 0 + assert "current: x" in capsys.readouterr().out + monkeypatch.setattr(proton_native, "ensure", _boom) + assert proton_native.main([]) == 1 + assert "bad signature" in capsys.readouterr().err + + +def test_check_reports_whether_the_stamp_matches_the_lock( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Check reports whether the stamp matches the lock.""" + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + assert proton_native.main(["--check"]) == 1 + tool = tmp_path / "compatibilitytools.d" / "proton-cachyos-native" + tool.mkdir(parents=True) + (tool / "VERSION").write_text(proton_pkg.read_lock().version + "\n") + assert proton_native.main(["--check"]) == 0 + + +def _absent() -> Never: + msg = "No Steam installation found" + raise FileNotFoundError(msg) + + +def test_no_steam_installation_means_nothing_to_install( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """No steam installation means nothing to install.""" + monkeypatch.setattr(proton_native, "find_steam_root", _absent) + assert proton_native.main([]) == 0 + assert proton_native.main(["--check"]) == 0 + + +def test_no_steam_installation_says_it_was_skipped( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + """No steam installation says it was skipped.""" + monkeypatch.setattr(proton_native, "find_steam_root", _absent) + assert proton_native.main([]) == 0 + assert "skipped" in capsys.readouterr().out + + +def test_the_repo_can_be_pointed_at_a_test_mirror( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """The repo can be pointed at a test mirror.""" + seen = {} + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + monkeypatch.setenv("SGI_PROTON_REPO", "file:///mirror/cachyos.db") + monkeypatch.setattr( + proton_native, "ensure", lambda d, lock: seen.setdefault("repo", lock.repo) + ) + assert proton_native.main([]) == 0 + assert seen["repo"] == "file:///mirror/cachyos.db" + + +def test_module_runs_as_a_script( + tmp_path: Path, coverage_subprocess_env: dict[str, str] +) -> None: + """Module runs as a script.""" + env = {**coverage_subprocess_env, "HOME": str(tmp_path)} + result = run_captured( + [sys.executable, "-m", "steam_installer.proton_native", "--check"], env + ) + assert result.returncode == 0 + assert "skipped" in result.stdout + + +def test_downloads_send_a_user_agent( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """The CachyOS mirror answers 403 to urllib's default agent (2026-11-22).""" + seen = {} + + class _Response(io.BytesIO): + """A urlopen response: BytesIO already works as a context manager.""" + + def fake_urlopen(request: Request, **_kw: float) -> _Response: + seen["agent"] = request.get_header("User-agent") + return _Response(b"data") + + monkeypatch.setattr(proton_native.urllib.request, "urlopen", fake_urlopen) + _download("https://m/x", tmp_path / "x") + assert (tmp_path / "x").read_bytes() == b"data" + assert seen["agent"] == "steam-game-installer" + + +def test_a_file_url_is_a_plain_copy(tmp_path: Path) -> None: + """A file:// mirror (SGI_PROTON_REPO) is read straight from disk.""" + source = tmp_path / "a b.db" + source.write_bytes(b"db") + _download(source.as_uri(), tmp_path / "out") + assert (tmp_path / "out").read_bytes() == b"db" diff --git a/steam_installer/tests/test_proton_native_copy.py b/steam_installer/tests/test_proton_native_copy.py new file mode 100644 index 0000000..fe976c1 --- /dev/null +++ b/steam_installer/tests/test_proton_native_copy.py @@ -0,0 +1,100 @@ +"""An existing tool folder: adopted, replaced, deferred or updated.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from steam_installer import proton_native, proton_pkg +from steam_installer.tests._proton_fixture import ( + FILES, + VERSION, +) + +if TYPE_CHECKING: + from collections.abc import Callable + from pathlib import Path + + from steam_installer.tests._proton_fixture import Setup + + +def _existing_copy(tools: Path) -> Path: + tool = tools / "proton-cachyos-native" + for name, data in FILES.items(): + (tool / name).parent.mkdir(parents=True, exist_ok=True) + (tool / name).write_bytes(data) + (tool / "files/bin/msidb").symlink_to("wine") + (tool / "__pycache__").mkdir() + (tool / "__pycache__" / "x.pyc").write_bytes(b"made by running it") + return tool + + +def test_an_identical_copy_is_adopted_without_extracting( + setup: Setup, monkeypatch: pytest.MonkeyPatch +) -> None: + """An identical copy is adopted without extracting.""" + tools, _, run = setup + tool = _existing_copy(tools) + monkeypatch.setattr(proton_pkg, "extract", lambda *a: pytest.fail("extracted")) + status = run() + assert status.startswith( + f"adopted: all {len(FILES) + 1} files match .MTREE (signature" + ) + assert (tool / "VERSION").read_text() == VERSION + "\n" + + +@pytest.mark.parametrize( + ("change", "where"), + [ + (lambda t: (t / "files/bin/wine").write_bytes(b"patched"), "files/bin/wine"), + (lambda t: (t / "user_settings.py").write_text("x"), "user_settings.py"), + (lambda t: (t / "proton").unlink(), "proton"), + ( + lambda t: ( + (t / "files/bin/msidb").unlink() + or (t / "files/bin/msidb").symlink_to("x") + ), + "msidb", + ), + (lambda t: (t / "files/bin/msidb").unlink(), "msidb"), + ], +) +def test_a_differing_copy_is_replaced( + setup: Setup, + change: Callable[[Path], object], + where: str, +) -> None: + """A differing copy is replaced.""" + tools, _, run = setup + tool = _existing_copy(tools) + change(tool) + status = run() + assert status.startswith("replaced: mismatch in") + assert where in status + assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] + assert not (tool / "user_settings.py").exists() + assert sorted(p.name for p in tools.iterdir()) == ["proton-cachyos-native"] + + +def test_replacing_is_deferred_while_steam_or_a_game_runs( + setup: Setup, monkeypatch: pytest.MonkeyPatch +) -> None: + """Replacing is deferred while steam or a game runs.""" + tools, _, run = setup + tool = _existing_copy(tools) + (tool / "proton").write_text("old") + monkeypatch.setattr(proton_native, "in_use", lambda t: [4242]) + status = run() + assert status.startswith("deferred") + assert (tool / "proton").read_text() == "old" + assert not (tool / "VERSION").exists() + + +def test_an_older_stamp_is_updated(setup: Setup) -> None: + """An older stamp is updated.""" + tools, _, run = setup + tool = _existing_copy(tools) + (tool / "VERSION").write_text("1:10.0-1\n") + assert run().startswith("updated: 1:10.0-1 ->") + assert (tool / "VERSION").read_text() == VERSION + "\n" diff --git a/steam_installer/tests/test_proton_native_in_use.py b/steam_installer/tests/test_proton_native_in_use.py new file mode 100644 index 0000000..ce1dbb5 --- /dev/null +++ b/steam_installer/tests/test_proton_native_in_use.py @@ -0,0 +1,35 @@ +"""Which processes keep the native Proton in use.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from steam_installer import proton_native + +if TYPE_CHECKING: + from pathlib import Path + + import pytest + + +def test_in_use_sees_steam_and_processes_running_from_the_tool( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """In use sees steam and processes running from the tool.""" + proc = tmp_path / "proc" + tool = tmp_path / "tool" + (tool / "files/bin").mkdir(parents=True) + (tool / "files/bin/wine").write_text("") + (tmp_path / "other").write_text("") + for pid, target in ( + ("10", tool / "files/bin/wine"), + ("11", tmp_path / "other"), + ("13", tmp_path / "gone"), + ): + (proc / pid).mkdir(parents=True) + (proc / pid / "exe").symlink_to(target) + (proc / "self").mkdir() + (proc / "12").mkdir() + monkeypatch.setattr(proton_native.recognize, "PROC_DIR", proc) + monkeypatch.setattr(proton_native.recognize, "steam_client_pids", lambda: [99]) + assert proton_native.in_use(tool) == [99, 10] diff --git a/_installer_import/tests/test_proton_pkg.py b/steam_installer/tests/test_proton_pkg.py similarity index 57% rename from _installer_import/tests/test_proton_pkg.py rename to steam_installer/tests/test_proton_pkg.py index 68fb12c..736b701 100644 --- a/_installer_import/tests/test_proton_pkg.py +++ b/steam_installer/tests/test_proton_pkg.py @@ -1,69 +1,88 @@ """The package formats and checks behind the native Proton fetcher.""" +from __future__ import annotations + import hashlib import tarfile +from typing import TYPE_CHECKING import pytest -from _proton_fixture import PREFIX, build_repo, stop_agent from steam_installer import proton_pkg +from steam_installer.tests._proton_fixture import PREFIX, Repo, build_repo, stop_agent + +if TYPE_CHECKING: + from collections.abc import Iterator + from pathlib import Path -@pytest.fixture(scope="module") -def repo(tmp_path_factory): +@pytest.fixture(name="repo", scope="module") +def _repo(tmp_path_factory: pytest.TempPathFactory) -> Iterator[Repo]: + """The repo fixture.""" built = build_repo(tmp_path_factory.mktemp("repo")) yield built stop_agent(built.gpg_home) -def test_the_real_lock_pins_the_proven_build(): +def test_the_real_lock_pins_the_proven_build() -> None: + """The real lock pins the proven build.""" lock = proton_pkg.read_lock() assert lock.name == "proton-cachyos-native" assert lock.version == "1:11.0.20261015-1" - assert lock.sha256 == "25bf302f90e8e01118555b997b873c3cd06a33b5647e79d049a59e29794b43e0" + assert ( + lock.sha256 + == "25bf302f90e8e01118555b997b873c3cd06a33b5647e79d049a59e29794b43e0" + ) assert lock.repo == "https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db" -def test_a_lock_missing_a_key_is_refused(tmp_path): +def test_a_lock_missing_a_key_is_refused(tmp_path: Path) -> None: + """A lock missing a key is refused.""" path = tmp_path / "x.lock" path.write_text("# c\nname=a\nrepo=b.db\n") with pytest.raises(proton_pkg.PkgError, match="version"): proton_pkg.read_lock(path) -def test_the_pinned_key_file_holds_the_pinned_fingerprint(tmp_path): +def test_the_pinned_key_file_holds_the_pinned_fingerprint(tmp_path: Path) -> None: + """The pinned key file holds the pinned fingerprint.""" home = tmp_path / "g" home.mkdir(mode=0o700) try: - assert proton_pkg.key_fingerprints(proton_pkg.KEY, home) == [proton_pkg.FINGERPRINT] + assert proton_pkg.key_fingerprints(proton_pkg.KEY, home) == [ + proton_pkg.FINGERPRINT + ] finally: stop_agent(home) -def test_package_urls_quote_the_epoch_colon(): +def test_package_urls_quote_the_epoch_colon() -> None: + """Package urls quote the epoch colon.""" lock = proton_pkg.Lock("p", "https://m/repo/cachyos.db", "1:2-3", "x") assert proton_pkg.package_url(lock, "p-1:2-3-x86_64.pkg.tar.zst") == ( "https://m/repo/p-1%3A2-3-x86_64.pkg.tar.zst" ) -def test_sha256_file_streams(tmp_path): +def test_sha256_file_streams(tmp_path: Path) -> None: + """Sha256 file streams.""" path = tmp_path / "f" path.write_bytes(b"abc" * 100000) assert proton_pkg.sha256_file(path) == hashlib.sha256(b"abc" * 100000).hexdigest() -def test_mtree_unescapes_octal_names(repo): +def test_mtree_unescapes_octal_names(repo: Repo) -> None: + """Mtree unescapes octal names.""" entries = proton_pkg.read_mtree(repo.package) assert entries["user settings.py"].kind == "file" assert entries["files/bin/msidb"].link == "wine" - assert ".INSTALL" not in entries and "" not in entries + assert ".INSTALL" not in entries + assert "" not in entries assert PREFIX not in entries - - -def test_db_entries_other_than_desc_are_ignored(tmp_path): +def test_db_entries_other_than_desc_are_ignored(tmp_path: Path) -> None: + """Db entries other than desc are ignored.""" db = tmp_path / "x.db" with tarfile.open(db, "w:zst") as tar: info = tarfile.TarInfo("p-1/files") @@ -72,7 +91,10 @@ def test_db_entries_other_than_desc_are_ignored(tmp_path): proton_pkg.find_in_db(db, "p") -def test_a_key_file_without_the_pinned_fingerprint_is_refused(repo, tmp_path): +def test_a_key_file_without_the_pinned_fingerprint_is_refused( + repo: Repo, tmp_path: Path +) -> None: + """A key file without the pinned fingerprint is refused.""" home = tmp_path / "g" home.mkdir(mode=0o700) try: @@ -84,15 +106,17 @@ def test_a_key_file_without_the_pinned_fingerprint_is_refused(repo, tmp_path): stop_agent(home) -def test_a_package_without_mtree_is_refused(tmp_path): +def test_a_package_without_mtree_is_refused(tmp_path: Path) -> None: + """A package without mtree is refused.""" package = tmp_path / "p.pkg.tar.zst" with tarfile.open(package, "w:zst") as tar: tar.addfile(tarfile.TarInfo("usr/x")) - with pytest.raises(proton_pkg.PkgError, match="no .MTREE"): + with pytest.raises(proton_pkg.PkgError, match=r"no \.MTREE"): proton_pkg.read_mtree(package) -def test_directories_listed_in_mtree_need_no_content_check(tmp_path): +def test_directories_listed_in_mtree_need_no_content_check(tmp_path: Path) -> None: + """Directories listed in mtree need no content check.""" entries = {"files": proton_pkg.MtreeEntry("dir")} (tmp_path / "files").mkdir() assert proton_pkg.first_mismatch(tmp_path, entries) is None diff --git a/_installer_import/tests/test_recognize.py b/steam_installer/tests/test_recognize.py similarity index 78% rename from _installer_import/tests/test_recognize.py rename to steam_installer/tests/test_recognize.py index c4a85d3..511ad90 100644 --- a/_installer_import/tests/test_recognize.py +++ b/steam_installer/tests/test_recognize.py @@ -1,4 +1,9 @@ +"""Tests for recognize.""" + +from __future__ import annotations + from pathlib import Path +from typing import TYPE_CHECKING from steam_installer import vdf from steam_installer.library import SteamLibrary @@ -10,11 +15,14 @@ from steam_installer.recognize import ( read_app_state, steam_client_pids, ) +from steam_installer.tests._recognize_helpers import _fake_proc -from _recognize_helpers import _fake_proc +if TYPE_CHECKING: + import pytest -def test_discover_cached_depot_manifests_parses_filenames(tmp_path): +def test_discover_cached_depot_manifests_parses_filenames(tmp_path: Path) -> None: + """Discover cached depot manifests parses filenames.""" cache_dir = tmp_path / ".DepotDownloader" cache_dir.mkdir() (cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x") @@ -29,11 +37,15 @@ def test_discover_cached_depot_manifests_parses_filenames(tmp_path): } -def test_discover_cached_depot_manifests_missing_dir_returns_empty(tmp_path): +def test_discover_cached_depot_manifests_missing_dir_returns_empty( + tmp_path: Path, +) -> None: + """Discover cached depot manifests missing dir returns empty.""" assert discover_cached_depot_manifests(tmp_path) == {} -def test_manifest_reflects_real_install_true_when_consistent(tmp_path): +def test_manifest_reflects_real_install_true_when_consistent(tmp_path: Path) -> None: + """Manifest reflects real install true when consistent.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) data = { @@ -50,7 +62,10 @@ def test_manifest_reflects_real_install_true_when_consistent(tmp_path): ) -def test_manifest_reflects_real_install_false_when_installdir_mismatches(tmp_path): +def test_manifest_reflects_real_install_false_when_installdir_mismatches( + tmp_path: Path, +) -> None: + """Manifest reflects real install false when installdir mismatches.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) data = { @@ -67,13 +82,15 @@ def test_manifest_reflects_real_install_false_when_installdir_mismatches(tmp_pat ) -def test_manifest_reflects_real_install_false_when_no_manifest(tmp_path): +def test_manifest_reflects_real_install_false_when_no_manifest(tmp_path: Path) -> None: + """Manifest reflects real install false when no manifest.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) assert not manifest_reflects_real_install(library, 1771300, library.common / "Game") -def test_download_counters_settled_detects_partial_download(): +def test_download_counters_settled_detects_partial_download() -> None: + """Download counters settled detects partial download.""" assert not download_counters_settled( {"BytesToDownload": "92", "BytesDownloaded": "22"} ) @@ -86,7 +103,10 @@ def test_download_counters_settled_detects_partial_download(): assert download_counters_settled({}) -def test_manifest_reflects_real_install_false_when_counters_unsettled(tmp_path): +def test_manifest_reflects_real_install_false_when_counters_unsettled( + tmp_path: Path, +) -> None: + """Manifest reflects real install false when counters unsettled.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) data = { @@ -102,11 +122,12 @@ def test_manifest_reflects_real_install_false_when_counters_unsettled(tmp_path): assert not manifest_reflects_real_install(library, 1771300, library.common / "Game") -def test_read_app_state_missing_file(tmp_path): +def test_read_app_state_missing_file(tmp_path: Path) -> None: + """Read app state missing file.""" assert read_app_state(tmp_path / "nope.acf") == {} -def test_clear_steam_staging_removes_tree_and_patches(tmp_path): +def test_clear_steam_staging_removes_tree_and_patches(tmp_path: Path) -> None: """A StateFlags=4 manifest next to a full staging tree is the same contradiction that made Steam resume a 90 GB download.""" library = SteamLibrary(tmp_path / "Steam") @@ -131,13 +152,16 @@ def test_clear_steam_staging_removes_tree_and_patches(tmp_path): assert (downloading / "state_1628350_1.patch").is_file() -def test_clear_steam_staging_when_nothing_staged(tmp_path): +def test_clear_steam_staging_when_nothing_staged(tmp_path: Path) -> None: + """Clear steam staging when nothing staged.""" library = SteamLibrary(tmp_path / "Steam") (library.steamapps / "downloading").mkdir(parents=True) assert clear_steam_staging(library, 1771300) == 0 -def test_steam_client_pids_finds_native_and_flatpak(tmp_path, monkeypatch): +def test_steam_client_pids_finds_native_and_flatpak( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """Matching on the executable name only: an earlier version also demanded 'ubuntu12_32' in the path, so a Flatpak Steam (/app/bin/steam) silently failed open and the whole guard stopped guarding.""" @@ -157,7 +181,9 @@ def test_steam_client_pids_finds_native_and_flatpak(tmp_path, monkeypatch): assert sorted(steam_client_pids()) == [111, 222] -def test_steam_client_pids_ignores_vanished_processes(tmp_path, monkeypatch): +def test_steam_client_pids_ignores_vanished_processes( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """A pid can exit between listing /proc and resolving its exe.""" proc = _fake_proc(tmp_path, {"111": "steam"}) (proc / "999").mkdir() @@ -171,8 +197,9 @@ def test_steam_client_pids_ignores_vanished_processes(tmp_path, monkeypatch): def test_manifest_reflects_real_install_false_on_malformed_appstate( - tmp_path, monkeypatch -): + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Manifest reflects real install false on malformed appstate.""" library = SteamLibrary(tmp_path / "Steam") library.steamapps.mkdir(parents=True) (library.steamapps / "appmanifest_1771300.acf").write_text( @@ -184,7 +211,9 @@ def test_manifest_reflects_real_install_false_on_malformed_appstate( assert not manifest_reflects_real_install(library, 1771300, library.common / "Game") -def test_steam_client_pids_skips_unreadable_exe(tmp_path, monkeypatch): +def test_steam_client_pids_skips_unreadable_exe( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """/proc//exe raises for processes we don't own; that must not abort the scan and silently disable the guard.""" proc = tmp_path / "proc" @@ -198,9 +227,10 @@ def test_steam_client_pids_skips_unreadable_exe(tmp_path, monkeypatch): original = Path.resolve - def flaky(self, *args, **kwargs): + def flaky(self: Path, *args: object, **kwargs: object) -> Path: if self.parent.name == "111": - raise PermissionError("not owned by this user") + msg = "not owned by this user" + raise PermissionError(msg) return original(self, *args, **kwargs) monkeypatch.setattr(Path, "resolve", flaky) diff --git a/_installer_import/tests/test_resolve.py b/steam_installer/tests/test_resolve.py similarity index 62% rename from _installer_import/tests/test_resolve.py rename to steam_installer/tests/test_resolve.py index 57544d0..f22aca8 100644 --- a/_installer_import/tests/test_resolve.py +++ b/steam_installer/tests/test_resolve.py @@ -5,40 +5,47 @@ scripted clients from this network; these tests pin the fallback order, the retry, and the exact-title rule that stops "Portal" installing Portal 2. """ +from __future__ import annotations + import io import json +from typing import TYPE_CHECKING, Never import urllib.error import pytest -from steam_installer import resolve +from steam_installer import _http, resolve + +if TYPE_CHECKING: + from collections.abc import Callable + from urllib.request import Request COMMUNITY_PAYLOAD = [{"appid": "1771300", "name": "Kingdom Come: Deliverance II"}] STORE_PAYLOAD = {"items": [{"id": 1771300, "name": "Kingdom Come: Deliverance II"}]} class _FakeResponse(io.BytesIO): - def __enter__(self): - return self - - def __exit__(self, *exc): - return False + """A urlopen response: BytesIO already works as a context manager.""" -def _responder(monkeypatch, handler): +def _responder( + monkeypatch: pytest.MonkeyPatch, handler: Callable[[str], io.BytesIO] +) -> list[str]: """Route a fake urlopen by URL so each backend can be driven separately.""" seen = [] - def fake_urlopen(request, timeout): + def fake_urlopen(request: Request, **_kw: float) -> io.BytesIO: seen.append(request.full_url) return handler(request.full_url) - monkeypatch.setattr(resolve.urllib.request, "urlopen", fake_urlopen) + monkeypatch.setattr(_http.urllib.request, "urlopen", fake_urlopen) monkeypatch.setattr(resolve.time, "sleep", lambda _: None) return seen -def test_community_backend_is_tried_first_and_parses_string_appids(monkeypatch): +def test_community_backend_is_tried_first_and_parses_string_appids( + monkeypatch: pytest.MonkeyPatch, +) -> None: """The community endpoint returns appid as a string, unlike the store one.""" seen = _responder( monkeypatch, lambda url: _FakeResponse(json.dumps(COMMUNITY_PAYLOAD).encode()) @@ -53,10 +60,15 @@ def test_community_backend_is_tried_first_and_parses_string_appids(monkeypatch): assert len(seen) == 1 -def test_store_backend_is_used_when_the_community_one_fails(monkeypatch): - def handler(url): +def test_store_backend_is_used_when_the_community_one_fails( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Store backend is used when the community one fails.""" + + def handler(url: str) -> _FakeResponse: if "steamcommunity.com" in url: - raise urllib.error.URLError("dns") + msg = "dns" + raise urllib.error.URLError(msg) return _FakeResponse(json.dumps(STORE_PAYLOAD).encode()) seen = _responder(monkeypatch, handler) @@ -65,15 +77,18 @@ def test_store_backend_is_used_when_the_community_one_fails(monkeypatch): assert "store.steampowered.com" in seen[-1] -def test_a_transient_failure_is_retried_rather_than_given_up_on(monkeypatch): +def test_a_transient_failure_is_retried_rather_than_given_up_on( + monkeypatch: pytest.MonkeyPatch, +) -> None: """DNS here fails intermittently; one blip must not send the user off to look up an AppID by hand.""" attempts = [] - def handler(url): + def handler(url: str) -> _FakeResponse: attempts.append(url) if len(attempts) == 1: - raise urllib.error.URLError("temporary failure in name resolution") + msg = "temporary failure in name resolution" + raise urllib.error.URLError(msg) return _FakeResponse(json.dumps(COMMUNITY_PAYLOAD).encode()) _responder(monkeypatch, handler) @@ -82,7 +97,8 @@ def test_a_transient_failure_is_retried_rather_than_given_up_on(monkeypatch): assert len(attempts) == 2 -def test_search_quotes_the_term(monkeypatch): +def test_search_quotes_the_term(monkeypatch: pytest.MonkeyPatch) -> None: + """Search quotes the term.""" seen = _responder(monkeypatch, lambda url: _FakeResponse(b"[]")) resolve.search("kingdom come/2") @@ -90,48 +106,58 @@ def test_search_quotes_the_term(monkeypatch): assert "kingdom%20come/2" in seen[0] or "kingdom%20come%2F2" in seen[0] -def test_search_sends_an_explicit_user_agent(monkeypatch): +def test_search_sends_an_explicit_user_agent(monkeypatch: pytest.MonkeyPatch) -> None: """urllib's default UA gets refused outright by Steam's edge.""" captured = {} - def fake_urlopen(request, timeout): + def fake_urlopen(request: Request, **_kw: float) -> io.BytesIO: captured["ua"] = request.get_header("User-agent") return _FakeResponse(b"[]") - monkeypatch.setattr(resolve.urllib.request, "urlopen", fake_urlopen) + monkeypatch.setattr(_http.urllib.request, "urlopen", fake_urlopen) resolve.search("anything") assert captured["ua"] == resolve.USER_AGENT -def test_rows_with_a_non_numeric_appid_are_skipped(monkeypatch): +def test_rows_with_a_non_numeric_appid_are_skipped( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Rows with a non numeric appid are skipped.""" payload = [{"appid": "nope", "name": "Broken"}, *COMMUNITY_PAYLOAD] _responder(monkeypatch, lambda url: _FakeResponse(json.dumps(payload).encode())) assert [r.appid for r in resolve.search("x")] == [1771300] -def test_store_missing_items_key_returns_empty(monkeypatch): - def handler(url): +def test_store_missing_items_key_returns_empty(monkeypatch: pytest.MonkeyPatch) -> None: + """Store missing items key returns empty.""" + + def handler(url: str) -> _FakeResponse: if "steamcommunity.com" in url: - raise urllib.error.URLError("dns") + msg = "dns" + raise urllib.error.URLError(msg) return _FakeResponse(b"{}") _responder(monkeypatch, handler) assert resolve.search("nothing") == [] -def test_an_html_error_page_is_a_search_failure_not_a_crash(monkeypatch): +def test_an_html_error_page_is_a_search_failure_not_a_crash( + monkeypatch: pytest.MonkeyPatch, +) -> None: """A captive portal or block page returns HTML with a 200; that must not surface as a JSONDecodeError traceback.""" _responder(monkeypatch, lambda url: _FakeResponse(b"nope")) - with pytest.raises(resolve.SearchUnavailable): + with pytest.raises(resolve.SearchUnavailableError): resolve.search("kingdom come") -def test_a_dict_payload_falls_through_to_the_store_parser(monkeypatch): +def test_a_dict_payload_falls_through_to_the_store_parser( + monkeypatch: pytest.MonkeyPatch, +) -> None: """The community endpoint returns a list. A dict is the store's shape, so it is not a crash -- the store parser handles it and finds no items.""" _responder(monkeypatch, lambda url: _FakeResponse(b'{"not": "a list"}')) @@ -139,29 +165,34 @@ def test_a_dict_payload_falls_through_to_the_store_parser(monkeypatch): assert resolve.search("kingdom come") == [] -def test_a_payload_neither_backend_understands_is_a_search_failure(monkeypatch): +def test_a_payload_neither_backend_understands_is_a_search_failure( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A payload neither backend understands is a search failure.""" _responder(monkeypatch, lambda url: _FakeResponse(b'"just a string"')) - with pytest.raises(resolve.SearchUnavailable): + with pytest.raises(resolve.SearchUnavailableError): resolve.search("kingdom come") @pytest.mark.parametrize( "exc", [urllib.error.URLError("tls blocked"), TimeoutError("timed out")] ) -def test_search_network_failure_becomes_search_unavailable(monkeypatch, exc): +def test_search_network_failure_becomes_search_unavailable( + monkeypatch: pytest.MonkeyPatch, exc: BaseException +) -> None: """Neither endpoint reachable is not a code bug, so it gets its own type.""" - def boom(url): + def boom(url: str) -> Never: raise exc _responder(monkeypatch, boom) - with pytest.raises(resolve.SearchUnavailable, match="steamcommunity"): + with pytest.raises(resolve.SearchUnavailableError, match="steamcommunity"): resolve.search("kingdom come") -def test_exact_match_beats_steams_own_ranking(): +def test_exact_match_beats_steams_own_ranking() -> None: """Steam ranks Portal 2 above Portal; typing the full name must still win.""" results = [ resolve.SearchResult(620, "Portal 2"), @@ -171,44 +202,13 @@ def test_exact_match_beats_steams_own_ranking(): assert resolve.exact_match(results, " PORTAL ").appid == 400 -def test_exact_match_returns_none_without_one(): +def test_exact_match_returns_none_without_one() -> None: + """Exact match returns none without one.""" results = [resolve.SearchResult(620, "Portal 2")] assert resolve.exact_match(results, "Portal") is None -def test_exact_match_refuses_to_pick_between_duplicate_titles(): +def test_exact_match_refuses_to_pick_between_duplicate_titles() -> None: """Two apps with the same title is a genuine ambiguity, not a coin flip.""" results = [resolve.SearchResult(1, "Same"), resolve.SearchResult(2, "Same")] assert resolve.exact_match(results, "Same") is None - - -def _results(): - return [ - resolve.SearchResult(appid=1, name="One"), - resolve.SearchResult(appid=2, name="Two"), - ] - - -def test_prompt_confirm_returns_the_chosen_result(monkeypatch): - monkeypatch.setattr("builtins.input", lambda *a: "2") - assert resolve.prompt_confirm(_results()).appid == 2 - - -def test_prompt_confirm_reprompts_until_valid(monkeypatch, capsys): - answers = iter(["", "banana", "9", "1"]) - monkeypatch.setattr("builtins.input", lambda *a: next(answers)) - - assert resolve.prompt_confirm(_results()).appid == 1 - assert capsys.readouterr().out.count("Invalid choice") == 3 - - -def test_prompt_confirm_zero_cancels(monkeypatch): - monkeypatch.setattr("builtins.input", lambda *a: "0") - with pytest.raises(resolve.ResolveError, match="Cancelled"): - resolve.prompt_confirm(_results()) - - -def test_prompt_confirm_rejects_empty_result_list(): - """Never guess an app: no matches must be an error, not a default pick.""" - with pytest.raises(resolve.ResolveError, match="No matches"): - resolve.prompt_confirm([]) diff --git a/steam_installer/tests/test_resolve_prompt.py b/steam_installer/tests/test_resolve_prompt.py new file mode 100644 index 0000000..b620d5c --- /dev/null +++ b/steam_installer/tests/test_resolve_prompt.py @@ -0,0 +1,60 @@ +"""The numbered pick prompt of a name search.""" + +from __future__ import annotations + +import pytest + +from steam_installer import resolve + + +def _results() -> list[resolve.SearchResult]: + return [ + resolve.SearchResult(appid=1, name="One"), + resolve.SearchResult(appid=2, name="Two"), + ] + + +def test_prompt_confirm_returns_the_chosen_result( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Prompt confirm returns the chosen result.""" + monkeypatch.setattr("builtins.input", lambda *a: "2") + assert resolve.prompt_confirm(_results()).appid == 2 + + +def test_prompt_confirm_reprompts_until_valid( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Prompt confirm reprompts until valid.""" + answers = iter(["", "banana", "9", "1"]) + monkeypatch.setattr("builtins.input", lambda *a: next(answers)) + + assert resolve.prompt_confirm(_results()).appid == 1 + assert next(answers, None) is None + + +def test_prompt_confirm_lists_the_candidates_and_flags_bad_answers( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + """Prompt confirm lists the candidates and flags bad answers.""" + answers = iter(["", "banana", "9", "1"]) + monkeypatch.setattr("builtins.input", lambda *a: next(answers)) + + assert resolve.prompt_confirm(_results()).appid == 1 + out = capsys.readouterr().out + assert "One" in out + assert "Two" in out + assert out.count("Invalid choice") == 3 + + +def test_prompt_confirm_zero_cancels(monkeypatch: pytest.MonkeyPatch) -> None: + """Prompt confirm zero cancels.""" + monkeypatch.setattr("builtins.input", lambda *a: "0") + with pytest.raises(resolve.ResolveError, match="Cancelled"): + resolve.prompt_confirm(_results()) + + +def test_prompt_confirm_rejects_empty_result_list() -> None: + """Never guess an app: no matches must be an error, not a default pick.""" + with pytest.raises(resolve.ResolveError, match="No matches"): + resolve.prompt_confirm([]) diff --git a/_installer_import/tests/test_runlog.py b/steam_installer/tests/test_runlog.py similarity index 68% rename from _installer_import/tests/test_runlog.py rename to steam_installer/tests/test_runlog.py index 61521db..7802e13 100644 --- a/_installer_import/tests/test_runlog.py +++ b/steam_installer/tests/test_runlog.py @@ -1,26 +1,23 @@ +"""Tests for runlog.""" + +from __future__ import annotations + import errno -import logging -import os +from pathlib import Path import subprocess import sys +from typing import Never import pytest from steam_installer import runlog +from steam_installer.runlog import _point_latest_at, _read_pty + +pytestmark = pytest.mark.usefixtures("cli_logs") -@pytest.fixture(autouse=True) -def isolated_log_dir(tmp_path, monkeypatch): - """Never write to the real ~/.local/state during tests.""" - monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state") - yield - logger = logging.getLogger(runlog.LOGGER_NAME) - for handler in list(logger.handlers): - logger.removeHandler(handler) - handler.close() - - -def test_setup_creates_log_file_and_latest_symlink(): +def test_setup_creates_log_file_and_latest_symlink() -> None: + """Setup creates log file and latest symlink.""" log = runlog.setup(1771300) assert log.path.is_file() @@ -30,20 +27,24 @@ def test_setup_creates_log_file_and_latest_symlink(): assert latest.resolve() == log.path.resolve() -def test_setup_without_appid_still_names_the_file(): +def test_setup_without_appid_still_names_the_file() -> None: + """Setup without appid still names the file.""" log = runlog.setup(None) assert "noappid" in log.path.name -def test_setup_twice_does_not_duplicate_handlers(): +def test_setup_twice_does_not_duplicate_handlers() -> None: """A second setup() in one process must not write every line twice.""" runlog.setup(1) log = runlog.setup(2) log.logger.info("once") - assert log.path.read_text().count("once") == 1 + assert log.path.read_text(encoding="utf-8").count("once") == 1 -def test_run_logged_records_command_output_and_exit_code(capfd): +def test_run_logged_records_command_output_and_exit_code( + capfd: pytest.CaptureFixture[str], +) -> None: + """Run logged records command output and exit code.""" log = runlog.setup(1771300) returncode, output = runlog.run_logged( @@ -57,13 +58,13 @@ def test_run_logged_records_command_output_and_exit_code(capfd): # Mirrored to the terminal... assert "to-stdout" in capfd.readouterr().out # ...and recorded in the log, along with the argv and exit code. - contents = log.path.read_text() + contents = log.path.read_text(encoding="utf-8") assert "to-stdout" in contents assert "echo to-stdout" in contents assert "exit=7" in contents -def test_run_logged_child_sees_a_tty(): +def test_run_logged_child_sees_a_tty() -> None: """The whole reason for the pty: steamcmd only prompts interactively for a password when it believes it is attached to a terminal. A plain pipe makes the prompt invisible and the run looks hung. @@ -75,32 +76,34 @@ def test_run_logged_child_sees_a_tty(): assert "True" in output -def test_run_logged_times_out(): +def test_run_logged_times_out() -> None: + """Run logged times out.""" log = runlog.setup(1771300) with pytest.raises(subprocess.TimeoutExpired): runlog.run_logged(["sleep", "10"], log.logger, timeout=0.5) -def test_file_contents_records_manifest_verbatim(tmp_path): +def test_file_contents_records_manifest_verbatim(tmp_path: Path) -> None: + """File contents records manifest verbatim.""" log = runlog.setup(1771300) manifest = tmp_path / "appmanifest_1771300.acf" manifest.write_text('"AppState"\n{\n\t"StateFlags"\t\t"4"\n}\n') log.file_contents("Manifest before run", manifest) - contents = log.path.read_text() + contents = log.path.read_text(encoding="utf-8") assert "Manifest before run" in contents assert '"StateFlags"' in contents -def test_file_contents_survives_missing_file(tmp_path): +def test_file_contents_survives_missing_file(tmp_path: Path) -> None: """A first-ever install has no manifest yet; that must not kill the run.""" log = runlog.setup(1771300) log.file_contents("Manifest before run", tmp_path / "absent.acf") - assert "unreadable" in log.path.read_text() + assert "unreadable" in log.path.read_text(encoding="utf-8") -def test_run_logged_records_output_even_on_timeout(): +def test_run_logged_records_output_even_on_timeout() -> None: """A steamcmd hang at the 3600s timeout is exactly when the log matters most; discarding what the child said before hanging defeats the point.""" log = runlog.setup(1771300) @@ -112,37 +115,37 @@ def test_run_logged_records_output_even_on_timeout(): timeout=1.0, ) - contents = log.path.read_text() + contents = log.path.read_text(encoding="utf-8") assert "--- output" in contents assert marker in contents assert "TIMED OUT" in contents assert marker.encode() in excinfo.value.output -def test_point_latest_at_survives_a_broken_log_dir(monkeypatch, tmp_path): +def test_point_latest_at_survives_a_broken_log_dir() -> None: """A stale or unwritable latest.log must never fail an otherwise fine run.""" log = runlog.setup(1) latest = runlog.LOG_DIR / "latest.log" latest.unlink() latest.mkdir() # a directory where the symlink should go - runlog._point_latest_at(log.path) # must not raise + _point_latest_at(log.path) # must not raise assert latest.is_dir() -def test_run_logged_closes_pty_fds_when_spawn_fails(monkeypatch): +def test_run_logged_closes_pty_fds_when_spawn_fails() -> None: """A leaked master/slave pair per failed spawn would exhaust the fd table.""" log = runlog.setup(1) - before = len(os.listdir("/proc/self/fd")) + before = len(list(Path("/proc/self/fd").iterdir())) with pytest.raises(FileNotFoundError): runlog.run_logged(["definitely-not-a-real-binary-xyz"], log.logger) - assert len(os.listdir("/proc/self/fd")) == before + assert len(list(Path("/proc/self/fd").iterdir())) == before -def test_run_logged_stops_when_select_fails(monkeypatch): +def test_run_logged_stops_when_select_fails(monkeypatch: pytest.MonkeyPatch) -> None: """A closed master fd surfaces as OSError from select; end the relay.""" log = runlog.setup(1) monkeypatch.setattr( @@ -155,24 +158,28 @@ def test_run_logged_stops_when_select_fails(monkeypatch): assert output == "" -def test_run_logged_reraises_unexpected_read_errors(monkeypatch): +def test_run_logged_reraises_unexpected_read_errors( + monkeypatch: pytest.MonkeyPatch, +) -> None: """EIO means "child closed the pty" and ends the loop; anything else is a real failure and must not be swallowed.""" log = runlog.setup(1) # Force select to always report readable so os.read is definitely reached. monkeypatch.setattr(runlog.select, "select", lambda *a, **kw: ([a[0][0]], [], [])) - def bad_read(fd, size): + def bad_read(fd: int, size: int) -> Never: raise OSError(errno.EBADF, "bad file descriptor") monkeypatch.setattr(runlog, "_read_pty", bad_read) - with pytest.raises(OSError) as excinfo: + with pytest.raises(OSError, match="bad file descriptor") as excinfo: runlog.run_logged(["bash", "-c", "echo hi; sleep 0.2"], log.logger) assert excinfo.value.errno == errno.EBADF -def test_run_logged_exits_when_child_dies_without_output(monkeypatch): +def test_run_logged_exits_when_child_dies_without_output( + monkeypatch: pytest.MonkeyPatch, +) -> None: """select never reports readable, so the loop must notice via poll() that the child is gone rather than spinning forever.""" log = runlog.setup(1) @@ -184,13 +191,13 @@ def test_run_logged_exits_when_child_dies_without_output(monkeypatch): assert output == "" -def test_run_logged_handles_empty_read(monkeypatch): +def test_run_logged_handles_empty_read(monkeypatch: pytest.MonkeyPatch) -> None: """A zero-byte read is EOF on the pty.""" log = runlog.setup(1) - real_read = runlog._read_pty + real_read = _read_pty calls = {"n": 0} - def once_then_eof(fd, size): + def once_then_eof(fd: int, size: int) -> bytes: calls["n"] += 1 if calls["n"] == 1: return real_read(fd, size) @@ -202,7 +209,7 @@ def test_run_logged_handles_empty_read(monkeypatch): assert returncode == 0 -def test_run_logged_survives_a_child_that_writes_nothing(): +def test_run_logged_survives_a_child_that_writes_nothing() -> None: """Exercises the poll()-based exit when the pty never becomes readable.""" log = runlog.setup(1) returncode, output = runlog.run_logged(["true"], log.logger) diff --git a/_installer_import/tests/test_steamctl.py b/steam_installer/tests/test_steamctl.py similarity index 62% rename from _installer_import/tests/test_steamctl.py rename to steam_installer/tests/test_steamctl.py index 0a95f14..be3abef 100644 --- a/_installer_import/tests/test_steamctl.py +++ b/steam_installer/tests/test_steamctl.py @@ -5,6 +5,8 @@ and say so, never to escalate to a signal: SIGKILL mid-write is how a manifest ends up truncated. """ +from __future__ import annotations + import logging import pytest @@ -14,7 +16,10 @@ from steam_installer import recognize, steamctl LOGGER = logging.getLogger("test-steamctl") -def test_stop_is_a_no_op_when_steam_is_not_running(monkeypatch): +def test_stop_is_a_no_op_when_steam_is_not_running( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Stop is a no op when steam is not running.""" monkeypatch.setattr(recognize, "steam_client_pids", list) called = [] monkeypatch.setattr(steamctl.subprocess, "run", lambda *a, **k: called.append(a)) @@ -23,7 +28,10 @@ def test_stop_is_a_no_op_when_steam_is_not_running(monkeypatch): assert called == [] -def test_stop_asks_steam_to_shut_down_and_waits_for_it(monkeypatch): +def test_stop_asks_steam_to_shut_down_and_waits_for_it( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Stop asks steam to shut down and waits for it.""" pids = iter([[42], [42], [], []]) monkeypatch.setattr(recognize, "steam_client_pids", lambda: next(pids)) seen = {} @@ -32,21 +40,25 @@ def test_stop_asks_steam_to_shut_down_and_waits_for_it(monkeypatch): ) assert steamctl.stop(LOGGER, sleep=lambda _: None) is True - assert seen["cmd"] == ["steam", "-shutdown"] + assert seen["cmd"] == [steamctl.STEAM, "-shutdown"] -def test_stop_gives_up_rather_than_killing_a_client_that_will_not_exit(monkeypatch): +def test_stop_gives_up_rather_than_killing_a_client_that_will_not_exit( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Stop gives up rather than killing a client that will not exit.""" monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42]) monkeypatch.setattr(steamctl.subprocess, "run", lambda *a, **k: None) - with pytest.raises(steamctl.SteamStillRunning, match="42"): + with pytest.raises(steamctl.SteamStillRunningError, match="42"): steamctl.stop(LOGGER, timeout=1.0, sleep=lambda _: None) -def test_start_launches_steam_detached(monkeypatch): +def test_start_launches_steam_detached(monkeypatch: pytest.MonkeyPatch) -> None: + """Start launches steam detached.""" seen = {} - def fake_popen(cmd, **kwargs): + def fake_popen(cmd: list[str], **kwargs: object) -> None: seen["cmd"] = cmd seen["kwargs"] = kwargs @@ -54,5 +66,5 @@ def test_start_launches_steam_detached(monkeypatch): steamctl.start(LOGGER) - assert seen["cmd"] == ["steam"] + assert seen["cmd"] == [steamctl.STEAM] assert seen["kwargs"]["start_new_session"] is True diff --git a/_installer_import/tests/test_takeover.py b/steam_installer/tests/test_takeover.py similarity index 72% rename from _installer_import/tests/test_takeover.py rename to steam_installer/tests/test_takeover.py index de3777b..7bc5f03 100644 --- a/_installer_import/tests/test_takeover.py +++ b/steam_installer/tests/test_takeover.py @@ -5,25 +5,32 @@ user waited for. A tool whose purpose is speed must reuse them, and must not destroy them before the install it feeds has actually succeeded. """ +from __future__ import annotations + import logging +from typing import TYPE_CHECKING, Never import pytest from steam_installer import takeover from steam_installer.library import SteamLibrary +if TYPE_CHECKING: + from pathlib import Path + LOGGER = logging.getLogger("test-takeover") -@pytest.fixture -def library(tmp_path): +@pytest.fixture(name="library") +def _library(tmp_path: Path) -> SteamLibrary: + """The library fixture.""" lib = SteamLibrary(tmp_path / "Steam") lib.steamapps.mkdir(parents=True) lib.common.mkdir(parents=True) return lib -def _stage(library, appid, files): +def _stage(library: SteamLibrary, appid: int, files: dict[str, bytes]) -> Path: staged = library.steamapps / "downloading" / str(appid) for name, payload in files.items(): path = staged / name @@ -32,16 +39,21 @@ def _stage(library, appid, files): return staged -def test_no_staging_directory_means_nothing_to_adopt(library): +def test_no_staging_directory_means_nothing_to_adopt(library: SteamLibrary) -> None: + """No staging directory means nothing to adopt.""" assert takeover.find_staged(library, 284160, library.common / "Game") is None -def test_an_empty_staging_directory_means_nothing_to_adopt(library): +def test_an_empty_staging_directory_means_nothing_to_adopt( + library: SteamLibrary, +) -> None: + """An empty staging directory means nothing to adopt.""" (library.steamapps / "downloading" / "284160").mkdir(parents=True) assert takeover.find_staged(library, 284160, library.common / "Game") is None -def test_find_staged_measures_the_whole_tree(library): +def test_find_staged_measures_the_whole_tree(library: SteamLibrary) -> None: + """Find staged measures the whole tree.""" _stage(library, 284160, {"Bin64/game.exe": b"x" * 100, "content/a.zip": b"y" * 50}) staged = takeover.find_staged(library, 284160, library.common / "Game") @@ -50,7 +62,9 @@ def test_find_staged_measures_the_whole_tree(library): assert staged.bytes_staged == 150 -def test_adoption_hardlinks_rather_than_moving_or_copying(library): +def test_adoption_hardlinks_rather_than_moving_or_copying( + library: SteamLibrary, +) -> None: """Nothing is destroyed before the install is proven, and a 47 GB tree costs no extra disk.""" source = _stage(library, 284160, {"Bin64/game.exe": b"x" * 100}) @@ -70,7 +84,7 @@ def test_adoption_hardlinks_rather_than_moving_or_copying(library): assert result.failed == 0 -def test_a_patched_file_is_corrected_in_both_trees(library): +def test_a_patched_file_is_corrected_in_both_trees(library: SteamLibrary) -> None: """depotdownloader seeks and writes into an existing file rather than replacing it. Through a shared inode that also repairs Steam's staged copy, so whichever downloader finishes, the other one's view is already right.""" @@ -80,14 +94,17 @@ def test_a_patched_file_is_corrected_in_both_trees(library): takeover.adopt(takeover.find_staged(library, 284160, install_dir), LOGGER) # Stand in for depotdownloader: open the adopted file and patch it in place. - with open(install_dir / "part.pak", "r+b") as handle: + with (install_dir / "part.pak").open("r+b") as handle: handle.seek(0) handle.write(b"new") assert (source / "part.pak").read_bytes() == b"new-content" -def test_adoption_never_overwrites_a_file_already_in_place(library): +def test_adoption_never_overwrites_a_file_already_in_place( + library: SteamLibrary, +) -> None: + """Adoption never overwrites a file already in place.""" _stage(library, 284160, {"game.pak": b"staged"}) install_dir = library.common / "Game" install_dir.mkdir() @@ -101,7 +118,11 @@ def test_adoption_never_overwrites_a_file_already_in_place(library): assert result.linked == 0 -def test_a_link_failure_is_survivable(library, monkeypatch, caplog): +def test_a_link_failure_is_survivable( + library: SteamLibrary, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: """depotdownloader simply fetches whatever could not be adopted, so a failed link is slower, never wrong.""" _stage(library, 284160, {"a.pak": b"x", "b.pak": b"y"}) @@ -109,8 +130,9 @@ def test_a_link_failure_is_survivable(library, monkeypatch, caplog): install_dir.mkdir() staged = takeover.find_staged(library, 284160, install_dir) - def refuse(src, dst): - raise OSError("cross-device link") + def refuse(_src: object, _dst: object) -> Never: + msg = "cross-device link" + raise OSError(msg) monkeypatch.setattr(takeover.os, "link", refuse) @@ -121,7 +143,8 @@ def test_a_link_failure_is_survivable(library, monkeypatch, caplog): assert result.linked == 0 -def test_symlinks_in_the_staging_tree_are_ignored(library): +def test_symlinks_in_the_staging_tree_are_ignored(library: SteamLibrary) -> None: + """Symlinks in the staging tree are ignored.""" staged_dir = _stage(library, 284160, {"real.pak": b"x" * 10}) (staged_dir / "link.pak").symlink_to(staged_dir / "real.pak") install_dir = library.common / "Game" diff --git a/_installer_import/tests/test_target.py b/steam_installer/tests/test_target.py similarity index 62% rename from _installer_import/tests/test_target.py rename to steam_installer/tests/test_target.py index f02495f..3cbeca2 100644 --- a/_installer_import/tests/test_target.py +++ b/steam_installer/tests/test_target.py @@ -4,17 +4,22 @@ Four shapes have to work -- an AppID, a Steam URL, an exact title and a partial title -- and the ambiguous cases have to fail rather than guess. """ +from __future__ import annotations + +from typing import Never + import pytest from steam_installer import appinfo, resolve, target -def _metadata(appid): +def _metadata(appid: int) -> appinfo.AppMetadata: return appinfo.AppMetadata(appid, "BeamNG.drive", "BeamNG.drive") @pytest.fixture -def steam_knows_the_app(monkeypatch): +def steam_knows_the_app(monkeypatch: pytest.MonkeyPatch) -> None: + """The steam knows the app fixture.""" monkeypatch.setattr(appinfo, "get_metadata", _metadata) @@ -34,25 +39,36 @@ def steam_knows_the_app(monkeypatch): ("https://steamcommunity.com/id/someone/", None), ], ) -def test_parse_literal_recognises_appids_and_urls(text, expected): +def test_parse_literal_recognises_appids_and_urls( + text: str, expected: tuple[int, str] | None +) -> None: + """Parse literal recognises appids and urls.""" assert target.parse_literal(text) == expected -def test_an_appid_is_confirmed_through_steamcmd(steam_knows_the_app): +@pytest.mark.usefixtures("steam_knows_the_app") +def test_an_appid_is_confirmed_through_steamcmd() -> None: + """An appid is confirmed through steamcmd.""" resolved = target.resolve_target("284160", None, interactive=False) assert resolved.metadata.appid == 284160 assert resolved.source == "appid" -def test_the_appid_flag_skips_detection_entirely(steam_knows_the_app): +@pytest.mark.usefixtures("steam_knows_the_app") +def test_the_appid_flag_skips_detection_entirely() -> None: + """The appid flag skips detection entirely.""" resolved = target.resolve_target("ignored name", 284160, interactive=False) assert resolved.metadata.appid == 284160 assert resolved.source == "flag" -def test_a_url_resolves_without_searching(monkeypatch, steam_knows_the_app): - def no_search(name): - raise AssertionError("a URL must not trigger a search") +@pytest.mark.usefixtures("steam_knows_the_app") +def test_a_url_resolves_without_searching(monkeypatch: pytest.MonkeyPatch) -> None: + """A url resolves without searching.""" + + def no_search(_name: str) -> Never: + msg = "a URL must not trigger a search" + raise AssertionError(msg) monkeypatch.setattr(resolve, "search", no_search) resolved = target.resolve_target( @@ -61,7 +77,9 @@ def test_a_url_resolves_without_searching(monkeypatch, steam_knows_the_app): assert resolved.source == "url" -def test_a_single_search_hit_is_taken(monkeypatch, steam_knows_the_app): +@pytest.mark.usefixtures("steam_knows_the_app") +def test_a_single_search_hit_is_taken(monkeypatch: pytest.MonkeyPatch) -> None: + """A single search hit is taken.""" monkeypatch.setattr( resolve, "search", lambda name: [resolve.SearchResult(284160, "BeamNG.drive")] ) @@ -70,7 +88,9 @@ def test_a_single_search_hit_is_taken(monkeypatch, steam_knows_the_app): assert resolved.source == "only match" -def test_an_exact_title_wins_over_a_higher_ranked_sequel(monkeypatch): +def test_an_exact_title_wins_over_a_higher_ranked_sequel( + monkeypatch: pytest.MonkeyPatch, +) -> None: """Steam ranks Portal 2 first for "Portal"; typing the full name must not install the sequel.""" monkeypatch.setattr( @@ -91,7 +111,11 @@ def test_an_exact_title_wins_over_a_higher_ranked_sequel(monkeypatch): assert resolved.source == "exact" -def test_an_ambiguous_name_prompts_when_interactive(monkeypatch, steam_knows_the_app): +@pytest.mark.usefixtures("steam_knows_the_app") +def test_an_ambiguous_name_prompts_when_interactive( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """An ambiguous name prompts when interactive.""" results = [resolve.SearchResult(1, "Witcher 3"), resolve.SearchResult(2, "Witcher")] monkeypatch.setattr(resolve, "search", lambda name: results) monkeypatch.setattr(resolve, "prompt_confirm", lambda r: r[1]) @@ -101,10 +125,7 @@ def test_an_ambiguous_name_prompts_when_interactive(monkeypatch, steam_knows_the assert resolved.source == "picked" -def test_an_ambiguous_name_fails_closed_when_not_interactive( - monkeypatch, steam_knows_the_app, capsys -): - """Guessing here is how the wrong 90 GB gets downloaded.""" +def _two_witchers(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( resolve, "search", @@ -114,6 +135,26 @@ def test_an_ambiguous_name_fails_closed_when_not_interactive( ], ) + +@pytest.mark.usefixtures("steam_knows_the_app") +def test_an_ambiguous_name_fails_closed_when_not_interactive( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Guessing here is how the wrong 90 GB gets downloaded.""" + _two_witchers(monkeypatch) + + with pytest.raises(target.TargetError, match="Ambiguous"): + target.resolve_target("witch", None, interactive=False) + + +@pytest.mark.usefixtures("steam_knows_the_app") +def test_an_ambiguous_name_lists_the_candidates( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + """An ambiguous name lists the candidates.""" + _two_witchers(monkeypatch) + with pytest.raises(target.TargetError, match="Ambiguous"): target.resolve_target("witch", None, interactive=False) @@ -122,15 +163,23 @@ def test_an_ambiguous_name_fails_closed_when_not_interactive( assert "Witcher 2" in err -def test_no_matches_is_an_error_not_a_default_pick(monkeypatch): +def test_no_matches_is_an_error_not_a_default_pick( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """No matches is an error not a default pick.""" monkeypatch.setattr(resolve, "search", lambda name: []) with pytest.raises(target.TargetError, match="No Steam app matches"): target.resolve_target("kwyjibo", None, interactive=False) -def test_search_being_unavailable_points_at_the_appid_workaround(monkeypatch): - def boom(name): - raise resolve.SearchUnavailable("tls blocked") +def test_search_being_unavailable_points_at_the_appid_workaround( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Search being unavailable points at the appid workaround.""" + + def boom(_name: str) -> Never: + msg = "tls blocked" + raise resolve.SearchUnavailableError(msg) monkeypatch.setattr(resolve, "search", boom) @@ -138,14 +187,17 @@ def test_search_being_unavailable_points_at_the_appid_workaround(monkeypatch): target.resolve_target("BeamNG", None, interactive=False) -def test_digits_that_are_not_an_appid_fall_through_to_a_name_search(monkeypatch): +def test_digits_that_are_not_an_appid_fall_through_to_a_name_search( + monkeypatch: pytest.MonkeyPatch, +) -> None: """A game literally called "2048" exists, so digits are not unambiguous.""" tried = [] - def metadata(appid): + def metadata(appid: int) -> appinfo.AppMetadata: tried.append(appid) if appid == 2048: - raise appinfo.AppInfoError("no such app") + msg = "no such app" + raise appinfo.AppInfoError(msg) return appinfo.AppMetadata(appid, "2048", "2048") monkeypatch.setattr(appinfo, "get_metadata", metadata) @@ -159,11 +211,14 @@ def test_digits_that_are_not_an_appid_fall_through_to_a_name_search(monkeypatch) assert tried == [2048, 999] -def test_an_unknown_appid_from_a_url_does_not_fall_through(monkeypatch): +def test_an_unknown_appid_from_a_url_does_not_fall_through( + monkeypatch: pytest.MonkeyPatch, +) -> None: """A URL cannot also be a game's name, so there is nothing to fall back to.""" - def boom(appid): - raise appinfo.AppInfoError("no such app") + def boom(_appid: int) -> Never: + msg = "no such app" + raise appinfo.AppInfoError(msg) monkeypatch.setattr(appinfo, "get_metadata", boom) @@ -173,6 +228,7 @@ def test_an_unknown_appid_from_a_url_does_not_fall_through(monkeypatch): ) -def test_no_target_at_all_is_an_error(): +def test_no_target_at_all_is_an_error() -> None: + """No target at all is an error.""" with pytest.raises(target.TargetError, match="No target given"): target.resolve_target(None, None, interactive=False) diff --git a/_installer_import/tests/test_vdf.py b/steam_installer/tests/test_vdf.py similarity index 71% rename from _installer_import/tests/test_vdf.py rename to steam_installer/tests/test_vdf.py index bab3a95..fa06555 100644 --- a/_installer_import/tests/test_vdf.py +++ b/steam_installer/tests/test_vdf.py @@ -1,9 +1,14 @@ +"""Tests for vdf.""" + +from __future__ import annotations + import pytest from steam_installer import vdf -def test_parse_flat_block(): +def test_parse_flat_block() -> None: + """Parse flat block.""" text = """ "AppState" { @@ -20,7 +25,8 @@ def test_parse_flat_block(): } -def test_parse_nested_blocks(): +def test_parse_nested_blocks() -> None: + """Parse nested blocks.""" text = """ "AppState" { @@ -41,7 +47,8 @@ def test_parse_nested_blocks(): ) -def test_parse_handles_comments_and_blank_lines(): +def test_parse_handles_comments_and_blank_lines() -> None: + """Parse handles comments and blank lines.""" text = """ // leading comment "AppState" @@ -54,7 +61,8 @@ def test_parse_handles_comments_and_blank_lines(): assert result == {"AppState": {"appid": "1"}} -def test_roundtrip_dumps_then_parse(): +def test_roundtrip_dumps_then_parse() -> None: + """Roundtrip dumps then parse.""" data = { "appid": "1771300", "InstalledDepots": { @@ -66,11 +74,12 @@ def test_roundtrip_dumps_then_parse(): assert reparsed == {"AppState": data} -def test_empty_input_returns_empty_dict(): +def test_empty_input_returns_empty_dict() -> None: + """Empty input returns empty dict.""" assert vdf.parse("") == {} -def test_dumps_reescapes_backslashes_so_round_trip_is_stable(): +def test_dumps_reescapes_backslashes_so_round_trip_is_stable() -> None: r"""Real regression: appmanifest_228980.acf (Steamworks Common Redistributables) stores Windows paths like `_CommonRedist\\vcredist\\2015\\installscript.vdf`. The tokenizer consumes @@ -93,52 +102,57 @@ def test_dumps_reescapes_backslashes_so_round_trip_is_stable(): ) -def test_dumps_escapes_embedded_quotes(): +def test_dumps_escapes_embedded_quotes() -> None: + """Dumps escapes embedded quotes.""" data = {"name": 'Game "Deluxe" Edition'} assert vdf.parse(vdf.dumps("AppState", data))["AppState"] == data -def test_parse_error_message_includes_position(): +def test_parse_error_message_includes_position() -> None: + """Parse error message includes position.""" err = vdf.VdfParseError("boom", 7) assert str(err) == "boom (at position 7)" -def test_parse_empty_input_returns_empty_dict(): +def test_parse_empty_input_returns_empty_dict() -> None: + """Parse empty input returns empty dict.""" assert vdf.parse("") == {} assert vdf.parse(" \n\t ") == {} -def test_parse_rejects_root_key_without_a_block(): - with pytest.raises(vdf.VdfParseError, match="expected '{' after root key"): +def test_parse_rejects_root_key_without_a_block() -> None: + """Parse rejects root key without a block.""" + with pytest.raises(vdf.VdfParseError, match=r"expected '\{' after root key"): vdf.parse('"AppState"\n') -def test_parse_rejects_truncated_input_after_a_key(): +def test_parse_rejects_truncated_input_after_a_key() -> None: """A crash mid-write used to leave exactly this shape on disk.""" with pytest.raises(vdf.VdfParseError, match="unexpected end of input"): vdf.parse('"AppState"\n{\n\t"installdir"') -def test_parse_tolerates_unquoted_tokens(): +def test_parse_tolerates_unquoted_tokens() -> None: """Rare in Steam's own files, but the tokenizer accepts them.""" assert vdf.parse("AppState\n{\n\tinstalldir\tGame\n}\n") == { "AppState": {"installdir": "Game"} } -def test_parse_ignores_comment_lines(): +def test_parse_ignores_comment_lines() -> None: + """Parse ignores comment lines.""" text = '// leading comment\n"AppState"\n{\n\t"installdir"\t\t"Game"\n}\n' assert vdf.parse(text) == {"AppState": {"installdir": "Game"}} -def test_parse_reads_scalar_value_after_key(): +def test_parse_reads_scalar_value_after_key() -> None: """The non-block branch: a key whose value is a plain token.""" assert vdf.parse('"AppState"\n{\n\t"StateFlags"\t\t"4"\n}\n') == { "AppState": {"StateFlags": "4"} } -def test_parse_tolerates_an_unterminated_block(): +def test_parse_tolerates_an_unterminated_block() -> None: """A truncated manifest (what a crash mid-write leaves behind) parses to whatever was complete, rather than raising -- the caller's own validity checks then reject it.""" diff --git a/_installer_import/steam_installer/vdf.py b/steam_installer/vdf.py similarity index 79% rename from _installer_import/steam_installer/vdf.py rename to steam_installer/vdf.py index 850680b..04fb35c 100644 --- a/_installer_import/steam_installer/vdf.py +++ b/steam_installer/vdf.py @@ -1,23 +1,33 @@ """Minimal reader/writer for Valve's KeyValues (VDF) text format. Steam uses this format for appmanifest_*.acf and libraryfolders.vdf. -(Its binary cousin, appcache/appinfo.vdf, is read by :mod:`appinfo_cache`.) Only the plain-text variant is -supported (no binary VDF) -- that covers every file this project touches. +(Its binary cousin, appcache/appinfo.vdf, is read by :mod:`appinfo_cache`.) +Only the plain-text variant is supported (no binary VDF) -- that covers every +file this project touches. """ from __future__ import annotations from dataclasses import dataclass +from typing import Any -VdfValue = "str | dict[str, VdfValue]" +from steam_installer import errors + +# A string, or a nested block of the same shape. Left as Any (the original +# alias was a bare string, which mypy reads as Any): callers narrow with +# isinstance and tests index into parsed manifests freely. +type VdfValue = Any @dataclass -class VdfParseError(Exception): +class VdfParseError(errors.InstallerError): + """Text is not valid VDF.""" + message: str position: int def __str__(self) -> str: + """The parse error with its position.""" return f"{self.message} (at position {self.position})" @@ -30,21 +40,22 @@ def parse(text: str) -> dict[str, VdfValue]: nonlocal pos result: dict[str, VdfValue] = {} while pos < len(tokens): - token = tokens[pos] - if token == "}": + piece = tokens[pos] + if piece == "}": pos += 1 return result, pos - key = token + key = piece pos += 1 if pos >= len(tokens): - raise VdfParseError(f"unexpected end of input after key {key!r}", pos) + msg = f"unexpected end of input after key {key!r}" + raise VdfParseError(msg, pos) if tokens[pos] == "{": pos += 1 - value, pos = parse_block() + block, pos = parse_block() + result[key] = block else: - value = tokens[pos] + result[key] = tokens[pos] pos += 1 - result[key] = value return result, pos # A VDF file is one root key followed by one root block. @@ -53,7 +64,8 @@ def parse(text: str) -> dict[str, VdfValue]: root_key = tokens[0] pos = 1 if pos >= len(tokens) or tokens[pos] != "{": - raise VdfParseError("expected '{' after root key", pos) + msg = "expected '{' after root key" + raise VdfParseError(msg, pos) pos += 1 root_value, pos = parse_block() return {root_key: root_value}