Skip to content
Myr

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 file

Take it

1import io2import subprocess3from unittest.mock import patch45import pytest6import typer78from tests.utils import needs_linux, needs_macos, needs_windows910url = "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)535455@pytest.fixture56def allow_dev_null(monkeypatch):57    real_open = open5859    def fake_open(path, *args, **kwargs):60        if path == "/dev/null":61            return io.StringIO()62        return real_open(path, *args, **kwargs)  # pragma: no cover6364    monkeypatch.setattr("builtins.open", fake_open)656667@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"848586@needs_windows87def 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_windows138def test_launch_file():139    with (140        patch("typer._click._termui_impl.sys.platform", "win32"),141        patch("typer._click._termui_impl.WIN", True),142        patch("typer._click._termui_impl.CYGWIN", False),143        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"])