Fix test failing on CI

This commit is contained in:
Kovid Goyal 2020-08-18 13:51:01 +05:30
parent fd896540d7
commit 5ceb657088
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -39,13 +39,17 @@ def safe_xml_fromstring(string_or_bytes, recover=True):
def find_tests(): def find_tests():
import unittest, tempfile, os import unittest, tempfile, os
from calibre.constants import iswindows
class TestXMLParse(unittest.TestCase): class TestXMLParse(unittest.TestCase):
def setUp(self): def setUp(self):
with tempfile.NamedTemporaryFile(delete=False) as tf: with tempfile.NamedTemporaryFile(delete=False) as tf:
tf.write(b'external') tf.write(b'external')
self.temp_file = tf.name self.temp_file = os.path.abspath(tf.name)
if iswindows:
import win32api
self.temp_file = win32api.GetLongPathNameW(self.temp_file)
def tearDown(self): def tearDown(self):
os.remove(self.temp_file) os.remove(self.temp_file)
@ -53,7 +57,20 @@ def find_tests():
def test_safe_xml_fromstring(self): def test_safe_xml_fromstring(self):
templ = '''<!DOCTYPE foo [ <!ENTITY e {id} "{val}" > ]><r>&e;</r>''' templ = '''<!DOCTYPE foo [ <!ENTITY e {id} "{val}" > ]><r>&e;</r>'''
external = 'file:///' + self.temp_file.replace(os.sep, '/') external = 'file:///' + self.temp_file.replace(os.sep, '/')
self.assertEqual(etree.fromstring(templ.format(id='SYSTEM', val=external)).text, 'external')
def t(tid, val, expected, safe=True):
raw = templ.format(id=tid, val=val)
err = None
try:
root = safe_xml_fromstring(raw) if safe else etree.fromstring(raw)
except Exception as e:
err = str(e)
root = None
got = getattr(root, 'text', object())
self.assertEqual(got, expected, f'Unexpected result parsing: {raw!r}, got: {got!r} expected: {expected!r} with XML parser error: {err}')
t('SYSTEM', external, 'external', safe=False)
for eid, val, expected in ( for eid, val, expected in (
('', 'normal entity', 'normal entity'), ('', 'normal entity', 'normal entity'),
('', external, external), ('', external, external),
@ -64,15 +81,7 @@ def find_tests():
('PUBLIC', external, None), ('PUBLIC', external, None),
('PUBLIC', 'http://example.com', None), ('PUBLIC', 'http://example.com', None),
): ):
raw = templ.format(id=eid, val=val) t(eid, val, expected)
err = None
try:
root = safe_xml_fromstring(raw)
except Exception as e:
err = str(e)
root = None
got = getattr(root, 'text', object())
self.assertEqual(got, expected, f'Unexpected result parsing: {raw!r}, got: {got!r} expected: {expected!r} with XML parser error: {err}')
def test_lxml_unicode_parsing(self): def test_lxml_unicode_parsing(self):
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode