2 # -*- encoding: utf-8 -*-
10 from mako.template import Template
11 from openerp.modules import module
12 from openerp import http
13 from openerp.http import request
15 from .main import module_topological_sort
17 NOMODULE_TEMPLATE = Template(u"""<!DOCTYPE html>
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>
25 <form action="/web/tests" method="GET">
26 <button name="mod" value="*">Run all tests</button>
28 % for name, module in modules:
29 <li>${name} <button name="mod" value="${module}">
30 Run Tests</button></li>
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"><< 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>
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"/>
51 <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css">
52 <script src="/web/static/lib/qunit/qunit.js"></script>
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;
60 <body id="oe" class="openerp">
61 <div id="qunit"></div>
62 <div id="qunit-fixture"></div>
64 <!-- TODO xmo please use the regular template even for testing -->
65 % for module, jss, tests, templates in files:
67 % if not js.endswith('/apps.js'):
68 <script src="${to_path(module, js)}"></script>
71 % if tests or templates:
73 openerp.testing.current_module = "${module}";
74 % for template in templates:
75 openerp.testing.add_template("${to_path(module, template)}");
81 <script type="text/javascript" src="${to_path(module, test)}"></script>
86 """, default_filters=['h'])
88 class TestRunnerController(http.Controller):
90 @http.route('/web/tests', type='http', auth="none")
91 def index(self, mod=None, **kwargs):
92 ms = module.get_modules()
95 for name, desc in zip(ms, map(self.load_manifest, ms))
96 if desc # remove not-actually-openerp-modules
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'])
106 sorted_mods = module_topological_sort(dict(
107 (name, manifest.get('depends', []))
108 for name, manifest in manifests.iteritems()
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
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)
122 filter(lambda path: path.endswith('.js'),
123 manifests[mod]['test'] if mod else [])
125 # remove trailing test-less modules
126 tests = reversed(list(
129 reversed(tests_candicates))))
132 (mod, manifests[mod]['js'], tests, manifests[mod]['qweb'])
133 for mod, tests in itertools.izip(sorted_mods, tests)
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']]))
141 def load_manifest(self, name):
142 manifest = module.load_information_from_description_file(name)
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', [])))
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, '/')