Debian Codegraph @

testing/web-platform/tests/tools/wpt/create.py

raw
1 # mypy: allow-untyped-defs
2
3 import subprocess
4 import os
5
6 here = os.path.dirname(__file__)
7
8 template_prefix = """<!doctype html>
9 %(documentElement)s<meta charset=utf-8>
10 """
11 template_long_timeout = "<meta name=timeout content=long>\n"
12
13 template_body_th = """<title></title>
14 <script src=/resources/testharness.js></script>
15 <script src=/resources/testharnessreport.js></script>
16 <script>
17
18 </script>
19 """
20
21 template_body_reftest = """<title></title>
22 <link rel=%(match)s href=%(ref)s>
23 """
24
25 template_body_reftest_wait = """<script src="/common/reftest-wait.js"></script>
26 """
27
28 def get_parser():
29 import argparse
30 p = argparse.ArgumentParser()
31 p.add_argument("--no-editor", action="store_true",
32 help="Don't try to open the test in an editor")
33 p.add_argument("-e", "--editor", help="Editor to use")
34 p.add_argument("--long-timeout", action="store_true",
35 help="Test should be given a long timeout (typically 60s rather than 10s, but varies depending on environment)")
36 p.add_argument("--overwrite", action="store_true",
37 help="Allow overwriting an existing test file")
38 p.add_argument("-r", "--reftest", action="store_true",
39 help="Create a reftest rather than a testharness (js) test"),
40 p.add_argument("-m", "--reference", dest="ref", help="Path to the reference file")
41 p.add_argument("--mismatch", action="store_true",
42 help="Create a mismatch reftest")
43 p.add_argument("--wait", action="store_true",
44 help="Create a reftest that waits until takeScreenshot() is called")
45 p.add_argument("--tests-root", default=os.path.join(here, "..", ".."),
46 help="Path to the root of the wpt directory")
47 p.add_argument("path", help="Path to the test file")
48 return p
49
50
51
52 def rel_path(path, tests_root):
53 if path is None:
54 return
55
56 abs_path = os.path.normpath(os.path.abspath(path))
57 return os.path.relpath(abs_path, tests_root)
58
59
60 def run(_venv, **kwargs):
61 path = rel_path(kwargs["path"], kwargs["tests_root"])
62 ref_path = rel_path(kwargs["ref"], kwargs["tests_root"])
63
64 if kwargs["ref"]:
65 kwargs["reftest"] = True
66
67 if ".." in path:
68 print("""Test path %s is not under wpt root.""" % path)
69 return 1
70
71 if ref_path and ".." in ref_path:
72 print("""Reference path %s is not under wpt root""" % ref_path)
73 return 1
74
75
76 if os.path.exists(path) and not kwargs["overwrite"]:
77 print("Test path already exists, pass --overwrite to replace")
78 return 1
79
80 if kwargs["mismatch"] and not kwargs["reftest"]:
81 print("--mismatch only makes sense for a reftest")
82 return 1
83
84 if kwargs["wait"] and not kwargs["reftest"]:
85 print("--wait only makes sense for a reftest")
86 return 1
87
88 args = {"documentElement": "<html class=reftest-wait>\n" if kwargs["wait"] else ""}
89 template = template_prefix % args
90 if kwargs["long_timeout"]:
91 template += template_long_timeout
92
93 if kwargs["reftest"]:
94 args = {"match": "match" if not kwargs["mismatch"] else "mismatch",
95 "ref": os.path.relpath(ref_path, path) if kwargs["ref"] else '""'}
96 template += template_body_reftest % args
97 if kwargs["wait"]:
98 template += template_body_reftest_wait
99 else:
100 template += template_body_th
101 try:
102 os.makedirs(os.path.dirname(path))
103 except OSError:
104 pass
105 with open(path, "w") as f:
106 f.write(template)
107
108 ref_path = kwargs["ref"]
109 if ref_path and not os.path.exists(ref_path):
110 with open(ref_path, "w") as f:
111 f.write(template_prefix % {"documentElement": ""})
112
113 if kwargs["no_editor"]:
114 editor = None
115 elif kwargs["editor"]:
116 editor = kwargs["editor"]
117 elif "VISUAL" in os.environ:
118 editor = os.environ["VISUAL"]
119 elif "EDITOR" in os.environ:
120 editor = os.environ["EDITOR"]
121 else:
122 editor = None
123
124 proc = None
125 if editor:
126 if ref_path:
127 path = f"{path} {ref_path}"
128 proc = subprocess.Popen(f"{editor} {path}", shell=True)
129 else:
130 print("Created test %s" % path)
131
132 if proc:
133 proc.wait()