Skip to content
Myr

test_launch.py

This file

Path

tests/test_launch.py

183 lines · 4.9 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 3780a29 point at it.

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

line 1why, one step back
1import io

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

line 2why, one step back
2import subprocess

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

line 3why, one step back
3from unittest.mock import Mock, patch

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 4–7why, one step back
4
5import pytest6import typer7

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

line 8why, one step back
8from tests.utils import needs_linux, needs_macos, needs_windows

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 9–52why, one step back
9
10url = "http://example.com"111213@pytest.mark.parametrize(14 "system, command",15 [16 ("Darwin", "open"),17 ("Linux", "xdg-open"),18 ("FreeBSD", "xdg-open"),19 ],20)21def test_launch_url_unix(system: str, command: str):22 with (23 patch("platform.system", return_value=system),24 patch("shutil.which", return_value=True),25 patch("subprocess.Popen") as mock_popen,26 ):27 typer.launch(url)2829 mock_popen.assert_called_once_with(30 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT31 )323334def test_launch_url_windows():35 with (36 patch("platform.system", return_value="Windows"),37 patch("webbrowser.open") as mock_webbrowser_open,38 ):39 typer.launch(url)4041 mock_webbrowser_open.assert_called_once_with(url)424344def test_launch_url_no_xdg_open():45 with (46 patch("platform.system", return_value="Linux"),47 patch("shutil.which", return_value=None),48 patch("webbrowser.open") as mock_webbrowser_open,49 ):50 typer.launch(url)5152 mock_webbrowser_open.assert_called_once_with(url)

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

lines 53–87why, one step back
53
5455def 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()

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 88–89why, one step back
88
89

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

lines 90–92why, one step back
90@pytest.fixture
91def allow_dev_null(monkeypatch):92 real_open = open

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

line 93why, one step back
93

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

lines 94–97why, one step back
94 def fake_open(path, *args, **kwargs):
95 if path == "/dev/null":96 return io.StringIO()97 return real_open(path, *args, **kwargs) # pragma: no cover

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

line 98why, one step back
98

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

line 99why, one step back
99 monkeypatch.setattr("builtins.open", fake_open)

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

line 100why, one step back
100

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

lines 101–120why, one step back
101
102@needs_macos103def test_open_url_macos(monkeypatch, allow_dev_null):104 recorded: list[list[str]] = []105106 class Proc:107 def wait(self) -> int:108 return 42109110 def fake_popen(args, **kwargs):111 recorded.append(list(args))112 return Proc()113114 monkeypatch.setattr(subprocess, "Popen", fake_popen)115116 assert typer.launch("/path/to/file", wait=True, locate=True) == 42117 assert recorded[0][:3] == ["open", "-W", "-R"]118 assert recorded[0][-1] == "/path/to/file"119120

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

line 121why, one step back
121@needs_windows

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

lines 122–172why, one step back
122def test_launch_files_windows(monkeypatch):
123 calls: list[list[str]] = []124125 def fake_call(args):126 calls.append(list(args))127 return 0128129 monkeypatch.setattr(subprocess, "call", fake_call)130131 assert typer.launch("C:/Tools/readme.txt", wait=True, locate=False) == 0132 assert typer.launch("file:///C:/tmp/a.txt", wait=False, locate=True) == 0133 assert calls.pop(0) == ["start", "/WAIT", "", "C:/Tools/readme.txt"]134 assert calls.pop(0) == ["explorer", "/select,/C:/tmp/a.txt"]135136 monkeypatch.setattr(subprocess, "call", lambda a: (_ for _ in ()).throw(OSError()))137 assert typer.launch("D:/no/such/file.txt", wait=False, locate=False) == 127138139140@needs_linux141def test_open_url_linux_wait(monkeypatch):142 class Proc:143 def __init__(self, code: int = 0) -> None:144 self._code = code145146 def wait(self) -> int:147 return self._code148149 monkeypatch.setattr(subprocess, "Popen", lambda *a, **k: Proc(7))150151 assert typer.launch("/file", wait=True, locate=False) == 7152153154@needs_linux155def test_open_url_linux_locate(monkeypatch):156 recorded: list[list[str]] = []157158 class Proc:159 def wait(self) -> int:160 return 0 # pragma: no cover161162 def fake_popen(args, **kwargs):163 recorded.append(list(args))164 return Proc()165166 monkeypatch.setattr(subprocess, "Popen", fake_popen)167168 assert typer.launch("/tmp/sub/file.txt", wait=False, locate=True) == 0169 assert recorded[-1] == ["xdg-open", "/tmp/sub"]170171172@needs_windows

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 173–174why, one step back
173def test_launch_file():
174 with (

➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73

lines 175–177why, one step back
175 patch("typer._click._termui_impl.sys.platform", "win32"),
176 patch("typer._click._termui_impl.WIN", True),177 patch("typer._click._termui_impl.CYGWIN", False),

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 178–183why, one step back
178 patch("subprocess.call", return_value=0) as call_mock,
179 ):180 result = typer.launch("C:/tmp/file.txt", locate=True)181182 assert result == 0183 call_mock.assert_called_once_with(["explorer", "/select,C:/tmp/file.txt"])