Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MISC: attrs_parse() fixed error when multi-line value appeared #53

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
thalman marked this conversation as resolved.
Show resolved Hide resolved
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