nodejs / whatwg-stream

WIP Official support for WHATWG Stream in Node.js
MIT License
37 stars 8 forks source link

[POC]: Porting WPT utilities in Node.js core #2

Open joyeecheung opened 5 years ago

joyeecheung commented 5 years ago

Per the request from @mcollina , this is a proof of concept on how to use the WPT utilities in Node.js core to run WPT outside Node.js core.

Here is an outline of how the WPT currently works in Node.js core:

  1. There are scripts in node-core-utils (git node wpt) that download tests into a specific folder and keep them in sync while maintaining a map of commit hashes. node-core-utils is a stand-alone CLI toolkit available on npm.
  2. There is a WPT driver in Node.js core's test directory that can load a status file about expected failures, prerequisites of tests, etc., run the test in a vm, then display the results in an ad-hoc format.
  3. The actual tests are merely calls to the driver mentioned above, which are used to communicate with the python test runner in Node.js core and to specify the globals that needs to expose to the vm (roughly what gets exposed in the IDL). For example, see the console tests.

On how the current workflow looks like, and how the status file works the detailed documentation can be found in test/wpt/README.md in Node.js core.

These are the steps run to create this PR:

git clone git@github.com:nodejs/whatwg-stream.git
cd whatwg-stream

# See https://github.com/nodejs/node/tree/2742f38/test/wpt#how-to-add-tests-for-a-new-module
mkdir -p test/wpt/status
echo "{}" > test/wpt/status/streams.json

# Pull the WPT subtree. `npm install -g node-core-utils` if you don't have it already
git node wpt streams    # for stream tests
git node wpt resources # for test harness
# Note: whatwg streams don't currently have IDL tests
# otherwise we need to run `git node wpt interfaces` to pull the IDL

# Copy the test driver
mkdir -p test/common
curl https://raw.githubusercontent.com/nodejs/node/master/test/common/wpt.js -o test/common/wpt.js

# Download and install the reference implementation for POC
git submodule add git@github.com:whatwg/streams.git streams
cd streams/reference-implementation
npm install

# Mock Node.js core test utilities
vim test/common/index.js
vim test/common/fixtures.js
# Write the test script
vim test/wpt/test-whatwg-streams.js
# Write package.json
vim package.json

Result:

npm run test

> whatwg-stream@0.0.1 test /Users/joyee/projects/whatwg-stream
> node --expose-internals --experimental-worker --expose_gc test/wpt/test-whatwg-streams.js

(node:62844) ExperimentalWarning: The fs.promises API is experimental
---- byte-length-queuing-strategy.any.js ----
[PASSED] Can construct a ByteLengthQueuingStrategy with a valid high water mark
[PASSED] Can construct a ByteLengthQueuingStrategy with any value as its high water mark
[PASSED] ByteLengthQueuingStrategy constructor behaves as expected with strange arguments
[PASSED] ByteLengthQueuingStrategy size behaves as expected with strange arguments
[PASSED] ByteLengthQueuingStrategy.prototype.size should work generically on its this and its arguments
[PASSED] ByteLengthQueuingStrategy instances have the correct properties
[PASSED] ByteLengthQueuingStrategy's highWaterMark property can be set to anything
[PASSED] ByteLengthQueuingStrategy.name is correct
---- count-queuing-strategy.any.js ----
[PASSED] Can construct a CountQueuingStrategy with a valid high water mark
[PASSED] Can construct a CountQueuingStrategy with any value as its high water mark
[PASSED] CountQueuingStrategy constructor behaves as expected with strange arguments
[PASSED] CountQueuingStrategy.prototype.size should work generically on its this and its arguments
[PASSED] CountQueuingStrategy size behaves as expected with strange arguments
[PASSED] CountQueuingStrategy instances have the correct properties
[PASSED] CountQueuingStrategy's highWaterMark property can be set to anything
[PASSED] CountQueuingStrategy.name is correct

As you can see here, we are not quite there yet - the WPT runner in Node.js core currently only runs tests found in the top level of the specified directory, but the stream tests are placed in subdirectories - though it should be easy to fix with some recursive readdirs in https://github.com/nodejs/node/blob/2742f38/test/common/wpt.js

AFAICT, the differences between the WPT runner and https://github.com/domenic/wpt-runner which is used to run the tests of the reference implementation are:

  1. wpt-runner uses JSDOM, the WPT runner in Node.js core only has a very thin abstraction over vm (this also means that the Node.js core runner cannot run tests in HTML files and doesn't understand a lot of browser stuff).
  2. wpt-runner runs a clone of WPT (managed as a git submodule), the WPT runner in Node.js core only runs a subset of WPT that can be checked in since we don't do git submodules in Node.js core. We also need to pin the version of tests rolled into our repository, it's somewhat convenient to maintain if different subsets are versioned independently as we are still in the process of porting and fixing more tests.
  3. wpt-runner does not seem to support status files, Node.js core has a custom status file format in order to play nice with our python test runner and the Jenkins CI, as the implementations in Node.js core are not always spec-compliant, or not spec-compliant in special builds e.g. builds without i18n support.

I don't have an opinion on how the tests should be run in this repo, but I hope an explanation would help since the this project may eventually end up being in Node.js core.

cc @domenic

mcollina commented 5 years ago

Good work! I'll go through this asap, and use it as a basis for an initial implementation PR.