gitstats: Teach 'stats.py author -f' to sort output by commit count
[git-stats.git] / src / git_stats / tests.py
blob590ed3c29061329fa31d43eb3d400535456d5205
1 #!/usr/bin/env python
3 import sys
4 import os
6 from optparse import OptionParser
8 from git_stats import testing
9 from git_stats import config_tests
11 all_modules = {
12 "config" : config_tests
15 def dispatch(*args):
16 """Dispatches test related commands
17 """
19 progname = os.path.basename(sys.argv[0]) + " test"
20 parser = OptionParser(prog=progname)
22 parser.add_option(
23 "-a", "--all",
24 action="store_true",
25 help="run tests for all known modules")
27 parser.add_option(
28 "-m", "--module",
29 help="run the tests defined in the specified module")
31 parser.add_option(
32 "-s", "--single",
33 metavar="TEST",
34 help="run only the tests for the specified module")
36 parser.add_option(
37 "-t", "--tests",
38 metavar="TESTS",
39 action="store_true",
40 help="run only the specified tests")
42 parser.add_option(
43 "-v", "--verbose",
44 action="store_true",
45 help="enable verbose output")
47 if not args:
48 args.append("--help")
50 options, args = parser.parse_args(list(args))
52 tests = []
53 modules = []
55 # Add all modules we know about
56 if options.all:
57 tests.extend(all_modules.keys())
59 # Add only one test
60 if options.single:
61 tests.append(options.single)
63 # Also add all tests as specified as command arguments
64 if options.tests:
65 tests.extend(args)
67 if options.module:
68 try:
69 module = __import__("git_stats." + options.module, fromlist=True)
70 except ImportError, e:
71 parser.error("Could not import module '%s'.\n%s" % (
72 options.module, str(e)))
74 modules.append(module)
76 for test in tests:
77 if not test in all_modules:
78 parser.error("Unknown module '%s'." % test)
80 module = all_modules[test]
81 modules.append(module)
83 if not modules:
84 parser.error("Please specify which modules to test")
86 classes = []
88 for module in modules:
89 module_classes = module.getTestClasses()
90 classes.extend(module_classes)
92 runner = testing.GitTestRunner()
93 res = runner.runTests(options, classes)
95 return res