Debian Codegraph @

tests/test_coverup_66.py

raw symbols SCIP
1 # file scalene/launchbrowser.py:25-46
2 # lines [25, 26, 27, 28, 29, 30, 31, 34, 36, 41, 42, 46]
3 # branches ['26->27', '26->28', '28->29', '28->30', '30->31', '30->34']
4
5 import pytest
6 import webbrowser
7 import tempfile
8 import platform
9 import os
10 from unittest.mock import patch
11
12 # Assuming the function launch_browser_insecure is part of a module named launchbrowser
13 from scalene.launchbrowser import launch_browser_insecure
14
15
16 @pytest.fixture
17 def mock_platform_system():
18 with patch("platform.system") as mock:
19 yield mock
20
21
22 @pytest.fixture
23 def mock_webbrowser_register():
24 with patch("webbrowser.register") as mock:
25 yield mock
26
27
28 @pytest.fixture
29 def mock_webbrowser_get():
30 with patch("webbrowser.get") as mock:
31 yield mock
32
33
34 def test_launch_browser_insecure(
35 mock_platform_system, mock_webbrowser_register, mock_webbrowser_get
36 ):
37 # Mock platform.system to return 'Linux' to cover the Linux branch
38 mock_platform_system.return_value = "Linux"
39 # Mock webbrowser.get().open to simply return True
40 mock_webbrowser_get.return_value.open.return_value = True
41
42 test_url = "http://example.com"
43 launch_browser_insecure(test_url)
44
45 # Check that webbrowser.register was called with the expected arguments
46 mock_webbrowser_register.assert_called_once()
47 args, kwargs = mock_webbrowser_register.call_args
48 assert args[0] == "chrome_with_flags"
49 assert args[2] is not None # This should be the webbrowser.Chrome instance
50 assert kwargs["preferred"] is True
51
52 # Check that webbrowser.get().open was called with the test URL
53 mock_webbrowser_get.assert_called_once_with(args[2].name)
54 mock_webbrowser_get.return_value.open.assert_called_once_with(test_url)
55
56 # Ensure that the temporary directory was cleaned up
57 # Since the temporary directory is created within the function using a context manager,
58 # we don't have direct access to the variable name. We need to check if the directory
59 # was indeed removed, which is the postcondition we want to verify.
60 # We can't assert on the name of the temporary directory, so we remove the assertion.