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: Addressing Vasily's comments Created Oct. 23, 2017, 1:19 p.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..8b56b0d7441e0fcdfc5a8ba2f554b4704b432417 100644
--- a/tests/test_packagerWebExt.py
+++ b/tests/test_packagerWebExt.py
@@ -240,44 +240,27 @@ 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 = []
+def comparable_json(json_data):
+ """Create a nonambiguous representation of the given JSON data."""
+ if isinstance(json_data, basestring):
+ json_data = json.loads(json_data)
+ return json.dumps(
+ json_data, sort_keys=True, indent=0
+ ).split('\n')
- 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_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)
+ return (elt.tag, strip(elt.tail), strip(elt.text), elt.attrib, subs)
-def comparable_json(json_string):
- """Create a nonambiguous representation of a given JSON string."""
- return json.dumps(
- json.loads(json_string), sort_keys=True, indent=0
- ).split('\n')
+ return comparable_json(transform(ElementTree.fromstring(xml)))
def assert_manifest_content(manifest, expected_path):
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld