commit 8560a386c3ea1e868a8e294c1e318a6ee5319580 Author: Tomas Radej Date: Wed Aug 20 13:32:32 2014 +0200 Ported test suite diff --git a/test/grabberperf.py b/test/grabberperf.py index 820da2c..d9142fa 100644 --- a/test/grabberperf.py +++ b/test/grabberperf.py @@ -21,11 +21,15 @@ import sys import os -from os.path import dirname, join as joinpath import tempfile import time +import six + +# Hack for Python 3 +sys.path.insert(0, os.path.expandvars(os.path.abspath('..'))) -import urlgrabber.grabber as grabber +from os.path import dirname, join as joinpath +from urlgrabber import grabber from urlgrabber.grabber import URLGrabber, urlgrab, urlopen, urlread from urlgrabber.progress import text_progress_meter @@ -48,7 +52,7 @@ def main(): os.unlink(tempdst) def setuptemp(size): - if DEBUG: print 'writing %d KB to temporary file (%s).' % (size / 1024, tempsrc) + if DEBUG: print('writing %d KB to temporary file (%s).' % (size / 1024, tempsrc)) file = open(tempsrc, 'w', 1024) chars = '0123456789' for i in range(size): @@ -65,9 +69,9 @@ def speedtest(size): try: from urlgrabber.progress import text_progress_meter - except ImportError, e: + except ImportError as e: tpm = None - print 'not using progress meter' + print('not using progress meter') else: tpm = text_progress_meter(fo=open('/dev/null', 'w')) @@ -83,15 +87,15 @@ def speedtest(size): # module. # get it nicely cached before we start comparing - if DEBUG: print 'pre-caching' + if DEBUG: print('pre-caching') for i in range(100): urlgrab(tempsrc, tempdst, copy_local=1, throttle=None, proxies=proxies) - if DEBUG: print 'running speed test.' + if DEBUG: print('running speed test.') reps = 500 for i in range(reps): if DEBUG: - print '\r%4i/%-4i' % (i+1, reps), + six.print_('\r%4i/%-4i' % (i+1, reps), end=' ') sys.stdout.flush() t = time.time() urlgrab(tempsrc, tempdst, @@ -111,14 +115,14 @@ def speedtest(size): while 1: s = in_fo.read(1024 * 8) if not s: break - out_fo.write(s) + out_fo.write(s if not six.PY3 else s.encode('utf-8')) in_fo.close() out_fo.close() none_times.append(1000 * (time.time() - t)) - if DEBUG: print '\r' + if DEBUG: print('\r') - print "%d KB Results:" % (size / 1024) + print("%d KB Results:" % (size / 1024)) print_result('full', full_times) print_result('raw', raw_times) print_result('none', none_times) @@ -131,7 +135,7 @@ def print_result(label, result_list): for i in result_list: mean += i mean = mean/len(result_list) median = result_list[int(len(result_list)/2)] - print format % (label, mean, median, result_list[0], result_list[-1]) + print(format % (label, mean, median, result_list[0], result_list[-1])) if __name__ == '__main__': main() diff --git a/test/munittest.py b/test/munittest.py index 16a61ae..7e7969e 100644 --- a/test/munittest.py +++ b/test/munittest.py @@ -103,9 +103,9 @@ SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. import time import sys import traceback -import string import os import types +import collections ############################################################################## # Exported classes and functions @@ -190,7 +190,7 @@ class TestResult: def _exc_info_to_string(self, err): """Converts a sys.exc_info()-style tuple of values into a string.""" - return string.join(traceback.format_exception(*err), '') + return ''.join(traceback.format_exception(*err)) def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ @@ -251,8 +251,8 @@ class TestCase: testMethod = getattr(self, methodName) self._testMethodDoc = testMethod.__doc__ except AttributeError: - raise ValueError, "no such test method in %s: %s" % \ - (self.__class__, methodName) + raise ValueError("no such test method in %s: %s" % \ + (self.__class__, methodName)) def setUp(self): "Hook method for setting up the test fixture before exercising it." @@ -276,7 +276,7 @@ class TestCase: the specified test method's docstring. """ doc = self._testMethodDoc - return doc and string.strip(string.split(doc, "\n")[0]) or None + return doc and doc.split("\n")[0].strip() or None def id(self): return "%s.%s" % (_strclass(self.__class__), self._testMethodName) @@ -361,15 +361,15 @@ class TestCase: def fail(self, msg=None): """Fail immediately, with the given message.""" - raise self.failureException, msg + raise self.failureException(msg) def failIf(self, expr, msg=None): "Fail the test if the expression is true." - if expr: raise self.failureException, msg + if expr: raise self.failureException(msg) def failUnless(self, expr, msg=None): """Fail the test unless the expression is true.""" - if not expr: raise self.failureException, msg + if not expr: raise self.failureException(msg) def failUnlessRaises(self, excClass, callableObj, *args, **kwargs): """Fail unless an exception of class excClass is thrown @@ -386,23 +386,21 @@ class TestCase: else: if hasattr(excClass,'__name__'): excName = excClass.__name__ else: excName = str(excClass) - raise self.failureException, excName + raise self.failureException(excName) def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' operator. """ if not first == second: - raise self.failureException, \ - (msg or '%s != %s' % (`first`, `second`)) + raise self.failureException(msg or '%s != %s' % (repr(first), repr(second))) def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. """ if first == second: - raise self.failureException, \ - (msg or '%s == %s' % (`first`, `second`)) + raise self.failureException(msg or '%s == %s' % (repr(first), repr(second))) def failUnlessAlmostEqual(self, first, second, places=7, msg=None): """Fail if the two objects are unequal as determined by their @@ -413,8 +411,7 @@ class TestCase: as significant digits (measured from the most significant digit). """ if round(second-first, places) != 0: - raise self.failureException, \ - (msg or '%s != %s within %s places' % (`first`, `second`, `places` )) + raise self.failureException(msg or '%s != %s within %s places' % (repr(first), repr(second), repr(places) )) def failIfAlmostEqual(self, first, second, places=7, msg=None): """Fail if the two objects are equal as determined by their @@ -425,8 +422,7 @@ class TestCase: as significant digits (measured from the most significant digit). """ if round(second-first, places) == 0: - raise self.failureException, \ - (msg or '%s == %s within %s places' % (`first`, `second`, `places`)) + raise self.failureException(msg or '%s == %s within %s places' % (repr(first), repr(second), repr(places))) assertEqual = assertEquals = failUnlessEqual @@ -442,15 +438,15 @@ class TestCase: def skip(self, msg=None): """Skip the test""" - raise self.skipException, msg + raise self.skipException(msg) def skipIf(self, expr, msg=None): "Skip the test if the expression is true." - if expr: raise self.skipException, msg + if expr: raise self.skipException(msg) def skipUnless(self, expr, msg=None): """Skip the test unless the expression is true.""" - if not expr: raise self.skipException, msg + if not expr: raise self.skipException(msg) @@ -554,7 +550,7 @@ class FunctionTestCase(TestCase): def shortDescription(self): if self._description is not None: return self._description doc = self._testFunc.__doc__ - return doc and string.strip(string.split(doc, "\n")[0]) or None + return doc and doc.split("\n")[0].strip() or None @@ -567,13 +563,12 @@ class TestLoader: criteria and returning them wrapped in a Test """ testMethodPrefix = 'test' - sortTestMethodsUsing = cmp suiteClass = TestSuite def loadTestsFromTestCase(self, testCaseClass): """Return a suite of all tests cases contained in testCaseClass""" name_list = self.getTestCaseNames(testCaseClass) - instance_list = map(testCaseClass, name_list) + instance_list = list(map(testCaseClass, name_list)) description = getattr(testCaseClass, '__doc__') \ or testCaseClass.__name__ description = (description.splitlines()[0]).strip() @@ -585,7 +580,7 @@ class TestLoader: tests = [] for name in dir(module): obj = getattr(module, name) - if (isinstance(obj, (type, types.ClassType)) and + if (isinstance(obj, type) and issubclass(obj, TestCase) and not obj in [TestCase, FunctionTestCase]): tests.append(self.loadTestsFromTestCase(obj)) @@ -603,15 +598,15 @@ class TestLoader: The method optionally resolves the names relative to a given module. """ - parts = string.split(name, '.') + parts = name.split('.') if module is None: if not parts: - raise ValueError, "incomplete test name: %s" % name + raise ValueError("incomplete test name: %s" % name) else: parts_copy = parts[:] while parts_copy: try: - module = __import__(string.join(parts_copy,'.')) + module = __import__('.'.join(parts_copy,)) break except ImportError: del parts_copy[-1] @@ -624,20 +619,19 @@ class TestLoader: import unittest if type(obj) == types.ModuleType: return self.loadTestsFromModule(obj) - elif (isinstance(obj, (type, types.ClassType)) and + elif (isinstance(obj, type) and issubclass(obj, unittest.TestCase)): return self.loadTestsFromTestCase(obj) elif type(obj) == types.UnboundMethodType: - return obj.im_class(obj.__name__) - elif callable(obj): + return obj.__self__.__class__(obj.__name__) + elif isinstance(obj, collections.Callable): test = obj() if not isinstance(test, unittest.TestCase) and \ not isinstance(test, unittest.TestSuite): - raise ValueError, \ - "calling %s returned %s, not a test" % (obj,test) + raise ValueError("calling %s returned %s, not a test" % (obj,test)) return test else: - raise ValueError, "don't know how to make test from: %s" % obj + raise ValueError("don't know how to make test from: %s" % obj) def loadTestsFromNames(self, names, module=None): """Return a suite of all tests cases found using the given sequence @@ -651,14 +645,13 @@ class TestLoader: def getTestCaseNames(self, testCaseClass): """Return a sorted sequence of method names found within testCaseClass """ - testFnNames = filter(lambda n,p=self.testMethodPrefix: n[:len(p)] == p, - dir(testCaseClass)) + testFnNames = list(filter(lambda n,p=self.testMethodPrefix: n[:len(p)] == p, + dir(testCaseClass))) for baseclass in testCaseClass.__bases__: for testFnName in self.getTestCaseNames(baseclass): if testFnName not in testFnNames: # handle overridden methods testFnNames.append(testFnName) - if self.sortTestMethodsUsing: - testFnNames.sort(self.sortTestMethodsUsing) + testFnNames.sort() return testFnNames @@ -670,21 +663,20 @@ defaultTestLoader = TestLoader() # Patches for old functions: these functions should be considered obsolete ############################################################################## -def _makeLoader(prefix, sortUsing, suiteClass=None): +def _makeLoader(prefix, suiteClass=None): loader = TestLoader() - loader.sortTestMethodsUsing = sortUsing loader.testMethodPrefix = prefix if suiteClass: loader.suiteClass = suiteClass return loader -def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): - return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) +def getTestCaseNames(testCaseClass, prefix): + return _makeLoader(prefix).getTestCaseNames(testCaseClass) -def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite): - return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass) +def makeSuite(testCaseClass, prefix='test',suiteClass=TestSuite): + return _makeLoader(prefix, suiteClass).loadTestsFromTestCase(testCaseClass) -def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite): - return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module) +def findTestCases(module, prefix='test',suiteClass=TestSuite): + return _makeLoader(prefix, suiteClass).loadTestsFromModule(module) ############################################################################## @@ -825,8 +817,8 @@ class TextTestRunner: self.stream.writeln() if not result.wasSuccessful(): self.stream.write("FAILED (") - failed, errored, skipped = map(len, \ - (result.failures, result.errors, result.skipped)) + failed, errored, skipped = list(map(len, \ + (result.failures, result.errors, result.skipped))) if failed: self.stream.write("failures=%d" % failed) if errored: @@ -871,7 +863,7 @@ Examples: argv=None, testRunner=None, testLoader=defaultTestLoader): if type(module) == type(''): self.module = __import__(module) - for part in string.split(module,'.')[1:]: + for part in module.split('.')[1:]: self.module = getattr(self.module, part) else: self.module = module @@ -886,8 +878,8 @@ Examples: self.runTests() def usageExit(self, msg=None): - if msg: print msg - print self.USAGE % self.__dict__ + if msg: print(msg) + print(self.USAGE % self.__dict__) sys.exit(2) def parseArgs(self, argv): @@ -910,7 +902,7 @@ Examples: else: self.testNames = (self.defaultTest,) self.createTests() - except getopt.error, msg: + except getopt.error as msg: self.usageExit(msg) def createTests(self): diff --git a/test/runtests.py b/test/runtests.py index c48bd1d..78a5974 100644 --- a/test/runtests.py +++ b/test/runtests.py @@ -54,7 +54,7 @@ def parse_args(): return (descriptions,verbosity) def usage(): - print __doc__ + print(__doc__) if __name__ == '__main__': main() diff --git a/test/test_byterange.py b/test/test_byterange.py index 0f75807..0863be8 100644 --- a/test/test_byterange.py +++ b/test/test_byterange.py @@ -24,8 +24,11 @@ # $Id: test_byterange.py,v 1.6 2004/03/31 17:02:00 mstenner Exp $ import sys +import six -from cStringIO import StringIO +from io import StringIO + +import urlgrabber from urlgrabber.byterange import RangeableFileObject from base_test_code import * @@ -37,7 +40,7 @@ class RangeableFileObjectTestCase(TestCase): # 0 1 2 3 4 5 6 7 8 9 # 0123456789012345678901234567890123456789012345678901234567 890123456789012345678901234567890 self.test = 'Why cannot we write the entire 24 volumes of Encyclopaedia\nBrittanica on the head of a pin?\n' - self.fo = StringIO(self.test) + self.fo = StringIO(unicode(self.test) if not six.PY3 else self.test) self.rfo = RangeableFileObject(self.fo, (20,69)) def tearDown(self): @@ -61,7 +64,8 @@ class RangeableFileObjectTestCase(TestCase): def test_readall(self): """RangeableFileObject.read(): to end of file.""" - rfo = RangeableFileObject(StringIO(self.test),(11,)) + text_compat = unicode(self.test) if not six.PY3 else self.test + rfo = RangeableFileObject(StringIO(text_compat),(11,)) self.assertEquals(self.test[11:],rfo.read()) def test_readline(self): diff --git a/test/test_grabber.py b/test/test_grabber.py index 8e45d25..bd36d66 100644 --- a/test/test_grabber.py +++ b/test/test_grabber.py @@ -24,11 +24,13 @@ # $Id: test_grabber.py,v 1.31 2006/12/08 00:14:16 mstenner Exp $ import sys +import six import os -import string, tempfile, random, cStringIO, os -import urllib2 +import tempfile, random, os +from six.moves import urllib import socket +from io import StringIO from base_test_code import * import urlgrabber @@ -41,12 +43,12 @@ class FileObjectTests(TestCase): def setUp(self): self.filename = tempfile.mktemp() - fo = file(self.filename, 'wb') - fo.write(reference_data) + fo = open(self.filename, 'wb') + fo.write(reference_data.encode('utf-8')) fo.close() - self.fo_input = cStringIO.StringIO(reference_data) - self.fo_output = cStringIO.StringIO() + self.fo_input = StringIO(unicode(reference_data) if not six.PY3 else reference_data) + self.fo_output = StringIO() (url, parts) = grabber.default_grabber.opts.urlparser.parse( self.filename, grabber.default_grabber.opts) self.wrapper = grabber.PyCurlFileObject( @@ -73,7 +75,7 @@ class FileObjectTests(TestCase): def test_readlines(self): "PyCurlFileObject .readlines() method" li = self.wrapper.readlines() - self.fo_output.write(string.join(li, '')) + self.fo_output.write(''.join(li)) self.assert_(reference_data == self.fo_output.getvalue()) def test_smallread(self): @@ -90,7 +92,7 @@ class HTTPTests(TestCase): filename = tempfile.mktemp() grabber.urlgrab(ref_http, filename) - fo = file(filename, 'rb') + fo = open(filename, 'rb' if not six.PY3 else 'r') contents = fo.read() fo.close() @@ -136,7 +138,7 @@ class URLGrabberTestCase(TestCase): def setUp(self): - self.meter = text_progress_meter( fo=cStringIO.StringIO() ) + self.meter = text_progress_meter( fo=StringIO() ) pass def tearDown(self): @@ -149,7 +151,7 @@ class URLGrabberTestCase(TestCase): values into the URLGrabber constructor and checks that they've been set properly. """ - opener = urllib2.OpenerDirector() + opener = urllib.request.OpenerDirector() g = URLGrabber( progress_obj=self.meter, throttle=0.9, bandwidth=20, @@ -225,13 +227,13 @@ class URLParserTestCase(TestCase): self.assertEquals(parts, urllist[2]) else: if url == urllist[1] and parts == urllist[2]: - print 'OK: %s' % urllist[0] + print('OK: %s' % urllist[0]) else: - print 'ERROR: %s' % urllist[0] - print ' ' + urllist[1] - print ' ' + url - print ' ' + urllist[2] - print ' ' + parts + print('ERROR: %s' % urllist[0]) + print(' ' + urllist[1]) + print(' ' + url) + print(' ' + urllist[2]) + print(' ' + parts) url_tests_all = ( @@ -380,7 +382,7 @@ class CheckfuncTestCase(TestCase): if hasattr(obj, 'filename'): # we used urlgrab - fo = file(obj.filename) + fo = open(obj.filename) data = fo.read() fo.close() else: @@ -447,12 +449,12 @@ class RegetTestBase: except: pass def _make_half_zero_file(self): - fo = file(self.filename, 'wb') - fo.write('0'*self.hl) + fo = open(self.filename, 'wb' if not six.PY3 else 'w') + fo.write('0'*int(self.hl)) fo.close() def _read_file(self): - fo = file(self.filename, 'rb') + fo = open(self.filename, 'rb' if not six.PY3 else 'r') data = fo.read() fo.close() return data @@ -470,7 +472,7 @@ class FTPRegetTests(RegetTestBase, TestCase): # this tests to see if the server is available. If it's not, # then these tests will be skipped try: - fo = urllib2.urlopen(self.url).close() + fo = urllib.request.urlopen(self.url).close() except IOError: self.skip() @@ -480,8 +482,8 @@ class FTPRegetTests(RegetTestBase, TestCase): self.grabber.urlgrab(self.url, self.filename, reget='simple') data = self._read_file() - self.assertEquals(data[:self.hl], '0'*self.hl) - self.assertEquals(data[self.hl:], self.ref[self.hl:]) + self.assertEquals(data[:int(self.hl)], '0'*int(self.hl)) + self.assertEquals(data[int(self.hl):], self.ref[int(self.hl):]) class HTTPRegetTests(FTPRegetTests): def setUp(self): @@ -498,8 +500,8 @@ class HTTPRegetTests(FTPRegetTests): self.grabber.urlgrab(self.url, self.filename, reget='check_timestamp') data = self._read_file() - self.assertEquals(data[:self.hl], '0'*self.hl) - self.assertEquals(data[self.hl:], self.ref[self.hl:]) + self.assertEquals(data[:int(self.hl)], '0'*int(self.hl)) + self.assertEquals(data[int(self.hl):], self.ref[int(self.hl):]) except NotImplementedError: self.skip() @@ -521,7 +523,7 @@ class FileRegetTests(HTTPRegetTests): def setUp(self): self.ref = short_reference_data tmp = tempfile.mktemp() - tmpfo = file(tmp, 'wb') + tmpfo = open(tmp, 'wb' if not six.PY3 else 'w') tmpfo.write(self.ref) tmpfo.close() self.tmp = tmp @@ -545,7 +547,7 @@ class ProFTPDSucksTests(TestCase): def setUp(self): self.url = ref_proftp try: - fo = urllib2.urlopen(self.url).close() + fo = urllib.request.urlopen(self.url).close() except IOError: self.skip() @@ -592,7 +594,7 @@ class ProxyFTPAuthTests(ProxyHTTPAuthTests): if not self.have_proxy(): self.skip() try: - fo = urllib2.urlopen(self.url).close() + fo = urllib.request.urlopen(self.url).close() except IOError: self.skip() self.g = URLGrabber() diff --git a/test/test_mirror.py b/test/test_mirror.py index 7f493d0..c46cd33 100644 --- a/test/test_mirror.py +++ b/test/test_mirror.py @@ -24,8 +24,9 @@ # $Id: test_mirror.py,v 1.12 2005/10/22 21:57:27 mstenner Exp $ import sys +import six import os -import string, tempfile, random, cStringIO, os +import string, tempfile, random, os import urlgrabber.grabber from urlgrabber.grabber import URLGrabber, URLGrabError, URLGrabberOptions @@ -268,7 +269,8 @@ class ActionTests(TestCase): self.assertEquals(self.g.calls, expected_calls) self.assertEquals(urlgrabber.mirror.DEBUG.logs, expected_logs) -import thread, socket +from six.moves import _thread as thread +import socket LOCALPORT = 'localhost', 2000 class HttpReplyCode(TestCase): @@ -282,11 +284,14 @@ class HttpReplyCode(TestCase): while 1: c, a = s.accept() if self.exit: c.close(); break - while not c.recv(4096).endswith('\r\n\r\n'): pass - c.sendall('HTTP/1.1 %d %s\r\n' % self.reply) + ending_compat = '\r\n\r\n' if not six.PY3 else b'\r\n\r\n' + while not c.recv(4096).endswith(ending_compat): pass + http_compat = 'HTTP/1.1 %d %s\r\n' % self.reply + c.sendall(http_compat if not six.PY3 else http_compat.encode('utf-8')) if self.content is not None: - c.sendall('Content-Length: %d\r\n\r\n' % len(self.content)) - c.sendall(self.content) + cont_length_compat = 'Content-Length: %d\r\n\r\n' % len(self.content) + c.sendall(cont_length_compat if not six.PY3 else cont_length_compat.encode('utf-8')) + c.sendall(self.content if not six.PY3 else self.content.encode('utf-8')) c.close() s.close() self.exit = False