steam-backlog-enforcer/steam_installer/tests/test_vdf.py
Krzysztof kuhy Rudnicki a76e726e36 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w
2026-11-22 20:25:39 +01:00

162 lines
4.6 KiB
Python

"""Tests for vdf."""
from __future__ import annotations
import pytest
from steam_installer import vdf
def test_parse_flat_block() -> None:
"""Parse flat block."""
text = """
"AppState"
{
"appid" "1771300"
"name" "Kingdom Come: Deliverance II"
}
"""
result = vdf.parse(text)
assert result == {
"AppState": {
"appid": "1771300",
"name": "Kingdom Come: Deliverance II",
}
}
def test_parse_nested_blocks() -> None:
"""Parse nested blocks."""
text = """
"AppState"
{
"InstalledDepots"
{
"1771302"
{
"manifest" "257127247679847518"
"size" "199419496"
}
}
}
"""
result = vdf.parse(text)
assert (
result["AppState"]["InstalledDepots"]["1771302"]["manifest"]
== "257127247679847518"
)
def test_parse_handles_comments_and_blank_lines() -> None:
"""Parse handles comments and blank lines."""
text = """
// leading comment
"AppState"
{
"appid" "1" // trailing comment
}
"""
result = vdf.parse(text)
assert result == {"AppState": {"appid": "1"}}
def test_roundtrip_dumps_then_parse() -> None:
"""Roundtrip dumps then parse."""
data = {
"appid": "1771300",
"InstalledDepots": {
"1771302": {"manifest": "257127247679847518", "size": "199419496"},
},
}
text = vdf.dumps("AppState", data)
reparsed = vdf.parse(text)
assert reparsed == {"AppState": data}
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() -> None:
r"""Real regression: appmanifest_228980.acf (Steamworks Common
Redistributables) stores Windows paths like
`_CommonRedist\\vcredist\\2015\\installscript.vdf`. The tokenizer consumes
the escapes, so writing values back unescaped halved the backslashes on
every parse/dump cycle and silently corrupted the InstallScripts paths."""
original = (
'"AppState"\n{\n\t"InstallScripts"\n\t{\n'
'\t\t"228990"\t\t"_CommonRedist\\\\vcredist\\\\2015\\\\installscript.vdf"\n'
"\t}\n}\n"
)
once = vdf.dumps("AppState", vdf.parse(original)["AppState"])
assert once.strip() == original.strip()
# And it must be a fixed point, not merely correct on the first pass.
twice = vdf.dumps("AppState", vdf.parse(once)["AppState"])
assert twice == once
assert (
vdf.parse(twice)["AppState"]["InstallScripts"]["228990"]
== "_CommonRedist\\vcredist\\2015\\installscript.vdf"
)
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() -> 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() -> None:
"""Parse empty input returns empty dict."""
assert vdf.parse("") == {}
assert vdf.parse(" \n\t ") == {}
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() -> 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() -> 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() -> 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() -> 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() -> 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."""
assert vdf.parse('"AppState"\n{\n\t"installdir"\t\t"Game"\n') == {
"AppState": {"installdir": "Game"}
}