Skip to content

Commit 84bac26

Browse files
authored
Handle trailing comments in mapping file (#123)
Ensures mapping file entries such as the following are handled: ``` key: value # inline comment for the line here ``` Furthermore, add ruff as a dev dependency so automatic formatting of snapshots actually works.
1 parent b9a0043 commit 84bac26

File tree

4 files changed

+280
-242
lines changed

4 files changed

+280
-242
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ test = [
4444
"pytest-timeout>=2.2.0",
4545
"hypothesis>=6.62.1",
4646
"inline-snapshot>=0.14.0",
47+
"ruff>=0.12.5",
4748
]
4849

4950
[project.urls]

tests/test_utils.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from hypothesis import given
1313
from hypothesis import settings
1414
from hypothesis import strategies as st
15+
from inline_snapshot import snapshot
1516
from pytest import LogCaptureFixture
1617
from zabbix_auto_config import utils
1718
from zabbix_auto_config.pyzabbix.types import HostTag
@@ -41,44 +42,49 @@ def test_is_valid_ip(ip_address: Union[IPv4Address, IPv6Address]):
4142
def 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

Comments
 (0)