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