mirror of
https://github.com/openSUSE/osc.git
synced 2025-08-06 23:53:39 +02:00
Merge branch 'tests_python3_fixes' of https://github.com/marcus-h/osc
Make the testsuite work with python3.8
This commit is contained in:
@@ -13,9 +13,6 @@ except ImportError:
|
||||
from xml.etree import ElementTree as ET
|
||||
EXPECTED_REQUESTS = []
|
||||
|
||||
if sys.version_info[0:2] in ((2, 6), (2, 7)):
|
||||
bytes = lambda x, *args: x
|
||||
|
||||
try:
|
||||
#python 2.x
|
||||
from cStringIO import StringIO
|
||||
@@ -51,6 +48,30 @@ def urlcompare(url, *args):
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def xml_equal(actual, exp):
|
||||
try:
|
||||
actual_xml = ET.fromstring(actual)
|
||||
exp_xml = ET.fromstring(exp)
|
||||
except ET.ParseError:
|
||||
return False
|
||||
todo = [(actual_xml, exp_xml)]
|
||||
while todo:
|
||||
actual_xml, exp_xml = todo.pop(0)
|
||||
if actual_xml.tag != exp_xml.tag:
|
||||
return False
|
||||
if actual_xml.attrib != exp_xml.attrib:
|
||||
return False
|
||||
if actual_xml.text != exp_xml.text:
|
||||
return False
|
||||
if actual_xml.tail != exp_xml.tail:
|
||||
return False
|
||||
if len(actual_xml) != len(exp_xml):
|
||||
return False
|
||||
todo.extend(list(zip(actual_xml, exp_xml)))
|
||||
return True
|
||||
|
||||
|
||||
class RequestWrongOrder(Exception):
|
||||
"""raised if an unexpected request is issued to urllib2"""
|
||||
def __init__(self, url, exp_url, method, exp_method):
|
||||
@@ -96,15 +117,24 @@ class MyHTTPHandler(HTTPHandler):
|
||||
if exp is not None and 'expfile' in kwargs:
|
||||
raise RuntimeError('either specify exp or expfile')
|
||||
elif 'expfile' in kwargs:
|
||||
exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'r').read()
|
||||
exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'rb').read()
|
||||
elif exp is None:
|
||||
raise RuntimeError('exp or expfile required')
|
||||
if exp is not None:
|
||||
# use req.data instead of req.get_data() for python3 compatiblity
|
||||
data = req.data
|
||||
if hasattr(data, 'read'):
|
||||
data = data.read()
|
||||
if data != bytes(exp, "utf-8"):
|
||||
else:
|
||||
# for now, assume exp is a str
|
||||
exp = exp.encode('utf-8')
|
||||
# use req.data instead of req.get_data() for python3 compatiblity
|
||||
data = req.data
|
||||
if hasattr(data, 'read'):
|
||||
data = data.read()
|
||||
if data != exp:
|
||||
# We do not have a notion to explicitly mark xml content. In case
|
||||
# of xml, we do not care about the exact xml representation (for
|
||||
# now). Hence, if both, data and exp, are xml and are "equal",
|
||||
# everything is fine (for now); otherwise, error out
|
||||
# (of course, this is problematic if we want to ensure that XML
|
||||
# documents are bit identical...)
|
||||
if not xml_equal(data, exp):
|
||||
raise RequestDataMismatch(req.get_full_url(), repr(data), repr(exp))
|
||||
return self.__get_response(req.get_full_url(), **kwargs)
|
||||
|
||||
@@ -115,7 +145,7 @@ class MyHTTPHandler(HTTPHandler):
|
||||
if 'text' not in kwargs and 'file' in kwargs:
|
||||
f = BytesIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'rb').read())
|
||||
elif 'text' in kwargs and 'file' not in kwargs:
|
||||
f = BytesIO(bytes(kwargs['text'], 'utf-8'))
|
||||
f = BytesIO(kwargs['text'].encode('utf-8'))
|
||||
else:
|
||||
raise RuntimeError('either specify text or file')
|
||||
resp = addinfourl(f, {}, url)
|
||||
@@ -199,14 +229,27 @@ class OscTestCase(unittest.TestCase):
|
||||
|
||||
def _check_digests(self, fname, *skipfiles):
|
||||
fname = os.path.join(self._get_fixtures_dir(), fname)
|
||||
self.assertEqual(open(os.path.join('.osc', '_files'), 'r').read(), open(fname, 'r').read())
|
||||
root = ET.parse(fname).getroot()
|
||||
with open(os.path.join('.osc', '_files'), 'r') as f:
|
||||
files_act = f.read()
|
||||
with open(fname, 'r') as f:
|
||||
files_exp = f.read()
|
||||
self.assertXMLEqual(files_act, files_exp)
|
||||
root = ET.fromstring(files_act)
|
||||
for i in root.findall('entry'):
|
||||
if i.get('name') in skipfiles:
|
||||
continue
|
||||
self.assertTrue(os.path.exists(os.path.join('.osc', i.get('name'))))
|
||||
self.assertEqual(osc.core.dgst(os.path.join('.osc', i.get('name'))), i.get('md5'))
|
||||
|
||||
def assertXMLEqual(self, act, exp):
|
||||
if xml_equal(act, exp):
|
||||
return
|
||||
# ok, xmls are different, hence, assertEqual is expected to fail
|
||||
# (we just use it in order to get a "nice" error message)
|
||||
self.assertEqual(act, exp)
|
||||
# not reached (unless assertEqual is overridden in an incompatible way)
|
||||
raise RuntimeError('assertEqual assumptions violated')
|
||||
|
||||
def assertEqualMultiline(self, got, exp):
|
||||
if (got + exp).find('\n') == -1:
|
||||
self.assertEqual(got, exp)
|
||||
|
@@ -46,7 +46,7 @@ class TestRequest(OscTestCase):
|
||||
<target package="bar" project="foobar" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_createsr_with_option(self):
|
||||
"""create a submitrequest with option"""
|
||||
@@ -75,7 +75,7 @@ class TestRequest(OscTestCase):
|
||||
</options>
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_createsr_missing_tgt_package(self):
|
||||
"""create a submitrequest with missing target package"""
|
||||
@@ -96,7 +96,7 @@ class TestRequest(OscTestCase):
|
||||
<target project="foobar" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_createsr_invalid_argument(self):
|
||||
"""create a submitrequest with invalid action argument"""
|
||||
@@ -125,7 +125,7 @@ class TestRequest(OscTestCase):
|
||||
<person name="user" role="reader" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_add_role_group(self):
|
||||
"""create an add_role request (group element)"""
|
||||
@@ -146,7 +146,7 @@ class TestRequest(OscTestCase):
|
||||
<group name="group" role="reviewer" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_add_role_person_group(self):
|
||||
"""create an add_role request (person+group element)"""
|
||||
@@ -169,7 +169,7 @@ class TestRequest(OscTestCase):
|
||||
<group name="group" role="reviewer" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_set_bugowner_project(self):
|
||||
"""create a set_bugowner request for a project"""
|
||||
@@ -187,7 +187,7 @@ class TestRequest(OscTestCase):
|
||||
<person name="buguser" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_set_bugowner_package(self):
|
||||
"""create a set_bugowner request for a package"""
|
||||
@@ -205,7 +205,7 @@ class TestRequest(OscTestCase):
|
||||
<person name="buguser" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_delete_project(self):
|
||||
"""create a delete request for a project"""
|
||||
@@ -221,7 +221,7 @@ class TestRequest(OscTestCase):
|
||||
<target project="foo" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_delete_package(self):
|
||||
"""create a delete request for a package"""
|
||||
@@ -237,7 +237,7 @@ class TestRequest(OscTestCase):
|
||||
<target package="deleteme" project="foo" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_create_change_devel(self):
|
||||
"""create a change devel request"""
|
||||
@@ -256,7 +256,7 @@ class TestRequest(OscTestCase):
|
||||
<target package="devpkg" project="devprj" />
|
||||
</action>
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_action_from_xml1(self):
|
||||
"""create action from xml"""
|
||||
@@ -273,7 +273,7 @@ class TestRequest(OscTestCase):
|
||||
self.assertEqual(action.person_role, 'reader')
|
||||
self.assertEqual(action.group_name, 'group')
|
||||
self.assertEqual(action.group_role, 'reviewer')
|
||||
self.assertEqual(xml, action.to_str())
|
||||
self.assertXMLEqual(xml, action.to_str())
|
||||
|
||||
def test_action_from_xml2(self):
|
||||
"""create action from xml"""
|
||||
@@ -294,7 +294,7 @@ class TestRequest(OscTestCase):
|
||||
self.assertEqual(action.opt_sourceupdate, 'cleanup')
|
||||
self.assertEqual(action.opt_updatelink, '1')
|
||||
self.assertTrue(action.src_rev is None)
|
||||
self.assertEqual(xml, action.to_str())
|
||||
self.assertXMLEqual(xml, action.to_str())
|
||||
|
||||
def test_action_from_xml3(self):
|
||||
"""create action from xml (with acceptinfo element)"""
|
||||
@@ -317,7 +317,7 @@ class TestRequest(OscTestCase):
|
||||
self.assertEqual(action.acceptinfo_xsrcmd5, 'ffffffffffffffffffffffffffffffff')
|
||||
self.assertTrue(action.acceptinfo_osrcmd5 is None)
|
||||
self.assertTrue(action.acceptinfo_oxsrcmd5 is None)
|
||||
self.assertEqual(xml, action.to_str())
|
||||
self.assertXMLEqual(xml, action.to_str())
|
||||
|
||||
def test_action_from_xml_unknown_type(self):
|
||||
"""try to create action from xml with unknown type"""
|
||||
@@ -353,7 +353,7 @@ class TestRequest(OscTestCase):
|
||||
self.assertEqual(r.description, 'this is a\nvery long\ndescription')
|
||||
self.assertTrue(len(r.statehistory) == 1)
|
||||
self.assertTrue(len(r.reviews) == 0)
|
||||
self.assertEqual(xml, r.to_str())
|
||||
self.assertXMLEqual(xml, r.to_str())
|
||||
|
||||
def test_read_request2(self):
|
||||
"""read in a request (with reviews)"""
|
||||
@@ -391,7 +391,7 @@ class TestRequest(OscTestCase):
|
||||
self.assertEqual(r.creator, 'creator')
|
||||
self.assertTrue(len(r.statehistory) == 1)
|
||||
self.assertTrue(len(r.reviews) == 1)
|
||||
self.assertEqual(xml, r.to_str())
|
||||
self.assertXMLEqual(xml, r.to_str())
|
||||
|
||||
def test_read_request3(self):
|
||||
"""read in a request (with an "empty" comment+description)"""
|
||||
@@ -427,7 +427,7 @@ class TestRequest(OscTestCase):
|
||||
<state name="new" when="2010-12-28T12:36:29" who="xyz" />
|
||||
</request>"""
|
||||
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_request_list_view1(self):
|
||||
"""test the list_view method"""
|
||||
@@ -555,7 +555,7 @@ Comment: <no comment>"""
|
||||
</action>
|
||||
<state name="new" when="2010-12-30T02:11:22" who="olduser" />
|
||||
</request>"""
|
||||
self.assertEqual(exp, r.to_str())
|
||||
self.assertXMLEqual(exp, r.to_str())
|
||||
|
||||
def test_get_actions(self):
|
||||
"""test get_actions method"""
|
||||
|
Reference in New Issue
Block a user