-
Notifications
You must be signed in to change notification settings - Fork 400
POC for basic web platform test integration #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
658104d
POC for basic web platform test integration
npaun 8e0aa22
automatically create wd-test files
anonrig 37e8864
attempt to generate files
anonrig 8fdaa29
auto generate dates
anonrig 2f8d316
use native.glob fork of wpt
npaun 275654d
Do not override build file
npaun bb842f7
Bazel test auto discovery
npaun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright (c) 2017-2022 Cloudflare, Inc. | ||
# Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
# https://opensource.org/licenses/Apache-2.0 | ||
|
||
directories = glob( | ||
["*"], | ||
exclude = glob( | ||
["*"], | ||
exclude_directories = 1, | ||
) + [ | ||
".*", | ||
], | ||
exclude_directories = 0, | ||
) | ||
|
||
[filegroup( | ||
name = dir, | ||
srcs = glob(["{}/**/*".format(dir)]), | ||
visibility = ["//visibility:public"], | ||
) for dir in directories] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright (c) 2017-2022 Cloudflare, Inc. | ||
# Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
# https://opensource.org/licenses/Apache-2.0 | ||
|
||
# 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. | ||
|
||
load("//:build/wd_test.bzl", "wd_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, | ||
"//src/workerd/io:trimmed-supported-compatibility-date.txt", | ||
], | ||
) | ||
|
||
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), | ||
), | ||
) | ||
|
||
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 = embed "../../../../../workerd/src/workerd/io/trimmed-supported-compatibility-date.txt", | ||
compatibilityFlags = ["nodejs_compat", "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), | ||
}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) 2017-2022 Cloudflare, Inc. | ||
# Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
# https://opensource.org/licenses/Apache-2.0 | ||
|
||
load("//:build/wpt_test.bzl", "wpt_test") | ||
|
||
[wpt_test( | ||
name = file.replace("-test.js", ""), | ||
test_js = file, | ||
wpt_directory = "@wpt//:{}".format(file.replace("-test.js", "")), | ||
) for file in glob(["*-test.js"])] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
import * as harness from 'harness'; | ||
|
||
export const urlConstructor = { | ||
async test() { | ||
harness.prepare(); | ||
await import('url-constructor.any.js'); | ||
harness.validate(); | ||
jasnell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}; | ||
|
||
export const urlOrigin = { | ||
async test() { | ||
harness.prepare(); | ||
await import('url-origin.any.js'); | ||
harness.validate(); | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
filegroup( | ||
name = "wpt-test-harness", | ||
srcs = glob( | ||
include = ["**/*"], | ||
allow_empty = True, | ||
), | ||
visibility = ["//visibility:public"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
// Copyright © web-platform-tests contributors. BSD license | ||
|
||
import { strictEqual } from 'node:assert'; | ||
npaun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
globalThis.fetch = async (url) => { | ||
const { default: data } = await import(url); | ||
return { | ||
async json() { | ||
return data; | ||
}, | ||
}; | ||
}; | ||
|
||
globalThis.promise_test = (callback) => { | ||
callback(); | ||
}; | ||
|
||
globalThis.assert_equals = (a, b, c) => { | ||
strictEqual(a, b, c); | ||
}; | ||
|
||
globalThis.test = (callback, message) => { | ||
try { | ||
callback(); | ||
} catch (err) { | ||
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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.