From 54b7af7eb4107f933d14da0737b539bbe38d0fc8 Mon Sep 17 00:00:00 2001 From: yura Date: Wed, 12 Feb 2020 13:38:06 +0300 Subject: [PATCH] Fixed #31250 -- Ignored processing instructions in assertXMLEqual()/assertXMLNotEqual(). --- django/test/utils.py | 10 +++++++--- docs/topics/testing/tools.txt | 4 ++-- tests/test_utils/tests.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 4dc1eac733..e626667b09 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -537,8 +537,8 @@ def compare_xml(want, got): """ Try to do a 'xml-comparison' of want and got. Plain string comparison doesn't always work because, for example, attribute ordering should not be - important. Ignore comment nodes, document type node, and leading and - trailing whitespaces. + important. Ignore comment nodes, processing instructions, document type + node, and leading and trailing whitespaces. Based on https://github.com/lxml/lxml/blob/master/src/lxml/doctestcompare.py """ @@ -576,7 +576,11 @@ def compare_xml(want, got): def first_node(document): for node in document.childNodes: - if node.nodeType not in (Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE): + if node.nodeType not in ( + Node.COMMENT_NODE, + Node.DOCUMENT_TYPE_NODE, + Node.PROCESSING_INSTRUCTION_NODE, + ): return node want = want.strip().replace('\\n', '\n') diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index 59f36d44b8..02433bbf5e 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -1613,8 +1613,8 @@ your test suite. syntax differences. When invalid XML is passed in any parameter, an ``AssertionError`` is always raised, even if both string are identical. - XML declaration, document type, and comments are ignored. Only the root - element and its children are compared. + XML declaration, document type, processing instructions, and comments are + ignored. Only the root element and its children are compared. Output in case of error can be customized with the ``msg`` argument. diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 8d7e6e4889..59f8ddd26b 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -926,6 +926,21 @@ class XMLEqualTests(SimpleTestCase): xml2 = '' self.assertXMLEqual(xml1, xml2) + def test_processing_instruction(self): + xml1 = ( + '' + '' + ) + xml2 = ( + '' + '' + ) + self.assertXMLEqual(xml1, xml2) + self.assertXMLEqual( + '', + '', + ) + class SkippingExtraTests(TestCase): fixtures = ['should_not_be_loaded.json']