Index: third_party/logilab/common/testlib.py
|
diff --git a/third_party/logilab/common/testlib.py b/third_party/logilab/common/testlib.py
|
index 31efe567bcc80cb48dc1e669eb62b101d05da657..da49387abdce581278caea1171ccfe85de771ce4 100644
|
--- a/third_party/logilab/common/testlib.py
|
+++ b/third_party/logilab/common/testlib.py
|
@@ -1,5 +1,5 @@
|
# -*- coding: utf-8 -*-
|
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
|
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
|
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
|
#
|
# This file is part of logilab-common.
|
@@ -36,9 +36,6 @@ If no non-option arguments are present, prefixes used are 'test',
|
'regrtest', 'smoketest' and 'unittest'.
|
|
"""
|
-
|
-from __future__ import print_function
|
-
|
__docformat__ = "restructuredtext en"
|
# modified copy of some functions from test/regrtest.py from PyXml
|
# disable camel case warning
|
@@ -55,13 +52,9 @@ import math
|
import warnings
|
from shutil import rmtree
|
from operator import itemgetter
|
-from itertools import dropwhile
|
-from inspect import isgeneratorfunction
|
-
|
-from six import string_types
|
-from six.moves import builtins, range, configparser, input
|
-
|
+from ConfigParser import ConfigParser
|
from logilab.common.deprecation import deprecated
|
+from itertools import dropwhile
|
|
import unittest as unittest_legacy
|
if not getattr(unittest_legacy, "__package__", None):
|
@@ -69,13 +62,31 @@ if not getattr(unittest_legacy, "__package__", None):
|
import unittest2 as unittest
|
from unittest2 import SkipTest
|
except ImportError:
|
- raise ImportError("You have to install python-unittest2 to use %s" % __name__)
|
+ sys.exit("You have to install python-unittest2 to use this module")
|
else:
|
import unittest
|
from unittest import SkipTest
|
|
-from functools import wraps
|
-
|
+try:
|
+ from functools import wraps
|
+except ImportError:
|
+ def wraps(wrapped):
|
+ def proxy(callable):
|
+ callable.__name__ = wrapped.__name__
|
+ return callable
|
+ return proxy
|
+try:
|
+ from test import test_support
|
+except ImportError:
|
+ # not always available
|
+ class TestSupport:
|
+ def unload(self, test):
|
+ pass
|
+ test_support = TestSupport()
|
+
|
+# pylint: disable=W0622
|
+from logilab.common.compat import any, InheritableSet, callable
|
+# pylint: enable=W0622
|
from logilab.common.debugger import Debugger, colorize_source
|
from logilab.common.decorators import cached, classproperty
|
from logilab.common import textutils
|
@@ -86,7 +97,23 @@ __all__ = ['main', 'unittest_main', 'find_tests', 'run_test', 'spawn']
|
DEFAULT_PREFIXES = ('test', 'regrtest', 'smoketest', 'unittest',
|
'func', 'validation')
|
|
-is_generator = deprecated('[lgc 0.63] use inspect.isgeneratorfunction')(isgeneratorfunction)
|
+
|
+if sys.version_info >= (2, 6):
|
+ # FIXME : this does not work as expected / breaks tests on testlib
|
+ # however testlib does not work on py3k for many reasons ...
|
+ from inspect import CO_GENERATOR
|
+else:
|
+ from compiler.consts import CO_GENERATOR
|
+
|
+if sys.version_info >= (3, 0):
|
+ def is_generator(function):
|
+ flags = function.__code__.co_flags
|
+ return flags & CO_GENERATOR
|
+
|
+else:
|
+ def is_generator(function):
|
+ flags = function.func_code.co_flags
|
+ return flags & CO_GENERATOR
|
|
# used by unittest to count the number of relevant levels in the traceback
|
__unittest = 1
|
@@ -95,21 +122,6 @@ __unittest = 1
|
def with_tempdir(callable):
|
"""A decorator ensuring no temporary file left when the function return
|
Work only for temporary file create with the tempfile module"""
|
- if isgeneratorfunction(callable):
|
- def proxy(*args, **kwargs):
|
- old_tmpdir = tempfile.gettempdir()
|
- new_tmpdir = tempfile.mkdtemp(prefix="temp-lgc-")
|
- tempfile.tempdir = new_tmpdir
|
- try:
|
- for x in callable(*args, **kwargs):
|
- yield x
|
- finally:
|
- try:
|
- rmtree(new_tmpdir, ignore_errors=True)
|
- finally:
|
- tempfile.tempdir = old_tmpdir
|
- return proxy
|
-
|
@wraps(callable)
|
def proxy(*args, **kargs):
|
|
@@ -178,27 +190,27 @@ def start_interactive_mode(result):
|
else:
|
while True:
|
testindex = 0
|
- print("Choose a test to debug:")
|
+ print "Choose a test to debug:"
|
# order debuggers in the same way than errors were printed
|
- print("\n".join(['\t%s : %s' % (i, descr) for i, (_, descr)
|
- in enumerate(descrs)]))
|
- print("Type 'exit' (or ^D) to quit")
|
- print()
|
+ print "\n".join(['\t%s : %s' % (i, descr) for i, (_, descr)
|
+ in enumerate(descrs)])
|
+ print "Type 'exit' (or ^D) to quit"
|
+ print
|
try:
|
- todebug = input('Enter a test name: ')
|
+ todebug = raw_input('Enter a test name: ')
|
if todebug.strip().lower() == 'exit':
|
- print()
|
+ print
|
break
|
else:
|
try:
|
testindex = int(todebug)
|
debugger = debuggers[descrs[testindex][0]]
|
except (ValueError, IndexError):
|
- print("ERROR: invalid test number %r" % (todebug, ))
|
+ print "ERROR: invalid test number %r" % (todebug, )
|
else:
|
debugger.start()
|
except (EOFError, KeyboardInterrupt):
|
- print()
|
+ print
|
break
|
|
|
@@ -352,7 +364,7 @@ def _handleModuleTearDown(self, result):
|
if tearDownModule is not None:
|
try:
|
tearDownModule()
|
- except Exception as e:
|
+ except Exception, e:
|
if isinstance(result, _DebugResult):
|
raise
|
errorName = 'tearDownModule (%s)' % previousModule
|
@@ -380,7 +392,7 @@ def _handleModuleFixture(self, test, result):
|
if setUpModule is not None:
|
try:
|
setUpModule()
|
- except Exception as e:
|
+ except Exception, e:
|
if isinstance(result, _DebugResult):
|
raise
|
result._moduleSetUpFailed = True
|
@@ -436,7 +448,7 @@ class InnerTest(tuple):
|
instance.name = name
|
return instance
|
|
-class Tags(set):
|
+class Tags(InheritableSet): # 2.4 compat
|
"""A set of tag able validate an expression"""
|
|
def __init__(self, *tags, **kwargs):
|
@@ -444,7 +456,7 @@ class Tags(set):
|
if kwargs:
|
raise TypeError("%s are an invalid keyword argument for this function" % kwargs.keys())
|
|
- if len(tags) == 1 and not isinstance(tags[0], string_types):
|
+ if len(tags) == 1 and not isinstance(tags[0], basestring):
|
tags = tags[0]
|
super(Tags, self).__init__(tags, **kwargs)
|
|
@@ -472,8 +484,14 @@ class TestCase(unittest.TestCase):
|
|
def __init__(self, methodName='runTest'):
|
super(TestCase, self).__init__(methodName)
|
- self.__exc_info = sys.exc_info
|
- self.__testMethodName = self._testMethodName
|
+ # internal API changed in python2.4 and needed by DocTestCase
|
+ if sys.version_info >= (2, 4):
|
+ self.__exc_info = sys.exc_info
|
+ self.__testMethodName = self._testMethodName
|
+ else:
|
+ # let's give easier access to _testMethodName to every subclasses
|
+ if hasattr(self, "__testMethodName"):
|
+ self._testMethodName = self.__testMethodName
|
self._current_test_descr = None
|
self._options_ = None
|
|
@@ -515,14 +533,6 @@ class TestCase(unittest.TestCase):
|
func(*args, **kwargs)
|
except (KeyboardInterrupt, SystemExit):
|
raise
|
- except unittest.SkipTest as e:
|
- if hasattr(result, 'addSkip'):
|
- result.addSkip(self, str(e))
|
- else:
|
- warnings.warn("TestResult has no addSkip method, skips not reported",
|
- RuntimeWarning, 2)
|
- result.addSuccess(self)
|
- return False
|
except:
|
result.addError(self, self.__exc_info())
|
return False
|
@@ -549,23 +559,13 @@ class TestCase(unittest.TestCase):
|
# if result.cvg:
|
# result.cvg.start()
|
testMethod = self._get_test_method()
|
- if (getattr(self.__class__, "__unittest_skip__", False) or
|
- getattr(testMethod, "__unittest_skip__", False)):
|
- # If the class or method was skipped.
|
- try:
|
- skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
|
- or getattr(testMethod, '__unittest_skip_why__', ''))
|
- self._addSkip(result, skip_why)
|
- finally:
|
- result.stopTest(self)
|
- return
|
if runcondition and not runcondition(testMethod):
|
return # test is skipped
|
result.startTest(self)
|
try:
|
if not self.quiet_run(result, self.setUp):
|
return
|
- generative = isgeneratorfunction(testMethod)
|
+ generative = is_generator(testMethod.im_func)
|
# generative tests
|
if generative:
|
self._proceed_generative(result, testMethod,
|
@@ -587,11 +587,10 @@ class TestCase(unittest.TestCase):
|
restartfile.write(descr+os.linesep)
|
finally:
|
restartfile.close()
|
- except Exception:
|
- print("Error while saving succeeded test into",
|
- osp.join(os.getcwd(), FILE_RESTART),
|
- file=sys.__stderr__)
|
- raise
|
+ except Exception, ex:
|
+ print >> sys.__stderr__, "Error while saving \
|
+succeeded test into", osp.join(os.getcwd(), FILE_RESTART)
|
+ raise ex
|
result.addSuccess(self)
|
finally:
|
# if result.cvg:
|
@@ -648,10 +647,10 @@ class TestCase(unittest.TestCase):
|
return 1
|
except KeyboardInterrupt:
|
raise
|
- except InnerTestSkipped as e:
|
+ except InnerTestSkipped, e:
|
result.addSkip(self, e)
|
return 1
|
- except SkipTest as e:
|
+ except SkipTest, e:
|
result.addSkip(self, e)
|
return 0
|
except:
|
@@ -705,7 +704,7 @@ class TestCase(unittest.TestCase):
|
base = ''
|
self.fail(base + '\n'.join(msgs))
|
|
- @deprecated('Please use assertCountEqual instead.')
|
+ @deprecated('Please use assertItemsEqual instead.')
|
def assertUnorderedIterableEquals(self, got, expected, msg=None):
|
"""compares two iterable and shows difference between both
|
|
@@ -827,10 +826,10 @@ class TestCase(unittest.TestCase):
|
parser = make_parser()
|
try:
|
parser.parse(stream)
|
- except SAXParseException as ex:
|
+ except SAXParseException, ex:
|
if msg is None:
|
stream.seek(0)
|
- for _ in range(ex.getLineNumber()):
|
+ for _ in xrange(ex.getLineNumber()):
|
line = stream.readline()
|
pointer = ('' * (ex.getLineNumber() - 1)) + '^'
|
msg = 'XML stream not well formed: %s\n%s%s' % (ex, line, pointer)
|
@@ -868,7 +867,7 @@ class TestCase(unittest.TestCase):
|
ParseError = ExpatError
|
try:
|
parse(data)
|
- except (ExpatError, ParseError) as ex:
|
+ except (ExpatError, ParseError), ex:
|
if msg is None:
|
if hasattr(data, 'readlines'): #file like object
|
data.seek(0)
|
@@ -889,11 +888,11 @@ class TestCase(unittest.TestCase):
|
line_number_length = len('%i' % end)
|
line_pattern = " %%%ii: %%s" % line_number_length
|
|
- for line_no in range(start, ex.lineno):
|
+ for line_no in xrange(start, ex.lineno):
|
context_lines.append(line_pattern % (line_no, lines[line_no-1]))
|
context_lines.append(line_pattern % (ex.lineno, lines[ex.lineno-1]))
|
context_lines.append('%s^\n' % (' ' * (1 + line_number_length + 2 +ex.offset)))
|
- for line_no in range(ex.lineno+1, end+1):
|
+ for line_no in xrange(ex.lineno+1, end+1):
|
context_lines.append(line_pattern % (line_no, lines[line_no-1]))
|
|
rich_context = ''.join(context_lines)
|
@@ -921,7 +920,7 @@ class TestCase(unittest.TestCase):
|
self.fail( "tuple %s has %i children%s (%i expected)"%(tup,
|
len(tup[2]),
|
('', 's')[len(tup[2])>1], len(element)))
|
- for index in range(len(tup[2])):
|
+ for index in xrange(len(tup[2])):
|
self.assertXMLEqualsTuple(element[index], tup[2][index])
|
#check text
|
if element.text or len(tup)>3:
|
@@ -951,7 +950,7 @@ class TestCase(unittest.TestCase):
|
def assertTextEquals(self, text1, text2, junk=None,
|
msg_prefix='Text differ', striplines=False):
|
"""compare two multiline strings (using difflib and splitlines())
|
-
|
+
|
:param text1: a Python BaseString
|
:param text2: a second Python Basestring
|
:param junk: List of Caracters
|
@@ -959,9 +958,9 @@ class TestCase(unittest.TestCase):
|
:param striplines: Boolean to trigger line stripping before comparing
|
"""
|
msg = []
|
- if not isinstance(text1, string_types):
|
+ if not isinstance(text1, basestring):
|
msg.append('text1 is not a string (%s)'%(type(text1)))
|
- if not isinstance(text2, string_types):
|
+ if not isinstance(text2, basestring):
|
msg.append('text2 is not a string (%s)'%(type(text2)))
|
if msg:
|
self.fail('\n'.join(msg))
|
@@ -1017,13 +1016,13 @@ class TestCase(unittest.TestCase):
|
ipath_a, idirs_a, ifiles_a = data_a = None, None, None
|
while True:
|
try:
|
- ipath_a, idirs_a, ifiles_a = datas_a = next(iter_a)
|
+ ipath_a, idirs_a, ifiles_a = datas_a = iter_a.next()
|
partial_iter = False
|
- ipath_b, idirs_b, ifiles_b = datas_b = next(iter_b)
|
+ ipath_b, idirs_b, ifiles_b = datas_b = iter_b.next()
|
partial_iter = True
|
|
|
- self.assertTrue(ipath_a == ipath_b,
|
+ self.assert_(ipath_a == ipath_b,
|
"unexpected %s in %s while looking %s from %s" %
|
(ipath_a, path_a, ipath_b, path_b))
|
|
@@ -1041,7 +1040,7 @@ class TestCase(unittest.TestCase):
|
|
|
msgs = [ "%s: %s"% (name, items)
|
- for name, items in errors.items() if items]
|
+ for name, items in errors.iteritems() if items]
|
|
if msgs:
|
msgs.insert(0, "%s and %s differ :" % (
|
@@ -1081,9 +1080,9 @@ class TestCase(unittest.TestCase):
|
msg = '%r is not an instance of %s but of %s'
|
msg = msg % (obj, klass, type(obj))
|
if strict:
|
- self.assertTrue(obj.__class__ is klass, msg)
|
+ self.assert_(obj.__class__ is klass, msg)
|
else:
|
- self.assertTrue(isinstance(obj, klass), msg)
|
+ self.assert_(isinstance(obj, klass), msg)
|
|
@deprecated('Please use assertIsNone instead.')
|
def assertNone(self, obj, msg=None):
|
@@ -1093,14 +1092,14 @@ class TestCase(unittest.TestCase):
|
"""
|
if msg is None:
|
msg = "reference to %r when None expected"%(obj,)
|
- self.assertTrue( obj is None, msg )
|
+ self.assert_( obj is None, msg )
|
|
@deprecated('Please use assertIsNotNone instead.')
|
def assertNotNone(self, obj, msg=None):
|
"""assert obj is not None"""
|
if msg is None:
|
msg = "unexpected reference to None"
|
- self.assertTrue( obj is not None, msg )
|
+ self.assert_( obj is not None, msg )
|
|
@deprecated('Non-standard. Please use assertAlmostEqual instead.')
|
def assertFloatAlmostEquals(self, obj, other, prec=1e-5,
|
@@ -1118,7 +1117,7 @@ class TestCase(unittest.TestCase):
|
msg = "%r != %r" % (obj, other)
|
if relative:
|
prec = prec*math.fabs(obj)
|
- self.assertTrue(math.fabs(obj - other) < prec, msg)
|
+ self.assert_(math.fabs(obj - other) < prec, msg)
|
|
def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs):
|
"""override default failUnlessRaises method to return the raised
|
@@ -1147,7 +1146,7 @@ class TestCase(unittest.TestCase):
|
return _assert(excClass, callableObj, *args, **kwargs)
|
try:
|
callableObj(*args, **kwargs)
|
- except excClass as exc:
|
+ except excClass, exc:
|
class ProxyException:
|
def __init__(self, obj):
|
self._obj = obj
|
@@ -1167,16 +1166,6 @@ class TestCase(unittest.TestCase):
|
|
assertRaises = failUnlessRaises
|
|
- if sys.version_info >= (3,2):
|
- assertItemsEqual = unittest.TestCase.assertCountEqual
|
- else:
|
- assertCountEqual = unittest.TestCase.assertItemsEqual
|
- if sys.version_info < (2,7):
|
- def assertIsNotNone(self, value, *args, **kwargs):
|
- self.assertNotEqual(None, value, *args, **kwargs)
|
-
|
-TestCase.assertItemsEqual = deprecated('assertItemsEqual is deprecated, use assertCountEqual')(
|
- TestCase.assertItemsEqual)
|
|
import doctest
|
|
@@ -1195,6 +1184,10 @@ class DocTestFinder(doctest.DocTestFinder):
|
def _get_test(self, obj, name, module, globs, source_lines):
|
"""override default _get_test method to be able to skip tests
|
according to skipped attribute's value
|
+
|
+ Note: Python (<=2.4) use a _name_filter which could be used for that
|
+ purpose but it's no longer available in 2.5
|
+ Python 2.5 seems to have a [SKIP] flag
|
"""
|
if getattr(obj, '__name__', '') in self.skipped:
|
return None
|
@@ -1212,19 +1205,16 @@ class DocTest(TestCase):
|
# pylint: disable=W0613
|
try:
|
finder = DocTestFinder(skipped=self.skipped)
|
- suite = doctest.DocTestSuite(self.module, test_finder=finder)
|
- # XXX iirk
|
- doctest.DocTestCase._TestCase__exc_info = sys.exc_info
|
+ if sys.version_info >= (2, 4):
|
+ suite = doctest.DocTestSuite(self.module, test_finder=finder)
|
+ if sys.version_info >= (2, 5):
|
+ # XXX iirk
|
+ doctest.DocTestCase._TestCase__exc_info = sys.exc_info
|
+ else:
|
+ suite = doctest.DocTestSuite(self.module)
|
except AttributeError:
|
suite = SkippedSuite()
|
- # doctest may gork the builtins dictionnary
|
- # This happen to the "_" entry used by gettext
|
- old_builtins = builtins.__dict__.copy()
|
- try:
|
- return suite.run(result)
|
- finally:
|
- builtins.__dict__.clear()
|
- builtins.__dict__.update(old_builtins)
|
+ return suite.run(result)
|
run = __call__
|
|
def test(self):
|
@@ -1252,11 +1242,11 @@ class MockSMTP:
|
"""ignore quit"""
|
|
|
-class MockConfigParser(configparser.ConfigParser):
|
+class MockConfigParser(ConfigParser):
|
"""fake ConfigParser.ConfigParser"""
|
|
def __init__(self, options):
|
- configparser.ConfigParser.__init__(self)
|
+ ConfigParser.__init__(self)
|
for section, pairs in options.iteritems():
|
self.add_section(section)
|
for key, value in pairs.iteritems():
|
|