Bug 1754222 [wpt PR 32750] - App history: basic focusReset support, a=testonly
[gecko.git] / testing / addtest.py
blob36ea64628fb4034944675907d449f883cc12ade7
1 from __future__ import absolute_import, unicode_literals, print_function
3 import io
4 import os
5 import manifestparser
8 class Creator(object):
9 def __init__(self, topsrcdir, test, suite, doc, **kwargs):
10 self.topsrcdir = topsrcdir
11 self.test = test
12 self.suite = suite
13 self.doc = doc
14 self.kwargs = kwargs
16 def check_args(self):
17 """Perform any validation required for suite-specific arguments"""
18 return True
20 def __iter__(self):
21 """Iterate over a list of (path, data) tuples corresponding to the files
22 to be created"""
23 yield (self.test, self._get_template_contents())
25 def _get_template_contents(self, **kwargs):
26 raise NotImplementedError
28 def update_manifest(self):
29 """Perform any manifest updates required to register the added tests"""
30 raise NotImplementedError
33 class XpcshellCreator(Creator):
34 template_body = """/* Any copyright is dedicated to the Public Domain.
35 http://creativecommons.org/publicdomain/zero/1.0/ */
37 "use strict";
39 add_task(async function test_TODO() {
40 ok(true, "TODO: implement the test");
41 });
42 """
44 def _get_template_contents(self):
45 return self.template_body
47 def update_manifest(self):
48 manifest_file = os.path.join(os.path.dirname(self.test), "xpcshell.ini")
49 filename = os.path.basename(self.test)
51 if not os.path.isfile(manifest_file):
52 print("Could not open manifest file {}".format(manifest_file))
53 return
54 write_to_ini_file(manifest_file, filename)
57 class MochitestCreator(Creator):
58 templates = {
59 "mochitest-browser-chrome": "browser.template.txt",
60 "mochitest-plain": "plain%(doc)s.template.txt",
61 "mochitest-chrome": "chrome%(doc)s.template.txt",
64 def _get_template_contents(self):
65 mochitest_templates = os.path.abspath(
66 os.path.join(os.path.dirname(__file__), "mochitest", "static")
68 template_file_name = None
70 template_file_name = self.templates.get(self.suite)
72 if template_file_name is None:
73 print(
74 "Sorry, `addtest` doesn't currently know how to add {}".format(
75 self.suite
78 return None
80 template_file_name = template_file_name % {"doc": self.doc}
82 template_file = os.path.join(mochitest_templates, template_file_name)
83 if not os.path.isfile(template_file):
84 print(
85 "Sorry, `addtest` doesn't currently know how to add {} with document type {}".format( # NOQA: E501
86 self.suite, self.doc
89 return None
91 with open(template_file) as f:
92 return f.read()
94 def update_manifest(self):
95 # attempt to insert into the appropriate manifest
96 guessed_ini = {
97 "mochitest-plain": "mochitest.ini",
98 "mochitest-chrome": "chrome.ini",
99 "mochitest-browser-chrome": "browser.ini",
100 }[self.suite]
101 manifest_file = os.path.join(os.path.dirname(self.test), guessed_ini)
102 filename = os.path.basename(self.test)
104 if not os.path.isfile(manifest_file):
105 print("Could not open manifest file {}".format(manifest_file))
106 return
108 write_to_ini_file(manifest_file, filename)
111 class WebPlatformTestsCreator(Creator):
112 template_prefix = """<!doctype html>
113 %(documentElement)s<meta charset=utf-8>
115 template_long_timeout = "<meta name=timeout content=long>\n"
117 template_body_th = """<title></title>
118 <script src=/resources/testharness.js></script>
119 <script src=/resources/testharnessreport.js></script>
120 <script>
122 </script>
125 template_body_reftest = """<title></title>
126 <link rel=%(match)s href=%(ref)s>
129 template_body_reftest_wait = """<script src="/common/reftest-wait.js"></script>
132 template_js = ""
133 template_js_long_timeout = "//META: timeout=long\n"
135 upstream_path = os.path.join("testing", "web-platform", "tests")
136 local_path = os.path.join("testing", "web-platform", "mozilla", "tests")
138 def __init__(self, *args, **kwargs):
139 super(WebPlatformTestsCreator, self).__init__(*args, **kwargs)
140 self.reftest = self.suite == "web-platform-tests-reftest"
142 @classmethod
143 def get_parser(cls, parser):
144 parser.add_argument(
145 "--long-timeout",
146 action="store_true",
147 help="Test should be given a long timeout "
148 "(typically 60s rather than 10s, but varies depending on environment)",
150 parser.add_argument(
151 "-m", "--reference", dest="ref", help="Path to the reference file"
153 parser.add_argument(
154 "--mismatch", action="store_true", help="Create a mismatch reftest"
156 parser.add_argument(
157 "--wait",
158 action="store_true",
159 help="Create a reftest that waits until takeScreenshot() is called",
162 def check_args(self):
163 if self.wpt_type(self.test) is None:
164 print(
165 """Test path %s is not in wpt directories:
166 testing/web-platform/tests for tests that may be shared
167 testing/web-platform/mozilla/tests for Gecko-only tests"""
168 % self.test
170 return False
172 if not self.reftest:
173 if self.kwargs["ref"]:
174 print("--ref only makes sense for a reftest")
175 return False
177 if self.kwargs["mismatch"]:
178 print("--mismatch only makes sense for a reftest")
179 return False
181 if self.kwargs["wait"]:
182 print("--wait only makes sense for a reftest")
183 return False
184 else:
185 # Set the ref to a url relative to the test
186 if self.kwargs["ref"]:
187 if self.ref_path(self.kwargs["ref"]) is None:
188 print("--ref doesn't refer to a path inside web-platform-tests")
189 return False
191 def __iter__(self):
192 yield (self.test, self._get_template_contents())
194 if self.reftest and self.kwargs["ref"]:
195 ref_path = self.ref_path(self.kwargs["ref"])
196 yield (ref_path, self._get_template_contents(reference=True))
198 def _get_template_contents(self, reference=False):
199 args = {
200 "documentElement": "<html class=reftest-wait>\n"
201 if self.kwargs["wait"]
202 else ""
205 if self.test.rsplit(".", 1)[1] == "js":
206 template = self.template_js
207 if self.kwargs["long_timeout"]:
208 template += self.template_js_long_timeout
209 else:
210 template = self.template_prefix % args
211 if self.kwargs["long_timeout"]:
212 template += self.template_long_timeout
214 if self.reftest:
215 if not reference:
216 args = {
217 "match": "match" if not self.kwargs["mismatch"] else "mismatch",
218 "ref": (
219 self.ref_url(self.kwargs["ref"])
220 if self.kwargs["ref"]
221 else '""'
224 template += self.template_body_reftest % args
225 if self.kwargs["wait"]:
226 template += self.template_body_reftest_wait
227 else:
228 template += "<title></title>"
229 else:
230 template += self.template_body_th
232 return template
234 def update_manifest(self):
235 pass
237 def src_rel_path(self, path):
238 if path is None:
239 return
241 abs_path = os.path.normpath(os.path.abspath(path))
242 return os.path.relpath(abs_path, self.topsrcdir)
244 def wpt_type(self, path):
245 path = self.src_rel_path(path)
246 if path.startswith(self.upstream_path):
247 return "upstream"
248 elif path.startswith(self.local_path):
249 return "local"
250 return None
252 def ref_path(self, path):
253 # The ref parameter can be one of several things
254 # 1. An absolute path to a reference file
255 # 2. A path to a file relative to the topsrcdir
256 # 3. A path relative to the test file
257 # These are not unambiguous, so it's somewhat best effort
259 if os.path.isabs(path):
260 path = os.path.normpath(path)
261 if not path.startswith(self.topsrcdir):
262 # Path is an absolute URL relative to the tests root
263 if path.startswith("/_mozilla/"):
264 base = self.local_path
265 path = path[len("/_mozilla/") :]
266 else:
267 base = self.upstream_path
268 path = path[1:]
269 path = path.replace("/", os.sep)
270 return os.path.join(base, path)
271 else:
272 return self.src_rel_path(path)
273 else:
274 if self.wpt_type(path) is not None:
275 return path
276 else:
277 test_rel_path = self.src_rel_path(
278 os.path.join(os.path.dirname(self.test), path)
280 if self.wpt_type(test_rel_path) is not None:
281 return test_rel_path
282 # Returning None indicates that the path wasn't valid
284 def ref_url(self, path):
285 ref_path = self.ref_path(path)
286 if not ref_path:
287 return
289 if path[0] == "/" and len(path) < len(ref_path):
290 # This is an absolute url
291 return path
293 # Othewise it's a file path
294 wpt_type_ref = self.wpt_type(ref_path)
295 wpt_type_test = self.wpt_type(self.test)
296 if wpt_type_ref == wpt_type_test:
297 return os.path.relpath(ref_path, os.path.dirname(self.test))
299 # If we have a local test referencing an upstream ref,
300 # or vice-versa use absolute paths
301 if wpt_type_ref == "upstream":
302 rel_path = os.path.relpath(ref_path, self.upstream_path)
303 url_base = "/"
304 elif wpt_type_ref == "local":
305 rel_path = os.path.relpath(ref_path, self.local_path)
306 url_base = "/_mozilla/"
307 else:
308 return None
309 return url_base + rel_path.replace(os.path.sep, "/")
312 # Insert a new test in the right place within a given manifest file
313 def write_to_ini_file(manifest_file, filename):
314 # Insert a new test in the right place within a given manifest file
315 manifest = manifestparser.TestManifest(manifests=[manifest_file])
316 insert_before = None
318 if any(t["name"] == filename for t in manifest.tests):
319 print("{} is already in the manifest.".format(filename))
320 return
322 for test in manifest.tests:
323 if test.get("name") > filename:
324 insert_before = test.get("name")
325 break
327 with open(manifest_file, "r") as f:
328 contents = f.readlines()
330 filename = "[{}]\n".format(filename)
332 if not insert_before:
333 contents.append(filename)
334 else:
335 insert_before = "[{}]".format(insert_before)
336 for i in range(len(contents)):
337 if contents[i].startswith(insert_before):
338 contents.insert(i, filename)
339 break
341 with io.open(manifest_file, "w", newline="\n") as f:
342 f.write("".join(contents))
345 TEST_CREATORS = {
346 "mochitest": MochitestCreator,
347 "web-platform-tests": WebPlatformTestsCreator,
348 "xpcshell": XpcshellCreator,
352 def creator_for_suite(suite):
353 if suite.split("-")[0] == "mochitest":
354 base_suite = "mochitest"
355 else:
356 base_suite = suite.rsplit("-", 1)[0]
357 return TEST_CREATORS.get(base_suite)