Skip to content

Commit

Permalink
automatically create wd-test files
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 4, 2024
1 parent e5803d7 commit 9fcd74c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 60 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ new_local_repository(
http_archive(
name = "wpt",
build_file = "//:build/BUILD.wpt",
integrity = "sha256-6mp9wOEkWuACXvSDmf1pL2Dlj5ruukYwQzwkNgcpCdI=",
integrity = "sha256-pJPjzEf1mkkyljFnePpT9MRV1jcfRJg3k9a45CEGxuo=",
strip_prefix = "wpt-merge_pr_47718",
url = "https://github.com/web-platform-tests/wpt/archive/refs/tags/merge_pr_47718.tar.gz",
)
12 changes: 2 additions & 10 deletions src/workerd/api/wpt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
load("//:build/wd_test.bzl", "wd_test")
load("//src/workerd/api/wpt:generate-tests.bzl", "gen_wpt_tests")

wd_test(
name = "url-test",
src = "url-test.wd-test",
args = ["--experimental"],
data = [
"url-test.js",
"//src/wpt:wpt-test-harness",
"@wpt//:url",
],
)
gen_wpt_tests(glob(["**/*.js"]))
62 changes: 62 additions & 0 deletions src/workerd/api/wpt/generate-tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//:build/wd_test.bzl", "wd_test")

# 0 - name of the service
# 1 - es module file path (ex: url-test for url-test.js)
WPT_TEST_TEMPLATE = """
using Workerd = import "/workerd/workerd.capnp";
const unitTests :Workerd.Config = (
services = [
( name = "{}",
worker = (
modules = [
(name = "worker", esModule = embed "{}.js"),
(name = "harness",
esModule = embed "../../../../../workerd/src/wpt/harness.js"),
(name = "url-origin.any.js",
esModule = embed "../../../../../wpt/url/url-origin.any.js"),
(name = "url-constructor.any.js",
esModule = embed "../../../../../wpt/url/url-constructor.any.js"),
(name = "resources/urltestdata.json",
json = embed "../../../../../wpt/url/resources/urltestdata.json"),
(name = "resources/urltestdata-javascript-only.json",
json = embed "../../../../../wpt/url/resources/urltestdata-javascript-only.json"),
],
bindings = [
(name = "wpt", service = "wpt"),
],
compatibilityDate = "2024-07-01",
compatibilityFlags = ["nodejs_compat_v2"],
)
),
(
name = "wpt",
disk = ".",
)
],
);"""

# Example: generate_wd_test_file("url-test")
def generate_wd_test_file(name):
return WPT_TEST_TEMPLATE.format(name, name)

def gen_wpt_tests(files):
for file in files:
name = file.removesuffix(".js")
src = "{}.wd-test".format(name)
write_file(
name = name + "@rule",
out = src,
content = [generate_wd_test_file(name)],
)
wd_test(
name = name,
src = src,
args = ["--experimental"],
data = [
file,
"//src/wpt:wpt-test-harness",
"@wpt//:url",
],
)
28 changes: 11 additions & 17 deletions src/workerd/api/wpt/url-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { strictEqual } from 'assert';

// The harness has no exports of it's own but does modify the global
// scope to include the additional stuff the Web Platform Tests expect.
import * as harness from 'harness';

export const test = {
export const urlConstructor = {
async test() {
// The tests will be run when the module is imported. Note that
// there are limitations to this in that the module is not allowed
// to perform any I/O... so any test that requires setTimeout,
// for instance, will fail... this also means we cannot run
// fetch tests from WPT... sad face.
const foo = await import('url-origin.any.js');
harness.prepare();
await import('url-constructor.any.js');
harness.validate();
},
};

if (globalThis.errors.length > 0) {
for (const err of globalThis.errors) {
console.error(err);
}
throw new Error('Test failed');
}
export const urlOrigin = {
async test() {
harness.prepare();
await import('url-origin.any.js');
harness.validate();
},
};
30 changes: 0 additions & 30 deletions src/workerd/api/wpt/url-test.wd-test

This file was deleted.

16 changes: 14 additions & 2 deletions src/wpt/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ globalThis.test = (callback, message) => {
try {
callback();
} catch (err) {
const aerr = new AggregateError([err], message);
globalThis.errors.push(aerr);
globalThis.errors.push(new AggregateError([err], message));
}
};

globalThis.errors = [];

export function prepare() {
globalThis.errors = [];
}

export function validate() {
if (globalThis.errors.length > 0) {
for (const err of globalThis.errors) {
console.error(err);
}
throw new Error('Test failed');
}
}

0 comments on commit 9fcd74c

Please sign in to comment.