Bug 1856942: part 5) Factor async loading of a sheet out of `Loader::LoadSheet`....
[gecko.git] / testing / addtest.py
blob893fbe576a95329a91b80d0eb1deb03dccaa2ac1
1 import io
2 import os
4 import manifestparser
7 class Creator(object):
8 def __init__(self, topsrcdir, test, suite, doc, **kwargs):
9 self.topsrcdir = topsrcdir
10 self.test = test
11 self.suite = suite
12 self.doc = doc
13 self.kwargs = kwargs
15 def check_args(self):
16 """Perform any validation required for suite-specific arguments"""
17 return True
19 def __iter__(self):
20 """Iterate over a list of (path, data) tuples corresponding to the files
21 to be created"""
22 yield (self.test, self._get_template_contents())
24 def _get_template_contents(self, **kwargs):
25 raise NotImplementedError
27 def update_manifest(self):
28 """Perform any manifest updates required to register the added tests"""
29 raise NotImplementedError
32 class XpcshellCreator(Creator):
33 template_body = """/* Any copyright is dedicated to the Public Domain.
34 http://creativecommons.org/publicdomain/zero/1.0/ */
36 "use strict";
38 add_task(async function test_TODO() {
39 ok(true, "TODO: implement the test");
40 });
41 """
43 def _get_template_contents(self):
44 return self.template_body
46 def update_manifest(self):
47 manifest_file = os.path.join(os.path.dirname(self.test), "xpcshell.ini")
48 filename = os.path.basename(self.test)
50 if not os.path.isfile(manifest_file):
51 print("Could not open manifest file {}".format(manifest_file))
52 return
53 write_to_ini_file(manifest_file, filename)
56 class MochitestCreator(Creator):
57 templates = {
58 "mochitest-browser-chrome": "browser.template.txt",
59 "mochitest-plain": "plain%(doc)s.template.txt",
60 "mochitest-chrome": "chrome%(doc)s.template.txt",
63 def _get_template_contents(self):
64 mochitest_templates = os.path.abspath(
65 os.path.join(os.path.dirname(__file__), "mochitest", "static")
67 template_file_name = None
69 template_file_name = self.templates.get(self.suite)
71 if template_file_name is None:
72 print(
73 "Sorry, `addtest` doesn't currently know how to add {}".format(
74 self.suite
77 return None
79 template_file_name = template_file_name % {"doc": self.doc}
81 template_file = os.path.join(mochitest_templates, template_file_name)
82 if not os.path.isfile(template_file):
83 print(
84 "Sorry, `addtest` doesn't currently know how to add {} with document type {}".format( # NOQA: E501
85 self.suite, self.doc
88 return None
90 with open(template_file) as f:
91 return f.read()
93 def update_manifest(self):
94 # attempt to insert into the appropriate manifest
95 guessed_ini = {
96 "mochitest-plain": "mochitest.ini",
97 "mochitest-chrome": "chrome.ini",
98 "mochitest-browser-chrome": "browser.ini",
99 }[self.suite]
100 manifest_file = os.path.join(os.path.dirname(self.test), guessed_ini)
101 filename = os.path.basename(self.test)
103 if not os.path.isfile(manifest_file):
104 print("Could not open manifest file {}".format(manifest_file))
105 return
107 write_to_ini_file(manifest_file, filename)
110 class WebPlatformTestsCreator(Creator):
111 template_prefix = """<!doctype html>
112 %(documentElement)s<meta charset=utf-8>
114 template_long_timeout = "<meta name=timeout content=long>\n"
116 template_body_th = """<title></title>
117 <script src=/resources/testharness.js></script>
118 <script src=/resources/testharnessreport.js></script>
119 <script>
121 </script>
124 template_body_reftest = """<title></title>
125 <link rel=%(match)s href=%(ref)s>
128 template_body_reftest_wait = """<script src="/common/reftest-wait.js"></script>
131 template_js = ""
132 template_js_long_timeout = "//META: timeout=long\n"
134 upstream_path = os.path.join("testing", "web-platform", "tests")
135 local_path = os.path.join("testing", "web-platform", "mozilla", "tests")
137 def __init__(self, *args, **kwargs):
138 super(WebPlatformTestsCreator, self).__init__(*args, **kwargs)
139 self.reftest = self.suite == "web-platform-tests-reftest"
141 @classmethod
142 def get_parser(cls, parser):
143 parser.add_argument(
144 "--long-timeout",
145 action="store_true",
146 help="Test should be given a long timeout "
147 "(typically 60s rather than 10s, but varies depending on environment)",
149 parser.add_argument(
150 "-m", "--reference", dest="ref", help="Path to the reference file"
152 parser.add_argument(
153 "--mismatch", action="store_true", help="Create a mismatch reftest"
155 parser.add_argument(
156 "--wait",
157 action="store_true",
158 help="Create a reftest that waits until takeScreenshot() is called",
161 def check_args(self):
162 if self.wpt_type(self.test) is None:
163 print(
164 """Test path %s is not in wpt directories:
165 testing/web-platform/tests for tests that may be shared
166 testing/web-platform/mozilla/tests for Gecko-only tests"""
167 % self.test
169 return False
171 if not self.reftest:
172 if self.kwargs["ref"]:
173 print("--ref only makes sense for a reftest")
174 return False
176 if self.kwargs["mismatch"]:
177 print("--mismatch only makes sense for a reftest")
178 return False
180 if self.kwargs["wait"]:
181 print("--wait only makes sense for a reftest")
182 return False
183 else:
184 # Set the ref to a url relative to the test
185 if self.kwargs["ref"]:
186 if self.ref_path(self.kwargs["ref"]) is None:
187 print("--ref doesn't refer to a path inside web-platform-tests")
188 return False
190 def __iter__(self):
191 yield (self.test, self._get_template_contents())
193 if self.reftest and self.kwargs["ref"]:
194 ref_path = self.ref_path(self.kwargs["ref"])
195 yield (ref_path, self._get_template_contents(reference=True))
197 def _get_template_contents(self, reference=False):
198 args = {
199 "documentElement": "<html class=reftest-wait>\n"
200 if self.kwargs["wait"]
201 else ""
204 if self.test.rsplit(".", 1)[1] == "js":
205 template = self.template_js
206 if self.kwargs["long_timeout"]:
207 template += self.template_js_long_timeout
208 else:
209 template = self.template_prefix % args
210 if self.kwargs["long_timeout"]:
211 template += self.template_long_timeout
213 if self.reftest:
214 if not reference:
215 args = {
216 "match": "match" if not self.kwargs["mismatch"] else "mismatch",
217 "ref": (
218 self.ref_url(self.kwargs["ref"])
219 if self.kwargs["ref"]
220 else '""'
223 template += self.template_body_reftest % args
224 if self.kwargs["wait"]:
225 template += self.template_body_reftest_wait
226 else:
227 template += "<title></title>"
228 else:
229 template += self.template_body_th
231 return template
233 def update_manifest(self):
234 pass
236 def src_rel_path(self, path):
237 if path is None:
238 return
240 abs_path = os.path.normpath(os.path.abspath(path))
241 return os.path.relpath(abs_path, self.topsrcdir)
243 def wpt_type(self, path):
244 path = self.src_rel_path(path)
245 if path.startswith(self.upstream_path):
246 return "upstream"
247 elif path.startswith(self.local_path):
248 return "local"
249 return None
251 def ref_path(self, path):
252 # The ref parameter can be one of several things
253 # 1. An absolute path to a reference file
254 # 2. A path to a file relative to the topsrcdir
255 # 3. A path relative to the test file
256 # These are not unambiguous, so it's somewhat best effort
258 if os.path.isabs(path):
259 path = os.path.normpath(path)
260 if not path.startswith(self.topsrcdir):
261 # Path is an absolute URL relative to the tests root
262 if path.startswith("/_mozilla/"):
263 base = self.local_path
264 path = path[len("/_mozilla/") :]
265 else:
266 base = self.upstream_path
267 path = path[1:]
268 path = path.replace("/", os.sep)
269 return os.path.join(base, path)
270 else:
271 return self.src_rel_path(path)
272 else:
273 if self.wpt_type(path) is not None:
274 return path
275 else:
276 test_rel_path = self.src_rel_path(
277 os.path.join(os.path.dirname(self.test), path)
279 if self.wpt_type(test_rel_path) is not None:
280 return test_rel_path
281 # Returning None indicates that the path wasn't valid
283 def ref_url(self, path):
284 ref_path = self.ref_path(path)
285 if not ref_path:
286 return
288 if path[0] == "/" and len(path) < len(ref_path):
289 # This is an absolute url
290 return path
292 # Othewise it's a file path
293 wpt_type_ref = self.wpt_type(ref_path)
294 wpt_type_test = self.wpt_type(self.test)
295 if wpt_type_ref == wpt_type_test:
296 return os.path.relpath(ref_path, os.path.dirname(self.test))
298 # If we have a local test referencing an upstream ref,
299 # or vice-versa use absolute paths
300 if wpt_type_ref == "upstream":
301 rel_path = os.path.relpath(ref_path, self.upstream_path)
302 url_base = "/"
303 elif wpt_type_ref == "local":
304 rel_path = os.path.relpath(ref_path, self.local_path)
305 url_base = "/_mozilla/"
306 else:
307 return None
308 return url_base + rel_path.replace(os.path.sep, "/")
311 # Insert a new test in the right place within a given manifest file
312 def write_to_ini_file(manifest_file, filename):
313 # Insert a new test in the right place within a given manifest file
314 manifest = manifestparser.TestManifest(manifests=[manifest_file])
315 insert_before = None
317 if any(t["name"] == filename for t in manifest.tests):
318 print("{} is already in the manifest.".format(filename))
319 return
321 for test in manifest.tests:
322 if test.get("name") > filename:
323 insert_before = test.get("name")
324 break
326 with open(manifest_file, "r") as f:
327 contents = f.readlines()
329 filename = "[{}]\n".format(filename)
331 if not insert_before:
332 contents.append(filename)
333 else:
334 insert_before = "[{}]".format(insert_before)
335 for i in range(len(contents)):
336 if contents[i].startswith(insert_before):
337 contents.insert(i, filename)
338 break
340 with io.open(manifest_file, "w", newline="\n") as f:
341 f.write("".join(contents))
344 TEST_CREATORS = {
345 "mochitest": MochitestCreator,
346 "web-platform-tests": WebPlatformTestsCreator,
347 "xpcshell": XpcshellCreator,
351 def creator_for_suite(suite):
352 if suite.split("-")[0] == "mochitest":
353 base_suite = "mochitest"
354 else:
355 base_suite = suite.rsplit("-", 1)[0]
356 return TEST_CREATORS.get(base_suite)