Skip to content

Commit

Permalink
MISC: attrs_parse() fixed error when multi-line value appeared
Browse files Browse the repository at this point in the history
  • Loading branch information
patriki01 authored and pbrezina committed Oct 23, 2023
1 parent 798c657 commit c09aee2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
17 changes: 13 additions & 4 deletions sssd_test_framework/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ def attrs_parse(lines: list[str], attrs: list[str] | None = None) -> dict[str, l
:rtype: dict[str, list[str]]
"""
out: dict[str, list[str]] = {}
for line in lines:
line = line.strip()
i = 0
while i < len(lines):
line = lines[i]
if not line:
i += 1
continue

(key, value) = map(lambda x: x.strip(), line.split(":", 1))
(key, value) = map(lambda x: x.lstrip(), line.split(":", 1))
while i < len(lines) - 1:
if lines[i + 1].startswith(" "):
value += lines[i + 1][1:]
i += 1
else:
break

if attrs is None or key in attrs:
out.setdefault(key, [])
out[key].append(value)

i += 1
return out


Expand Down
37 changes: 30 additions & 7 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def test_attrs_parse__nofilter():
param1: value1
param2: value2
param3: value3
""".split(
"\n"
)
"""
lines = textwrap.dedent(lines).strip().split("\n")

expected = {
"param1": ["value1"],
Expand All @@ -39,10 +38,8 @@ def test_attrs_parse__filter():
param2: value2
param3: value3
param4: value4
""".split(
"\n"
)

"""
lines = textwrap.dedent(lines).strip().split("\n")
expected = {
"param2": ["value2"],
"param3": ["value3"],
Expand All @@ -51,6 +48,32 @@ def test_attrs_parse__filter():
assert attrs_parse(lines, ["param2", "param3"]) == expected


@pytest.mark.parametrize(
"input,expected",
[
(
["cn: sudorules", "distinguishedName: objectSID=123,cn=id_m", " appings,cn=test,cn=sysdb"],
{"cn": ["sudorules"], "distinguishedName": ["objectSID=123,cn=id_mappings,cn=test,cn=sysdb"]},
),
(
[
"description: My teacher is a good ",
" one but I do not like him",
" very much: he wears a dirty c",
" oat.",
],
{"description": ["My teacher is a good one but I do not like him very much: he wears a dirty coat."]},
),
(
["cn: sudorules", "numbers: one,", " two,", " three"],
{"cn": ["sudorules"], "numbers": ["one, two, three"]},
),
],
)
def test_attrs_parse__long_line(input, expected):
assert attrs_parse(input) == expected


@pytest.mark.parametrize(
"value,include,expected",
[
Expand Down

0 comments on commit c09aee2

Please sign in to comment.