Skip to content

Commit 39f9cb2

Browse files
committed
fix: use absolute paths for subprocess commands in pathbrowser utils
1 parent 4e19096 commit 39f9cb2

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

tests/test_pathbrowser.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,37 +282,65 @@ def test_open_file_with_default_app_macos(self):
282282
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
283283
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', False):
284284
with patch('tkface.widget.pathbrowser.utils.subprocess.run') as mock_run:
285-
result = utils.open_file_with_default_app("/test/file.txt")
286-
assert result is True
287-
mock_run.assert_called_once_with(["open", "/test/file.txt"], check=False)
285+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
286+
mock_which.return_value = "/usr/bin/open"
287+
result = utils.open_file_with_default_app("/test/file.txt")
288+
assert result is True
289+
mock_run.assert_called_once_with(["/usr/bin/open", "/test/file.txt"], check=False)
288290

289291
def test_open_file_with_default_app_macos_exception(self):
290292
"""Test open_file_with_default_app function on macOS with exception."""
291293
with patch('tkface.widget.pathbrowser.utils.IS_MACOS', True):
292294
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
293295
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', False):
294296
with patch('tkface.widget.pathbrowser.utils.subprocess.run') as mock_run:
295-
mock_run.side_effect = Exception("Command not found")
296-
result = utils.open_file_with_default_app("/test/file.txt")
297-
assert result is False
297+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
298+
mock_which.return_value = "/usr/bin/open"
299+
mock_run.side_effect = Exception("Command not found")
300+
result = utils.open_file_with_default_app("/test/file.txt")
301+
assert result is False
298302

299303
def test_open_file_with_default_app_linux(self):
300304
"""Test open_file_with_default_app function on Linux."""
301305
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', True):
302306
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
303307
with patch('tkface.widget.pathbrowser.utils.IS_MACOS', False):
304308
with patch('tkface.widget.pathbrowser.utils.subprocess.run') as mock_run:
305-
result = utils.open_file_with_default_app("/test/file.txt")
306-
assert result is True
307-
mock_run.assert_called_once_with(["xdg-open", "/test/file.txt"], check=False)
309+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
310+
mock_which.return_value = "/usr/bin/xdg-open"
311+
result = utils.open_file_with_default_app("/test/file.txt")
312+
assert result is True
313+
mock_run.assert_called_once_with(["/usr/bin/xdg-open", "/test/file.txt"], check=False)
308314

309315
def test_open_file_with_default_app_linux_exception(self):
310316
"""Test open_file_with_default_app function on Linux with exception."""
311317
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', True):
312318
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
313319
with patch('tkface.widget.pathbrowser.utils.IS_MACOS', False):
314320
with patch('tkface.widget.pathbrowser.utils.subprocess.run') as mock_run:
315-
mock_run.side_effect = Exception("Command not found")
321+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
322+
mock_which.return_value = "/usr/bin/xdg-open"
323+
mock_run.side_effect = Exception("Command not found")
324+
result = utils.open_file_with_default_app("/test/file.txt")
325+
assert result is False
326+
327+
def test_open_file_with_default_app_macos_command_not_found(self):
328+
"""Test open_file_with_default_app function on macOS when command not found."""
329+
with patch('tkface.widget.pathbrowser.utils.IS_MACOS', True):
330+
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
331+
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', False):
332+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
333+
mock_which.return_value = None
334+
result = utils.open_file_with_default_app("/test/file.txt")
335+
assert result is False
336+
337+
def test_open_file_with_default_app_linux_command_not_found(self):
338+
"""Test open_file_with_default_app function on Linux when command not found."""
339+
with patch('tkface.widget.pathbrowser.utils.IS_LINUX', True):
340+
with patch('tkface.widget.pathbrowser.utils.IS_WINDOWS', False):
341+
with patch('tkface.widget.pathbrowser.utils.IS_MACOS', False):
342+
with patch('tkface.widget.pathbrowser.utils.shutil.which') as mock_which:
343+
mock_which.return_value = None
316344
result = utils.open_file_with_default_app("/test/file.txt")
317345
assert result is False
318346

tkface/widget/pathbrowser/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import fnmatch
99
import os
10+
import shutil
1011
import subprocess
1112
import sys
1213
from pathlib import Path
@@ -56,9 +57,17 @@ def open_file_with_default_app(file_path: str) -> bool:
5657
if IS_WINDOWS:
5758
os.startfile(file_path) # pylint: disable=no-member
5859
elif IS_MACOS:
59-
subprocess.run(["open", file_path], check=False)
60+
open_cmd = shutil.which("open")
61+
if open_cmd:
62+
subprocess.run([open_cmd, file_path], check=False)
63+
else:
64+
return False
6065
else:
61-
subprocess.run(["xdg-open", file_path], check=False)
66+
xdg_open_cmd = shutil.which("xdg-open")
67+
if xdg_open_cmd:
68+
subprocess.run([xdg_open_cmd, file_path], check=False)
69+
else:
70+
return False
6271
return True
6372
except Exception:
6473
return False

0 commit comments

Comments
 (0)