test_launch.py
This file
Path
tests/test_launch.py
148 lines · 3.9 KiB · Python
Last changed by
➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
History of this fileTake it
The walk stopped after 200 commits: lines older than 0880579 point at it.
➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 1why, one step back1import io📝 Update release notesgithub-actions[bot]before adoption0880579
lines 2–7why, one step back2import subprocess3from unittest.mock import patch45import 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📝 Update release notesgithub-actions[bot]before adoption0880579
lines 10–54why, 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)5354➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 55–57why, one step back55@pytest.fixture56def allow_dev_null(monkeypatch):57 real_open = open📝 Update release notesgithub-actions[bot]before adoption0880579
line 58why, one step back58➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 59–62why, one step back59 def fake_open(path, *args, **kwargs):60 if path == "/dev/null":61 return io.StringIO()62 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 63why, one step back63➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 64why, one step back64 monkeypatch.setattr("builtins.open", fake_open)🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 65why, one step back65➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 66–85why, one step back6667@needs_macos68def test_open_url_macos(monkeypatch, allow_dev_null):69 recorded: list[list[str]] = []7071 class Proc:72 def wait(self) -> int:73 return 427475 def fake_popen(args, **kwargs):76 recorded.append(list(args))77 return Proc()7879 monkeypatch.setattr(subprocess, "Popen", fake_popen)8081 assert typer.launch("/path/to/file", wait=True, locate=True) == 4282 assert recorded[0][:3] == ["open", "-W", "-R"]83 assert recorded[0][-1] == "/path/to/file"8485🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
line 86why, one step back86@needs_windows➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 87–137why, one step back87def test_launch_files_windows(monkeypatch):88 calls: list[list[str]] = []8990 def fake_call(args):91 calls.append(list(args))92 return 09394 monkeypatch.setattr(subprocess, "call", fake_call)9596 assert typer.launch("C:/Tools/readme.txt", wait=True, locate=False) == 097 assert typer.launch("file:///C:/tmp/a.txt", wait=False, locate=True) == 098 assert calls.pop(0) == ["start", "/WAIT", "", "C:/Tools/readme.txt"]99 assert calls.pop(0) == ["explorer", "/select,/C:/tmp/a.txt"]100101 monkeypatch.setattr(subprocess, "call", lambda a: (_ for _ in ()).throw(OSError()))102 assert typer.launch("D:/no/such/file.txt", wait=False, locate=False) == 127103104105@needs_linux106def test_open_url_linux_wait(monkeypatch):107 class Proc:108 def __init__(self, code: int = 0) -> None:109 self._code = code110111 def wait(self) -> int:112 return self._code113114 monkeypatch.setattr(subprocess, "Popen", lambda *a, **k: Proc(7))115116 assert typer.launch("/file", wait=True, locate=False) == 7117118119@needs_linux120def test_open_url_linux_locate(monkeypatch):121 recorded: list[list[str]] = []122123 class Proc:124 def wait(self) -> int:125 return 0 # pragma: no cover126127 def fake_popen(args, **kwargs):128 recorded.append(list(args))129 return Proc()130131 monkeypatch.setattr(subprocess, "Popen", fake_popen)132133 assert typer.launch("/tmp/sub/file.txt", wait=False, locate=True) == 0134 assert recorded[-1] == ["xdg-open", "/tmp/sub"]135136137@needs_windows🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
lines 138–139why, one step back138def test_launch_file():139 with (➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 140–142why, one step back140 patch("typer._click._termui_impl.sys.platform", "win32"),141 patch("typer._click._termui_impl.WIN", True),142 patch("typer._click._termui_impl.CYGWIN", False),🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
lines 143–148why, one step back143 patch("subprocess.call", return_value=0) as call_mock,144 ):145 result = typer.launch("C:/tmp/file.txt", locate=True)146147 assert result == 0148 call_mock.assert_called_once_with(["explorer", "/select,C:/tmp/file.txt"])