|
| 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 | +) |
0 commit comments