tests: add --list flag to show available tests
authorMike Frysinger <vapier@gentoo.org>
Sun, 9 Oct 2011 18:43:01 +0000 (14:43 -0400)
committerMike Frysinger <vapier@gentoo.org>
Sun, 9 Oct 2011 20:40:43 +0000 (16:40 -0400)
Trying to guess at what runtests actually wants in terms of command line
tests is pretty hard.  Any invalid value just gives you an ugly traceback.
So add a helper --list option so the user can easily figure out what the
code wants *exactly*.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
pym/portage/tests/__init__.py

index fa297b5bbdc6e8a6c02b46ed2c6cf5d9fdd85117..016a4e8d2e2c29ed5da24fc03b7227efe1e6e6e6 100644 (file)
@@ -17,38 +17,28 @@ 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 = []
 
        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)
 
+       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))
+               sys.exit(0)
+
        if len(args) > 1:
                suite.addTests(getTestFromCommandLine(args[1:], basedir))
                return TextTestRunner(verbosity=2).run(suite)
 
-       # 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 TEST_FILE in files:
-                       testDirs.append(root)
-
-       testDirs.sort()
-       for mydir in testDirs:
+       for mydir in getTestDirs(basedir):
                suite.addTests(getTests(os.path.join(basedir, mydir), basedir) )
        return TextTestRunner(verbosity=2).run(suite)
 
@@ -73,6 +63,29 @@ def getTestFromCommandLine(args, base_path):
                result.extend(getTestsFromFiles(path, base_path, [mymodule]))
        return result
 
+def getTestDirs(base_path):
+       TEST_FILE = b'__test__'
+       svn_dirname = b'.svn'
+       testDirs = []
+
+       # 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
+
+               if TEST_FILE in files:
+                       testDirs.append(root)
+
+       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") ]