Coverage for tests / test_launch.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
1import subprocess 1adebfgch
2from unittest.mock import patch 1adebfgch
4import pytest 1adebfgch
5import typer 1adebfgch
7url = "http://example.com" 1adebfgch
10@pytest.mark.parametrize( 1adebfgch
11 "system, command",
12 [
13 ("Darwin", "open"),
14 ("Linux", "xdg-open"),
15 ("FreeBSD", "xdg-open"),
16 ],
17)
18def test_launch_url_unix(system: str, command: str): 1adebfgch
19 with ( 1abc
20 patch("platform.system", return_value=system),
21 patch("shutil.which", return_value=True),
22 patch("subprocess.Popen") as mock_popen,
23 ):
24 typer.launch(url) 1adebfgch
26 mock_popen.assert_called_once_with( 1adebfgch
27 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
28 )
31def test_launch_url_windows(): 1adebfgch
32 with ( 1abc
33 patch("platform.system", return_value="Windows"),
34 patch("webbrowser.open") as mock_webbrowser_open,
35 ):
36 typer.launch(url) 1adebfgch
38 mock_webbrowser_open.assert_called_once_with(url) 1adebfgch
41def test_launch_url_no_xdg_open(): 1adebfgch
42 with ( 1abc
43 patch("platform.system", return_value="Linux"),
44 patch("shutil.which", return_value=None),
45 patch("webbrowser.open") as mock_webbrowser_open,
46 ):
47 typer.launch(url) 1adebfgch
49 mock_webbrowser_open.assert_called_once_with(url) 1adebfgch
52def test_calls_original_launch_when_not_passing_urls(): 1adebfgch
53 with patch("typer.main.click.launch", return_value=0) as launch_mock: 1adebfgch
54 typer.launch("not a url") 1adebfgch
56 launch_mock.assert_called_once_with("not a url") 1adebfgch