Merged from trunk 5521:5533
authorFabian Groffen <grobian@gentoo.org>
Fri, 12 Jan 2007 22:25:10 +0000 (22:25 -0000)
committerFabian Groffen <grobian@gentoo.org>
Fri, 12 Jan 2007 22:25:10 +0000 (22:25 -0000)
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

13 files changed:
TEST-NOTES [new file with mode: 0644]
tests/__init__.py
tests/portage/__init__.py [new file with mode: 0644]
tests/portage/test_match_from_list.py [new file with mode: 0644]
tests/portage/test_vercmp.py [moved from tests/test_vercmp.py with 92% similarity]
tests/portage_util/__init__.py [new file with mode: 0644]
tests/portage_util/test_normalizedPath.py [new file with mode: 0644]
tests/portage_util/test_stackDictList.py [moved from tests/test_util.py with 55% similarity]
tests/portage_util/test_stackDicts.py [new file with mode: 0644]
tests/portage_util/test_stackLists.py [new file with mode: 0644]
tests/portage_util/test_uniqueArray.py [new file with mode: 0644]
tests/portage_util/test_varExpand.py [new file with mode: 0644]
tests/runTests

diff --git a/TEST-NOTES b/TEST-NOTES
new file mode 100644 (file)
index 0000000..dbdbf20
--- /dev/null
@@ -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.
index 6acf0c99f6addf4ae89ce0666fed7751ce7426de..2808ed5a4927273a96b4cf73f6ecf91d012b5868 100644 (file)
@@ -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 (file)
index 0000000..e1c0ae5
--- /dev/null
@@ -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 (file)
index 0000000..7afe51c
--- /dev/null
@@ -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
similarity index 92%
rename from tests/test_vercmp.py
rename to tests/portage/test_vercmp.py
index 624950cf52a415b4793842af9512aac5b6355682..117472e492ab0a060bc83a4e91d05201ab88af53 100644 (file)
@@ -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 (file)
index 0000000..ede012f
--- /dev/null
@@ -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 (file)
index 0000000..9f9a605
--- /dev/null
@@ -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))
+
similarity index 55%
rename from tests/test_util.py
rename to tests/portage_util/test_stackDictList.py
index 59d82b9badb00c09e09f9faa81e67467de34f795..e6442ce43a60b85aac681c4743de685f1a12de24 100644 (file)
@@ -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 (file)
index 0000000..dd07435
--- /dev/null
@@ -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 (file)
index 0000000..955f41a
--- /dev/null
@@ -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 (file)
index 0000000..654eb4b
--- /dev/null
@@ -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 (file)
index 0000000..e7bb62b
--- /dev/null
@@ -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
index fed58f75db8a88f17c4f15f4a93bb1461b4d87d9..c44d3678845319bbc29d850720520fe526f7bc71 100755 (executable)
@@ -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()