1 from __future__
import absolute_import
, unicode_literals
, print_function
9 def __init__(self
, topsrcdir
, test
, suite
, doc
, **kwargs
):
10 self
.topsrcdir
= topsrcdir
17 """Perform any validation required for suite-specific arguments"""
21 """Iterate over a list of (path, data) tuples corresponding to the files
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/ */
39 add_task(async function test_TODO() {
40 ok(true, "TODO: implement the test");
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
))
54 write_to_ini_file(manifest_file
, filename
)
57 class MochitestCreator(Creator
):
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:
74 "Sorry, `addtest` doesn't currently know how to add {}".format(
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
):
85 "Sorry, `addtest` doesn't currently know how to add {} with document type {}".format( # NOQA: E501
91 with
open(template_file
) as f
:
94 def update_manifest(self
):
95 # attempt to insert into the appropriate manifest
97 "mochitest-plain": "mochitest.ini",
98 "mochitest-chrome": "chrome.ini",
99 "mochitest-browser-chrome": "browser.ini",
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
))
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>
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>
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"
143 def get_parser(cls
, parser
):
147 help="Test should be given a long timeout "
148 "(typically 60s rather than 10s, but varies depending on environment)",
151 "-m", "--reference", dest
="ref", help="Path to the reference file"
154 "--mismatch", action
="store_true", help="Create a mismatch reftest"
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:
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"""
173 if self
.kwargs
["ref"]:
174 print("--ref only makes sense for a reftest")
177 if self
.kwargs
["mismatch"]:
178 print("--mismatch only makes sense for a reftest")
181 if self
.kwargs
["wait"]:
182 print("--wait only makes sense for a reftest")
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")
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):
200 "documentElement": "<html class=reftest-wait>\n"
201 if self
.kwargs
["wait"]
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
210 template
= self
.template_prefix
% args
211 if self
.kwargs
["long_timeout"]:
212 template
+= self
.template_long_timeout
217 "match": "match" if not self
.kwargs
["mismatch"] else "mismatch",
219 self
.ref_url(self
.kwargs
["ref"])
220 if self
.kwargs
["ref"]
224 template
+= self
.template_body_reftest
% args
225 if self
.kwargs
["wait"]:
226 template
+= self
.template_body_reftest_wait
228 template
+= "<title></title>"
230 template
+= self
.template_body_th
234 def update_manifest(self
):
237 def src_rel_path(self
, path
):
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
):
248 elif path
.startswith(self
.local_path
):
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/") :]
267 base
= self
.upstream_path
269 path
= path
.replace("/", os
.sep
)
270 return os
.path
.join(base
, path
)
272 return self
.src_rel_path(path
)
274 if self
.wpt_type(path
) is not None:
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:
282 # Returning None indicates that the path wasn't valid
284 def ref_url(self
, path
):
285 ref_path
= self
.ref_path(path
)
289 if path
[0] == "/" and len(path
) < len(ref_path
):
290 # This is an absolute url
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
)
304 elif wpt_type_ref
== "local":
305 rel_path
= os
.path
.relpath(ref_path
, self
.local_path
)
306 url_base
= "/_mozilla/"
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
])
318 if any(t
["name"] == filename
for t
in manifest
.tests
):
319 print("{} is already in the manifest.".format(filename
))
322 for test
in manifest
.tests
:
323 if test
.get("name") > filename
:
324 insert_before
= test
.get("name")
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
)
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
)
341 with io
.open(manifest_file
, "w", newline
="\n") as f
:
342 f
.write("".join(contents
))
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"
356 base_suite
= suite
.rsplit("-", 1)[0]
357 return TEST_CREATORS
.get(base_suite
)