Skip to content

Commit

Permalink
run 'esm' against tc39/test262 (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored and jdalton committed Aug 31, 2018
1 parent 20a42cf commit c6b082c
Show file tree
Hide file tree
Showing 14 changed files with 852 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
/test/fixture/main-hook/symlink/symlink.js
/test/fixture/main-hook/symlink/symlink.mjs
/test/fixture/scenario/ava-nyc-tsc/*.js

/test/vendor/test262/.repo
/test/vendor/test262/.js-tests
/test/vendor/test262/.mjs-tests
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"sleep": "^5.2.3",
"sqreen": "^1.23.0",
"strip-ansi": "^4.0.0",
"test262-parser": "^2.0.7",
"trash": "^4.3.0",
"ts-node": "^7.0.1",
"typescript": "^3.0.3",
Expand Down
2 changes: 2 additions & 0 deletions script/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -114,5 +115,6 @@ Promise
cleanRepo(),
setupNode()
])
.then(bootstrapTest262)
.then(() => runTests())
.then(() => runTests(true))
26 changes: 26 additions & 0 deletions script/test262/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict"

const { resolve } = require("path")
const clone = require("./clone.js")
const copy = require("./copy.js")
const renameFileExtension = require("./file-extension.js")

const rootPath = resolve(".")
const mjsTestPath = resolve(rootPath, "test/vendor/test262/.mjs-tests")

function bootstrap() {
return (
clone()
.then(copy)
// TODO remove:
// temporary measure and can be safily removed once
// the `dynamic import` test PR is merged into test262
// -- BEGIN
.then(clone.dynamicImport)
.then(copy.dynamicImport)
// -- END
.then(() => renameFileExtension(mjsTestPath, "js", "mjs"))
)
}

module.exports = bootstrap
51 changes: 51 additions & 0 deletions script/test262/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict"

const { resolve } = require("path")
const execa = require("execa")
const { removeSync } = require("fs-extra")

const rootPath = resolve(".")
const test262Path = resolve(rootPath, "test/vendor/test262")
const test262RepoPath = resolve(test262Path, ".repo-clone")
// const test262RepoGitPath = resolve(test262RepoPath, ".git")

function run(cwd, file, args) {
return execa(file, args, {
cwd,
reject: false
})
}

function clone() {
removeSync(test262RepoPath)

return run(test262Path, "git", ["--version"]).then(() =>
run(test262Path, "git", [
"clone",
"--depth",
"1",
"https://github.com/tc39/test262.git",
".repo-clone"
])
)
}

// TODO remove:
// this is just a temporary measure and can be safily removed once
// the `dynamic import` test PR is merged into test262
// -- BEGIN
clone.dynamicImport = function () {
return run(test262RepoPath, "git", [
"fetch",
"origin",
"--depth",
"1",
"pull/1588/head:dynamic-import"
]).then(() => run(test262RepoPath, "git", ["checkout", "dynamic-import"]))
// .then(() =>
// trash(test262RepoGitPath)
// )
// -- END
}

module.exports = clone
48 changes: 48 additions & 0 deletions script/test262/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict"

const { resolve } = require("path")
const { copySync, removeSync } = require("fs-extra")

const rootPath = resolve(".")
const test262RepoPath = resolve(rootPath, "test/vendor/test262/.repo-clone")
const jsTestPath = resolve(rootPath, "test/vendor/test262/.js-tests")
const mjsTestPath = resolve(rootPath, "test/vendor/test262/.mjs-tests")

const testDirs = [
"test/language/export",
"test/language/import",
"test/language/module-code",
"harness"
]

function copyTests() {
removeSync(jsTestPath)
removeSync(mjsTestPath)

testDirs.map((testDir) => {
copySync(resolve(test262RepoPath, testDir), resolve(jsTestPath, testDir))
copySync(resolve(test262RepoPath, testDir), resolve(mjsTestPath, testDir))
})
}

// TODO remove:
// temporary measure and can be safily removed once
// the `dynamic import` test PR is merged into test262
// -- BEGIN
const testDirsDynamicImport = ["test/language/module-code/dynamic-import"]

copyTests.dynamicImport = function () {
testDirsDynamicImport.map((testDirDynamicImport) => {
copySync(
resolve(test262RepoPath, testDirDynamicImport),
resolve(jsTestPath, testDirDynamicImport)
)
copySync(
resolve(test262RepoPath, testDirDynamicImport),
resolve(mjsTestPath, testDirDynamicImport)
)
})
}
// -- END

module.exports = copyTests
33 changes: 33 additions & 0 deletions script/test262/file-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { sep } = require("path")
const { readFileSync, renameSync, writeFileSync } = require("fs-extra")
const globby = require("globby")

function renameFileExtension(test262Path, fromExt, toExt) {
const test262Files = globby.sync(
[
`test/language/export/**/*.${fromExt}`,
`test/language/import/**/*.${fromExt}`,
`test/language/module-code/**/*.${fromExt}`
],
{
absolute: true,
cwd: test262Path,
// https://github.com/sindresorhus/globby/issues/38
transform: (entry) => (sep === "\\" ? entry.replace(/\//g, "\\") : entry)
}
)

test262Files.forEach((file) => {
const raw = readFileSync(file, "utf-8")

const content = raw.replace(new RegExp("." + fromExt, "g"), "." + toExt)

writeFileSync(file, content)
})

test262Files.forEach((file) =>
renameSync(file, file.replace("." + fromExt, "." + toExt))
)
}

module.exports = renameFileExtension
27 changes: 27 additions & 0 deletions script/test262/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict"

const globby = require("globby")
const { resolve } = require("path")

const testPath = resolve(__dirname, "../../test/vendor/test262/.test")

exports.getTestFiles = function getTestFiles() {
return globby(
[
"test/language/export/**/*.js",
"test/language/import/**/*.js",
"test/language/module-code/**/*.js"
],
{
absolute: true,
cwd: testPath
}
)
}

exports.getHarnessFiles = function getHarnessFiles() {
return globby(["harness/*.js"], {
absolute: true,
cwd: testPath
})
}
1 change: 1 addition & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import "./main-hook-tests.mjs"
import "./require-hook-tests.js"
import "./repl-hook-tests.mjs"
import "./scenario-tests.mjs"
import "./vendor/test262/index.js"

const extensions = Object.assign({}, require.extensions)

Expand Down
35 changes: 35 additions & 0 deletions test/vendor/test262/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createContext, Script } from "vm"
import { basename, resolve } from "path"
import test262Parser from "test262-parser"
import fs from "fs-extra"
import globby from "globby"
import { stdout } from "process"

// TODO:
const testPath = resolve("../test/vendor/test262/.js-tests")

const harnessFiles = [
"assert.js",
"sta.js",
"doneprintHandle.js",
"fnGlobalObject.js"
]

function getHarnessFiles() {
return globby.sync(["harness/*.js"], {
absolute: true,
cwd: testPath
})
}

const sandbox = createContext(global)

export default function harnessContext() {
getHarnessFiles()
.filter((file) => harnessFiles.includes(basename(file)))
.map((filename) => fs.readFileSync(filename, "utf-8"))
.forEach((rawHarness) => {
const { contents } = test262Parser.parseFile(rawHarness)
new Script(contents).runInContext(sandbox)
})
}

0 comments on commit c6b082c

Please sign in to comment.