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