From 113232bf880cbd990e2a679dceb271a4bd7d5157 Mon Sep 17 00:00:00 2001 From: Fabian Groffen Date: Fri, 12 Jan 2007 22:25:10 +0000 Subject: [PATCH] Merged from trunk 5521:5533 rework test import code, rename test_vercmp to be more generic, add tests for =* glob matches (WarnerBro) Make the tests pass. Fix Fail test (WarnerBro) Take Genone's suggestion and break the modules up into module oriented fashion. Tests for portage.py go in portage/ and so on and so forth... (WarnerBro) shift test cases around (WarnerBro) shift test cases around (WarnerBro) rename atoms back to vercmp (WarnerBro) split out util tests (WarnerBro) remove monolithic test (WarnerBro) add a few tips on adding tests, and kinks in the current system, I hope to fix the import bug soon (WarnerBro) Fix module loading. Fix sys.path so that imports work correctly. svn path=/main/branches/prefix/; revision=5615 --- TEST-NOTES | 24 +++++++++++ tests/__init__.py | 43 +++++++++++++++---- tests/portage/__init__.py | 4 ++ tests/portage/test_match_from_list.py | 33 ++++++++++++++ tests/{ => portage}/test_vercmp.py | 6 --- tests/portage_util/__init__.py | 5 +++ tests/portage_util/test_normalizedPath.py | 16 +++++++ .../test_stackDictList.py} | 31 ++----------- tests/portage_util/test_stackDicts.py | 11 +++++ tests/portage_util/test_stackLists.py | 11 +++++ tests/portage_util/test_uniqueArray.py | 11 +++++ tests/portage_util/test_varExpand.py | 11 +++++ tests/runTests | 8 ++-- 13 files changed, 169 insertions(+), 45 deletions(-) create mode 100644 TEST-NOTES create mode 100644 tests/portage/__init__.py create mode 100644 tests/portage/test_match_from_list.py rename tests/{ => portage}/test_vercmp.py (92%) create mode 100644 tests/portage_util/__init__.py create mode 100644 tests/portage_util/test_normalizedPath.py rename tests/{test_util.py => portage_util/test_stackDictList.py} (55%) create mode 100644 tests/portage_util/test_stackDicts.py create mode 100644 tests/portage_util/test_stackLists.py create mode 100644 tests/portage_util/test_uniqueArray.py create mode 100644 tests/portage_util/test_varExpand.py diff --git a/TEST-NOTES b/TEST-NOTES new file mode 100644 index 000000000..dbdbf20d0 --- /dev/null +++ b/TEST-NOTES @@ -0,0 +1,24 @@ +UnitTests +--------- + +Portage has some tests that use the unittest framework that ships with python (2.3-2.4ish) +Tests have a specific naming convention. + +in tests/ there is a runTest script that invokes tests/__init__.py + +This init looks at a hardcoded list of test dirs to search for tests. +If you add a new dir and don't see your new tests, make sure that the dir is in this list. + +On the subject of adding more directories; the layout is basically 1 directory per portage +file at this point (we have few files, and even fewer large files). Inside of the dir +you should have files of the form test_${function}.py. + +So if I was to write a vercmp test, and vercmp is in portage_versions. + +tests/portage_versions/test_vercmp.py + +would be the filename. + +The __init__.py file in tests only goes 1 deep (it's non-recursive, in other words). If +you end up with more directories it will probably break at present. Need to replace the +'/' in the directory path with a '.' (for proper importing). This should be trivial to fix. diff --git a/tests/__init__.py b/tests/__init__.py index 6acf0c99f..2808ed5a4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,19 +3,46 @@ # Distributed under the terms of the GNU General Public License v2 # $Id$ -import unittest +import os, unittest def main(): - tests = ["test_vercmp", "test_util"] + testDirs = ["portage", "portage_util"] suite = unittest.TestSuite() - for mod in tests: - try: - test_mod = __import__(mod) - suite.addTest(test_mod.suite()) - except ImportError: - pass + basedir = os.path.dirname(__file__) + for mydir in testDirs: + suite.addTests(getTests(os.path.join(basedir, mydir))) return unittest.TextTestRunner(verbosity=2).run(suite) + +def my_import(name): + mod = __import__(name) + components = name.split('.') + for comp in components[1:]: + mod = getattr(mod, comp) + return mod + +def getTests( 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 + + """ + import os + files = os.listdir( path ) + files = [ f[:-3] for f in files if f.startswith("test_") and f.endswith(".py") ] + parent_module = ".".join(("tests", os.path.basename(path))) + result = [] + for mymodule in files: + try: + # Make the trailing / a . for module importing + modname = ".".join((parent_module, mymodule)) + mod = my_import(modname) + result.append( unittest.TestLoader().loadTestsFromModule(mod) ) + except ImportError: + raise + return result diff --git a/tests/portage/__init__.py b/tests/portage/__init__.py new file mode 100644 index 000000000..e1c0ae595 --- /dev/null +++ b/tests/portage/__init__.py @@ -0,0 +1,4 @@ +# tests/portage/__init__.py -- Portage Unit Test functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: __init__.py 5522 2007-01-10 12:30:05Z antarus $ diff --git a/tests/portage/test_match_from_list.py b/tests/portage/test_match_from_list.py new file mode 100644 index 000000000..7afe51c5e --- /dev/null +++ b/tests/portage/test_match_from_list.py @@ -0,0 +1,33 @@ +# test_match_from_list.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_atoms.py 5525 2007-01-10 13:35:03Z antarus $ + +from unittest import TestCase +from portage_dep import match_from_list + +class AtomCmpEqualGlob(TestCase): + """ A simple testcase for =* glob matching + """ + + def testEqualGlobPass(self): + tests = [ ("=sys-apps/portage-45*", "sys-apps/portage-045" ), + ("=sys-fs/udev-1*", "sys-fs/udev-123"), + ("=sys-fs/udev-4*", "sys-fs/udev-456" ) ] + +# I need to look up the cvs syntax +# ("=sys-fs/udev_cvs*","sys-fs/udev_cvs_pre4" ) ] + + for test in tests: + self.failIf( len(match_from_list( test[0], [test[1]] )) != 1, + msg="%s should match %s!" % (test[0], test[1]) ) + + def testEqualGlobFail(self): + tests = [ ("=sys-apps/portage-2*", "sys-apps/portage-2.1" ), + ("=sys-apps/portage-2.1*", "sys-apps/portage-2.1.2" ) ] + for test in tests: + try: + self.failIf( len( match_from_list( test[0], [test[1]] ) ) != 1, + msg="%s shouldn't match %s!" % (test[0], test[1]) ) + except TypeError: # failure is ok here + pass diff --git a/tests/test_vercmp.py b/tests/portage/test_vercmp.py similarity index 92% rename from tests/test_vercmp.py rename to tests/portage/test_vercmp.py index 624950cf5..117472e49 100644 --- a/tests/test_vercmp.py +++ b/tests/portage/test_vercmp.py @@ -4,13 +4,11 @@ # $Id$ from unittest import TestCase -from unittest import TestLoader from portage_versions import vercmp class VerCmpTestCase(TestCase): """ A simple testCase for portage_versions.vercmp() """ - def testVerCmpGreater(self): @@ -40,7 +38,3 @@ class VerCmpTestCase(TestCase): ("0", "0.0")] for test in tests: self.failIf( vercmp( test[0], test[1]) == 0, msg="%s == %s? Wrong!" % (test[0],test[1])) - -def suite(): - return TestLoader().loadTestsFromTestCase(VerCmpTestCase) - diff --git a/tests/portage_util/__init__.py b/tests/portage_util/__init__.py new file mode 100644 index 000000000..ede012f66 --- /dev/null +++ b/tests/portage_util/__init__.py @@ -0,0 +1,5 @@ +# tests/portage_util/__init__.py -- Portage Unit Test functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: __init__.py 5522 2007-01-10 12:30:05Z antarus $ + diff --git a/tests/portage_util/test_normalizedPath.py b/tests/portage_util/test_normalizedPath.py new file mode 100644 index 000000000..9f9a60597 --- /dev/null +++ b/tests/portage_util/test_normalizedPath.py @@ -0,0 +1,16 @@ +# test_normalizePath.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ + +from unittest import TestCase + +class NormalizePathTestCase(TestCase): + + def testNormalizePath(self): + + from portage_util import normalize_path + path = "///foo/bar/baz" + good = "/foo/bar/baz" + self.failUnless(normalize_path(path) == good, msg="NormalizePath(%s) failed to produce %s" % (path, good)) + diff --git a/tests/test_util.py b/tests/portage_util/test_stackDictList.py similarity index 55% rename from tests/test_util.py rename to tests/portage_util/test_stackDictList.py index 59d82b9ba..e6442ce43 100644 --- a/tests/test_util.py +++ b/tests/portage_util/test_stackDictList.py @@ -1,24 +1,12 @@ -# test_vercmp.py -- Portage Unit Testing Functionality +# test_stackDictList.py -- Portage Unit Testing Functionality # Copyright 2006 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ -from unittest import TestCase, TestLoader +from unittest import TestCase + +class StackDictListTestCase(TestCase): -class UtilTestCase(TestCase): - - def testUniqueArray(self): - pass - - def testVarexpand(self): - pass - - def testStackLists(self): - pass - - def testStackDicts(self): - pass - def testStackDictList(self): from portage_util import stack_dictlist @@ -29,14 +17,3 @@ class UtilTestCase(TestCase): self.failUnless(stack_dictlist([test[0],test[1]],incremental=test[2]) == test[3], msg="%s and %s combined, was expecting: %s and got: %s" % (test[0],test[1],test[3], stack_dictlist([test[0],test[1]],incremental=test[2])) ) - - def testNormalizePath(self): - - from portage_util import normalize_path - path = "///foo/bar/baz" - good = "/foo/bar/baz" - self.failUnless(normalize_path(path) == good, msg="NormalizePath(%s) failed to produce %s" % (path, good)) - -def suite(): - return TestLoader().loadTestsFromTestCase(UtilTestCase) - diff --git a/tests/portage_util/test_stackDicts.py b/tests/portage_util/test_stackDicts.py new file mode 100644 index 000000000..dd0743509 --- /dev/null +++ b/tests/portage_util/test_stackDicts.py @@ -0,0 +1,11 @@ +# test_stackDicts.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ + +from unittest import TestCase + +class StackDictsTestCase(TestCase): + + def testStackDicts(self): + pass diff --git a/tests/portage_util/test_stackLists.py b/tests/portage_util/test_stackLists.py new file mode 100644 index 000000000..955f41ad8 --- /dev/null +++ b/tests/portage_util/test_stackLists.py @@ -0,0 +1,11 @@ +# test_stackLists.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ + +from unittest import TestCase + +class StackListsTestCase(TestCase): + + def testStackLists(self): + pass diff --git a/tests/portage_util/test_uniqueArray.py b/tests/portage_util/test_uniqueArray.py new file mode 100644 index 000000000..654eb4b1c --- /dev/null +++ b/tests/portage_util/test_uniqueArray.py @@ -0,0 +1,11 @@ +# test_uniqueArray.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ + +from unittest import TestCase + +class UniqueArrayTestCase(TestCase): + + def testUniqueArray(self): + pass diff --git a/tests/portage_util/test_varExpand.py b/tests/portage_util/test_varExpand.py new file mode 100644 index 000000000..e7bb62b67 --- /dev/null +++ b/tests/portage_util/test_varExpand.py @@ -0,0 +1,11 @@ +# test_varExpand.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_vercmp.py 5213 2006-12-08 00:12:41Z antarus $ + +from unittest import TestCase, TestLoader + +class VarExpandTestCase(TestCase): + + def testVarexpand(self): + pass diff --git a/tests/runTests b/tests/runTests index fed58f75d..c44d36788 100755 --- a/tests/runTests +++ b/tests/runTests @@ -5,16 +5,16 @@ # $Id$ -import sys +import os, sys import os.path as osp -# Grab SVN portage files instead of normal ones. -sys.path.insert(0,'../pym') - # Insert our parent dir so we can do shiny import "tests" # This line courtesy of Marienz and Pkgcore ;) sys.path.insert(0, osp.dirname(osp.dirname(osp.abspath(__file__)))) +# Grab SVN portage files instead of normal ones. +sys.path.insert(0, os.path.join(sys.path[0], "pym")) + import tests if __name__ == "__main__": result = tests.main() -- 2.26.2