Skip to content

Commit 37e8864

Browse files
anonrignpaun
authored andcommitted
attempt to generate files
1 parent 8e0aa22 commit 37e8864

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

src/workerd/api/wpt/generate-tests.bzl

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("//:build/wd_test.bzl", "wd_test")
33

44
# 0 - name of the service
55
# 1 - es module file path (ex: url-test for url-test.js)
6+
# 2 - external modules required for the test suite to succeed
67
WPT_TEST_TEMPLATE = """
78
using Workerd = import "/workerd/workerd.capnp";
89
@@ -12,16 +13,8 @@ const unitTests :Workerd.Config = (
1213
worker = (
1314
modules = [
1415
(name = "worker", esModule = embed "{}.js"),
15-
(name = "harness",
16-
esModule = embed "../../../../../workerd/src/wpt/harness.js"),
17-
(name = "url-origin.any.js",
18-
esModule = embed "../../../../../wpt/url/url-origin.any.js"),
19-
(name = "url-constructor.any.js",
20-
esModule = embed "../../../../../wpt/url/url-constructor.any.js"),
21-
(name = "resources/urltestdata.json",
22-
json = embed "../../../../../wpt/url/resources/urltestdata.json"),
23-
(name = "resources/urltestdata-javascript-only.json",
24-
json = embed "../../../../../wpt/url/resources/urltestdata-javascript-only.json"),
16+
(name = "harness", esModule = embed "../../../../../workerd/src/wpt/harness.js"),
17+
{}
2518
],
2619
bindings = [
2720
(name = "wpt", service = "wpt"),
@@ -38,17 +31,48 @@ const unitTests :Workerd.Config = (
3831
);"""
3932

4033
# Example: generate_wd_test_file("url-test")
41-
def generate_wd_test_file(name):
42-
return WPT_TEST_TEMPLATE.format(name, name)
34+
def generate_wd_test_file(name, modules = ""):
35+
return WPT_TEST_TEMPLATE.format(name, name, modules)
36+
37+
def generate_external_modules(directory):
38+
"""
39+
Generates a string for all files in the given directory in the specified format.
40+
Example for a JS file:
41+
(name = "url-origin.any.js", esModule = embed "../../../../../wpt/url/url-origin.any.js"),
42+
Example for a JSON file:
43+
(name = "resources/urltestdata.json", json = embed "../../../../../wpt/url/resources/urltestdata.json"),
44+
"""
45+
files = native.glob([directory + "/**/*"], allow_empty = True)
46+
result = []
47+
48+
for file in files:
49+
file_name = file.split("/")[-1]
50+
file_path = "../" * 5 + file # Creates the "../../../../../" prefix
51+
52+
if file_name.endswith(".js"):
53+
entry = """(name = {}, esModule = embed "{}"),""".format(file_name, file_path)
54+
elif file_name.endswith(".json"):
55+
entry = """(name = {}, json = embed "{}"),""".format(file_name, file_path)
56+
else:
57+
# For other file types, you can add more conditions or skip them
58+
continue
59+
60+
result.append(entry)
61+
62+
return result.join("")
4363

4464
def gen_wpt_tests(files):
4565
for file in files:
66+
# For url-test.js, it should be url.
67+
# We'll use this to check wpt/ folder and load necessary files.
68+
wpt_directory = file.removesuffix("-test.js")
4669
name = file.removesuffix(".js")
4770
src = "{}.wd-test".format(name)
71+
modules = generate_external_modules("@wpt//:" + wpt_directory)
4872
write_file(
4973
name = name + "@rule",
5074
out = src,
51-
content = [generate_wd_test_file(name)],
75+
content = [generate_wd_test_file(name, modules)],
5276
)
5377
wd_test(
5478
name = name,

0 commit comments

Comments
 (0)