Updated copyright information
[be.git] / test.py
1 # Copyright (C) 2005-2010 Aaron Bentley and Panometrics, Inc.
2 #                         Marien Zwart <marienz@gentoo.org>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 """Usage: python test.py [module(s) ...]
20
21 When called without optional module names, run the test suites for
22 *all* modules.  This may raise lots of errors if you haven't installed
23 one of the versioning control systems.
24
25 When called with module name arguments, only run the test suites from
26 those modules and their submodules.  For example:
27   python test.py libbe.bugdir libbe.storage
28 """
29
30 import doctest
31 import os
32 import os.path
33 import sys
34 import unittest
35
36 import libbe
37 libbe.TESTING = True
38 from libbe.util.tree import Tree
39 from libbe.util.plugin import import_by_name
40
41 def python_tree(root_path='libbe', root_modname='libbe'):
42     tree = Tree()
43     tree.path = root_path
44     tree.parent = None
45     stack = [tree]
46     while len(stack) > 0:
47         f = stack.pop(0)
48         if f.path.endswith('.py'):
49             f.name = os.path.basename(f.path)[:-len('.py')]
50         elif os.path.isdir(f.path) \
51                 and os.path.exists(os.path.join(f.path, '__init__.py')):
52             f.name = os.path.basename(f.path)
53             f.is_module = True
54             for child in os.listdir(f.path):
55                 if child == '__init__.py':
56                     continue
57                 c = Tree()
58                 c.path = os.path.join(f.path, child)
59                 c.parent = f
60                 stack.append(c)
61         else:
62             continue
63         if f.parent == None:
64             f.modname = root_modname
65         else:
66             f.modname = f.parent.modname + '.' + f.name
67             f.parent.append(f)
68     return tree
69
70 def add_module_tests(suite, modname):
71     try:
72         mod = import_by_name(modname)
73     except ValueError, e:
74         print >> sys.stderr, 'Failed to import "%s"' % (modname)
75         raise e
76     if hasattr(mod, 'suite'):
77         s = mod.suite
78     else:
79         s = unittest.TestLoader().loadTestsFromModule(mod)
80         try:
81             sdoc = doctest.DocTestSuite(mod)
82             suite.addTest(sdoc)
83         except ValueError:
84             pass
85     suite.addTest(s)
86
87 suite = unittest.TestSuite()
88 tree = python_tree()
89 if len(sys.argv) <= 1:
90     for node in tree.traverse():
91         add_module_tests(suite, node.modname)
92 else:
93     added = []
94     for modname in sys.argv[1:]:
95         for node in tree.traverse():
96             if node.modname == modname:
97                 for n in node.traverse():
98                     if n.modname not in added:
99                         add_module_tests(suite, n.modname)
100                         added.append(n.modname)
101                 break
102
103 result = unittest.TextTestRunner(verbosity=2).run(suite)
104
105 numErrors = len(result.errors)
106 numFailures = len(result.failures)
107 numBad = numErrors + numFailures
108 if numBad > 126:
109     numBad = 1
110 sys.exit(numBad)