Debian Codegraph @

tests/test_coverup_20.py

raw symbols SCIP
1 # file scalene/launchbrowser.py:101-145
2 # lines [101, 104, 106, 107, 108, 109, 110, 114, 116, 117, 118, 119, 120, 121, 125, 126, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 141, 142, 143, 144, 145]
3 # branches []
4
5 import os
6 import pytest
7 import tempfile
8 from scalene.launchbrowser import generate_html
9 from unittest.mock import patch
10
11
12 @pytest.fixture
13 def mock_environment():
14 with patch("scalene.launchbrowser.Environment") as mock_env:
15 mock_env.return_value.get_template.return_value.render.return_value = (
16 "rendered content"
17 )
18 yield mock_env
19
20
21 @pytest.fixture
22 def mock_read_file_content():
23 with patch("scalene.launchbrowser.read_file_content") as mock_read:
24 mock_read.return_value = "file content"
25 yield mock_read
26
27
28 def test_generate_html_file_not_found(mock_environment, mock_read_file_content):
29 with tempfile.TemporaryDirectory() as tmpdirname:
30 profile_fname = os.path.join(tmpdirname, "nonexistent_profile.prof")
31 output_fname = os.path.join(tmpdirname, "output.html")
32 with pytest.raises(AssertionError):
33 generate_html(profile_fname, output_fname)
34 assert not os.path.exists(output_fname)
35
36
37 def test_generate_html_success(mock_environment, mock_read_file_content):
38 with tempfile.TemporaryDirectory() as tmpdirname:
39 profile_fname = os.path.join(tmpdirname, "profile.prof")
40 output_fname = os.path.join(tmpdirname, "output.html")
41 # Create a dummy profile file
42 with open(profile_fname, "w") as f:
43 f.write("{}")
44 generate_html(profile_fname, output_fname)
45 assert os.path.exists(output_fname)
46 with open(output_fname, "r") as f:
47 content = f.read()
48 assert content == "rendered content"
49
50
51 def test_generate_html_os_error(mock_environment, mock_read_file_content):
52 with tempfile.TemporaryDirectory() as tmpdirname:
53 profile_fname = os.path.join(tmpdirname, "profile.prof")
54 output_fname = os.path.join(tmpdirname, "output.html")
55 # Create a dummy profile file
56 with open(profile_fname, "w") as f:
57 f.write("{}")
58 with patch("builtins.open", side_effect=OSError):
59 generate_html(profile_fname, output_fname)
60 assert not os.path.exists(output_fname)