[FIX] correctly handle listification of args.addons
[odoo/odoo.git] / openerpcommand / run_tests.py
1 """
2 Execute the unittest2 tests available in OpenERP addons.
3 """
4
5 import sys
6 import types
7 import argparse
8
9 import common
10
11 def get_test_modules(module, submodule, explode):
12     """
13     Return a list of submodules containing tests.
14     `submodule` can be:
15       - None
16       - the name of a submodule
17       - '__fast_suite__'
18       - '__sanity_checks__'
19     """
20     # Turn command-line module, submodule into importable names.
21     if module is None:
22         pass
23     elif module == 'openerp':
24         module = 'openerp.tests'
25     else:
26         module = 'openerp.addons.' + module + '.tests'
27
28     # Try to import the module
29     try:
30         __import__(module)
31     except Exception, e:
32         if explode:
33             print 'Can not `import %s`.' % module
34             import logging
35             logging.exception('')
36             sys.exit(1)
37         else:
38             if str(e) == 'No module named tests':
39                 # It seems the module has no `tests` sub-module, no problem.
40                 pass
41             else:
42                 print 'Can not `import %s`.' % module
43             return []
44
45     # Discover available test sub-modules.
46     m = sys.modules[module]
47     submodule_names =  sorted([x for x in dir(m) \
48         if x.startswith('test_') and \
49         isinstance(getattr(m, x), types.ModuleType)])
50     submodules = [getattr(m, x) for x in submodule_names]
51
52     def show_submodules_and_exit():
53         if submodule_names:
54             print 'Available submodules are:'
55             for x in submodule_names:
56                 print ' ', x
57         sys.exit(1)
58
59     fast_suite = getattr(m, 'fast_suite', [])
60     checks = getattr(m, 'checks', [])
61     if submodule is None:
62         # Use auto-discovered sub-modules.
63         ms = submodules
64     elif submodule == '__fast_suite__':
65         # `suite` was used before the 6.1 release instead of `fast_suite`.
66         ms = fast_suite if fast_suite else getattr(m, 'suite', None)
67         if ms is None:
68             if explode:
69                 print 'The module `%s` has no defined test suite.' % (module,)
70                 show_submodules_and_exit()
71             else:
72                 ms = []
73     elif submodule == '__sanity_checks__':
74         ms = checks
75         if ms is None:
76             if explode:
77                 print 'The module `%s` has no defined sanity checks.' % (module,)
78                 show_submodules_and_exit()
79             else:
80                 ms = []
81     elif submodule == '__slow_suite__':
82         ms = list(set(submodules).difference(fast_suite, checks))
83     else:
84         # Pick the command-line-specified test sub-module.
85         m = getattr(m, submodule, None)
86         ms = [m]
87
88         if m is None:
89             if explode:
90                 print 'The module `%s` has no submodule named `%s`.' % \
91                     (module, submodule)
92                 show_submodules_and_exit()
93             else:
94                 ms = []
95
96     return ms
97
98 def run(args):
99     import unittest2
100
101     import openerp
102
103     config = openerp.tools.config
104     config['db_name'] = args.database
105     if args.port:
106         config['xmlrpc_port'] = int(args.port)
107     config['admin_passwd'] = 'admin'
108     config['db_password'] = 'a2aevl8w' # TODO from .openerpserverrc
109
110     if args.addons:
111         args.addons = args.addons.replace(':',',').split(',')
112     else:
113         args.addons = []
114
115     config['addons_path'] = ','.join(args.addons)
116
117     import logging
118     openerp.netsvc.init_alternative_logger()
119     logging.getLogger('openerp').setLevel(logging.CRITICAL)
120
121     # Install the import hook, to import openerp.addons.<module>.
122     openerp.modules.module.initialize_sys_path()
123
124     module = args.module
125     submodule = args.submodule
126
127     # Import the necessary modules and get the corresponding suite.
128     if module is None:
129         # TODO
130         modules = common.get_addons_from_paths(args.addons, []) # TODO openerp.addons.base is not included ?
131         test_modules = []
132         for module in ['openerp'] + modules:
133             test_modules.extend(
134                 get_test_modules(module, submodule, explode=False))
135     else:
136         test_modules = get_test_modules(module, submodule, explode=True)
137
138     # Run the test suite.
139     if not args.dry_run:
140         suite = unittest2.TestSuite()
141         for test_module in test_modules:
142             suite.addTests(unittest2.TestLoader().loadTestsFromModule(test_module))
143         r = unittest2.TextTestRunner(verbosity=2).run(suite)
144         if r.errors or r.failures:
145             sys.exit(1)
146     else:
147         print 'Test modules:'
148         for test_module in test_modules:
149             print ' ', test_module.__name__
150
151 def add_parser(subparsers):
152     parser = subparsers.add_parser('run-tests',
153         description='Run the OpenERP server and/or addons tests.')
154     parser.add_argument('-d', '--database', metavar='DATABASE', required=True,
155         help='the database to test. Depending on the test suites, the '
156         'database must already exist or not.')
157     parser.add_argument('-p', '--port', metavar='PORT',
158         help='the port used for WML-RPC tests')
159     common.add_addons_argument(parser)
160
161     parser.add_argument(
162         '-m', '--module', metavar='MODULE', action=ModuleAction, default=None,
163         help="the module to test in `module[.submodule]` notation. "
164              "Use `openerp` for the core OpenERP tests. "
165              "Leave empty to run every declared tests. "
166              "Give a module but no submodule to run all the module's declared "
167              "tests. If both the module and the submodule are given, "
168              "the sub-module can be run even if it is not declared in the module.")
169     group = parser.add_mutually_exclusive_group()
170     group.add_argument(
171         '--fast-suite',
172         dest='submodule', action=GuardAction, nargs=0, const='__fast_suite__',
173         help='run only the tests explicitly declared in the fast suite (this '
174         'makes sense only with the bare `module` notation or no module at '
175         'all).')
176     group.add_argument(
177         '--sanity-checks',
178         dest='submodule', action=GuardAction, nargs=0, const='__sanity_checks__',
179         help='run only the sanity check tests')
180     group.add_argument(
181         '--slow-suite',
182         dest='submodule', action=GuardAction, nargs=0, const='__slow_suite__',
183         help="Only run slow tests (tests which are neither in the fast nor in"
184              " the sanity suite)")
185     parser.add_argument('--dry-run', action='store_true',
186         help='do not run the tests')
187
188     parser.set_defaults(run=run, submodule=None)
189
190 class ModuleAction(argparse.Action):
191     def __call__(self, parser, namespace, values, option_string=None):
192         split = values.split('.')
193         if len(split) == 1:
194             module, submodule = values, None
195         elif len(split) == 2:
196             module, submodule = split
197         else:
198             raise argparse.ArgumentError(
199                 option_string,
200                 "must have the form 'module[.submodule]', got '%s'" % values)
201
202         setattr(namespace, self.dest, module)
203         if submodule is not None:
204             setattr(namespace, 'submodule', submodule)
205
206 class GuardAction(argparse.Action):
207     def __call__(self, parser, namespace, values, option_string=None):
208         if getattr(namespace, self.dest, None):
209             print "%s value provided, ignoring '%s'" % (self.dest, option_string)
210             return
211         setattr(namespace, self.dest, self.const)