Coverage for tests / test_launch.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
1import subprocess 1acdefbg
2from unittest.mock import patch 1acdefbg
4import pytest 1acdefbg
5import typer 1acdefbg
7url = "http://example.com" 1acdefbg
10@pytest.mark.parametrize( 1acdefbg
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): 1acdefbg
19 with ( 1ab
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) 1acdefbg
26 mock_popen.assert_called_once_with( 1acdefbg
27 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
28 )
31def test_launch_url_windows(): 1acdefbg
32 with ( 1ab
33 patch("platform.system", return_value="Windows"),
34 patch("webbrowser.open") as mock_webbrowser_open,
35 ):
36 typer.launch(url) 1acdefbg
38 mock_webbrowser_open.assert_called_once_with(url) 1acdefbg
41def test_launch_url_no_xdg_open(): 1acdefbg
42 with ( 1ab
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) 1acdefbg
49 mock_webbrowser_open.assert_called_once_with(url) 1acdefbg
52def test_calls_original_launch_when_not_passing_urls(): 1acdefbg
53 with patch("typer.main.click.launch", return_value=0) as launch_mock: 1acdefbg
54 typer.launch("not a url") 1acdefbg
56 launch_mock.assert_called_once_with("not a url") 1acdefbg