Skip to content

Commit

Permalink
Make XML element comparison util more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
DonGiovanni83 committed Dec 5, 2024
1 parent 3564a1a commit 2b85353
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
20 changes: 4 additions & 16 deletions plugins/module_utils/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,6 @@ def etree_to_dict(input_etree: Element) -> dict:
return {input_etree.tag: result}


def _is_whitespace_or_none(text) -> bool:
"""
Checks if a given string is a string of whitespace characters or None.
Args:
text: the string to perform the check on.
Returns:
bool: True if the 'text' string is None or a whitespace string.
"""
return text is None or text.strip() == ""


def elements_equal(e1, e2) -> bool:
"""
Compare two XML elements for equality.
Expand All @@ -216,11 +205,10 @@ def elements_equal(e1, e2) -> bool:
if len(e1) == 0 and len(e2) == 0:
# 1. Check if texts are exactly the same (ignoring whitespaces and None)
# 2. or check if one text is '1' and the other is None with no children
return (
(_is_whitespace_or_none(e1.text) == _is_whitespace_or_none(e2.text))
or (e1.text == "1" and not e2.text and not e2)
or (e2.text == "1" and not e1.text and not e1)
)
e1_text: Optional[str] = "" if e1.text is None else str(e1.text).strip()
e2_text: Optional[str] = "" if e2.text is None else str(e2.text).strip()

return e1_text == e2_text or e1_text == "1" and e2_text == "" or e2_text == "1" and e1_text == ""

# Tags have children
return all(
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/plugins/module_utils/test_xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ def test_elements_equal_without_children_whitespace_matches():
assert xml_utils.elements_equal(e1, e2)


def test_elements_equal_str_num_and_int():
"""
Test that elements_equal function considers a string number equal to an integer number
eg:
<test/> == <test> </test>
"""
e1 = Element("test")
e1.text = 1
e2 = Element("test")
e2.text = "1"

assert xml_utils.elements_equal(e1, e2)


def test_elements_equal_without_children_none_and_1():
"""
Tests elements_equal function matches a boolean flag "1" the same as an empty element.
Expand All @@ -440,10 +455,12 @@ def test_elements_equal_with_children():
e1c2 = Element("child_2")
e1c2.text = "some_text_as_well"
e1.extend([e1c1, e1c2])

e2 = Element("test")
e2c1 = Element("child_1")
e2c1.text = "some_text \n"
e2c2 = Element("child_2")
e2c2.text = "\n some_text \n"
e2c2.text = "\n some_text_as_well \n"
e2.extend([e2c1, e2c2])

assert xml_utils.elements_equal(e1, e2)

0 comments on commit 2b85353

Please sign in to comment.