Debian Codegraph @

test/test_formatter.py

raw symbols SCIP
1 import os
2 from unittest import mock
3 import pytest
4 from con_duct._formatter import SummaryFormatter
5 from con_duct._tracker import Report
6
7 GREEN_START = SummaryFormatter.COLOR_SEQ % SummaryFormatter.GREEN
8 RED_START = SummaryFormatter.COLOR_SEQ % SummaryFormatter.RED
9
10
11 @mock.patch("con_duct._duct_main.LogPaths")
12 @mock.patch("con_duct._duct_main.subprocess.Popen")
13 def test_execution_summary_formatted_wall_clock_time_nan(
14 mock_popen: mock.MagicMock, mock_log_paths: mock.MagicMock
15 ) -> None:
16 mock_log_paths.prefix = "mock_prefix"
17 wall_clock_format_string = "Wall Clock Time: {wall_clock_time:.3f} sec\n"
18 cwd = os.getcwd()
19 report = Report(
20 "_cmd", [], mock_log_paths, wall_clock_format_string, cwd, clobber=False
21 )
22 # It should not crash and it would render even where no wallclock time yet
23 assert report.execution_summary_formatted is not None
24 assert "wall clock time: nan" in report.execution_summary_formatted.lower()
25
26 # Test with process
27 report.process = mock_popen
28 report.process.returncode = 0
29 output = report.execution_summary_formatted
30 assert "None" not in output
31 # Process did not finish, we didn't set start_time, so remains nan but there
32 assert "wall clock time: nan" in report.execution_summary_formatted.lower()
33
34
35 @mock.patch("con_duct._duct_main.LogPaths")
36 @mock.patch("con_duct._duct_main.subprocess.Popen")
37 def test_execution_summary_formatted_wall_clock_time_rounded(
38 mock_popen: mock.MagicMock, mock_log_paths: mock.MagicMock
39 ) -> None:
40 mock_log_paths.prefix = "mock_prefix"
41 wall_clock_format_string = "{wall_clock_time:.3f}"
42 cwd = os.getcwd()
43 report = Report(
44 "_cmd", [], mock_log_paths, wall_clock_format_string, cwd, clobber=False
45 )
46 report.process = mock_popen
47 report.process.returncode = 0
48 report.start_time = 1727221840.0486171
49 report.end_time = report.start_time + 1.111111111
50 assert "1.111" == report.execution_summary_formatted
51
52
53 def test_summary_formatter_no_vars() -> None:
54 not_really_format_string = "test"
55 formatter = SummaryFormatter()
56 out = formatter.format(not_really_format_string, **{})
57 assert out == not_really_format_string
58
59
60 def test_summary_formatter_vars_provided_no_vars_in_format_string() -> None:
61 not_really_format_string = "test"
62 one_arg = {"ok": "pass"}
63 formatter = SummaryFormatter()
64 out = formatter.format(not_really_format_string, **one_arg)
65 assert out == not_really_format_string
66
67
68 def test_summary_formatter_one_var() -> None:
69 valid_format_string = "test {ok}"
70 one_arg = {"ok": "pass"}
71 formatter = SummaryFormatter()
72 out = formatter.format(valid_format_string, **one_arg)
73 assert out == "test pass"
74
75
76 def test_summary_formatter_many_vars() -> None:
77 valid_format_string = "{one} {two} {three} {four} {five}"
78 many_args = {"one": "1", "two": "2", "three": "3", "four": "4", "five": "5"}
79 formatter = SummaryFormatter()
80 out = formatter.format(valid_format_string, **many_args)
81 assert out == "1 2 3 4 5"
82
83
84 def test_summary_formatter_missing_vars() -> None:
85 valid_format_string = "{one}"
86 formatter = SummaryFormatter()
87 with pytest.raises(KeyError):
88 formatter.format(valid_format_string, **{})
89
90 valid_format_string = "{one} {two}"
91 formatter = SummaryFormatter()
92 with pytest.raises(KeyError):
93 formatter.format(valid_format_string, **{"one": 1})
94
95
96 def test_summary_formatter_none_replacement() -> None:
97 valid_format_string = "test {none}"
98 one_arg = {"none": None}
99 formatter = SummaryFormatter()
100 out = formatter.format(valid_format_string, **one_arg)
101 assert out == "test -"
102
103
104 def test_summary_formatter_S_e2e() -> None:
105 formatter = SummaryFormatter()
106 one_arg = {"big_num": 100000}
107
108 valid_format_string = "test {big_num}"
109 no_s_applied = formatter.format(valid_format_string, **one_arg)
110 assert no_s_applied == "test 100000"
111
112 s_format_string = "test {big_num!S}"
113 s_applied = formatter.format(s_format_string, **one_arg)
114 assert s_applied == "test 100.0 kB"
115
116 none_applied = formatter.format(s_format_string, **{"big_num": None})
117 assert none_applied == "test -"
118
119
120 # YB -> ZB rollover https://github.com/python-humanize/humanize/issues/205
121 @pytest.mark.parametrize(
122 "num,expected",
123 [
124 [1, "1 Byte"],
125 [10, "10 Bytes"],
126 [100, "100 Bytes"],
127 [1000, "1.0 kB"],
128 [10000, "10.0 kB"],
129 [100000, "100.0 kB"],
130 [1000000, "1.0 MB"],
131 [10000000, "10.0 MB"],
132 [100000000, "100.0 MB"],
133 [1000000000, "1.0 GB"],
134 [10000000000, "10.0 GB"],
135 [100000000000, "100.0 GB"],
136 [1000000000000, "1.0 TB"],
137 [10000000000000, "10.0 TB"],
138 [100000000000000, "100.0 TB"],
139 [1000000000000000, "1.0 PB"],
140 [10000000000000000, "10.0 PB"],
141 [100000000000000000, "100.0 PB"],
142 [1000000000000000000, "1.0 EB"],
143 [10000000000000000000, "10.0 EB"],
144 [100000000000000000000, "100.0 EB"],
145 [1000000000000000000000, "1.0 ZB"],
146 [10000000000000000000000, "10.0 ZB"],
147 [100000000000000000000000, "100.0 ZB"],
148 [1000000000000900000000000, "1.0 YB"], # see issue above
149 [10000000000000000000000000, "10.0 YB"],
150 [100000000000000000000000000, "100.0 YB"],
151 [1000000000000000000000000000, "1000.0 YB"],
152 ],
153 )
154 def test_summary_formatter_S_sizes(num: int, expected: str) -> None:
155 formatter = SummaryFormatter()
156 format_string = "{num!S}"
157 actual = formatter.format(format_string, **{"num": num})
158 assert actual == expected
159
160
161 def test_summary_formatter_S_e2e_colors() -> None:
162 formatter = SummaryFormatter(enable_colors=True)
163 s_format_string = "test {big_num!S}"
164
165 zero_applied = formatter.format(s_format_string, **{"big_num": 0})
166 assert zero_applied != "test 0 Bytes"
167 expected = f"test {GREEN_START}0 Bytes{formatter.RESET_SEQ}"
168 assert expected == zero_applied
169
170 ten_5 = formatter.format(s_format_string, **{"big_num": 100000})
171 expected = f"test {GREEN_START}100.0 kB{formatter.RESET_SEQ}"
172 assert expected == ten_5
173
174 zero_applied_c = formatter.format(s_format_string, **{"big_num": 0})
175 expected = f"test {GREEN_START}0 Bytes{formatter.RESET_SEQ}"
176 assert expected == zero_applied_c
177
178 none_applied_c = formatter.format(s_format_string, **{"big_num": None})
179 expected = f"test {RED_START}-{formatter.RESET_SEQ}"
180 assert expected == none_applied_c
181
182
183 def test_summary_formatter_E_e2e() -> None:
184 formatter = SummaryFormatter()
185
186 valid_format_string = "test {e}"
187 no_e_applied = formatter.format(valid_format_string, **{"e": 1})
188 assert no_e_applied == "test 1"
189
190 e_format_string = "test {e!E}"
191 e_applied = formatter.format(e_format_string, **{"e": 1})
192 assert e_applied == "test 1"
193
194 e_format_string = "test {e!E}"
195 e_zero_applied = formatter.format(e_format_string, **{"e": 0})
196 assert e_zero_applied == "test 0"
197
198
199 def test_summary_formatter_E_e2e_colors() -> None:
200 formatter = SummaryFormatter(enable_colors=True)
201
202 valid_format_string = "test {e}"
203 no_e_applied = formatter.format(valid_format_string, **{"e": 1})
204 assert no_e_applied == "test 1"
205
206 e_format_string = "test {e!E}"
207
208 # Test Red truthy
209 e_applied = formatter.format(e_format_string, **{"e": 1})
210 assert e_applied == f"test {RED_START}1{formatter.RESET_SEQ}"
211
212 # Test Green falsey
213 e_zero_applied = formatter.format(e_format_string, **{"e": 0})
214 assert e_zero_applied == f"test {GREEN_START}0{formatter.RESET_SEQ}"
215
216 # # Test Red None
217 e_none_applied = formatter.format(e_format_string, **{"e": None})
218 assert e_none_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
219
220
221 def test_summary_formatter_X_e2e() -> None:
222 formatter = SummaryFormatter()
223
224 valid_format_string = "test {x}"
225 no_x_applied = formatter.format(valid_format_string, **{"x": 1})
226 assert no_x_applied == "test 1"
227
228 x_format_string = "test {x!X}"
229
230 x_applied = formatter.format(x_format_string, **{"x": 1})
231 assert x_applied == "test 1"
232
233 x_zero_applied = formatter.format(x_format_string, **{"x": 0})
234 assert x_zero_applied == "test 0"
235
236 x_none_applied = formatter.format(x_format_string, **{"x": None})
237 assert x_none_applied == "test -"
238
239
240 def test_summary_formatter_X_e2e_colors() -> None:
241 formatter = SummaryFormatter(enable_colors=True)
242
243 valid_format_string = "test {x}"
244 no_x_applied = formatter.format(valid_format_string, **{"x": 1})
245 assert no_x_applied == "test 1"
246
247 x_format_string = "test {x!X}"
248
249 # Test Green truthy
250 x_applied = formatter.format(x_format_string, **{"x": 1})
251 assert x_applied == f"test {GREEN_START}1{formatter.RESET_SEQ}"
252
253 # Test Red falsey
254 x_zero_applied = formatter.format(x_format_string, **{"x": 0})
255 assert x_zero_applied == f"test {RED_START}0{formatter.RESET_SEQ}"
256
257 # Test Red None
258 x_zero_applied = formatter.format(x_format_string, **{"x": None})
259 assert x_zero_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
260
261
262 def test_summary_formatter_N_e2e() -> None:
263 formatter = SummaryFormatter()
264
265 valid_format_string = "test {n}"
266 no_n_applied = formatter.format(valid_format_string, **{"n": 1})
267 assert no_n_applied == "test 1"
268
269 n_format_string = "test {n!N}"
270
271 n_applied = formatter.format(n_format_string, **{"n": 1})
272 assert n_applied == "test 1"
273
274 n_zero_applied = formatter.format(n_format_string, **{"n": 0})
275 assert n_zero_applied == "test 0"
276
277 n_none_applied = formatter.format(n_format_string, **{"n": None})
278 assert n_none_applied == "test -"
279
280
281 def test_summary_formatter_N_e2e_colors() -> None:
282 formatter = SummaryFormatter(enable_colors=True)
283
284 valid_format_string = "test {n}"
285 no_n_applied = formatter.format(valid_format_string, **{"n": 1})
286 assert no_n_applied == "test 1"
287
288 no_n_applied = formatter.format(valid_format_string, **{"n": None})
289 assert no_n_applied == "test -"
290
291 n_format_string = "test {n!N}"
292
293 # Test Green truthy
294 n_applied = formatter.format(n_format_string, **{"n": 1})
295 assert n_applied == f"test {GREEN_START}1{formatter.RESET_SEQ}"
296
297 # Test Green falsey
298 n_zero_applied = formatter.format(n_format_string, **{"n": 0})
299 assert n_zero_applied == f"test {GREEN_START}0{formatter.RESET_SEQ}"
300
301 # Test Red None
302 n_zero_applied = formatter.format(n_format_string, **{"n": None})
303 assert n_zero_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
304
305
306 @mock.patch("con_duct._duct_main.LogPaths")
307 @mock.patch("con_duct._duct_main.subprocess.Popen")
308 @pytest.mark.parametrize("colors", [True, False])
309 def test_execution_summary_formatted_wall_clock_time_nowvalid(
310 mock_popen: mock.MagicMock, mock_log_paths: mock.MagicMock, colors: bool
311 ) -> None:
312 mock_log_paths.prefix = "mock_prefix"
313 wall_clock_format_string = "Rendering: {wall_clock_time:.3f!X}"
314 cwd = os.getcwd()
315 report = Report(
316 "_cmd",
317 [],
318 mock_log_paths,
319 wall_clock_format_string,
320 cwd,
321 clobber=False,
322 colors=colors,
323 )
324 report.process = mock_popen
325 report.process.returncode = 0
326 report.start_time = 1727221840.0486171
327 report.end_time = report.start_time + 1.111111111
328
329 if colors:
330 GREEN, STOP = GREEN_START, SummaryFormatter.RESET_SEQ
331 else:
332 GREEN, STOP = "", ""
333
334 # Assert ValueError not raised
335 assert f"Rendering: {GREEN}1.111{STOP}" == report.execution_summary_formatted
336
337 # It should not crash and it would render even where no wallclock time yet
338 report = Report(
339 "_cmd",
340 [],
341 mock_log_paths,
342 wall_clock_format_string,
343 cwd,
344 clobber=False,
345 colors=colors,
346 )
347 assert f"Rendering: {GREEN}nan{STOP}" == report.execution_summary_formatted
348
349 # or if we really provide bad formatting, e.g. the opposite order of conversion and formatting
350 report = Report(
351 "_cmd",
352 [],
353 mock_log_paths,
354 "Rendering: {wall_clock_time!X:.3f}",
355 cwd,
356 clobber=False,
357 colors=colors,
358 )
359 assert f"Rendering: {GREEN}nan{STOP}" == report.execution_summary_formatted
360
361
362 def test_summary_formatter_P_e2e() -> None:
363 """Test percentage conversion (!P)"""
364 formatter = SummaryFormatter()
365
366 # Test without conversion
367 valid_format_string = "test {cpu}"
368 no_p_applied = formatter.format(valid_format_string, **{"cpu": 85.567})
369 assert no_p_applied == "test 85.567"
370
371 # Test with !P conversion
372 p_format_string = "test {cpu!P}"
373 p_applied = formatter.format(p_format_string, **{"cpu": 85.567})
374 assert p_applied == "test 85.57%"
375
376 # Test with integer
377 p_int_applied = formatter.format(p_format_string, **{"cpu": 100})
378 assert p_int_applied == "test 100.00%"
379
380 # Test with None
381 p_none_applied = formatter.format(p_format_string, **{"cpu": None})
382 assert p_none_applied == "test -"
383
384
385 def test_summary_formatter_P_e2e_colors() -> None:
386 """Test percentage conversion (!P) with colors"""
387 formatter = SummaryFormatter(enable_colors=True)
388
389 p_format_string = "test {cpu!P}"
390
391 # Test normal value (no color for P conversion)
392 p_applied = formatter.format(p_format_string, **{"cpu": 85.567})
393 assert p_applied == "test 85.57%"
394
395 # Test None (should be red)
396 p_none_applied = formatter.format(p_format_string, **{"cpu": None})
397 assert p_none_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
398
399
400 def test_summary_formatter_T_e2e() -> None:
401 """Test time duration conversion (!T)"""
402 formatter = SummaryFormatter()
403
404 # Test without conversion
405 valid_format_string = "test {duration}"
406 no_t_applied = formatter.format(valid_format_string, **{"duration": 3661.5})
407 assert no_t_applied == "test 3661.5"
408
409 # Test with !T conversion - hours
410 t_format_string = "test {duration!T}"
411 t_hours_applied = formatter.format(t_format_string, **{"duration": 3661.5})
412 assert t_hours_applied == "test 1h 1m 1.5s"
413
414 # Test minutes
415 t_minutes_applied = formatter.format(t_format_string, **{"duration": 150.75})
416 assert t_minutes_applied == "test 2m 30.8s"
417
418 # Test seconds only
419 t_seconds_applied = formatter.format(t_format_string, **{"duration": 45.123})
420 assert t_seconds_applied == "test 45.12s"
421
422 # Test with None
423 t_none_applied = formatter.format(t_format_string, **{"duration": None})
424 assert t_none_applied == "test -"
425
426
427 def test_summary_formatter_T_e2e_colors() -> None:
428 """Test time duration conversion (!T) with colors"""
429 formatter = SummaryFormatter(enable_colors=True)
430
431 t_format_string = "test {duration!T}"
432
433 # Test normal value (no color for T conversion)
434 t_applied = formatter.format(t_format_string, **{"duration": 3661.5})
435 assert t_applied == "test 1h 1m 1.5s"
436
437 # Test None (should be red)
438 t_none_applied = formatter.format(t_format_string, **{"duration": None})
439 assert t_none_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
440
441
442 def test_summary_formatter_D_e2e() -> None:
443 """Test datetime conversion (!D)"""
444 formatter = SummaryFormatter()
445
446 # Test without conversion
447 valid_format_string = "test {timestamp}"
448 no_d_applied = formatter.format(valid_format_string, **{"timestamp": 1625400000})
449 assert no_d_applied == "test 1625400000"
450
451 # Test with !D conversion - use July 4, 2021 12:00 PM UTC (safe from timezone edge cases)
452 d_format_string = "test {timestamp!D}"
453 # This timestamp represents July 4, 2021 12:00:00 UTC
454 result = formatter.format(d_format_string, **{"timestamp": 1625400000})
455 assert "Jul" in result and "2021" in result and ":" in result
456
457 # Test with invalid timestamp (should return as string)
458 d_invalid_applied = formatter.format(d_format_string, **{"timestamp": "invalid"})
459 assert "test invalid" == d_invalid_applied
460
461 # Test with None
462 d_none_applied = formatter.format(d_format_string, **{"timestamp": None})
463 assert d_none_applied == "test -"
464
465
466 def test_summary_formatter_D_e2e_colors() -> None:
467 """Test datetime conversion (!D) with colors"""
468 formatter = SummaryFormatter(enable_colors=True)
469
470 d_format_string = "test {timestamp!D}"
471
472 # Test normal value (no color for D conversion)
473 result = formatter.format(d_format_string, **{"timestamp": 1625400000})
474 assert "Jul" in result and "2021" in result
475
476 # Test None (should be red)
477 d_none_applied = formatter.format(d_format_string, **{"timestamp": None})
478 assert d_none_applied == f"test {RED_START}-{formatter.RESET_SEQ}"
479
480
481 @pytest.mark.parametrize(
482 "duration,expected",
483 [
484 (0.5, "0.50s"),
485 (30, "30.00s"),
486 (90, "1m 30.0s"),
487 (3600, "1h 0m 0.0s"),
488 (3661.27, "1h 1m 1.3s"),
489 (7323.75, "2h 2m 3.8s"),
490 ],
491 )
492 def test_summary_formatter_T_duration_formats(duration: float, expected: str) -> None:
493 """Test various duration formatting scenarios"""
494 formatter = SummaryFormatter()
495 result = formatter.format("{duration!T}", duration=duration)
496 assert result == expected