diff options
| -rw-r--r-- | src/amd_debug/s2idle.py | 20 | ||||
| -rw-r--r-- | src/test_s2idle.py | 12 |
2 files changed, 20 insertions, 12 deletions
diff --git a/src/amd_debug/s2idle.py b/src/amd_debug/s2idle.py index 36d6759..8a3f0f2 100644 --- a/src/amd_debug/s2idle.py +++ b/src/amd_debug/s2idle.py @@ -10,6 +10,7 @@ from datetime import date, timedelta, datetime from amd_debug.common import ( convert_string_to_bool, colorize_choices, + fatal_error, is_root, relaunch_sudo, show_log_info, @@ -60,15 +61,16 @@ def display_report_file(fname, fmt) -> None: return user = os.environ.get("SUDO_USER") if user: - # ensure that xdg tools will know how to display the file - # (user may need to call tool with sudo -E) - if os.environ.get("XDG_SESSION_TYPE"): - subprocess.call(["sudo", "-E", "-u", user, "xdg-open", fname]) - else: - print( - "To display report automatically in browser launch tool " - f"with '-E' argument (Example: sudo -E {sys.argv[0]})" - ) + cmd = [ + "systemd-run", + "--user", + f"--machine={user}@.host", + "xdg-open", + os.path.abspath(fname), + ] + ret = subprocess.call(cmd) + if ret: + fatal_error(f"Failed to open report: {ret}") def get_report_file(report_file, extension) -> str: diff --git a/src/test_s2idle.py b/src/test_s2idle.py index 995a714..0c806fc 100644 --- a/src/test_s2idle.py +++ b/src/test_s2idle.py @@ -611,12 +611,18 @@ class TestDisplayReportFile(unittest.TestCase): self, mock_subprocess_call, mock_env_get, mock_is_root ): """Test display_report_file when format is html, user is root, and SUDO_USER is set""" - display_report_file("report.html", "html") + with self.assertRaises(SystemExit): + display_report_file("/tmp/report.html", "html") mock_is_root.assert_called_once() mock_env_get.assert_any_call("SUDO_USER") - mock_env_get.assert_any_call("XDG_SESSION_TYPE") mock_subprocess_call.assert_called_once_with( - ["sudo", "-E", "-u", "testuser", "xdg-open", "report.html"] + [ + "systemd-run", + "--user", + "--machine=testuser@.host", + "xdg-open", + "/tmp/report.html", + ] ) @patch("amd_debug.s2idle.is_root", return_value=True) |
