-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
junitxml doesn't escape newlines in attribute values #1218
Comments
Add post-run cleanup pass to propertly escape multiline error messages in test reports. Workaround for pytest-dev/pytest#1218.
@nicoddemus has anyone check on this? |
@dajose I don't think so. I would love to review a PR and merge this in though. 😉 |
I think I found the source of the issue: https://github.com/pytest-dev/py/blob/master/py/_xmlgen.py#L236 A way to monkey patch and test: >>> from _pytest import junitxml
>>> print(junitxml.Junit.failure(message=junitxml.bin_xml_escape('foo\nbar')).unicode())
<failure message="foo
bar"/>
>>> import re
>>> from py.xml import escape
>>> escape.escape['\n'] = '
'
>>> escape.charef_rex = re.compile('|'.join(escape.escape.keys()))
>>> print(junitxml.Junit.failure(message=junitxml.bin_xml_escape('foo\nbar')).unicode())
<failure message="foo
bar"/> Basically, the Not sure if this is a too intrusive change, as that function is used everywhere. We could then change: self.escape = {
u('"') : u('"'), u('<') : u('<'), u('>') : u('>'),
u('&') : u('&'), u("'") : u('''),
} To: self.escape = {
u('"') : u('"'),
u('<') : u('<'),
u('>') : u('>'),
u('&') : u('&'),
u("'") : u('''),
u("\n") : u('
'),
} |
Thanks @carlos-jenkins for the investigation. The patch seems reasonable. I wonder though if we can use |
The way to do it with >>> from xml.etree import ElementTree as et
>>> e = et.Element('failure', attrib={'message': 'foo\nbar'})
>>> et.tostring(e).decode('utf-8')
'<failure message="foo bar" />' A complete rewrite of this module to use @nicoddemus what do you think of just changing the |
Interesting enough the |
Sure, sounds good; it is definitely a step in the right direction. 👍 |
Mmmm I'm having issues, as the example above with https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L1075 The problem is that Also explored the Thoughts? |
Hmmm that's unfortunate, thanks again for tackling this. 👍
Perhaps the above would be simpler then? We don't even need a new pytest release for it, and should be simple to implement (which you already did actually) and test. |
attribute escape and text escape have different rules, we should start to use a real compliant xml generator instead of the hack we have in place which "works" |
@RonnyPfannschmidt definitely, I mentioned this (see #1218 (comment)). I was about to create #4970 but got side tracked, thanks for commenting which put me back on track. 👍 @carlos-jenkins I still think that if you want to contribute that fix to |
What @RonnyPfannschmidt mentions is correct. The above monkey patch / quick patch has the side effect of escaping the newlines in the text of a tag, which is undesired.
For CDATA (tag text): For attributes: While I feel urged to import them I don't think is a good idea because, first, is private and may change in the future, and also there is a C version of ElementTree (previously "cElementTree") that may be imported and used instead of the Python one, and I don't know if that module has those functions, are importable or how the C extension load currently works. One option may be to copy paste those functions as is, and use them in the Regards |
I'm OK with copying the functions and use them to fix this bug now: while it is dirty and hacky, I don't think anybody has the time to rewrite |
If error message has newlines in it then
<failure>
-s attributemessage
contains them verbatim, but they should be encoded, otherwise XML parser will lose them.The text was updated successfully, but these errors were encountered: