summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/wpt/create.py
blob: 27a23ca9019bddfe71bd473edb35ec6a508e3a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# mypy: allow-untyped-defs

import subprocess
import os

here = os.path.dirname(__file__)

template_prefix = """<!doctype html>
%(documentElement)s<meta charset=utf-8>
"""
template_long_timeout = "<meta name=timeout content=long>\n"

template_body_th = """<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>

</script>
"""

template_body_reftest = """<title></title>
<link rel=%(match)s href=%(ref)s>
"""

template_body_reftest_wait = """<script src="/common/reftest-wait.js"></script>
"""

def get_parser():
    import argparse
    p = argparse.ArgumentParser()
    p.add_argument("--no-editor", action="store_true",
                   help="Don't try to open the test in an editor")
    p.add_argument("-e", "--editor", action="store", help="Editor to use")
    p.add_argument("--long-timeout", action="store_true",
                   help="Test should be given a long timeout (typically 60s rather than 10s, but varies depending on environment)")
    p.add_argument("--overwrite", action="store_true",
                   help="Allow overwriting an existing test file")
    p.add_argument("-r", "--reftest", action="store_true",
                   help="Create a reftest rather than a testharness (js) test"),
    p.add_argument("-m", "--reference", dest="ref", help="Path to the reference file")
    p.add_argument("--mismatch", action="store_true",
                   help="Create a mismatch reftest")
    p.add_argument("--wait", action="store_true",
                   help="Create a reftest that waits until takeScreenshot() is called")
    p.add_argument("--tests-root", action="store", default=os.path.join(here, "..", ".."),
                   help="Path to the root of the wpt directory")
    p.add_argument("path", action="store", help="Path to the test file")
    return p



def rel_path(path, tests_root):
    if path is None:
        return

    abs_path = os.path.normpath(os.path.abspath(path))
    return os.path.relpath(abs_path, tests_root)


def run(_venv, **kwargs):
    path = rel_path(kwargs["path"], kwargs["tests_root"])
    ref_path = rel_path(kwargs["ref"], kwargs["tests_root"])

    if kwargs["ref"]:
        kwargs["reftest"] = True

    if ".." in path:
        print("""Test path %s is not under wpt root.""" % path)
        return 1

    if ref_path and ".." in ref_path:
        print("""Reference path %s is not under wpt root""" % ref_path)
        return 1


    if os.path.exists(path) and not kwargs["overwrite"]:
        print("Test path already exists, pass --overwrite to replace")
        return 1

    if kwargs["mismatch"] and not kwargs["reftest"]:
        print("--mismatch only makes sense for a reftest")
        return 1

    if kwargs["wait"] and not kwargs["reftest"]:
        print("--wait only makes sense for a reftest")
        return 1

    args = {"documentElement": "<html class=reftest-wait>\n" if kwargs["wait"] else ""}
    template = template_prefix % args
    if kwargs["long_timeout"]:
        template += template_long_timeout

    if kwargs["reftest"]:
        args = {"match": "match" if not kwargs["mismatch"] else "mismatch",
                "ref": os.path.relpath(ref_path, path) if kwargs["ref"] else '""'}
        template += template_body_reftest % args
        if kwargs["wait"]:
            template += template_body_reftest_wait
    else:
        template += template_body_th
    try:
        os.makedirs(os.path.dirname(path))
    except OSError:
        pass
    with open(path, "w") as f:
        f.write(template)

    ref_path = kwargs["ref"]
    if ref_path and not os.path.exists(ref_path):
        with open(ref_path, "w") as f:
            f.write(template_prefix % {"documentElement": ""})

    if kwargs["no_editor"]:
        editor = None
    elif kwargs["editor"]:
        editor = kwargs["editor"]
    elif "VISUAL" in os.environ:
        editor = os.environ["VISUAL"]
    elif "EDITOR" in os.environ:
        editor = os.environ["EDITOR"]
    else:
        editor = None

    proc = None
    if editor:
        if ref_path:
            path = f"{path} {ref_path}"
        proc = subprocess.Popen(f"{editor} {path}", shell=True)
    else:
        print("Created test %s" % path)

    if proc:
        proc.wait()