Skip to content

Commit 8e75bc3

Browse files
authored
🐛 Respect wait=False when launching URLs with xdg-open (#1820)
1 parent d2e002a commit 8e75bc3

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

tests/test_launch.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import io
22
import subprocess
3-
from unittest.mock import patch
3+
from unittest.mock import Mock, patch
44

55
import pytest
66
import typer
@@ -52,6 +52,41 @@ def test_launch_url_no_xdg_open():
5252
mock_webbrowser_open.assert_called_once_with(url)
5353

5454

55+
def test_launch_url_linux_xdg_open_does_not_wait_by_default():
56+
proc = Mock()
57+
58+
with (
59+
patch("platform.system", return_value="Linux"),
60+
patch("shutil.which", return_value=True),
61+
patch("subprocess.Popen", return_value=proc) as mock_popen,
62+
):
63+
result = typer.launch(url)
64+
65+
assert result == 0
66+
mock_popen.assert_called_once_with(
67+
["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
68+
)
69+
proc.wait.assert_not_called()
70+
71+
72+
def test_launch_url_linux_xdg_open_waits_when_requested():
73+
proc = Mock()
74+
proc.wait.return_value = 42
75+
76+
with (
77+
patch("platform.system", return_value="Linux"),
78+
patch("shutil.which", return_value=True),
79+
patch("subprocess.Popen", return_value=proc) as mock_popen,
80+
):
81+
result = typer.launch(url, wait=True)
82+
83+
assert result == 42
84+
mock_popen.assert_called_once_with(
85+
["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
86+
)
87+
proc.wait.assert_called_once_with()
88+
89+
5590
@pytest.fixture
5691
def allow_dev_null(monkeypatch):
5792
real_open = open

typer/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,6 @@ def launch(
19431943
Doc(
19441944
"""
19451945
Wait for the program to exit before returning. This only works if the launched program blocks.
1946-
In particular, `xdg-open` on Linux does not block.
19471946
"""
19481947
),
19491948
] = False,
@@ -1995,9 +1994,12 @@ def launch(
19951994
has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None
19961995

19971996
if has_xdg_open:
1998-
return subprocess.Popen(
1997+
process = subprocess.Popen(
19991998
["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
2000-
).wait()
1999+
)
2000+
if wait:
2001+
return process.wait()
2002+
return 0
20012003

20022004
import webbrowser
20032005

0 commit comments

Comments
 (0)