Coverage for tests/test_launch.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import subprocess 1abcdefghi

2from unittest.mock import patch 1abcdefghi

3 

4import pytest 1abcdefghi

5import typer 1abcdefghi

6 

7url = "http://example.com" 1abcdefghi

8 

9 

10@pytest.mark.parametrize( 1abcdefghi

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): 1abcdefghi

19 with patch("platform.system", return_value=system), patch( 1abcdefghi

20 "shutil.which", return_value=True 

21 ), patch("subprocess.Popen") as mock_popen: 

22 typer.launch(url) 1abcdefghi

23 

24 mock_popen.assert_called_once_with( 1abcdefghi

25 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT 

26 ) 

27 

28 

29def test_launch_url_windows(): 1abcdefghi

30 with patch("platform.system", return_value="Windows"), patch( 1abcdefghi

31 "webbrowser.open" 

32 ) as mock_webbrowser_open: 

33 typer.launch(url) 1abcdefghi

34 

35 mock_webbrowser_open.assert_called_once_with(url) 1abcdefghi

36 

37 

38def test_launch_url_no_xdg_open(): 1abcdefghi

39 with patch("platform.system", return_value="Linux"), patch( 1abcdefghi

40 "shutil.which", return_value=None 

41 ), patch("webbrowser.open") as mock_webbrowser_open: 

42 typer.launch(url) 1abcdefghi

43 

44 mock_webbrowser_open.assert_called_once_with(url) 1abcdefghi

45 

46 

47def test_calls_original_launch_when_not_passing_urls(): 1abcdefghi

48 with patch("typer.main.click.launch", return_value=0) as launch_mock: 1abcdefghi

49 typer.launch("not a url") 1abcdefghi

50 

51 launch_mock.assert_called_once_with("not a url") 1abcdefghi