diff options
Diffstat (limited to 'portage_with_autodep/pym/portage/tests/__init__.py')
-rw-r--r-- | portage_with_autodep/pym/portage/tests/__init__.py | 150 |
1 files changed, 92 insertions, 58 deletions
diff --git a/portage_with_autodep/pym/portage/tests/__init__.py b/portage_with_autodep/pym/portage/tests/__init__.py index a647aa2..492ece4 100644 --- a/portage_with_autodep/pym/portage/tests/__init__.py +++ b/portage_with_autodep/pym/portage/tests/__init__.py @@ -1,10 +1,13 @@ # tests/__init__.py -- Portage Unit Test functionality -# Copyright 2006-2010 Gentoo Foundation +# Copyright 2006-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 +from __future__ import print_function + import sys import time import unittest +from optparse import OptionParser, OptionValueError try: from unittest.runner import _TextTestResult # new in python-2.7 @@ -16,35 +19,33 @@ from portage import _encodings from portage import _unicode_decode def main(): - - TEST_FILE = b'__test__' - svn_dirname = b'.svn' suite = unittest.TestSuite() basedir = os.path.dirname(os.path.realpath(__file__)) - testDirs = [] - if len(sys.argv) > 1: - suite.addTests(getTestFromCommandLine(sys.argv[1:], basedir)) - return TextTestRunner(verbosity=2).run(suite) + usage = "usage: %s [options] [tests to run]" % os.path.basename(sys.argv[0]) + parser = OptionParser(usage=usage) + parser.add_option("-l", "--list", help="list all tests", + action="store_true", dest="list_tests") + (options, args) = parser.parse_args(args=sys.argv) - # the os.walk help mentions relative paths as being quirky - # I was tired of adding dirs to the list, so now we add __test__ - # to each dir we want tested. - for root, dirs, files in os.walk(basedir): - if svn_dirname in dirs: - dirs.remove(svn_dirname) - try: - root = _unicode_decode(root, - encoding=_encodings['fs'], errors='strict') - except UnicodeDecodeError: - continue + if options.list_tests: + testdir = os.path.dirname(sys.argv[0]) + for mydir in getTestDirs(basedir): + testsubdir = os.path.basename(mydir) + for name in getTestNames(mydir): + print("%s/%s/%s.py" % (testdir, testsubdir, name)) + return os.EX_OK - if TEST_FILE in files: - testDirs.append(root) + if len(args) > 1: + suite.addTests(getTestFromCommandLine(args[1:], basedir)) + else: + for mydir in getTestDirs(basedir): + suite.addTests(getTests(os.path.join(basedir, mydir), basedir)) - for mydir in testDirs: - suite.addTests(getTests(os.path.join(basedir, mydir), basedir) ) - return TextTestRunner(verbosity=2).run(suite) + result = TextTestRunner(verbosity=2).run(suite) + if not result.wasSuccessful(): + return 1 + return os.EX_OK def my_import(name): mod = __import__(name) @@ -54,7 +55,7 @@ def my_import(name): return mod def getTestFromCommandLine(args, base_path): - ret = [] + result = [] for arg in args: realpath = os.path.realpath(arg) path = os.path.dirname(realpath) @@ -64,28 +65,39 @@ def getTestFromCommandLine(args, base_path): raise Exception("Invalid argument: '%s'" % arg) mymodule = f[:-3] + result.extend(getTestsFromFiles(path, base_path, [mymodule])) + return result - parent_path = path[len(base_path)+1:] - parent_module = ".".join(("portage", "tests", parent_path)) - parent_module = parent_module.replace('/', '.') - result = [] +def getTestDirs(base_path): + TEST_FILE = b'__test__' + svn_dirname = b'.svn' + testDirs = [] - # Make the trailing / a . for module importing - modname = ".".join((parent_module, mymodule)) - mod = my_import(modname) - ret.append(unittest.TestLoader().loadTestsFromModule(mod)) - return ret + # the os.walk help mentions relative paths as being quirky + # I was tired of adding dirs to the list, so now we add __test__ + # to each dir we want tested. + for root, dirs, files in os.walk(base_path): + if svn_dirname in dirs: + dirs.remove(svn_dirname) + try: + root = _unicode_decode(root, + encoding=_encodings['fs'], errors='strict') + except UnicodeDecodeError: + continue -def getTests(path, base_path): - """ + if TEST_FILE in files: + testDirs.append(root) - path is the path to a given subdir ( 'portage/' for example) - This does a simple filter on files in that dir to give us modules - to import + testDirs.sort() + return testDirs - """ +def getTestNames(path): files = os.listdir(path) files = [ f[:-3] for f in files if f.startswith("test") and f.endswith(".py") ] + files.sort() + return files + +def getTestsFromFiles(path, base_path, files): parent_path = path[len(base_path)+1:] parent_module = ".".join(("portage", "tests", parent_path)) parent_module = parent_module.replace('/', '.') @@ -97,6 +109,16 @@ def getTests(path, base_path): result.append(unittest.TestLoader().loadTestsFromModule(mod)) return result +def getTests(path, base_path): + """ + + path is the path to a given subdir ( 'portage/' for example) + This does a simple filter on files in that dir to give us modules + to import + + """ + return getTestsFromFiles(path, base_path, getTestNames(path)) + class TextTestResult(_TextTestResult): """ We need a subclass of unittest._TextTestResult to handle tests with TODO @@ -109,6 +131,7 @@ class TextTestResult(_TextTestResult): def __init__(self, stream, descriptions, verbosity): super(TextTestResult, self).__init__(stream, descriptions, verbosity) self.todoed = [] + self.portage_skipped = [] def addTodo(self, test, info): self.todoed.append((test,info)) @@ -117,12 +140,20 @@ class TextTestResult(_TextTestResult): elif self.dots: self.stream.write(".") + def addPortageSkip(self, test, info): + self.portage_skipped.append((test,info)) + if self.showAll: + self.stream.writeln("SKIP") + elif self.dots: + self.stream.write(".") + def printErrors(self): if self.dots or self.showAll: self.stream.writeln() self.printErrorList('ERROR', self.errors) self.printErrorList('FAIL', self.failures) self.printErrorList('TODO', self.todoed) + self.printErrorList('SKIP', self.portage_skipped) class TestCase(unittest.TestCase): """ @@ -131,15 +162,12 @@ class TestCase(unittest.TestCase): and then fix the code later. This may not be a great approach (broken code!!??!11oneone) but it does happen at times. """ - - def __init__(self, methodName='runTest'): - # This method exists because unittest.py in python 2.4 stores - # the methodName as __testMethodName while 2.5 uses - # _testMethodName. - self._testMethodName = methodName - unittest.TestCase.__init__(self, methodName) + + def __init__(self, *pargs, **kwargs): + unittest.TestCase.__init__(self, *pargs, **kwargs) self.todo = False - + self.portage_skip = None + def defaultTestResult(self): return TextTestResult() @@ -162,7 +190,13 @@ class TestCase(unittest.TestCase): testMethod() ok = True except self.failureException: - if self.todo: + if self.portage_skip is not None: + if self.portage_skip is True: + result.addPortageSkip(self, "%s: SKIP" % testMethod) + else: + result.addPortageSkip(self, "%s: SKIP: %s" % + (testMethod, self.portage_skip)) + elif self.todo: result.addTodo(self,"%s: TODO" % testMethod) else: result.addFailure(self, sys.exc_info()) @@ -192,21 +226,21 @@ class TestCase(unittest.TestCase): unexpected exception. """ try: - callableObj(*args, **kwargs) + callableObj(*args, **kwargs) except excClass: - return + return else: - if hasattr(excClass,'__name__'): excName = excClass.__name__ - else: excName = str(excClass) - raise self.failureException("%s not raised: %s" % (excName, msg)) - + if hasattr(excClass,'__name__'): excName = excClass.__name__ + else: excName = str(excClass) + raise self.failureException("%s not raised: %s" % (excName, msg)) + class TextTestRunner(unittest.TextTestRunner): """ We subclass unittest.TextTestRunner to output SKIP for tests that fail but are skippable """ - + def _makeResult(self): - return TextTestResult(self.stream, self.descriptions, self.verbosity) + return TextTestResult(self.stream, self.descriptions, self.verbosity) def run(self, test): """ @@ -236,7 +270,7 @@ class TextTestRunner(unittest.TextTestRunner): else: self.stream.writeln("OK") return result - + test_cps = ['sys-apps/portage','virtual/portage'] test_versions = ['1.0', '1.0-r1','2.3_p4','1.0_alpha57'] test_slots = [ None, '1','gentoo-sources-2.6.17','spankywashere'] |