Skip to content
Myr

🐛 Respect wait=False when launching URLs with xdg-open (#1820)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Patrick Arminio · committed by GitHub · parent d2e002a · Browse files · 2 files+41 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+41 added−4 removed

  • tests:+36 added−1 removed
  • other:+5 added−3 removed

2 files+41 added−4 removed

modified:tests/test_launch.py+36 added-1 removed
1import io2import subprocess3from unittest.mock import patch3from unittest.mock import Mock, patch45import pytest6import typer7
43 unmodified lines
5152 mock_webbrowser_open.assert_called_once_with(url)535455def test_launch_url_linux_xdg_open_does_not_wait_by_default():56 proc = Mock()5758 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)6465 assert result == 066 mock_popen.assert_called_once_with(67 ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT68 )69 proc.wait.assert_not_called()707172def test_launch_url_linux_xdg_open_waits_when_requested():73 proc = Mock()74 proc.wait.return_value = 427576 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)8283 assert result == 4284 mock_popen.assert_called_once_with(85 ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT86 )87 proc.wait.assert_called_once_with()888990@pytest.fixture91def allow_dev_null(monkeypatch):92 real_open = open93
90 unmodified lines
modified:typer/main.py+5 added-3 removed
1941 unmodified lines
1942 bool,1943 Doc(1944 """1945 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.1946 """1947 ),1948 ] = False,1949 locate: Annotated[
43 unmodified lines
19931994 has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None19951996 if has_xdg_open:1998 return subprocess.Popen(1997 process = subprocess.Popen(1998 ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT2000 ).wait()1999 )2000 if wait:2001 return process.wait()2002 return 020032004 import webbrowser20052006 webbrowser.open(url)
5 unmodified lines