Debian Codegraph @

tests/test_html_formatter.py

raw symbols SCIP
1 """Tests for the HtmlFormatter."""
2
3 from textwrap import dedent
4
5 from cyclopts._markup import escape_html, extract_text
6 from cyclopts.help import HelpEntry, HelpPanel
7 from cyclopts.help.formatters.html import HtmlFormatter
8
9
10 def test_html_formatter_empty_panel():
11 """Test that empty panels produce no output."""
12 formatter = HtmlFormatter()
13 panel = HelpPanel(format="command", title="Commands")
14
15 formatter(None, None, panel)
16 output = formatter.get_output()
17
18 assert output == ""
19
20
21 def test_html_formatter_command_panel():
22 """Test command panel formatting as HTML table."""
23 formatter = HtmlFormatter()
24 panel = HelpPanel(
25 format="command",
26 title="Commands",
27 entries=[
28 HelpEntry(positive_names=("serve",), description="Start the server"),
29 HelpEntry(positive_names=("build",), positive_shorts=("-b",), description="Build the project"),
30 ],
31 )
32
33 formatter(None, None, panel)
34 output = formatter.get_output()
35
36 # Check structure
37 assert '<section class="help-panel">' in output
38 assert "Commands" in output
39 # Check structure contains list
40 assert "<ul" in output
41 assert "<code>serve</code>" in output
42 assert "<code>build</code>" in output
43 assert "(-b)" in output # Aliases are shown in parentheses without code tags
44 assert "Start the server" in output
45 assert "Build the project" in output
46
47
48 def test_html_formatter_parameter_panel():
49 """Test parameter panel formatting as HTML table."""
50 formatter = HtmlFormatter()
51 panel = HelpPanel(
52 format="parameter",
53 title="Parameters",
54 entries=[
55 HelpEntry(
56 positive_names=("--port",),
57 positive_shorts=("-p",),
58 description="Port number",
59 required=True,
60 type="int",
61 default="8080",
62 ),
63 HelpEntry(
64 positive_names=("--verbose",),
65 positive_shorts=("-v",),
66 description="Enable verbose mode",
67 required=False,
68 choices=("true", "false"),
69 ),
70 ],
71 )
72
73 formatter(None, None, panel)
74 output = formatter.get_output()
75
76 # Check structure
77 assert '<section class="help-panel">' in output
78 assert "Parameters" in output
79 # Check structure contains list
80 assert "<ul" in output
81
82 # Check required marker
83 assert '<span class="metadata-item metadata-required">Required</span>' in output
84
85 # Check parameter details
86 assert "<code>--port</code>" in output
87 assert "<code>-p</code>" in output
88 assert "Port number" in output
89 assert "<code>8080</code>" in output
90
91 assert "<code>--verbose</code>" in output
92 assert "Enable verbose mode" in output
93
94
95 def test_html_formatter_heading_level():
96 """Test custom heading levels."""
97 formatter = HtmlFormatter(heading_level=3)
98 panel = HelpPanel(
99 format="command", title="Commands", entries=[HelpEntry(positive_names=("test",), description="Test command")]
100 )
101
102 formatter(None, None, panel)
103 output = formatter.get_output()
104
105 assert "Commands" in output
106 assert "<code>test</code>" in output
107 assert "Test command" in output
108
109
110 def test_html_formatter_with_panel_description():
111 """Test panel with description."""
112 formatter = HtmlFormatter()
113 panel = HelpPanel(
114 format="command",
115 title="Commands",
116 description="Available commands for the application",
117 entries=[HelpEntry(positive_names=("test",), description="Test command")],
118 )
119
120 formatter(None, None, panel)
121 output = formatter.get_output()
122
123 assert '<div class="panel-description">Available commands for the application</div>' in output
124
125
126 def test_html_formatter_render_usage():
127 """Test usage rendering."""
128 formatter = HtmlFormatter()
129 usage = "myapp [OPTIONS] COMMAND"
130
131 formatter.render_usage(None, None, usage)
132 output = formatter.get_output()
133
134 expected = dedent("""\
135 <div class="usage-block">
136 <pre class="usage">Usage: myapp [OPTIONS] COMMAND</pre>
137 </div>
138 """)
139
140 assert output == expected
141
142
143 def test_html_formatter_render_description():
144 """Test description rendering."""
145 formatter = HtmlFormatter()
146 description = "This is a CLI application for doing awesome things."
147
148 formatter.render_description(None, None, description)
149 output = formatter.get_output()
150
151 expected = '<div class="description">This is a CLI application for doing awesome things.</div>\n'
152
153 assert output == expected
154
155
156 def test_html_formatter_reset():
157 """Test that reset clears the output buffer."""
158 formatter = HtmlFormatter()
159
160 # Add some content
161 formatter.render_description(None, None, "Test description")
162 assert formatter.get_output() != ""
163
164 # Reset and check it's empty
165 formatter.reset()
166 assert formatter.get_output() == ""
167
168
169 def testescape_html():
170 """Test HTML escaping function."""
171 # Basic escaping
172 assert escape_html("<script>alert('XSS')</script>") == "&lt;script&gt;alert(&#x27;XSS&#x27;)&lt;/script&gt;"
173 assert escape_html("foo & bar") == "foo &amp; bar"
174 assert escape_html('"quoted"') == "&quot;quoted&quot;"
175
176 # Handle None
177 assert escape_html(None) == ""
178 assert escape_html("") == ""
179
180
181 def test_extract_text():
182 """Test text extraction from various objects."""
183 # Test None
184 assert extract_text(None) == ""
185
186 # Test string
187 assert extract_text("hello world") == "hello world"
188
189 # Test object with __str__
190 class TestObj:
191 def __str__(self):
192 return "test object"
193
194 assert extract_text(TestObj()) == "test object"
195
196
197 def test_parameter_table_with_all_metadata():
198 """Test parameter table with all possible metadata fields."""
199 formatter = HtmlFormatter()
200 panel = HelpPanel(
201 format="parameter",
202 title="Parameters",
203 entries=[
204 HelpEntry(
205 positive_names=("--config",),
206 positive_shorts=("-c",),
207 description="Configuration file path",
208 required=True,
209 type="Path",
210 choices=("/etc/app.conf", "~/.app.conf", "./app.conf"),
211 env_var=("APP_CONFIG", "CONFIG_PATH"),
212 default="/etc/app.conf",
213 ),
214 ],
215 )
216
217 formatter(None, None, panel)
218 output = formatter.get_output()
219
220 # Check all metadata is present
221 assert "Configuration file path" in output
222 assert "<code>/etc/app.conf</code>" in output
223 assert "<code>~/.app.conf</code>" in output
224 assert "<code>./app.conf</code>" in output
225 assert "<code>APP_CONFIG</code>" in output
226 assert "<code>CONFIG_PATH</code>" in output
227
228
229 def test_parameter_table_no_required_column():
230 """Test parameter table when no parameters are required."""
231 formatter = HtmlFormatter()
232 panel = HelpPanel(
233 format="parameter",
234 title="Options",
235 entries=[
236 HelpEntry(
237 positive_names=("--debug",),
238 description="Enable debug mode",
239 type="bool",
240 default="False",
241 ),
242 HelpEntry(
243 positive_names=("--quiet",),
244 positive_shorts=("-q",),
245 description="Suppress output",
246 ),
247 ],
248 )
249
250 formatter(None, None, panel)
251 output = formatter.get_output()
252
253 # Should be using list format now
254 # Check structure contains list
255 assert "<ul" in output
256 assert "<code>--debug</code>" in output
257 assert "<code>--quiet</code>" in output
258
259
260 def test_command_with_no_description():
261 """Test command entry with no description."""
262 formatter = HtmlFormatter()
263 panel = HelpPanel(
264 format="command",
265 title="Commands",
266 entries=[
267 HelpEntry(positive_names=("test",)), # No description
268 HelpEntry(positive_names=("run",), description="Run the application"),
269 ],
270 )
271
272 formatter(None, None, panel)
273 output = formatter.get_output()
274
275 # Both commands should be in table
276 assert "<code>test</code>" in output
277 assert "<code>run</code>" in output
278 assert "Run the application" in output
279
280
281 def test_multiple_panels_accumulate():
282 """Test that multiple panels accumulate in the formatter."""
283 formatter = HtmlFormatter()
284
285 # First panel
286 panel1 = HelpPanel(
287 format="command", title="Commands", entries=[HelpEntry(positive_names=("help",), description="Show help")]
288 )
289
290 # Second panel
291 panel2 = HelpPanel(
292 format="parameter",
293 title="Global Options",
294 entries=[
295 HelpEntry(positive_names=("--verbose",), description="Verbose output", type="bool"),
296 ],
297 )
298
299 formatter(None, None, panel1)
300 formatter(None, None, panel2)
301 output = formatter.get_output()
302
303 # Both panels should be in output
304 assert "Commands" in output
305 assert "Global Options" in output
306 assert "<code>help</code>" in output
307 assert "<code>--verbose</code>" in output
308
309
310 def test_html_injection_prevention():
311 """Test that user input is properly escaped to prevent HTML injection."""
312 formatter = HtmlFormatter()
313
314 # Try to inject HTML through description
315 panel = HelpPanel(
316 format="command",
317 title="Commands",
318 entries=[
319 HelpEntry(
320 positive_names=("<script>alert('XSS')</script>",),
321 description="<b>Bold</b> and <i>italic</i> text",
322 ),
323 ],
324 )
325
326 formatter(None, None, panel)
327 output = formatter.get_output()
328
329 # Check that HTML is escaped
330 assert "&lt;script&gt;" in output
331 assert "&lt;b&gt;" in output
332 assert "&lt;i&gt;" in output
333 # Should not contain raw HTML
334 assert "<script>" not in output
335 assert "<b>Bold</b>" not in output