Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
- close resources with context manager
- add tests for etree `Element()`
- `makeSuite` is deprecated
  • Loading branch information
tiran committed Sep 26, 2023
1 parent 22d0abd commit 4e6cea5
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import warnings

from xml.etree import ElementTree as orig_elementtree
from xml.etree.ElementTree import Element as ElementBeforeImport
from xml.sax.saxutils import XMLGenerator
from xml.sax import SAXParseException
from pyexpat import ExpatError
Expand All @@ -20,6 +21,8 @@
NotSupportedError,
)

from xml.etree.ElementTree import Element as ElementAfterImport

if sys.version_info < (3, 7):
warnings.filterwarnings("once", category=DeprecationWarning)

Expand Down Expand Up @@ -47,7 +50,7 @@
lxml_warnings = None


warnings.filterwarnings("error", category=DeprecationWarning, module=r"defusedxml\..*")
warnings.filterwarnings("always", category=DeprecationWarning, module=r"defusedxml\..*")


HERE = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -196,10 +199,12 @@ def iterparse(self, source, **kwargs):
return list(self.module.iterparse(source, **kwargs))

def test_html_arg(self):
with self.assertRaises(DeprecationWarning):
ElementTree.XMLParse(html=0)
with warnings.catch_warnings(record=True) as w:
self.module.XMLParse(html=0)
self.assertEqual(len(w), 1)
self.assertIs(w[0].category, DeprecationWarning)
with self.assertRaises(TypeError):
ElementTree.XMLParse(html=1)
self.module.XMLParse(html=1)

def test_aliases(self):
parser = self.module.DefusedXMLParser
Expand Down Expand Up @@ -227,6 +232,15 @@ def test_orig_parseerror(self):
self.assertIsInstance(e, orig_elementtree.ParseError)
self.assertIsInstance(e, self.module.ParseError)

def test_etree_element(self):
tree = tree = self.module.parse(self.xml_simple)
root = tree.getroot()
root.append(orig_elementtree.Element("module-import"))
root.append(ElementBeforeImport("before-import"))
root.append(ElementAfterImport("after-import"))
s = orig_elementtree.tostring(root)
self.assertEqual(s.count(b"import"), 3, s)


class TestDefusedcElementTree(TestDefusedElementTree):
module = cElementTree
Expand Down Expand Up @@ -293,16 +307,16 @@ class TestDefusedSax(BaseTests):
dtd_external_ref = True

def parse(self, xmlfile, **kwargs):
result = io.StringIO()
handler = XMLGenerator(result)
self.module.parse(xmlfile, handler, **kwargs)
return result.getvalue()
with io.StringIO() as result:
handler = XMLGenerator(result)
self.module.parse(xmlfile, handler, **kwargs)
return result.getvalue()

def parseString(self, xmlstring, **kwargs):
result = io.StringIO()
handler = XMLGenerator(result)
self.module.parseString(xmlstring, handler, **kwargs)
return result.getvalue()
with io.StringIO() as result:
handler = XMLGenerator(result)
self.module.parseString(xmlstring, handler, **kwargs)
return result.getvalue()

def test_exceptions(self):
with self.assertRaises(EntitiesForbidden) as ctx:
Expand All @@ -327,6 +341,7 @@ def test_exceptions(self):
self.assertEqual(repr(ctx.exception), msg)


@unittest.skipUnless(lxml is not None, "test requires lxml")
class TestDefusedLxml(BaseTests):
module = lxml

Expand Down Expand Up @@ -488,9 +503,11 @@ def test_monkeypatch(self):
xmlrpc.unmonkey_patch()


@unittest.skipUnless(gzip is not None, "test requires gzip")
class TestDefusedGzip(DefusedTestCase):
def get_gzipped(self, length):
f = io.BytesIO()
self.addCleanup(f.close)
gzf = gzip.GzipFile(mode="wb", fileobj=f)
gzf.write(b"d" * length)
gzf.close()
Expand Down Expand Up @@ -542,17 +559,19 @@ def test_defused_gzip_response(self):

def test_main():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(TestDefusedcElementTree))
suite.addTests(unittest.makeSuite(TestDefusedElementTree))
suite.addTests(unittest.makeSuite(TestDefusedMinidom))
suite.addTests(unittest.makeSuite(TestDefusedMinidomWithParser))
suite.addTests(unittest.makeSuite(TestDefusedPulldom))
suite.addTests(unittest.makeSuite(TestDefusedSax))
suite.addTests(unittest.makeSuite(TestXmlRpc))
if lxml is not None:
suite.addTests(unittest.makeSuite(TestDefusedLxml))
if gzip is not None:
suite.addTests(unittest.makeSuite(TestDefusedGzip))
cls = [
TestDefusedElementTree,
TestDefusedcElementTree,
TestDefusedMinidom,
TestDefusedMinidomWithParser,
TestDefusedPulldom,
TestDefusedSax,
TestDefusedLxml,
TestXmlRpc,
TestDefusedGzip,
]
for c in cls:
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(c))
return suite


Expand Down

0 comments on commit 4e6cea5

Please sign in to comment.