Skip to content

Commit 59ce311

Browse files
committed
Bazel test auto discovery
1 parent 275654d commit 59ce311

File tree

6 files changed

+137
-115
lines changed

6 files changed

+137
-115
lines changed

WORKSPACE

+4-3
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ http_file(
524524

525525
http_archive(
526526
name = "wpt",
527-
integrity = "sha256-qSEOTIhox20EBQBFsBhvvqNHISNV2COHrz6ozmQfd3k=",
528-
strip_prefix = "wpt-native-glob",
529-
url = "https://github.com/npaun/wpt/archive/refs/tags/native-glob.tar.gz",
527+
build_file = "//:build/BUILD.wpt",
528+
integrity = "sha256-Hxn/D6x6lI9ISlCQFq620sb8x9iXplVzXPV6zumX84A=",
529+
strip_prefix = "wpt-merge_pr_48695",
530+
url = "https://github.com/web-platform-tests/wpt/archive/refs/tags/merge_pr_48695.tar.gz",
530531
)

build/BUILD.wpt

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
filegroup(
2-
name = "url",
3-
srcs = glob(
4-
include = ["url/**/*"],
5-
allow_empty = True,
6-
),
7-
visibility = ["//visibility:public"],
1+
directories = glob(
2+
["*"],
3+
exclude = glob(
4+
["*"],
5+
exclude_directories = 1,
6+
) + [
7+
".*",
8+
],
9+
exclude_directories = 0,
810
)
11+
12+
[filegroup(
13+
name = dir,
14+
srcs = glob(["{}/**/*".format(dir)]),
15+
visibility = ["//visibility:public"],
16+
) for dir in directories]

build/wpt_test.bzl

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
load("//:build/wd_test.bzl", "wd_test")
2+
3+
# The public entry point is a macro named wpt_test. It first invokes a private
4+
# rule named _wpt_test_gen to access the files in the wpt filegroup and
5+
# generate a corresponding wd-test file. It then invokes the wd_test macro
6+
# to set up the test.
7+
8+
def wpt_test(name, wpt_directory, test_js):
9+
test_gen_rule = "{}@_wpt_test_gen".format(name)
10+
_wpt_test_gen(
11+
name = test_gen_rule,
12+
test_name = name,
13+
wpt_directory = wpt_directory,
14+
test_js = test_js,
15+
)
16+
17+
wd_test(
18+
name = "{}".format(name),
19+
src = test_gen_rule,
20+
args = ["--experimental"],
21+
data = [
22+
"//src/wpt:wpt-test-harness",
23+
test_js,
24+
wpt_directory,
25+
"//src/workerd/io:trimmed-supported-compatibility-date.txt",
26+
],
27+
)
28+
29+
def _wpt_test_gen_impl(ctx):
30+
src = ctx.actions.declare_file("{}.wd-test".format(ctx.attr.test_name))
31+
ctx.actions.write(
32+
output = src,
33+
content = WPT_TEST_TEMPLATE.format(
34+
test_name = ctx.attr.test_name,
35+
test_js = wd_relative_path(ctx.file.test_js),
36+
modules = generate_external_modules(ctx.attr.wpt_directory.files),
37+
date = "2024-10-10", # FIXME: we don't wanna be hardcoding that type of shizz
38+
),
39+
)
40+
41+
return DefaultInfo(
42+
files = depset([src]),
43+
)
44+
45+
WPT_TEST_TEMPLATE = """
46+
using Workerd = import "/workerd/workerd.capnp";
47+
const unitTests :Workerd.Config = (
48+
services = [
49+
( name = "{test_name}",
50+
worker = (
51+
modules = [
52+
(name = "worker", esModule = embed "{test_js}"),
53+
(name = "harness", esModule = embed "../../../../../workerd/src/wpt/harness.js"),
54+
{modules}
55+
],
56+
bindings = [
57+
(name = "wpt", service = "wpt"),
58+
],
59+
compatibilityDate = embed "../../../../../workerd/src/workerd/io/trimmed-supported-compatibility-date.txt",
60+
compatibilityFlags = ["nodejs_compat_v2", "experimental"],
61+
)
62+
),
63+
(
64+
name = "wpt",
65+
disk = ".",
66+
)
67+
],
68+
);"""
69+
70+
def wd_relative_path(file):
71+
"""
72+
Returns a relative path which can be referenced in the .wd-test file.
73+
This is four directories up from the bazel short_path
74+
"""
75+
return "../" * 4 + file.short_path
76+
77+
def generate_external_modules(files):
78+
"""
79+
Generates a string for all files in the given directory in the specified format.
80+
Example for a JS file:
81+
(name = "url-origin.any.js", esModule = embed "../../../../../wpt/url/url-origin.any.js"),
82+
Example for a JSON file:
83+
(name = "resources/urltestdata.json", json = embed "../../../../../wpt/url/resources/urltestdata.json"),
84+
"""
85+
result = []
86+
87+
for file in files.to_list():
88+
file_path = wd_relative_path(file)
89+
if file.basename.endswith(".js"):
90+
entry = """(name = "{}", esModule = embed "{}")""".format(file.basename, file_path)
91+
elif file.basename.endswith(".json"):
92+
entry = """(name = "{}", json = embed "{}")""".format(file.basename, file_path)
93+
else:
94+
# For other file types, you can add more conditions or skip them
95+
continue
96+
97+
result.append(entry)
98+
99+
return ",\n".join(result)
100+
101+
_wpt_test_gen = rule(
102+
implementation = _wpt_test_gen_impl,
103+
attrs = {
104+
# A string to use as the test name. Used in the wd-test filename and the worker's name
105+
"test_name": attr.string(),
106+
# A file group representing a directory of wpt tests. All files in the group will be embedded.
107+
"wpt_directory": attr.label(),
108+
# A JS file containing the actual test logic.
109+
"test_js": attr.label(allow_single_file = True),
110+
},
111+
)

src/workerd/api/wpt/BUILD.bazel

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
load("//:build/wd_test.bzl", "wd_test")
2-
load("//src/workerd/api/wpt:generate-tests.bzl", "gen_wpt_tests")
1+
load("//:build/wpt_test.bzl", "wpt_test")
32

4-
gen_wpt_tests(glob(["**/*.js"]))
3+
[wpt_test(
4+
name = file.replace("-test.js", ""),
5+
test_js = file,
6+
wpt_directory = "@wpt//:{}".format(file.replace("-test.js", "")),
7+
) for file in glob(["*-test.js"])]

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

-102
This file was deleted.

src/workerd/io/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ genrule(
242242
outs = ["trimmed-supported-compatibility-date.txt"],
243243
cmd = "tr -d '\n' < $(location supported-compatibility-date.txt) > $(location trimmed-supported-compatibility-date.txt)",
244244
cmd_ps = "(Get-Content $(location supported-compatibility-date.txt) -Raw -Encoding Ascii).TrimEnd() | Set-Content $(location trimmed-supported-compatibility-date.txt) -NoNewLine -Encoding Ascii",
245+
visibility = ["//visibility:public"],
245246
)
246247

247248
capnp_embed(

0 commit comments

Comments
 (0)