1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-08-27 16:48:54 +02: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

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