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

1import io2import subprocess3from unittest.mock import Mock, 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)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 = open9394    def fake_open(path, *args, **kwargs):95        if path == "/dev/null":96            return io.StringIO()97        return real_open(path, *args, **kwargs)  # pragma: no cover9899    monkeypatch.setattr("builtins.open", fake_open)100101102@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"119120121@needs_windows122def 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_windows173def test_launch_file():174    with (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),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"])