From a1ac262e36f7607591fc9e4d046616113042283e Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 5 Aug 2018 20:43:10 -0400 Subject: [PATCH] initial attempt to run 'esm' against tc39/test262 --- .gitignore | 3 ++ package-lock.json | 10 +++++ package.json | 1 + script/test.js | 2 + script/test262/bootstrap.js | 13 ++++++ script/test262/clone.js | 34 +++++++++++++++ script/test262/copy.js | 27 ++++++++++++ script/test262/inject.js | 87 +++++++++++++++++++++++++++++++++++++ script/test262/tests.js | 19 ++++++++ test/tests.js | 1 + 10 files changed, 197 insertions(+) create mode 100644 script/test262/bootstrap.js create mode 100644 script/test262/clone.js create mode 100644 script/test262/copy.js create mode 100644 script/test262/inject.js create mode 100644 script/test262/tests.js diff --git a/.gitignore b/.gitignore index 37bdebb44..d2c3b55eb 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ /test/fixture/main-hook/symlink/symlink.js /test/fixture/main-hook/symlink/symlink.mjs /test/fixture/scenario/ava-nyc-tsc/*.js + +/test/_external/test262/temp +/test262 diff --git a/package-lock.json b/package-lock.json index 5e5e15cca..1a3d9b0a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15168,6 +15168,16 @@ } } }, + "test262-parser": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/test262-parser/-/test262-parser-2.0.7.tgz", + "integrity": "sha1-cztGv3dZ50fq40tbFNajyNIIKt0=", + "dev": true, + "requires": { + "js-yaml": "^3.2.1", + "through": "^2.3.4" + } + }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", diff --git a/package.json b/package.json index 3e1e98d2a..889cff1b7 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "sleep": "^5.2.3", "sqreen": "^1.21.0", "strip-ansi": "^4.0.0", + "test262-parser": "^2.0.7", "trash": "^4.3.0", "typescript": "^2.9.2", "uglify-es": "^3.3.10", diff --git a/script/test.js b/script/test.js index 08039192d..e72bf3d60 100644 --- a/script/test.js +++ b/script/test.js @@ -6,6 +6,7 @@ const ignorePaths = require("./ignore-paths.js") const path = require("path") const trash = require("./trash.js") const uglify = require("uglify-es").minify +const bootstrapTest262 = require("./test262/bootstrap.js") const argv = require("yargs") .boolean("prod") @@ -114,5 +115,6 @@ Promise cleanRepo(), setupNode() ]) + .then(bootstrapTest262) .then(() => runTests()) .then(() => runTests(true)) diff --git a/script/test262/bootstrap.js b/script/test262/bootstrap.js new file mode 100644 index 000000000..6375e532b --- /dev/null +++ b/script/test262/bootstrap.js @@ -0,0 +1,13 @@ +"use strict" + +const clone = require("./clone.js") +const copy = require("./copy.js") +const inject = require("./inject.js") + +function bootstrap() { + return clone() + .then(copy) + .then(inject) +} + +module.exports = bootstrap diff --git a/script/test262/clone.js b/script/test262/clone.js new file mode 100644 index 000000000..5ca47650f --- /dev/null +++ b/script/test262/clone.js @@ -0,0 +1,34 @@ +"use strict" + +const execa = require("execa") +const trash = require("trash") +const { resolve } = require( "path") + +const rootPath = resolve(".") + +function run(file, args) { + return execa(file, args, { + cwd: rootPath, + reject: false + }) +} + +const test262RepoPath = resolve(rootPath, "test262") + +function clone() { + return trash(test262RepoPath) + .then(() => + run("git", ["--version"]) + ) + .then(({ stdout }) => + console.log("git version: ", stdout) + ) + .then(() => + run("git", ["clone", "--depth", "1", "https://github.com/tc39/test262.git"]) + ) + .catch((e) => { + console.log(e) + }) +} + +module.exports = clone diff --git a/script/test262/copy.js b/script/test262/copy.js new file mode 100644 index 000000000..bd2ef3eae --- /dev/null +++ b/script/test262/copy.js @@ -0,0 +1,27 @@ +"use strict" + +const trash = require("trash") +const { resolve } = require("path") +const { copy } = require("fs-extra") + +const rootPath = resolve(".") +const test262Path = resolve(rootPath, "test262") +const testPath = resolve(rootPath, "test/_external/test262/temp") + +const testDirs = [ + "test/language/module-code", + "harness" +] + +function copyTests() { + return trash(testPath) + .then(() => + Promise.all( + testDirs.map((testDir) => + copy(resolve(test262Path, testDir), resolve(testPath, testDir)) + ) + ) + ) +} + +module.exports = copyTests diff --git a/script/test262/inject.js b/script/test262/inject.js new file mode 100644 index 000000000..a46024202 --- /dev/null +++ b/script/test262/inject.js @@ -0,0 +1,87 @@ +"use strict" + +const test262Parser = require("test262-parser") +const tests = require("./tests") +const { resolve } = require("path") +const { readFile, writeFile } = require("fs-extra") + +const rootPath = resolve(".") +const testPath = resolve(rootPath, "test/_external/test262/temp") +const harnessPath = resolve(testPath, "harness") + +let test262Assert + +function loadTest262Assert() { + return readFile(resolve(harnessPath, "assert.js"), "utf-8") + .then((rawTest) => { + const { contents } = test262Parser.parseFile(rawTest) + test262Assert = contents + }) +} + +const testIncludes = new Map() + +function getInclude(name) { + + const include = testIncludes.get(name) + + if (include) { + return Promise.resolve(include) + } + + return readFile(resolve(harnessPath, name), "utf-8") + .then((rawTest) => { + const { contents } = test262Parser.parseFile(rawTest) + + testIncludes.set(name, contents) + + return contents + }) +} + +function injectIncludes(filepath) { + return readFile(filepath, "utf-8") + .then((rawTest) => { + + const parsedFile = test262Parser.parseFile(rawTest) + + let includes = [] + + if (Reflect.has(parsedFile.attrs, "includes")) { + includes = parsedFile.attrs.includes.map((name) => + getInclude(name) + ) + } + + return Promise.all(includes) + .then((includesContent) => + includesContent.join("\n") + "\n" + rawTest + ) + }) + .catch((e) => + console.log(e) + ) +} + +function inject() { + return loadTest262Assert() + .then(tests) + .then((tests) => + Promise.all( + tests.map((filepath) => + injectIncludes(filepath) + .then((testCode) => + '"use module"\n' + test262Assert + "\n" + testCode + ) + .then((testCodeInjected) => + writeFile(filepath, testCodeInjected) + ) + ) + ) + ) + .catch((e) => + console.log(e) + ) +} + +module.exports = inject diff --git a/script/test262/tests.js b/script/test262/tests.js new file mode 100644 index 000000000..16859e9c6 --- /dev/null +++ b/script/test262/tests.js @@ -0,0 +1,19 @@ +"use strict" + +const globby = require("globby") +const { resolve } = require("path") + +const rootPath = resolve(".") +const testPath = resolve(rootPath, "test/_external/test262/temp") + +function tests() { + return globby( + ["test/language/module-code/*.js"], + { + absolute: true, + cwd: testPath + } + ) +} + +module.exports = tests diff --git a/test/tests.js b/test/tests.js index d48ed405a..b948019c7 100644 --- a/test/tests.js +++ b/test/tests.js @@ -18,6 +18,7 @@ import "./main-hook-tests.mjs" import "./require-hook-tests.js" import "./repl-hook-tests.mjs" import "./scenario-tests.mjs" +import "./_external/test262/index.js" const extensions = Object.assign({}, require.extensions)