Files
cosmoteer-pressure-relief-r…/tests/test_steam_paths.py
T

127 lines
4.2 KiB
Python

#!/usr/bin/env python3
# ABOUT ME
# Tests steam_paths.find_data_dir, which locates Cosmoteer's Data folder.
#
# Why this exists: the game can be installed to any Steam library on any disk,
# so the Data folder cannot be guessed from a fixed list of paths. Discovery
# reads Steam's own libraryfolders.vdf instead. These tests build fake Steam
# trees and check that each realistic layout is found.
#
# Usage: python3 tests/test_steam_paths.py
import os
import shutil
import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import steam_paths
_failures = []
def check(name, expected, actual):
if expected == actual:
print(f"PASS: {name}")
else:
print(f"FAIL: {name}")
print(f" expected: {expected}")
print(f" actual: {actual}")
_failures.append(name)
def make_steam_home(home, libraries):
"""Write a libraryfolders.vdf under `home` listing the given library roots."""
vdf = os.path.join(home, ".local/share/Steam/steamapps/libraryfolders.vdf")
os.makedirs(os.path.dirname(vdf), exist_ok=True)
with open(vdf, "w", encoding="utf-8") as handle:
handle.write('"libraryfolders"\n{\n')
for index, library in enumerate(libraries):
handle.write(f'\t"{index}"\n\t{{\n\t\t"path"\t\t"{library}"\n\t}}\n')
handle.write("}\n")
def make_data_dir(library):
"""Create the Cosmoteer Data folder inside a library root."""
data = os.path.join(library, "steamapps/common/Cosmoteer/Data")
os.makedirs(data, exist_ok=True)
return data
def main():
tmp_root = tempfile.mkdtemp()
try:
# 1. The game lives in a secondary library on another disk.
case = os.path.join(tmp_root, "case1")
home = os.path.join(case, "home")
library = os.path.join(case, "disk2/steamapps")
make_steam_home(home, [os.path.join(home, ".local/share/Steam"), library])
expected = make_data_dir(library)
check(
"finds Data in a secondary library from libraryfolders.vdf",
expected,
steam_paths.find_data_dir(home=home, environ={}),
)
# 2. The game lives in the default library inside the Steam client dir.
case = os.path.join(tmp_root, "case2")
home = os.path.join(case, "home")
steam = os.path.join(home, ".local/share/Steam")
make_steam_home(home, [steam])
expected = make_data_dir(steam)
check(
"finds Data in the default Steam library",
expected,
steam_paths.find_data_dir(home=home, environ={}),
)
# 3. Library paths may contain spaces.
case = os.path.join(tmp_root, "case3")
home = os.path.join(case, "home")
library = os.path.join(case, "my games/steamapps")
make_steam_home(home, [os.path.join(home, ".local/share/Steam"), library])
expected = make_data_dir(library)
check(
"finds Data under a library path containing spaces",
expected,
steam_paths.find_data_dir(home=home, environ={}),
)
# 4. Nothing installed: discovery reports None instead of guessing.
case = os.path.join(tmp_root, "case4")
home = os.path.join(case, "home")
os.makedirs(home, exist_ok=True)
check(
"returns None when there is no install",
None,
steam_paths.find_data_dir(home=home, environ={}),
)
# 5. COSMOTEER_DATA overrides discovery.
case = os.path.join(tmp_root, "case5")
home = os.path.join(case, "home")
steam = os.path.join(home, ".local/share/Steam")
make_steam_home(home, [steam])
make_data_dir(steam)
override = os.path.join(case, "elsewhere/Data")
os.makedirs(override, exist_ok=True)
check(
"COSMOTEER_DATA overrides discovery",
override,
steam_paths.find_data_dir(home=home, environ={"COSMOTEER_DATA": override}),
)
finally:
shutil.rmtree(tmp_root, ignore_errors=True)
print()
if _failures:
print(f"{len(_failures)} test(s) failed.")
return 1
print("All Steam path discovery tests passed.")
return 0
if __name__ == "__main__":
sys.exit(main())