From 8aca23cf766f107bc2dbd4902acd2cf2a9b45f9f Mon Sep 17 00:00:00 2001 From: aclist <92275929+aclist@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:41:05 +0900 Subject: [PATCH] chore: cherry pick changes from test-typehints branch --- dzgui/config/xdg.py | 9 ++++----- dzgui/init/prereqs.py | 4 ++-- dzgui/init/proc.py | 33 +++++++++++++++++++++++++++------ dzgui/strings/wizard.py | 2 +- tests/test_home_in_sandbox.py | 23 +++++++++++++++++++++++ 5 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 tests/test_home_in_sandbox.py diff --git a/dzgui/config/xdg.py b/dzgui/config/xdg.py index eac3dcf4..c23f4529 100644 --- a/dzgui/config/xdg.py +++ b/dzgui/config/xdg.py @@ -6,10 +6,8 @@ from dataclasses import dataclass from dzgui.const.constants import ( APP_NAME_LOWER, DEBUG_LOG, - FLATPAK_ROOT_PATH, SYSTEM_LOG, ) -from dzgui.init.proc import is_sandboxed @dataclass(slots=True, frozen=True) @@ -53,6 +51,10 @@ def is_writeable(path_str: str) -> bool: def get_xdg_paths() -> dict[str, Path]: home = Path.home() + if resolved := os.environ.get("SNAP_REAL_HOME"): # Snap + home = Path(resolved) + elif resolved := os.environ.get("HOME"): # Flatpak + home = Path(resolved) xdg_paths = { "XDG_CONFIG_HOME": home / ".config", "XDG_STATE_HOME": home / ".local" / "state", @@ -62,9 +64,6 @@ def get_xdg_paths() -> dict[str, Path]: resolved_paths = {} for path in xdg_paths: real_path = os.environ.get(path) - if real_path is not None and is_sandboxed(): - search_string = FLATPAK_ROOT_PATH + "/" - real_path = real_path.replace(search_string, "") if real_path is not None and is_writeable(real_path): new_path = Path(real_path) else: diff --git a/dzgui/init/prereqs.py b/dzgui/init/prereqs.py index 9266bfbe..91278048 100644 --- a/dzgui/init/prereqs.py +++ b/dzgui/init/prereqs.py @@ -3,13 +3,13 @@ import shutil import subprocess from dzgui.const.constants import APP_NAME, FLATPAK_APPID, FLATPAK_CMD, STEAM_CMD -from dzgui.init.proc import is_sandboxed +from dzgui.init.proc import is_flatpak_sandbox logger = logging.getLogger(APP_NAME) def has_flatpak_client() -> bool: - if is_sandboxed(): + if is_flatpak_sandbox(): return True try: res = subprocess.run( diff --git a/dzgui/init/proc.py b/dzgui/init/proc.py index 0c1a2a06..dbf3bf55 100644 --- a/dzgui/init/proc.py +++ b/dzgui/init/proc.py @@ -27,19 +27,40 @@ def is_steam_running(cmd: str) -> bool: raise TypeError("Not a valid Steam client selection") -def is_sandboxed() -> bool: +def is_snap_sandbox() -> bool: + return bool(os.environ.get("SNAP")) + + +def is_flatpak_sandbox() -> bool: return bool(os.environ.get("FLATPAK_ID")) +def is_sandboxed() -> bool: + if is_snap_sandbox() or is_flatpak_sandbox(): + return True + return False + + +def get_flatpak_output() -> list[str] | None: + try: + proc = subprocess.check_output( + [FLATPAK_CMD, "ps", "--columns=application"], text=True + ) + lines = proc.splitlines() + return lines + except Exception as e: + logger.debug(e) + return None + + def is_flatpak_steam_running() -> bool: - if is_sandboxed(): + if is_flatpak_sandbox(): return True if has_cmd(FLATPAK_CMD) is False: return False - proc = subprocess.check_output( - [FLATPAK_CMD, "ps", "--columns=application"], text=True - ) - lines = proc.splitlines() + lines = get_flatpak_output() + if lines is None: + return False if FLATPAK_APPID in lines: return True return False diff --git a/dzgui/strings/wizard.py b/dzgui/strings/wizard.py index 8bf12f73..7537d654 100644 --- a/dzgui/strings/wizard.py +++ b/dzgui/strings/wizard.py @@ -3,7 +3,7 @@ title_intro = "Welcome" blurb_intro = """ This wizard will help you set up some common config options before launching the application. """ -error_sandbox = "Setup cannot be run from inside Flatpak sandbox.\nPlease launch the application directly." +error_sandbox = "Setup cannot be run from inside a sandboxed version of Steam (Snap/Flatpak).\nPlease launch the application directly first." welcome = "Welcome to {0}!" ### SteamPathPage diff --git a/tests/test_home_in_sandbox.py b/tests/test_home_in_sandbox.py new file mode 100644 index 00000000..4404b62a --- /dev/null +++ b/tests/test_home_in_sandbox.py @@ -0,0 +1,23 @@ +import pytest +import tempfile + +from pathlib import Path +from typing import TYPE_CHECKING + +from dzgui.config.xdg import get_xdg_paths + +if TYPE_CHECKING: + from _pytest.monkeypatch import MonkeyPatch + + +@pytest.mark.parametrize("env", ["SNAP_REAL_HOME", "HOME"]) +def test_sandbox_home_resolution(monkeypatch: "MonkeyPatch", env: str) -> None: + with tempfile.NamedTemporaryFile(delete=False) as f: + monkeypatch.setenv(env, f.name) + paths = get_xdg_paths() + assert paths["XDG_CONFIG_HOME"] == Path(f.name).joinpath(".config/dzgui") + + +def test_home_fallback() -> None: + paths = get_xdg_paths() + assert paths["XDG_CONFIG_HOME"] == Path.home().joinpath(".config/dzgui")