Skip to content

Commit

Permalink
wpt: refactoring and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Oct 28, 2024
1 parent 7085ca1 commit b403b56
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wpt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
set -x
cd webcrypto/tests
sh checkout.sh
go test -race ./... -tags=wpt
go test -timeout 120s -race ./... -tags=wpt
4 changes: 4 additions & 0 deletions webcrypto/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The entry point is the [`checkout.sh`](./checkout.sh) script, which checks out t

If you work on a new web platfrom test, you could easily re-generate patches by running `./generate-patches.sh`.

We try to keep the diff as small as possible, and we aim to upstream the changes to the wpt repository, but still sometimes
we need to overload some wpt's functions locally with the minimal or noop implementation to make the tests work. You could find the overloaded functions in the
[`util/overloads.js`](./utils/overloads.js) file.

**How to use**
1. Run `./checkout.sh` to check out the web-platform-tests sources.
2. Run `go test ../... -tags=wpt` to run the tests.
20 changes: 9 additions & 11 deletions webcrypto/tests/subtle_crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ import (

"github.com/grafana/sobek"
"github.com/stretchr/testify/assert"
"go.k6.io/k6/js/modulestest"
)

const webPlatformTestSuite = "./wpt/WebCryptoAPI/"

func newWebPlatformTestRuntime(t testing.TB) *modulestest.Runtime {
func TestWebPlatformTestSuite(t *testing.T) {
t.Parallel()

// check if the test is running in the correct environment
info, err := os.Stat(webPlatformTestSuite)
if os.IsNotExist(err) || err != nil || !info.IsDir() {
t.Fatalf("The Web Platform Test directory does not exist, err: %s", err)
t.Fatalf(
"The Web Platform Test directory does not exist, err: %s. Please check webcrypto/tests/README.md how to setup it",
err,
)
}

return newConfiguredRuntime(t)
}

func TestWebPlatformTestSuite(t *testing.T) {
t.Parallel()

tests := []struct {
// catalog is the catalog relatively webPlatformTestSuite where to look files
catalog string
Expand Down Expand Up @@ -159,7 +157,7 @@ func TestWebPlatformTestSuite(t *testing.T) {
t.Run(testName, func(t *testing.T) {
t.Parallel()

ts := newWebPlatformTestRuntime(t)
ts := newConfiguredRuntime(t)

gotErr := ts.EventLoop.Start(func() error {
if err := executeTestScripts(ts.VU.Runtime(), webPlatformTestSuite+tt.catalog, tt.files...); err != nil {
Expand All @@ -180,7 +178,7 @@ func TestWebPlatformTestSuite(t *testing.T) {

func executeTestScripts(rt *sobek.Runtime, base string, scripts ...string) error {
for _, script := range scripts {
program, err := CompileFile(base, script)
program, err := compileFile(base, script)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime {
require.NoError(t, err)

// We compile the Web Platform testharness script into a sobek.Program
harnessProgram, err := CompileFile("./util", "testharness.js")
harnessProgram, err := compileFile("./util", "testharness.js")
require.NoError(t, err)

// We execute the harness script in the goja runtime
Expand All @@ -48,7 +48,7 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime {
require.NoError(t, err)

// We compile the Web Platform helpers script into a sobek.Program
helpersProgram, err := CompileFile("./util", "helpers.js")
helpersProgram, err := compileFile("./util", "helpers.js")
require.NoError(t, err)

// We execute the helpers script in the goja runtime
Expand All @@ -57,6 +57,12 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime {
_, err = runtime.VU.Runtime().RunProgram(helpersProgram)
require.NoError(t, err)

// some function overloads for the Web Platform tests
overloads, err := compileFile("./util", "overloads.js")
require.NoError(t, err)
_, err = runtime.VU.Runtime().RunProgram(overloads)
require.NoError(t, err)

m := new(webcrypto.RootModule).NewModuleInstance(runtime.VU)

err = runtime.VU.Runtime().Set("crypto", m.Exports().Named["crypto"])
Expand All @@ -74,8 +80,8 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime {
return runtime
}

// CompileFile compiles a javascript file as a sobek.Program.
func CompileFile(base, name string) (*sobek.Program, error) {
// compileFile compiles a javascript file as a sobek.Program.
func compileFile(base, name string) (*sobek.Program, error) {
filename := path.Join(base, name)

//nolint:forbidigo // Allow os.Open in tests
Expand Down
23 changes: 23 additions & 0 deletions webcrypto/tests/util/overloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// file contains overloads for the web platform test, to make it work with k6
// javascript runtime.


// this is a minimal implementation of the promise_test function
// which used in many web platform tests
function promise_test(fn, name) {
try {
fn();
} catch (e) {
throw Error(`Error in test "${name}": ${e}`);
}
}

// this is a minimal implementation of the done function
// which used in many web platform tests
function done() {}

// this is a minimal implementation of the setup function
function setup() {}

// some tests use the self object, so we need to define it
globalThis.self = globalThis;
22 changes: 0 additions & 22 deletions webcrypto/tests/util/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,3 @@ function assert_in_array(actual, expected, description) {
function assert_unreached(description) {
throw `reached unreachable code, reason: ${description}`
}

// overloads of the some test functions

// this is a minimal implementation of the promise_test function
// which used in many web platform tests
function promise_test(fn, name) {
try {
fn();
} catch (e) {
throw Error(`Error in test "${name}": ${e}`);
}
}

// this is a minimal implementation of the done function
// which used in many web platform tests
function done() {}

// this is a minimal implementation of the setup function
function setup() {}

// some tests use the global object, so we need to define it
const self = globalThis;

0 comments on commit b403b56

Please sign in to comment.