steam-backlog-enforcer/steam_backlog_enforcer/tests/test_vdf_text.py
Krzysztof kuhy Rudnicki 01d750f36d Remove unneeded Steam tools after an uninstall
Steamworks Common Redistributables, Steam Linux Runtime 4.0 and Proton
Experimental stay in PROTECTED_APP_IDS, so no guard ever calls them an
unauthorized game. uninstall_other_games now ends with a step that
uninstalls them, through uninstall_game, only when all of these hold:

- Steam is not running (pgrep steam.sh, as _ensure_steam_running);
- config.vdf parses and has a CompatToolMapping block;
- every installed non-tool appmanifest maps to proton-cachyos-native;
- that tool's toolmanifest.vdf exists.

Anything else keeps all three and logs "kept Steam tools: <reason>";
a running Steam logs "Steam running, deferring tool removal". No game
installed counts as not needed and says so.

- _tool_need.py decides. Every error keeps the tools.
- _vdf_text.py is a stdlib KeyValues parser. The repo had none, and the
  root daemon's system python has no vdf package.
- _steam_client.steam_client_running() returns None when pgrep cannot
  tell. _ensure_steam_running behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 14:53:11 +01:00

62 lines
1.9 KiB
Python

"""Tests for the stdlib KeyValues (``.vdf`` / ``.acf``) parser."""
from __future__ import annotations
import pytest
from steam_backlog_enforcer._vdf_text import lookup, parse_vdf
TREE = {"Outer": {"Inner": {"leaf": "v"}, "flat": "s"}}
class TestParse:
"""parse_vdf builds the whole tree or raises."""
def test_nested_blocks(self) -> None:
"""Blocks nest and empty strings survive."""
text = '"A"\n{\n\t"b"\t\t"1"\n\t"C"\n\t{\n\t\t"d"\t""\n\t}\n}\n'
assert parse_vdf(text) == {"A": {"b": "1", "C": {"d": ""}}}
def test_escapes_comments_conditionals_and_bare_tokens(self) -> None:
"""Escapes, comments, conditionals and bare words parse."""
text = (
"// header comment\n"
'"root" {\n'
' "path" "C:\\\\Games\\"x\\"\\n\\t" [$WIN32]\n'
" bare word // trailing\n"
"}\n"
)
assert parse_vdf(text) == {"root": {"path": 'C:\\Games"x"\n\t', "bare": "word"}}
@pytest.mark.parametrize(
"text",
[
'"a" "unterminated',
'"a" "1" }',
'"a" { "b" "1"',
'{ "b" "1" }',
'"a" { "b" }',
'"dangling"',
],
)
def test_malformed_raises(self, text: str) -> None:
"""Every malformed shape raises instead of returning part of a tree."""
with pytest.raises(ValueError, match="malformed KeyValues"):
parse_vdf(text)
class TestLookup:
"""lookup walks keys case-insensitively."""
def test_case_insensitive_path(self) -> None:
"""Keys match regardless of case."""
assert lookup(TREE, "outer", "INNER", "Leaf") == "v"
def test_missing_key(self) -> None:
"""A missing key returns None."""
assert lookup(TREE, "outer", "nope") is None
def test_through_a_string(self) -> None:
"""A path through a plain string returns None."""
assert lookup(TREE, "outer", "flat", "deeper") is None