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