Opened 5 years ago

Closed 5 years ago

#31250 closed Bug (fixed)

assertXMLEqual() crashes on processing instructions.

Reported by: Matt Fisher Owned by: Yuri Savin
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Similar bug to https://code.djangoproject.com/ticket/30497, so I've based this report on that one.

Prepare Django project:

$ python -m venv env
$ . ./env/bin/activate
$ pip install django
$ django-admin startproject p1
$ cd p1

Create p1/tests.py, with xml taken from W3 xml-model and W3 xml-stylesheet examples:

from django.test import TestCase

class MyTestCase(TestCase):
    def test_assert_xml_equal_xml_model(self):
        xml1 = '''
            <?xml version="1.0"?>
            <?xml-model href="http://www.docbook.org/xml/5.0/rng/docbook.rng"?>
            <?xml-model href="http://www.docbook.org/xml/5.0/xsd/docbook.xsd"?>
            <book xmlns="http://docbook.org/ns/docbook">
            </book>
        '''
        self.assertXMLEqual(xml1, xml1)

    def test_assert_xml_equal_stylesheet(self):
        xml = '''
            <?xml-stylesheet href="common.css"?>
            <?xml-stylesheet href="default.css" title="Default style"?>
            <?xml-stylesheet alternate="yes" href="alt.css" title="Alternative style"?>
            <?xml-stylesheet href="single-col.css" media="all and (max-width: 30em)"?>
            <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                    <title>Example with xml-stylesheet processing instructions</title>
                 </head>
                 <body>
                 </body>
            </html>
        '''
        self.assertXMLEqual(xml, xml)

Run the tests:

$ ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
FF
======================================================================
FAIL: test_assert_xml_equal_stylesheet (tests.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/testcases.py", line 843, in assertXMLEqual
    result = compare_xml(xml1, xml2)
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/utils.py", line 596, in compare_xml
    return check_element(want_root, got_root)
AttributeError: 'ProcessingInstruction' object has no attribute 'tagName'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../dev/django/p1/tests.py", line 28, in test_assert_xml_equal_stylesheet
    self.assertXMLEqual(xml, xml)
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/testcases.py", line 846, in assertXMLEqual
    self.fail(self._formatMessage(msg, standardMsg))
AssertionError: First or second argument is not valid XML
'ProcessingInstruction' object has no attribute 'tagName'

======================================================================
FAIL: test_assert_xml_equal_xml_model (tests.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/testcases.py", line 843, in assertXMLEqual
    result = compare_xml(xml1, xml2)
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/utils.py", line 596, in compare_xml
    return check_element(want_root, got_root)
AttributeError: 'ProcessingInstruction' object has no attribute 'tagName'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../dev/django/p1/tests.py", line 12, in test_assert_xml_equal_xml_model
    self.assertXMLEqual(xml1, xml1)
  File ".../dev/django/env/lib/python3.7/site-packages/django/test/testcases.py", line 846, in assertXMLEqual
    self.fail(self._formatMessage(msg, standardMsg))
AssertionError: First or second argument is not valid XML
'ProcessingInstruction' object has no attribute 'tagName'

----------------------------------------------------------------------
Ran 2 tests in 0.007s

FAILED (failures=2)
Destroying test database for alias 'default'...

As discussed in the PR for the previous fix, assertXMLEqual probably shouldn't compare what's outside the root, so ignoring Node.PROCESSING_INSTRUCTION_NODE in first_node would probably do the job (tests pass after this change).

Change History (9)

comment:1 by Mariusz Felisiak, 5 years ago

Easy pickings: set
Summary: assertXMLEqual fails on processing instructionsassertXMLEqual() crashes on processing instructions.
Triage Stage: UnreviewedAccepted
Version: 3.0master

comment:2 by Yuri Savin, 5 years ago

Owner: changed from nobody to Yuri Savin
Status: newassigned

comment:3 by Yuri Savin, 5 years ago

Write PR for this ticket https://github.com/django/django/pull/12446
Added a check for the availability of fields in the node. The original branch was passed nodes COMMENT_NODE and DOCUMENT_TYPE_NODE. I left pass only COMMENT_NODE

comment:4 by Yuri Savin, 5 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak, 5 years ago

Has patch: set
Triage Stage: Ready for checkinAccepted

Yuri, you shouldn't mark you own patches as "Ready for checkin".

in reply to:  5 comment:6 by Yuri Savin, 5 years ago

Replying to felixxm:

Yuri, you shouldn't mark you own patches as "Ready for checkin".

Oh, I'm sorry. I'm trying to notify you that the patch is ready

comment:7 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:8 by Mariusz Felisiak, 5 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 54b7af7e:

Fixed #31250 -- Ignored processing instructions in assertXMLEqual()/assertXMLNotEqual().

Note: See TracTickets for help on using tickets.
Back to Top