diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-03-23 21:06:09 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-03-23 21:06:09 -0400 |
commit | a868dd01713cb3020bd768830fca7d1c0eef1720 (patch) | |
tree | b2a0d2dd3af6e5d3bc62b196d2a8b59514b5af4d /tests/goldtest.py | |
parent | 7e6716562f3b7818c19e06f5cd9c063c02cb7980 (diff) | |
download | python-coveragepy-git-a868dd01713cb3020bd768830fca7d1c0eef1720.tar.gz |
Rewrite XML tests to use xml parsing instead of regexes
Diffstat (limited to 'tests/goldtest.py')
-rw-r--r-- | tests/goldtest.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/tests/goldtest.py b/tests/goldtest.py index 4c6c3c96..b5e32f5f 100644 --- a/tests/goldtest.py +++ b/tests/goldtest.py @@ -10,6 +10,7 @@ import os import os.path import re import sys +import xml.etree.ElementTree from unittest_mixins import change_dir # pylint: disable=unused-import @@ -79,12 +80,19 @@ def compare( # ourselves. text_diff = [] for f in diff_files: + expected_file = os.path.join(expected_dir, f) - actual_file = os.path.join(actual_dir, f) with open(expected_file, READ_MODE) as fobj: expected = fobj.read() + if expected_file.endswith(".xml"): + expected = canonicalize_xml(expected) + + actual_file = os.path.join(actual_dir, f) with open(actual_file, READ_MODE) as fobj: actual = fobj.read() + if actual_file.endswith(".xml"): + actual = canonicalize_xml(actual) + if scrubs: expected = scrub(expected, scrubs) actual = scrub(actual, scrubs) @@ -102,6 +110,15 @@ def compare( assert not actual_only, "Files in %s only: %s" % (actual_dir, actual_only) +def canonicalize_xml(xtext): + """Canonicalize some XML text.""" + root = xml.etree.ElementTree.fromstring(xtext) + for node in root.iter(): + node.attrib = dict(sorted(node.items())) + xtext = xml.etree.ElementTree.tostring(root) + return xtext.decode('utf8') + + def contains(filename, *strlist): """Check that the file contains all of a list of strings. |