Bug 1770047 [wpt PR 34117] - [Clipboard API] Clipboard Web Custom Formats implementat...
[gecko.git] / testing / web-platform / unittestrunner.py
blobf1d59760096200aacc2b8a755e42ea8e6b673c3a
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import argparse
6 import configparser
7 import os
8 import re
9 import subprocess
10 import sys
13 here = os.path.abspath(os.path.dirname(__file__))
15 local_requirements = {
16 b"mozinfo": "testing/mozbase/mozinfo",
17 b"mozlog": "testing/mozbase/mozlog",
18 b"mozdebug": "testing/mozbase/mozdebug",
19 b"marionette_driver": "testing/marionette/client/",
20 b"mozprofile": "testing/mozbase/mozprofile",
21 b"mozprocess": "testing/mozbase/mozprocess",
22 b"mozcrash": "testing/mozbase/mozcrash",
23 b"mozrunner": "testing/mozbase/mozrunner",
24 b"mozleak": "testing/mozbase/mozleak",
25 b"mozversion": "testing/mozbase/mozversion",
28 requirements_re = re.compile(rb"(%s)[^\w]" % b"|".join(local_requirements.keys()))
31 class ReplaceRequirements(object):
32 def __init__(self, top_src_path, tox_path):
33 self.top_src_path = top_src_path
34 self.tox_path = tox_path
35 self.file_cache = {}
37 def __enter__(self):
38 self.file_cache = {}
39 deps = self.read_deps()
40 for dep in deps:
41 self.replace_path(dep)
43 def __exit__(self, *args, **kwargs):
44 for path, data in self.file_cache.items():
45 with open(path, "wb") as f:
46 f.write(data)
48 def read_deps(self):
49 rv = []
50 parser = configparser.ConfigParser()
51 path = os.path.join(self.tox_path, "tox.ini")
52 with open(path) as f:
53 parser.readfp(f)
54 deps = parser.get("testenv", "deps")
55 dep_re = re.compile("(?:.*:\s*)?-r(.*)")
57 # This can break if we start using more features of tox
58 for dep in deps.splitlines():
59 m = dep_re.match(dep)
60 if m:
61 rv.append(m.group(1).replace("{toxinidir}", self.tox_path))
62 return rv
64 def replace_path(self, requirements_path):
65 lines = []
66 with open(requirements_path, "rb") as f:
67 self.file_cache[requirements_path] = f.read()
68 f.seek(0)
69 for line in f:
70 m = requirements_re.match(line)
71 if not m:
72 lines.append(line)
73 else:
74 key = m.group(1)
75 path = local_requirements[key]
76 lines.append(
77 b"-e %s\n"
78 % (os.path.join(self.top_src_path, path).encode("utf8"),)
81 with open(requirements_path, "wb") as f:
82 for line in lines:
83 f.write(line)
85 with open(requirements_path, "rb") as f:
86 print(f.read())
89 def get_parser():
90 parser = argparse.ArgumentParser()
91 parser.add_argument(
92 "--no-tools",
93 dest="tools",
94 action="store_false",
95 default=True,
96 help="Don't run the tools unittests",
98 parser.add_argument(
99 "--no-wptrunner",
100 dest="wptrunner",
101 action="store_false",
102 default=True,
103 help="Don't run the wptrunner unittests",
105 parser.add_argument(
106 "tox_kwargs", nargs=argparse.REMAINDER, help="Arguments to pass through to tox"
108 return parser
111 def run(top_src_dir, tools=True, wptrunner=True, tox_kwargs=None, **kwargs):
112 tox_paths = []
113 if tox_kwargs is None:
114 tox_kwargs = []
115 if tools:
116 tox_paths.append(
117 os.path.join(top_src_dir, "testing", "web-platform", "tests", "tools")
119 if wptrunner:
120 tox_paths.append(
121 os.path.join(
122 top_src_dir, "testing", "web-platform", "tests", "tools", "wptrunner"
126 success = True
128 for tox_path in tox_paths:
129 with ReplaceRequirements(top_src_dir, tox_path):
130 cmd = ["tox"] + tox_kwargs
131 try:
132 subprocess.check_call(cmd, cwd=tox_path)
133 except subprocess.CalledProcessError:
134 success = False
135 return success
138 def main():
139 kwargs = vars(get_parser().parse_args())
140 top_src_path = os.path.abspath(os.path.join(here, os.pardir, os.pardir))
141 return run(top_src_path, **kwargs)
144 if __name__ == "__main__":
145 if not main():
146 sys.exit(1)