diff --git a/.github/workflows/wpt.yml b/.github/workflows/wpt.yml index 66af526..79b4d76 100644 --- a/.github/workflows/wpt.yml +++ b/.github/workflows/wpt.yml @@ -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 diff --git a/webcrypto/tests/README.md b/webcrypto/tests/README.md index 6f6f8c7..b6e5527 100644 --- a/webcrypto/tests/README.md +++ b/webcrypto/tests/README.md @@ -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. diff --git a/webcrypto/tests/subtle_crypto_test.go b/webcrypto/tests/subtle_crypto_test.go index 368840d..840ab9f 100644 --- a/webcrypto/tests/subtle_crypto_test.go +++ b/webcrypto/tests/subtle_crypto_test.go @@ -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 @@ -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 { @@ -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 } diff --git a/webcrypto/tests/test_setup.go b/webcrypto/tests/test_setup_test.go similarity index 84% rename from webcrypto/tests/test_setup.go rename to webcrypto/tests/test_setup_test.go index db066af..e135869 100644 --- a/webcrypto/tests/test_setup.go +++ b/webcrypto/tests/test_setup_test.go @@ -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 @@ -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 @@ -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"]) @@ -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 diff --git a/webcrypto/tests/util/overloads.js b/webcrypto/tests/util/overloads.js new file mode 100644 index 0000000..0cab17d --- /dev/null +++ b/webcrypto/tests/util/overloads.js @@ -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; \ No newline at end of file diff --git a/webcrypto/tests/util/testharness.js b/webcrypto/tests/util/testharness.js index 1a112cf..92ce2b3 100644 --- a/webcrypto/tests/util/testharness.js +++ b/webcrypto/tests/util/testharness.js @@ -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; \ No newline at end of file