[MERGE] trunk
[odoo/odoo.git] / addons / web / controllers / testing.py
1 # coding=utf-8
2 # -*- encoding: utf-8 -*-
3
4 import glob
5 import itertools
6 import json
7 import operator
8 import os
9
10 from mako.template import Template
11 from openerp.modules import module
12 from openerp import http
13 from openerp.http import request
14
15 from .main import module_topological_sort
16
17 NOMODULE_TEMPLATE = Template(u"""<!DOCTYPE html>
18 <html>
19     <head>
20         <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
21         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
22         <title>OpenERP Testing</title>
23     </head>
24     <body>
25         <form action="/web/tests" method="GET">
26             <button name="mod" value="*">Run all tests</button>
27             <ul>
28             % for name, module in modules:
29                 <li>${name} <button name="mod" value="${module}">
30                     Run Tests</button></li>
31             % endfor
32             </ul>
33         </form>
34     </body>
35 </html>
36 """, default_filters=['h'])
37 NOTFOUND = Template(u"""
38 <p>Unable to find the module [${module}], please check that the module
39    name is correct and the module is on OpenERP's path.</p>
40 <a href="/web/tests">&lt;&lt; Back to tests</a>
41 """, default_filters=['h'])
42 TESTING = Template(u"""<!DOCTYPE html>
43 <html style="height: 100%">
44 <%def name="to_path(module, p)">/${module}/${p}</%def>
45 <head>
46     <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
47     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
48     <title>OpenERP Web Tests</title>
49     <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon"/>
50
51     <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css">
52     <script src="/web/static/lib/qunit/qunit.js"></script>
53
54     <script type="text/javascript">
55         // List of modules, each module is preceded by its dependencies
56         var oe_all_dependencies = ${dependencies | n};
57         QUnit.config.testTimeout = 5 * 60 * 1000;
58     </script>
59 </head>
60 <body id="oe" class="openerp">
61     <div id="qunit"></div>
62     <div id="qunit-fixture"></div>
63 </body>
64 <!-- TODO xmo please use the regular template even for testing -->
65 % for module, jss, tests, templates in files:
66     % for js in jss:
67         % if not js.endswith('/apps.js'):
68             <script src="${to_path(module, js)}"></script>
69         % endif
70     % endfor
71     % if tests or templates:
72     <script>
73         openerp.testing.current_module = "${module}";
74         % for template in templates:
75         openerp.testing.add_template("${to_path(module, template)}");
76         % endfor
77     </script>
78     % endif
79     % if tests:
80         % for test in tests:
81             <script type="text/javascript" src="${to_path(module, test)}"></script>
82         % endfor
83     % endif
84 % endfor
85 </html>
86 """, default_filters=['h'])
87
88 class TestRunnerController(http.Controller):
89
90     @http.route('/web/tests', type='http', auth="none")
91     def index(self, mod=None, **kwargs):
92         ms = module.get_modules()
93         manifests = dict(
94             (name, desc)
95             for name, desc in zip(ms, map(self.load_manifest, ms))
96             if desc # remove not-actually-openerp-modules
97         )
98
99         if not mod:
100             return NOMODULE_TEMPLATE.render(modules=(
101                 (manifest['name'], name)
102                 for name, manifest in manifests.iteritems()
103                 if any(testfile.endswith('.js')
104                        for testfile in manifest['test'])
105             ))
106         sorted_mods = module_topological_sort(dict(
107             (name, manifest.get('depends', []))
108             for name, manifest in manifests.iteritems()
109         ))
110         # to_load and to_test should be zippable lists of the same length.
111         # A falsy value in to_test indicate nothing to test at that index (just
112         # load the corresponding part of to_load)
113         to_test = sorted_mods
114         if mod != '*':
115             if mod not in manifests:
116                 return request.not_found(NOTFOUND.render(module=mod))
117             idx = sorted_mods.index(mod)
118             to_test = [None] * len(sorted_mods)
119             to_test[idx] = mod
120
121         tests_candicates = [
122             filter(lambda path: path.endswith('.js'),
123                    manifests[mod]['test'] if mod else [])
124             for mod in to_test]
125         # remove trailing test-less modules
126         tests = reversed(list(
127             itertools.dropwhile(
128                 operator.not_,
129                 reversed(tests_candicates))))
130
131         files = [
132             (mod, manifests[mod]['js'], tests, manifests[mod]['qweb'])
133             for mod, tests in itertools.izip(sorted_mods, tests)
134         ]
135
136         return TESTING.render(files=files, dependencies=json.dumps(
137             [name for name in sorted_mods
138              if module.get_module_resource(name, 'static')
139              if manifests[name]['js']]))
140
141     def load_manifest(self, name):
142         manifest = module.load_information_from_description_file(name)
143         if manifest:
144             path = module.get_module_path(name)
145             manifest['js'] = list(
146                 self.expand_patterns(path, manifest.get('js', [])))
147             manifest['test'] = list(
148                 self.expand_patterns(path, manifest.get('test', [])))
149             manifest['qweb'] = list(
150                 self.expand_patterns(path, manifest.get('qweb', [])))
151         return manifest
152
153     def expand_patterns(self, root, patterns):
154         for pattern in patterns:
155             normalized_pattern = os.path.normpath(os.path.join(root, pattern))
156             for path in glob.glob(normalized_pattern):
157                 # replace OS path separators (from join & normpath) by URI ones
158                 yield path[len(root):].replace(os.path.sep, '/')
159