Skip to content

Commit 2705319

Browse files
Make XML element comparison util more generic (#164)
* Make XML element comparison util more generic * Add changelog fragment * Lint * Add inverse test to xml util * Lint * Update some unit test descriptions
1 parent 3564a1a commit 2705319

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- plugins.module_utils.xml_utils:elements_equal - Fix element content comparison behaviour

plugins/module_utils/xml_utils.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,6 @@ def etree_to_dict(input_etree: Element) -> dict:
187187
return {input_etree.tag: result}
188188

189189

190-
def _is_whitespace_or_none(text) -> bool:
191-
"""
192-
Checks if a given string is a string of whitespace characters or None.
193-
Args:
194-
text: the string to perform the check on.
195-
Returns:
196-
bool: True if the 'text' string is None or a whitespace string.
197-
"""
198-
return text is None or text.strip() == ""
199-
200-
201190
def elements_equal(e1, e2) -> bool:
202191
"""
203192
Compare two XML elements for equality.
@@ -216,10 +205,13 @@ def elements_equal(e1, e2) -> bool:
216205
if len(e1) == 0 and len(e2) == 0:
217206
# 1. Check if texts are exactly the same (ignoring whitespaces and None)
218207
# 2. or check if one text is '1' and the other is None with no children
208+
e1_text: Optional[str] = "" if e1.text is None else str(e1.text).strip()
209+
e2_text: Optional[str] = "" if e2.text is None else str(e2.text).strip()
210+
219211
return (
220-
(_is_whitespace_or_none(e1.text) == _is_whitespace_or_none(e2.text))
221-
or (e1.text == "1" and not e2.text and not e2)
222-
or (e2.text == "1" and not e1.text and not e1)
212+
e1_text == e2_text
213+
or (e1_text == "1" and e2_text == "")
214+
or (e2_text == "1" and e1_text == "")
223215
)
224216

225217
# Tags have children

tests/unit/plugins/module_utils/test_xml_utils.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ def test_elements_equal_without_children_whitespace_matches():
415415
assert xml_utils.elements_equal(e1, e2)
416416

417417

418+
def test_elements_equal_str_num_and_int():
419+
"""
420+
Test that elements_equal function considers a string number equal to an integer number
421+
422+
eg:
423+
<test/> == <test> </test>
424+
"""
425+
e1 = Element("test")
426+
e1.text = 1
427+
e2 = Element("test")
428+
e2.text = "1"
429+
430+
assert xml_utils.elements_equal(e1, e2)
431+
432+
418433
def test_elements_equal_without_children_none_and_1():
419434
"""
420435
Tests elements_equal function matches a boolean flag "1" the same as an empty element.
@@ -433,17 +448,42 @@ def test_elements_equal_without_children_none_and_1():
433448
def test_elements_equal_with_children():
434449
"""
435450
Tests elements_equal function matches recursively equal elements.
451+
Elements having children with the same content should be equal.
436452
"""
437453
e1 = Element("test")
438454
e1c1 = Element("child_1")
439455
e1c1.text = "some_text"
440456
e1c2 = Element("child_2")
441457
e1c2.text = "some_text_as_well"
442458
e1.extend([e1c1, e1c2])
459+
443460
e2 = Element("test")
444461
e2c1 = Element("child_1")
445462
e2c1.text = "some_text \n"
446463
e2c2 = Element("child_2")
447-
e2c2.text = "\n some_text \n"
464+
e2c2.text = "\n some_text_as_well \n"
448465
e2.extend([e2c1, e2c2])
466+
449467
assert xml_utils.elements_equal(e1, e2)
468+
469+
470+
def test_elements_not_equal_with_children():
471+
"""
472+
Tests elements_equal function checks elements recursively.
473+
Elements having children with different content should not be equal.
474+
"""
475+
e1 = Element("test")
476+
e1c1 = Element("child_1")
477+
e1c1.text = "some_text"
478+
e1c2 = Element("child_2")
479+
e1c2.text = "some_text_as_well"
480+
e1.extend([e1c1, e1c2])
481+
482+
e2 = Element("test")
483+
e2c1 = Element("child_1")
484+
e2c1.text = "some_text \n"
485+
e2c2 = Element("child_2")
486+
e2c2.text = "\n wrong_text \n"
487+
e2.extend([e2c1, e2c2])
488+
489+
assert not xml_utils.elements_equal(e1, e2)

0 commit comments

Comments
 (0)