From 4d8c6bdb695e00bb0c69a8b720d945dc962485dd Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sun, 21 Jun 2015 05:04:51 +0000 Subject: [PATCH] Add XML improvements. Signed-off-by: Robin H. Johnson --- s3tests/common.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/s3tests/common.py b/s3tests/common.py index 7b5cb47..55e669a 100644 --- a/s3tests/common.py +++ b/s3tests/common.py @@ -5,8 +5,12 @@ import os import random import string import yaml +import re from lxml import etree +from doctest import Example +from lxml.doctestcompare import LXMLOutputChecker + s3 = bunch.Bunch() config = bunch.Bunch() prefix = '' @@ -261,10 +265,23 @@ def normalize_xml_whitespace(xml, pretty_print=True): if element.text is not None and not element.text.strip(): element.text = None if element.text is not None: - element.text = element.text.strip().replace("\n","").replace("\r","") + element.text = element.text.strip().replace("\n", "").replace("\r", "") if element.tail is not None and not element.tail.strip(): element.tail = None if element.tail is not None: - element.tail = element.tail.strip().replace("\n","").replace("\r","") + element.tail = element.tail.strip().replace("\n", "").replace("\r", "") - return etree.tostring(root, encoding="utf-8", xml_declaration=True, pretty_print=pretty_print) + xmlstr = etree.tostring(root, encoding="utf-8", xml_declaration=True, pretty_print=pretty_print) + # there are two different DTD URIs + xmlstr = re.sub(r'xmlns="[^"]+"', 'xmlns="DTD-URI"', xmlstr) + xmlstr = re.sub(r'xmlns=\'[^\']+\'', 'xmlns="DTD-URI"', xmlstr) + for uri in ['http://doc.s3.amazonaws.com/doc/2006-03-01/', 'http://s3.amazonaws.com/doc/2006-03-01/']: + xmlstr = xmlstr.replace(uri, 'URI-DTD') + #xmlstr = re.sub(r'>\s+', '>', xmlstr, count=0, flags=re.MULTILINE) + return xmlstr + +def assert_xml_equal(got, want): + checker = LXMLOutputChecker() + if not checker.check_output(want, got, 0): + message = checker.output_difference(Example("", want), got, 0) + raise AssertionError(message)