From f748b09ba776695536e017a17707a062b97b1124 Mon Sep 17 00:00:00 2001 From: Sylvain Girard Date: Thu, 8 Oct 2026 15:58:51 +0200 Subject: [PATCH] fix: detect running Flatpak Steam correctly Request only application IDs from flatpak ps so running Steam instances match correctly. Fix client selection validation and cover detection with regression tests. Refs aclist/dztui#478. --- dzgui/init/proc.py | 6 ++-- tests/test_process_detection.py | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/test_process_detection.py diff --git a/dzgui/init/proc.py b/dzgui/init/proc.py index faca556..ca9a243 100644 --- a/dzgui/init/proc.py +++ b/dzgui/init/proc.py @@ -20,7 +20,7 @@ def is_steam_running(cmd: str) -> bool: if has_cmd(STEAM_CMD) is False: return False return is_running(STEAM_CMD) - elif cmd == FLATPAK_RUN_CMD or FLATPAK_SANDBOX: + elif cmd in (FLATPAK_RUN_CMD, FLATPAK_SANDBOX): return is_flatpak_steam_running() else: raise TypeError("Not a valid Steam client selection") @@ -31,7 +31,9 @@ def is_steam_running(cmd: str) -> bool: def is_flatpak_steam_running() -> bool: if has_cmd(FLATPAK_CMD) is False: return False - proc = subprocess.check_output([FLATPAK_CMD, "ps"], text=True) + proc = subprocess.check_output( + [FLATPAK_CMD, "ps", "--columns=application"], text=True + ) lines = proc.splitlines() if FLATPAK_APPID in lines: return True diff --git a/tests/test_process_detection.py b/tests/test_process_detection.py new file mode 100644 index 0000000..4643458 --- /dev/null +++ b/tests/test_process_detection.py @@ -0,0 +1,57 @@ +import unittest +from unittest.mock import patch + +from dzgui.const.constants import ( + FLATPAK_APPID, + FLATPAK_RUN_CMD, + FLATPAK_SANDBOX, + STEAM_CMD, +) +from dzgui.init import proc + + +class TestSteamDetection(unittest.TestCase): + def test_flatpak_detection_with_multiple_running_apps(self): + def flatpak_output(args, *, text): + # Default `flatpak ps` includes instance, PID, app and runtime columns. + if args == ["flatpak", "ps", "--columns=application"]: + return f"com.spotify.Client\n{FLATPAK_APPID}\n{FLATPAK_APPID}\n" + return f"3904921611\t1257689\t{FLATPAK_APPID}\torg.freedesktop.Platform\n" + + with ( + patch.object(proc, "has_cmd", return_value=True), + patch.object(proc.subprocess, "check_output", side_effect=flatpak_output), + ): + for client in (FLATPAK_RUN_CMD, FLATPAK_SANDBOX): + with self.subTest(client=client): + self.assertTrue(proc.is_steam_running(client)) + + def test_flatpak_not_running(self): + with patch.object(proc, "has_cmd", return_value=True): + for output in ("", "com.spotify.Client\n", f"{FLATPAK_APPID}Helper\n"): + with ( + self.subTest(output=output), + patch.object(proc.subprocess, "check_output", return_value=output), + ): + self.assertFalse(proc.is_flatpak_steam_running()) + + def test_flatpak_not_installed(self): + with ( + patch.object(proc, "has_cmd", return_value=False), + patch.object(proc.subprocess, "check_output") as check_output, + ): + self.assertFalse(proc.is_flatpak_steam_running()) + check_output.assert_not_called() + + def test_native_steam(self): + with patch.object(proc, "has_cmd", return_value=True): + for running in (True, False): + with ( + self.subTest(running=running), + patch.object(proc, "is_running", return_value=running), + ): + self.assertEqual(proc.is_steam_running(STEAM_CMD), running) + + def test_invalid_client(self): + with self.assertRaises(TypeError): + proc.is_steam_running("invalid-client")