Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: tests/test_packagerWebExt.py

Issue 29586561: Noissue - Make comparable_xml safe against ambiguous tags (Closed)
Patch Set: Created Oct. 23, 2017, 10:20 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/test_packagerWebExt.py
diff --git a/tests/test_packagerWebExt.py b/tests/test_packagerWebExt.py
index f13ab10ac164a9055d06340d1d9b02c08339b598..bcdf34f591e02c7db018743e6a976ae1ffa2574e 100644
--- a/tests/test_packagerWebExt.py
+++ b/tests/test_packagerWebExt.py
@@ -240,39 +240,6 @@ def lib_files(tmpdir):
return files
-def comparable_xml(xml):
- """Create a nonambiguous representation of a given XML tree.
-
- Note that this function is not safe against ambiguous tags
- containing differently distributed children, e.g.:
-
- '<a><b><c/></b><b><d/></b></a>'
- vs.
- '<a><b/><b><c/><d/></b></a>'
-
- This is irrelevant for comparing the XML used by the tests of this
- module.
- """
- def get_leafs_string(tree):
- """Recursively build a string representing all xml leaf-nodes."""
- root_str = '{}|{}|{}'.format(tree.tag, tree.tail, tree.text).strip()
- result = []
-
- if len(tree) > 0:
- for subtree in tree:
- for leaf in get_leafs_string(subtree):
- result.append('{}__{}'.format(root_str, leaf))
- for k, v in tree.attrib.items():
- result.append('{}__{}:{}'.format(root_str, k, v))
- else:
- result.append(root_str)
- return result
-
- # XML data itself shall not be sorted, hence we can safely sort
- # our string representations in order to produce a valid unified diff.
- return sorted(get_leafs_string(ElementTree.fromstring(xml)))
-
-
def comparable_json(json_string):
"""Create a nonambiguous representation of a given JSON string."""
return json.dumps(
@@ -280,6 +247,21 @@ def comparable_json(json_string):
).split('\n')
+def comparable_xml(xml):
+ """Create a nonambiguous representation of a given XML tree."""
+ def strip(s):
+ if s is None:
+ return ''
+ return s.strip()
+
+ def transform(elt):
+ subs = sorted(transform(s) for s in elt)
+ attrs = tuple('{}={}'.format(k, v) for k, v in elt.attrib.items())
Vasily Kuznetsov 2017/10/23 12:48:20 Since you're doing a `comparable_json` later, we c
tlucas 2017/10/23 13:21:36 Good point - Done.
+ return (elt.tag, strip(elt.tail), strip(elt.text), attrs, subs)
+
+ return comparable_json(json.dumps(transform(ElementTree.fromstring(xml))))
Vasily Kuznetsov 2017/10/23 12:48:20 Its kind of a pity that we're doing `json.dumps` h
tlucas 2017/10/23 13:21:36 No, you are right - dumps(loads(dumps())) is defin
+
+
def assert_manifest_content(manifest, expected_path):
extension = os.path.basename(expected_path).split('.')[-1]
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld