Merged Anton Batenev's report of Nicolas Alvarez' unicode-in-be-new bug
[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 import doctest
20 import os
21 import os.path
22 import sys
23 import unittest
24
25 import libbe
26 libbe.TESTING = True
27 from libbe.util.tree import Tree
28 from libbe.util.plugin import import_by_name
29 from libbe.version import version
30
31 def python_tree(root_path='libbe', root_modname='libbe'):
32     tree = Tree()
33     tree.path = root_path
34     tree.parent = None
35     stack = [tree]
36     while len(stack) > 0:
37         f = stack.pop(0)
38         if f.path.endswith('.py'):
39             f.name = os.path.basename(f.path)[:-len('.py')]
40         elif os.path.isdir(f.path) \
41                 and os.path.exists(os.path.join(f.path, '__init__.py')):
42             f.name = os.path.basename(f.path)
43             f.is_module = True
44             for child in os.listdir(f.path):
45                 if child == '__init__.py':
46                     continue
47                 c = Tree()
48                 c.path = os.path.join(f.path, child)
49                 c.parent = f
50                 stack.append(c)
51         else:
52             continue
53         if f.parent == None:
54             f.modname = root_modname
55         else:
56             f.modname = f.parent.modname + '.' + f.name
57             f.parent.append(f)
58     return tree
59
60 def add_module_tests(suite, modname):
61     try:
62         mod = import_by_name(modname)
63     except ValueError, e:
64         print >> sys.stderr, 'Failed to import "%s"' % (modname)
65         raise e
66     if hasattr(mod, 'suite'):
67         s = mod.suite
68     else:
69         s = unittest.TestLoader().loadTestsFromModule(mod)
70         try:
71             sdoc = doctest.DocTestSuite(mod)
72             suite.addTest(sdoc)
73         except ValueError:
74             pass
75     suite.addTest(s)
76
77 if __name__ == '__main__':
78     import optparse
79     parser = optparse.OptionParser(usage='%prog [options] [modules ...]',
80                                    description=
81 """When called without optional module names, run the test suites for
82 *all* modules.  This may raise lots of errors if you haven't installed
83 one of the versioning control systems.
84
85 When called with module name arguments, only run the test suites from
86 those modules and their submodules.  For example::
87
88     $ python test.py libbe.bugdir libbe.storage
89 """)
90     parser.add_option('-q', '--quiet', action='store_true', default=False,
91                       help='Run unittests in quiet mode (verbosity 1).')
92     options,args = parser.parse_args()
93     print >> sys.stderr, 'Testing BE\n%s' % version(verbose=True)
94
95     verbosity = 2
96     if options.quiet == True:
97         verbosity = 1
98
99     suite = unittest.TestSuite()
100     tree = python_tree()
101     if len(args) == 0:
102         for node in tree.traverse():
103             add_module_tests(suite, node.modname)
104     else:
105         added = []
106         for modname in args:
107             for node in tree.traverse():
108                 if node.modname == modname:
109                     for n in node.traverse():
110                         if n.modname not in added:
111                             add_module_tests(suite, n.modname)
112                             added.append(n.modname)
113                     break
114     
115     result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
116     
117     numErrors = len(result.errors)
118     numFailures = len(result.failures)
119     numBad = numErrors + numFailures
120     if numBad > 126:
121         numBad = 1
122     sys.exit(numBad)