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 fileTake it
The walk stopped after 200 commits: lines older than d29528c point at it.
➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 1why, one step back1import io⬆ Bump prek from 0.3.6 to 0.3.8 (#1648)dependabot[bot]before adoptiond29528c
line 2why, one step back2import subprocess🐛 Respect wait=False when launching URLs with xdg-open (#1820)Patrick Arminiobefore adoption8e75bc3
line 3why, one step back3from unittest.mock import Mock, patch⬆ Bump prek from 0.3.6 to 0.3.8 (#1648)dependabot[bot]before adoptiond29528c
lines 4–7why, one step back45import pytest6import typer7➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 8why, one step back8from tests.utils import needs_linux, needs_macos, needs_windows🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 9why, one step back9⬆ Bump prek from 0.3.6 to 0.3.8 (#1648)dependabot[bot]before adoptiond29528c
lines 10–52why, one step back10url = "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 back535455def 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()⬆ Bump prek from 0.3.6 to 0.3.8 (#1648)dependabot[bot]before adoptiond29528c
lines 88–89why, one step back8889➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 90–92why, one step back90@pytest.fixture91def allow_dev_null(monkeypatch):92 real_open = open⬆ Bump prek from 0.3.6 to 0.3.8 (#1648)dependabot[bot]before adoptiond29528c
line 93why, one step back93➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 94–97why, one step back94 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🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 98why, one step back98➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 99why, one step back99 monkeypatch.setattr("builtins.open", fake_open)🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 100why, one step back100➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 101–120why, one step back101102@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🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 121why, one step back121@needs_windows➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 122–172why, one step back122def 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🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
lines 173–174why, one step back173def 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 back175 patch("typer._click._termui_impl.sys.platform", "win32"),176 patch("typer._click._termui_impl.WIN", True),177 patch("typer._click._termui_impl.CYGWIN", False),🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
lines 178–183why, one step back178 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"])