Skip to content

Commit

Permalink
Add initial test262 support to test optional chaining
Browse files Browse the repository at this point in the history
For now, we only run two subdirectories and ignore various tests (sloppy mode,
tests expecting syntax error, and TCO tests). This reveals a few issues with the
current implementation, which will be addressed later. Since it's not a passing
build yet, I'll leave it out of CI for now.

Also drop the Travis build for Node 8 since one of the new dependencies requires
Node 10. I won't officially drop Node 8 support just yet, but will soon, and
it's at end-of-life now.
  • Loading branch information
alangpierce committed Dec 30, 2019
1 parent ed3995f commit ed268c5
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
"**/example-runner/**",
"**/generator/**",
"**/test/**",
"**/test262/**",
"**/script/**",
],
optionalDependencies: false,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
dist-self-build
dist-types
example-runner/example-repos
test262/test262-checkout
integrations/gulp-plugin/dist
.nyc_output
coverage.lcov
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ matrix:
script:
- yarn build
- yarn test-only
- node_js: '8'
script:
- yarn build
- yarn test-only
# Exclude the default build; we only want to run explicitly-included builds.
exclude:
node_js: '12'
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"build": "sucrase-node script/build.ts",
"fast-build": "sucrase-node script/build.ts --fast",
"clean": "rm -rf ./build ./dist ./dist-self-build ./dist-types ./example-runner/example-repos",
"clean": "rm -rf ./build ./dist ./dist-self-build ./dist-types ./example-runner/example-repos ./test262/test262-checkout",
"generate": "sucrase-node generator/generate.ts",
"benchmark": "sucrase-node benchmark/benchmark.ts",
"microbenchmark": "sucrase-node benchmark/microbenchmark.ts",
Expand All @@ -28,6 +28,7 @@
"run-examples": "sucrase-node example-runner/example-runner.ts",
"test": "yarn lint && yarn test-only",
"test-only": "mocha './test/**/*.ts'",
"test262": "sucrase-node test262/run-test262.ts",
"test-with-coverage": "nyc mocha './test/**/*.ts'",
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
},
Expand Down Expand Up @@ -58,6 +59,7 @@
"@types/node": "^12.12.7",
"@types/yargs-parser": "^13.1.0",
"@typescript-eslint/parser": "^2.6.1",
"chalk": "2.4.1",
"codecov": "^3.6.1",
"eslint": "^6.6.0",
"eslint-config-airbnb-base": "^14.0.0",
Expand All @@ -69,6 +71,7 @@
"nyc": "^14.1.1",
"prettier": "^1.19.1",
"sucrase": "^3.11.0",
"test262-harness": "^6.5.0",
"ts-interface-builder": "^0.2.1",
"tslint": "^5.20.1",
"typescript": "^3.7.2",
Expand Down
2 changes: 1 addition & 1 deletion script/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function checkSucrase(): Promise<void> {
run(`${TSC} --project . --noEmit`),
run(`${TSLINT} --project .`),
run(
`${ESLINT} ${["benchmark", "example-runner", "generator", "script", "src", "test"]
`${ESLINT} ${["benchmark", "example-runner", "generator", "script", "src", "test", "test262"]
.map((dir) => `'${dir}/**/*.ts'`)
.join(" ")}`,
),
Expand Down
71 changes: 71 additions & 0 deletions test262/run-test262.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!./node_modules/.bin/sucrase-node
/* eslint-disable no-console */
import chalk from "chalk";
import {exec} from "mz/child_process";
import {exists} from "mz/fs";

import run from "../script/run";

const TEST262_HARNESS = "./node_modules/.bin/test262-harness";
const TEST262_DIR = "./test262/test262-checkout";
const TEST262_REPO_URL = "https://github.com/tc39/test262.git";
const TEST262_REVISION = "157b18d16b5d52501c4d75ac422d3a80bfad1c17";

/**
* Run the test262 suite on some tests that we know are useful.
*/
async function main(): Promise<void> {
if (!(await exists(TEST262_DIR))) {
console.log(`Directory ${TEST262_DIR} not found, cloning a new one.`);
await run(`git clone ${TEST262_REPO_URL} ${TEST262_DIR}`);
}

// Force a specific revision so we don't get a breakage from changes to the master branch.
const originalCwd = process.cwd();
try {
process.chdir(TEST262_DIR);
await run(`git reset --hard ${TEST262_REVISION}`);
await run(`git clean -f`);
} catch (e) {
await run("git fetch");
await run(`git reset --hard ${TEST262_REVISION}`);
await run(`git clean -f`);
} finally {
process.chdir(originalCwd);
}

console.log("Running test262...");
const harnessStdout = (
await exec(`${TEST262_HARNESS} \
--preprocessor "./test262/test262Preprocessor.js" \
--reporter "json" \
"${TEST262_DIR}/test/language/expressions/coalesce/**/*.js" \
"${TEST262_DIR}/test/language/expressions/optional-chaining/**/*.js"`)
)[0].toString();

const harnessOutput = JSON.parse(harnessStdout);
let numPassed = 0;
let numFailed = 0;
for (const result of harnessOutput) {
if (!result.result.pass) {
numFailed++;
console.error(`${chalk.bold(chalk.bgRed("FAIL"))} ${chalk.bold(result.file)}`);
console.error(result.result.message);
console.error(`stdout:\n${result.rawResult.stdout}`);
console.error(`stderr:\n${result.rawResult.stderr}`);
console.error();
} else {
numPassed++;
}
}
console.log(`${numPassed} passed, ${numFailed} failed`);
if (numFailed > 0) {
process.exitCode = 1;
}
}

main().catch((e) => {
console.error("Unhandled error:");
console.error(e);
process.exitCode = 1;
});
34 changes: 34 additions & 0 deletions test262/test262Preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const sucrase = require("..");

/**
* test262-harness preprocessor documented here:
https://github.com/bterlson/test262-harness#preprocessor
*/
module.exports = function(test) {
// Sucrase doesn't attempt to throw SyntaxError on bad syntax, so skip those tests.
if (test.attrs.negative) {
return null;
}

// Sucrase assumes strict mode, so skip sloppy mode tests.
if (test.scenario === "default") {
return null;
}

// TCO tests seem to fail in V8 normally, so skip those.
if (test.attrs.features.includes("tail-call-optimization")) {
return null;
}

try {
test.contents = sucrase.transform(test.contents, {transforms: []}).code;
} catch (error) {
test.result = {
stderr: `${error.name}: ${error.message}\n`,
stdout: "",
error,
};
}

return test;
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"generator",
"script",
"src",
"test"
"test",
"test262"
],
"exclude": [
"benchmark/sample",
Expand Down
Loading

0 comments on commit ed268c5

Please sign in to comment.