1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-02-23 10:42:14 +01:00

python3 compatibility: fix all tests

This commit is contained in:
Michal Vyskocil 2013-04-09 14:22:45 +02:00 committed by Adrian Schröter
parent 419367fca3
commit f0186dbde8
5 changed files with 37 additions and 24 deletions

View File

@ -7412,7 +7412,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
entries.update(entries_new) entries.update(entries_new)
for name in sorted(entries.keys()): for name in sorted(entries.keys()):
if name.startswith('_service:') or name.startswith('_service_'): if name.startswith('_service:') or name.startswith('_service_'):
continue continue
md5_old = entries_old.get(name, '') md5_old = entries_old.get(name, '')
md5_new = entries_new.get(name, '') md5_new = entries_new.get(name, '')
md5_oldpatched = entries_oldpatched.get(name, '') md5_oldpatched = entries_oldpatched.get(name, '')

View File

@ -1,7 +1,5 @@
import unittest import unittest
import urllib2
import osc.core import osc.core
import StringIO
import shutil import shutil
import tempfile import tempfile
import os import os
@ -9,6 +7,13 @@ import sys
from xml.etree import cElementTree as ET from xml.etree import cElementTree as ET
EXPECTED_REQUESTS = [] EXPECTED_REQUESTS = []
try:
from cStringIO import StringIO
from urllib2 import HTTPHandler, addinfourl, build_opener
except ImportError:
from io import StringIO
from urllib.request import HTTPHandler, addinfourl, build_opener
class RequestWrongOrder(Exception): class RequestWrongOrder(Exception):
"""raised if an unexpected request is issued to urllib2""" """raised if an unexpected request is issued to urllib2"""
def __init__(self, url, exp_url, method, exp_method): def __init__(self, url, exp_url, method, exp_method):
@ -31,9 +36,9 @@ class RequestDataMismatch(Exception):
def __str__(self): def __str__(self):
return '%s, %s, %s' % (self.url, self.got, self.exp) return '%s, %s, %s' % (self.url, self.got, self.exp)
class MyHTTPHandler(urllib2.HTTPHandler): class MyHTTPHandler(HTTPHandler):
def __init__(self, exp_requests, fixtures_dir): def __init__(self, exp_requests, fixtures_dir):
urllib2.HTTPHandler.__init__(self) HTTPHandler.__init__(self)
self.__exp_requests = exp_requests self.__exp_requests = exp_requests
self.__fixtures_dir = fixtures_dir self.__fixtures_dir = fixtures_dir
@ -51,9 +56,9 @@ class MyHTTPHandler(urllib2.HTTPHandler):
def __mock_PUT(self, req, **kwargs): def __mock_PUT(self, req, **kwargs):
exp = kwargs.get('exp', None) exp = kwargs.get('exp', None)
if exp is not None and kwargs.has_key('expfile'): if exp is not None and 'expfile' in kwargs:
raise RuntimeError('either specify exp or expfile') raise RuntimeError('either specify exp or expfile')
elif kwargs.has_key('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']), 'r').read()
elif exp is None: elif exp is None:
raise RuntimeError('exp or expfile required') raise RuntimeError('exp or expfile required')
@ -64,15 +69,15 @@ class MyHTTPHandler(urllib2.HTTPHandler):
def __get_response(self, url, **kwargs): def __get_response(self, url, **kwargs):
f = None f = None
if kwargs.has_key('exception'): if 'exception' in kwargs:
raise kwargs['exception'] raise kwargs['exception']
if not kwargs.has_key('text') and kwargs.has_key('file'): if 'text' not in kwargs and 'file' in kwargs:
f = StringIO.StringIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'r').read()) f = StringIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'r').read())
elif kwargs.has_key('text') and not kwargs.has_key('file'): elif 'text' in kwargs and 'file' not in kwargs:
f = StringIO.StringIO(kwargs['text']) f = StringIO(kwargs['text'])
else: else:
raise RuntimeError('either specify text or file') raise RuntimeError('either specify text or file')
resp = urllib2.addinfourl(f, {}, url) resp = addinfourl(f, {}, url)
resp.code = kwargs.get('code', 200) resp.code = kwargs.get('code', 200)
resp.msg = '' resp.msg = ''
return resp return resp
@ -116,9 +121,9 @@ class OscTestCase(unittest.TestCase):
shutil.copytree(os.path.join(self._get_fixtures_dir(), 'osctest'), os.path.join(self.tmpdir, 'osctest')) shutil.copytree(os.path.join(self._get_fixtures_dir(), 'osctest'), os.path.join(self.tmpdir, 'osctest'))
global EXPECTED_REQUESTS global EXPECTED_REQUESTS
EXPECTED_REQUESTS = [] EXPECTED_REQUESTS = []
osc.core.conf._build_opener = lambda u: urllib2.build_opener(MyHTTPHandler(EXPECTED_REQUESTS, self._get_fixtures_dir())) osc.core.conf._build_opener = lambda u: build_opener(MyHTTPHandler(EXPECTED_REQUESTS, self._get_fixtures_dir()))
self.stdout = sys.stdout self.stdout = sys.stdout
sys.stdout = StringIO.StringIO() sys.stdout = StringIO()
def tearDown(self): def tearDown(self):
self.assertTrue(len(EXPECTED_REQUESTS) == 0) self.assertTrue(len(EXPECTED_REQUESTS) == 0)

View File

@ -4,7 +4,6 @@ import osc.oscerr
import os import os
import re import re
import sys import sys
import urllib2
from common import GET, POST, OscTestCase, addExpectedRequest, EXPECTED_REQUESTS from common import GET, POST, OscTestCase, addExpectedRequest, EXPECTED_REQUESTS

View File

@ -202,11 +202,14 @@ class TestRepairWC(OscTestCase):
def test_invalidapiurl_param(self): def test_invalidapiurl_param(self):
"""pass an invalid apiurl to wc_repair""" """pass an invalid apiurl to wc_repair"""
import urllib2 try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError
self._change_to_pkg('invalid_apiurl') self._change_to_pkg('invalid_apiurl')
p = osc.core.Package('.', wc_check=False) p = osc.core.Package('.', wc_check=False)
self.assertRaises(urllib2.URLError, p.wc_repair, 'http:/localhost') self.assertRaises(URLError, p.wc_repair, 'http:/localhost')
self.assertRaises(urllib2.URLError, p.wc_repair, 'invalid') self.assertRaises(URLError, p.wc_repair, 'invalid')
def test_noapiurlNotExistingApiurl(self): def test_noapiurlNotExistingApiurl(self):
"""the package wc has no _apiurl file and no apiurl is passed to repairwc""" """the package wc has no _apiurl file and no apiurl is passed to repairwc"""
@ -245,14 +248,17 @@ class TestRepairWC(OscTestCase):
def test_project_invalidapiurl_param(self): def test_project_invalidapiurl_param(self):
"""pass an invalid apiurl to wc_repair""" """pass an invalid apiurl to wc_repair"""
import shutil import shutil
import urllib2 try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError
prj_dir = os.path.join(self.tmpdir, 'prj_invalidapiurl') prj_dir = os.path.join(self.tmpdir, 'prj_invalidapiurl')
shutil.copytree(os.path.join(self._get_fixtures_dir(), 'prj_invalidapiurl'), prj_dir) shutil.copytree(os.path.join(self._get_fixtures_dir(), 'prj_invalidapiurl'), prj_dir)
storedir = os.path.join(prj_dir, osc.core.store) storedir = os.path.join(prj_dir, osc.core.store)
self.assertRaises(osc.oscerr.WorkingCopyInconsistent, osc.core.Project, prj_dir, getPackageList=False) self.assertRaises(osc.oscerr.WorkingCopyInconsistent, osc.core.Project, prj_dir, getPackageList=False)
prj = osc.core.Project(prj_dir, wc_check=False, getPackageList=False) prj = osc.core.Project(prj_dir, wc_check=False, getPackageList=False)
self.assertRaises(urllib2.URLError, prj.wc_repair, 'http:/localhost') self.assertRaises(URLError, prj.wc_repair, 'http:/localhost')
self.assertRaises(urllib2.URLError, prj.wc_repair, 'invalid') self.assertRaises(URLError, prj.wc_repair, 'invalid')
if __name__ == '__main__': if __name__ == '__main__':
import unittest import unittest

View File

@ -50,9 +50,12 @@ class TestSetLinkRev(OscTestCase):
@GET('http://localhost/source/srcprj/srcpkg?rev=latest&expand=1', text='conflict in file merge', code=404) @GET('http://localhost/source/srcprj/srcpkg?rev=latest&expand=1', text='conflict in file merge', code=404)
def test_linkerror(self): def test_linkerror(self):
"""link is broken""" """link is broken"""
import urllib2 try:
from urllib.error import HTTPError
except ImportError:
from urllib2 import HTTPError
# the backend returns status 404 if we try to expand a broken _link # the backend returns status 404 if we try to expand a broken _link
self.assertRaises(urllib2.HTTPError, osc.core.set_link_rev, 'http://localhost', 'osctest', 'simple', expand=True) self.assertRaises(HTTPError, osc.core.set_link_rev, 'http://localhost', 'osctest', 'simple', expand=True)
@GET('http://localhost/source/osctest/simple/_link', file='rev_link') @GET('http://localhost/source/osctest/simple/_link', file='rev_link')
@PUT('http://localhost/source/osctest/simple/_link', @PUT('http://localhost/source/osctest/simple/_link',