test_launch.py
This file
Path
tests/test_launch.py
72 lines · 1.9 KiB · Python
Last changed by
🐛 Ensure that `typer.launch` forwards correctly when launching a file (#1708)Sofie Van Landeghembefore adoption31b468b
History of this fileTake it
1import subprocess2from unittest.mock import patch34import pytest5import typer67from tests.utils import needs_windows89url = "http://example.com"101112@pytest.mark.parametrize(13 "system, command",14 [15 ("Darwin", "open"),16 ("Linux", "xdg-open"),17 ("FreeBSD", "xdg-open"),18 ],19)20def test_launch_url_unix(system: str, command: str):21 with (22 patch("platform.system", return_value=system),23 patch("shutil.which", return_value=True),24 patch("subprocess.Popen") as mock_popen,25 ):26 typer.launch(url)2728 mock_popen.assert_called_once_with(29 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT30 )313233def test_launch_url_windows():34 with (35 patch("platform.system", return_value="Windows"),36 patch("webbrowser.open") as mock_webbrowser_open,37 ):38 typer.launch(url)3940 mock_webbrowser_open.assert_called_once_with(url)414243def test_launch_url_no_xdg_open():44 with (45 patch("platform.system", return_value="Linux"),46 patch("shutil.which", return_value=None),47 patch("webbrowser.open") as mock_webbrowser_open,48 ):49 typer.launch(url)5051 mock_webbrowser_open.assert_called_once_with(url)525354def test_calls_original_launch_when_not_passing_urls():55 with patch("typer.main.click.launch", return_value=0) as launch_mock:56 typer.launch("not a url")5758 launch_mock.assert_called_once_with("not a url", wait=False, locate=False)596061@needs_windows62def test_launch_file():63 with (64 patch("click._termui_impl.sys.platform", "win32"),65 patch("click._termui_impl.WIN", True),66 patch("click._termui_impl.CYGWIN", False),67 patch("subprocess.call", return_value=0) as call_mock,68 ):69 result = typer.launch("C:/tmp/file.txt", locate=True)7071 assert result == 072 call_mock.assert_called_once_with(["explorer", "/select,C:/tmp/file.txt"])