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 var oe_db_info = ${db_info | n};
56 // List of modules, each module is preceded by its dependencies
57 var oe_all_dependencies = ${dependencies | n};
58 QUnit.config.testTimeout = 5 * 60 * 1000;
61 <body id="oe" class="openerp">
62 <div id="qunit"></div>
63 <div id="qunit-fixture"></div>
65 <!-- TODO xmo please use the regular template even for testing -->
66 % for module, jss, tests, templates in files:
68 % if not js.endswith('/apps.js'):
69 <script src="${to_path(module, js)}"></script>
72 % if tests or templates:
74 openerp.testing.current_module = "${module}";
75 % for template in templates:
76 openerp.testing.add_template("${to_path(module, template)}");
82 <script type="text/javascript" src="${to_path(module, test)}"></script>
87 """, default_filters=['h'])
89 class TestRunnerController(http.Controller):
91 @http.route('/web/tests', type='http', auth="none")
92 def index(self, mod=None, **kwargs):
93 ms = module.get_modules()
96 for name, desc in zip(ms, map(self.load_manifest, ms))
97 if desc # remove not-actually-openerp-modules
101 return NOMODULE_TEMPLATE.render(modules=(
102 (manifest['name'], name)
103 for name, manifest in manifests.iteritems()
104 if any(testfile.endswith('.js')
105 for testfile in manifest['test'])
107 sorted_mods = module_topological_sort(dict(
108 (name, manifest.get('depends', []))
109 for name, manifest in manifests.iteritems()
111 # to_load and to_test should be zippable lists of the same length.
112 # A falsy value in to_test indicate nothing to test at that index (just
113 # load the corresponding part of to_load)
114 to_test = sorted_mods
116 if mod not in manifests:
117 return request.not_found(NOTFOUND.render(module=mod))
118 idx = sorted_mods.index(mod)
119 to_test = [None] * len(sorted_mods)
123 filter(lambda path: path.endswith('.js'),
124 manifests[mod]['test'] if mod else [])
126 # remove trailing test-less modules
127 tests = reversed(list(
130 reversed(tests_candicates))))
133 (mod, manifests[mod]['js'], tests, manifests[mod]['qweb'])
134 for mod, tests in itertools.izip(sorted_mods, tests)
137 # if all three db_info parameters are present, send them to the page
138 db_info = dict((k, v) for k, v in kwargs.iteritems()
139 if k in ['source', 'supadmin', 'password'])
140 if len(db_info) != 3:
143 return TESTING.render(files=files, dependencies=json.dumps(
144 [name for name in sorted_mods
145 if module.get_module_resource(name, 'static')
146 if manifests[name]['js']]), db_info=json.dumps(db_info))
148 def load_manifest(self, name):
149 manifest = module.load_information_from_description_file(name)
151 path = module.get_module_path(name)
152 manifest['js'] = list(
153 self.expand_patterns(path, manifest.get('js', [])))
154 manifest['test'] = list(
155 self.expand_patterns(path, manifest.get('test', [])))
156 manifest['qweb'] = list(
157 self.expand_patterns(path, manifest.get('qweb', [])))
160 def expand_patterns(self, root, patterns):
161 for pattern in patterns:
162 normalized_pattern = os.path.normpath(os.path.join(root, pattern))
163 for path in glob.glob(normalized_pattern):
164 # replace OS path separators (from join & normpath) by URI ones
165 yield path[len(root):].replace(os.path.sep, '/')
167 @http.route('/web/tests/set_session_value', type='json', auth="none")
168 def set_session_value(self, value):
169 request.session.some_test_value = value
171 @http.route('/web/tests/get_session_value', type='json', auth="none")
172 def get_session_value(self):
173 return request.session.some_test_value