6
6
"""
7
7
8
8
import os
9
+ from pathlib import Path
9
10
from typing import Any
10
11
from unittest .mock import MagicMock , call , patch
11
12
20
21
21
22
from ..st import JINJA_CONTEXT_DICT
22
23
23
- ROOT_PATH = os .path .dirname (os .path .abspath (__file__ ))
24
-
25
24
26
25
class MockRecord (Record ):
27
26
"""A mock class for the record."""
@@ -43,41 +42,52 @@ def get_dict(self) -> dict:
43
42
return self .mock_data
44
43
45
44
46
- def test_no_html_template_found () -> None :
45
+ @pytest .fixture (scope = "module" , name = "custom_jinja_env" )
46
+ def fixture_custom_jinja_env () -> Environment :
47
+ """Return the custom Jinja2 environment for testing.
48
+
49
+ Returns
50
+ -------
51
+ Environment
52
+ The custom Jinja2 environment.
53
+ """
54
+ root_path = Path (__file__ ).parent .joinpath ("resources" )
55
+ return Environment (
56
+ loader = FileSystemLoader (root_path ),
57
+ autoescape = select_autoescape (enabled_extensions = ["html" , "j2" ]),
58
+ trim_blocks = True ,
59
+ lstrip_blocks = True ,
60
+ )
61
+
62
+
63
+ def test_no_html_template_found (custom_jinja_env : Environment ) -> None :
47
64
"""Test initializing a HTMLReporter instance with a non-existing template."""
48
- no_template_reporter = HTMLReporter (target_template = "not_exist_template.html" )
65
+ no_template_reporter = HTMLReporter (env = custom_jinja_env , target_template = "not_exist_template.html" )
49
66
assert not no_template_reporter .template
50
67
51
68
52
- @given (mock_data = JINJA_CONTEXT_DICT , num_dep = st .integers (min_value = 0 , max_value = 3 ))
53
- def test_gen_json_reports (mock_data : Any , num_dep : int ) -> None :
54
- """Test if JSONReporter can print JSON files without errors."""
55
- report = Report (MockRecord (mock_data ))
56
- for _ in range (num_dep ):
57
- report .root_record .dependencies .append (MockRecord (mock_data ))
69
+ def test_syntax_err_template (custom_jinja_env : Environment ) -> None :
70
+ """Test generating the HTML report from a template with syntax errors."""
71
+ syntax_err = HTMLReporter (env = custom_jinja_env , target_template = "syntax_err.html" )
72
+ assert not syntax_err .template
58
73
59
- reporter = JSONReporter ()
60
74
75
+ def test_runtime_err_template (custom_jinja_env : Environment ) -> None :
76
+ """Test generating the HTML report from a template with runtime errors."""
77
+ report = Report (MockRecord ({}))
78
+ runtime_err = HTMLReporter (env = custom_jinja_env , target_template = "runtime_err.html" )
61
79
with patch ("builtins.open" ) as mock_open :
62
- reporter .generate ("report_paths" , report )
63
- calls = [call (os .path .join ("report_paths" , "dependencies.json" ), mode = "w" , encoding = "utf-8" )]
64
- mock_open .assert_has_calls (calls )
80
+ runtime_err .generate ("report_paths" , report )
81
+ mock_open .assert_not_called ()
65
82
66
83
67
84
@given (mock_data = JINJA_CONTEXT_DICT , num_dep = st .integers (min_value = 0 , max_value = 3 ))
68
- def test_gen_html_reports (mock_data : Any , num_dep : int ) -> None :
85
+ def test_gen_html_reports (custom_jinja_env : Environment , mock_data : Any , num_dep : int ) -> None :
69
86
"""Test if HTMLReporter can print HTML files without errors."""
70
87
report = Report (MockRecord (mock_data ))
71
88
for _ in range (num_dep ):
72
89
report .root_record .dependencies .append (MockRecord (mock_data ))
73
90
74
- custom_jinja_env = Environment (
75
- loader = FileSystemLoader (ROOT_PATH ),
76
- autoescape = select_autoescape (enabled_extensions = ["html" , "j2" ]),
77
- trim_blocks = True ,
78
- lstrip_blocks = True ,
79
- )
80
-
81
91
reporter = HTMLReporter (env = custom_jinja_env , target_template = "template.html" )
82
92
with patch ("builtins.open" ) as mock_open :
83
93
reporter .generate ("report_paths" , report )
@@ -97,8 +107,10 @@ def test_gen_html_reports(mock_data: Any, num_dep: int) -> None:
97
107
(SCMStatus .AVAILABLE , True , False ),
98
108
],
99
109
)
100
- def test_main_target_status (main_status : SCMStatus , has_deps : bool , gen_empty_main : bool ) -> None :
101
- """WIP"""
110
+ def test_main_target_status (
111
+ custom_jinja_env : Environment , main_status : SCMStatus , has_deps : bool , gen_empty_main : bool
112
+ ) -> None :
113
+ """Test the different scenarios for the main target's analysis status."""
102
114
main_record : Record = Record (
103
115
record_id = "record" ,
104
116
description = "sample_desc" ,
@@ -110,28 +122,21 @@ def test_main_target_status(main_status: SCMStatus, has_deps: bool, gen_empty_ma
110
122
policies_passed = [],
111
123
)
112
124
113
- dep_record : Record = Record (
114
- record_id = "dep" ,
115
- description = "sample_desc" ,
116
- pre_config = Configuration ({}),
117
- status = SCMStatus .AVAILABLE ,
118
- context = MagicMock (),
119
- dependencies = [],
120
- policies_failed = [],
121
- policies_passed = [],
122
- )
123
-
124
125
report = Report (main_record )
125
126
126
127
if has_deps :
127
- report .add_dep_record (dep_record )
128
+ dep_record : Record = Record (
129
+ record_id = "dep" ,
130
+ description = "sample_desc" ,
131
+ pre_config = Configuration ({}),
132
+ status = SCMStatus .AVAILABLE ,
133
+ context = MagicMock (),
134
+ dependencies = [],
135
+ policies_failed = [],
136
+ policies_passed = [],
137
+ )
138
+ report .root_record .dependencies .append (dep_record )
128
139
129
- custom_jinja_env = Environment (
130
- loader = FileSystemLoader (ROOT_PATH ),
131
- autoescape = select_autoescape (enabled_extensions = ["html" , "j2" ]),
132
- trim_blocks = True ,
133
- lstrip_blocks = True ,
134
- )
135
140
reporter = HTMLReporter (env = custom_jinja_env , target_template = "template.html" )
136
141
empty_main_call = call (os .path .join ("report_paths" , "index.html" ), mode = "w" , encoding = "utf-8" )
137
142
with patch ("builtins.open" ) as mock_open :
@@ -140,3 +145,18 @@ def test_main_target_status(main_status: SCMStatus, has_deps: bool, gen_empty_ma
140
145
mock_open .assert_has_calls ([empty_main_call ])
141
146
else :
142
147
assert empty_main_call .args not in mock_open .call_args_list
148
+
149
+
150
+ @given (mock_data = JINJA_CONTEXT_DICT , num_dep = st .integers (min_value = 0 , max_value = 3 ))
151
+ def test_gen_json_reports (mock_data : Any , num_dep : int ) -> None :
152
+ """Test if JSONReporter can print JSON files without errors."""
153
+ report = Report (MockRecord (mock_data ))
154
+ for _ in range (num_dep ):
155
+ report .root_record .dependencies .append (MockRecord (mock_data ))
156
+
157
+ reporter = JSONReporter ()
158
+
159
+ with patch ("builtins.open" ) as mock_open :
160
+ reporter .generate ("report_paths" , report )
161
+ calls = [call (os .path .join ("report_paths" , "dependencies.json" ), mode = "w" , encoding = "utf-8" )]
162
+ mock_open .assert_has_calls (calls )
0 commit comments