1212from hypothesis import given
1313from hypothesis import settings
1414from hypothesis import strategies as st
15+ from inline_snapshot import snapshot
1516from pytest import LogCaptureFixture
1617from zabbix_auto_config import utils
1718from zabbix_auto_config .pyzabbix .types import HostTag
@@ -41,44 +42,49 @@ def test_is_valid_ip(ip_address: Union[IPv4Address, IPv6Address]):
4142def test_read_map_file (tmp_path : Path , caplog : pytest .LogCaptureFixture ):
4243 tmpfile = tmp_path / "map.txt"
4344 tmpfile .write_text (
44- "\n " . join (
45- [
46- "a:1" ,
47- "b:2,3" ,
48- "invalid line here" , # warning (no colon)
49- "c:4" ,
50- "d:5" ,
51- "e:" ,
52- "f: " ,
53- "g:," ,
54- "# this is a comment" , # ignored (comment)
55- "h:6," ,
56- "h:6" , # duplicate key+ value
57- "i:7:8" , # colon in value (allowed)
58- "j:9,9,10" , # duplicate values
59- "k :11,12,13" , # trailing whitespace in key
60- "l: 14 , 15,16 " , # leading and trailing whitespace in values
61- "l:17" , # duplicate key (extends existing values)
62- "" , # ignored (empty line)
63- ]
64- ) ,
45+ """ \
46+ a:1
47+ b:2,3
48+ invalid line here # WARNING: no colon
49+ c:4
50+ d:6
51+ e: # WARNING: no value after colon
52+ f: # WARNING: no value
53+ g:, # WARNING: no value + trailing comma
54+ # this is a comment # IGNORED: comment
55+ h:6, # OK: trailing comma
56+ h:6 # WARNING: duplicate key+value
57+ i:7:8 # OK: colon in value
58+ j:9,9,10 # WARNING: duplicate values
59+ k :11,12,13 # OK: whitespace before colon
60+ l: 14 , 15,16 # OK: leading and trailing whitespace in values
61+ l:17 # WARNING: duplicate key (extends existing values)
62+
63+ # More complex cases
64+ Spaces in key: Spaces in values, are, ok
65+ """ ,
6566 encoding = "utf-8" ,
6667 )
6768
6869 with caplog .at_level (logging .WARNING ):
6970 m = utils .read_map_file (tmpfile )
7071
71- assert m == {
72- "a" : ["1" ],
73- "b" : ["2" , "3" ],
74- "c" : ["4" ],
75- "d" : ["5" ],
76- "h" : ["6" ],
77- "i" : ["7:8" ],
78- "j" : ["9" , "10" ],
79- "k" : ["11" , "12" , "13" ],
80- "l" : ["14" , "15" , "16" , "17" ],
81- }
72+ assert m == snapshot (
73+ {
74+ "a" : ["1" ],
75+ "b" : ["2" , "3" ],
76+ "c" : ["4" ],
77+ "d" : ["6" ],
78+ "h" : ["6" ],
79+ "i" : ["7:8" ],
80+ "j" : ["9" , "10" ],
81+ "k" : ["11" , "12" , "13" ],
82+ "l" : ["14" , "15" , "16" , "17" ],
83+ "Spaces in key" : ["Spaces in values" , "are" , "ok" ],
84+ }
85+ )
86+
87+ # Test logging output
8288 invalid_lines_contain = [
8389 "'invalid line here'" ,
8490 "'e:'" ,
@@ -97,7 +103,7 @@ def test_read_map_file(tmp_path: Path, caplog: pytest.LogCaptureFixture):
97103 "line 16" ,
98104 ]
99105 for phrase in invalid_lines_contain :
100- assert phrase in caplog .text
106+ assert phrase in caplog .text , f"Expected ' { phrase } ' in log output { caplog . text } "
101107 assert caplog .text .count ("WARNING" ) == 8
102108
103109
0 commit comments