Skip to content

Commit

Permalink
Bazel test auto discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
npaun committed Oct 18, 2024
1 parent 275654d commit 83f80f2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 114 deletions.
7 changes: 4 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ http_file(

http_archive(
name = "wpt",
integrity = "sha256-qSEOTIhox20EBQBFsBhvvqNHISNV2COHrz6ozmQfd3k=",
strip_prefix = "wpt-native-glob",
url = "https://github.com/npaun/wpt/archive/refs/tags/native-glob.tar.gz",
build_file = "//:build/BUILD.wpt",
integrity = "sha256-CRySB83UWb4KWe/Uf6GQdxJNvnIsNyV0PvcFCuB7nNY=",
strip_prefix = "wpt-merge_pr_48561",
url = "https://github.com/web-platform-tests/wpt/archive/refs/tags/merge_pr_48561.tar.gz",
)
21 changes: 15 additions & 6 deletions build/BUILD.wpt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
filegroup(
name = "url",
srcs = glob(
include = ["url/**/*"],
allow_empty = True,
directories = glob(
["*"],
exclude = glob(
[
"*",
".*",
],
exclude_directories = 1,
),
visibility = ["//visibility:public"],
exclude_directories = 0,
)

[filegroup(
name = dir,
srcs = glob(["{}/**/*".format(dir)]),
visibility = ["//visibility:public"],
) for dir in directories]
110 changes: 110 additions & 0 deletions build/wpt_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
load("//:build/wd_test.bzl", "wd_test")

# The public entry point is a macro named wpt_test. It first invokes a private
# rule named _wpt_test_gen to access the files in the wpt filegroup and
# generate a corresponding wd-test file. It then invokes the wd_test macro
# to set up the test.

def wpt_test(name, wpt_directory, test_js):
test_gen_rule = "{}@_wpt_test_gen".format(name)
_wpt_test_gen(
name = test_gen_rule,
test_name = name,
wpt_directory = wpt_directory,
test_js = test_js,
)

wd_test(
name = "{}".format(name),
src = test_gen_rule,
args = ["--experimental"],
data = [
"//src/wpt:wpt-test-harness",
test_js,
wpt_directory,
],
)

def _wpt_test_gen_impl(ctx):
src = ctx.actions.declare_file("{}.wd-test".format(ctx.attr.test_name))
ctx.actions.write(
output = src,
content = WPT_TEST_TEMPLATE.format(
test_name = ctx.attr.test_name,
test_js = wd_relative_path(ctx.file.test_js),
modules = generate_external_modules(ctx.attr.wpt_directory.files),
date = "2024-10-10", # FIXME: we don't wanna be hardcoding that type of shizz
),
)

return DefaultInfo(
files = depset([src]),
)

WPT_TEST_TEMPLATE = """
using Workerd = import "/workerd/workerd.capnp";
const unitTests :Workerd.Config = (
services = [
( name = "{test_name}",
worker = (
modules = [
(name = "worker", esModule = embed "{test_js}"),
(name = "harness", esModule = embed "../../../../../workerd/src/wpt/harness.js"),
{modules}
],
bindings = [
(name = "wpt", service = "wpt"),
],
compatibilityDate = "{date}",
compatibilityFlags = ["nodejs_compat_v2", "experimental"],
)
),
(
name = "wpt",
disk = ".",
)
],
);"""

def wd_relative_path(file):
"""
Returns a relative path which can be referenced in the .wd-test file.
This is four directories up from the bazel short_path
"""
return "../" * 4 + file.short_path

def generate_external_modules(files):
"""
Generates a string for all files in the given directory in the specified format.
Example for a JS file:
(name = "url-origin.any.js", esModule = embed "../../../../../wpt/url/url-origin.any.js"),
Example for a JSON file:
(name = "resources/urltestdata.json", json = embed "../../../../../wpt/url/resources/urltestdata.json"),
"""
result = []

for file in files.to_list():
file_path = wd_relative_path(file)
if file.basename.endswith(".js"):
entry = """(name = "{}", esModule = embed "{}")""".format(file.basename, file_path)
elif file.basename.endswith(".json"):
entry = """(name = "{}", json = embed "{}")""".format(file.basename, file_path)
else:
# For other file types, you can add more conditions or skip them
continue

result.append(entry)

return ",\n".join(result)

_wpt_test_gen = rule(
implementation = _wpt_test_gen_impl,
attrs = {
# A string to use as the test name. Used in the wd-test filename and the worker's name
"test_name": attr.string(),
# A file group representing a directory of wpt tests. All files in the group will be embedded.
"wpt_directory": attr.label(),
# A JS file containing the actual test logic.
"test_js": attr.label(allow_single_file = True),
},
)
9 changes: 6 additions & 3 deletions src/workerd/api/wpt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("//:build/wd_test.bzl", "wd_test")
load("//src/workerd/api/wpt:generate-tests.bzl", "gen_wpt_tests")
load("//:build/wpt_test.bzl", "wpt_test")

gen_wpt_tests(glob(["**/*.js"]))
[wpt_test(
name = file.replace("-test.js", ""),
test_js = file,
wpt_directory = "@wpt//:{}".format(file.replace("-test.js", "")),
) for file in glob(["*-test.js"])]
102 changes: 0 additions & 102 deletions src/workerd/api/wpt/generate-tests.bzl

This file was deleted.

0 comments on commit 83f80f2

Please sign in to comment.