Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Babel's unittests in a custom sandbox. #5135

Merged
merged 1 commit into from Jan 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 89 additions & 21 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -10,11 +10,97 @@ import extend from "lodash/extend";
import merge from "lodash/merge";
import assert from "assert";
import chai from "chai";
import "babel-polyfill";
import fs from "fs";
import path from "path";
import vm from "vm";
import Module from "module";

const babelHelpers = eval(buildExternalHelpers(null, "var"));
const moduleCache = {};
const testContext = {
...helpers,
babelHelpers: null,
assert: chai.assert,
transform: babel.transform,
global: null,
};
testContext.global = testContext;
vm.createContext(testContext);

// Initialize the test context with the polyfill, and then freeze the global to prevent implicit
// global creation in tests, which could cause things to bleed between tests.
runModuleInTestContext("babel-polyfill", __filename);

// Populate the "babelHelpers" global with Babel's helper utilities.
runCodeInTestContext(buildExternalHelpers());

/**
* A basic implementation of CommonJS so we can execute `babel-polyfill` inside our test context.
* This allows us to run our unittests
*/
function runModuleInTestContext(id: string, relativeFilename: string) {
let filename;
if (id[0] === ".") {
filename = require.resolve(path.resolve(path.dirname(relativeFilename), id));
} else {
// This code is a gross hack using internal APIs, but we also have the same logic in babel-core
// to resolve presets and plugins, so if this breaks, we'll have even worse issues to deal with.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

const relativeMod = new Module();
relativeMod.id = relativeFilename;
relativeMod.filename = relativeFilename;
relativeMod.paths = Module._nodeModulePaths(path.dirname(relativeFilename));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, for resolving inside node_modules dirs as would be seen by the test file

try {
filename = Module._resolveFilename(id, relativeMod);
} catch (err) {
filename = null;
}

// Expose Node-internal modules if the tests want them. Note, this will not execute inside
// the context's global scope.
if (filename === id) return require(id);
}

if (moduleCache[filename]) return moduleCache[filename].exports;

const module = moduleCache[filename] = {
id: filename,
exports: {},
};
const dirname = path.dirname(filename);
const req = (id) => runModuleInTestContext(id, filename);

const src = fs.readFileSync(filename, "utf8");
const code = `(function (exports, require, module, __filename, __dirname) {${src}\n});`;

vm.runInContext(code, testContext, {
filename,
displayErrors: true,
}).call(module.exports, module.exports, req, module, filename, dirname);

return module.exports;
}

/**
* Run the given snippet of code inside a CommonJS module
*/
function runCodeInTestContext(code: string, opts: {filename?: string} = {}) {
const filename = opts.filename || null;
const dirname = filename ? path.dirname(filename) : null;
const req = filename ? ((id) => runModuleInTestContext(id, filename)) : null;

const module = {
id: filename,
exports: {},
};

// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, the top level this in sloppy commonjs is supposed to be the exports? 😱

// rely on 'this === global'.
const src = `(function(exports, require, module, __filename, __dirname, opts) {${code}\n});`;
return vm.runInContext(src, testContext, {
filename,
displayErrors: true,
})(module.exports, req, module, filename, dirname, opts);
}

function wrapPackagesArray(type, names, optionsDir) {
return (names || []).map(function (val) {
Expand Down Expand Up @@ -68,12 +154,11 @@ function run(task) {

if (execCode) {
const execOpts = getOpts(exec);
const execDirName = path.dirname(exec.loc);
result = babel.transform(execCode, execOpts);
execCode = result.code;

try {
resultExec = runExec(execOpts, execCode, execDirName);
resultExec = runCodeInTestContext(execCode, execOpts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the test files would (default) export a function / promise / boolean instead of relying on top level return, but fixing this would require refactoring all tests 😖

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All internal/non breaking so someone can do it 😄

} catch (err) {
err.message = exec.loc + ": " + err.message;
err.message += codeFrame(execCode);
Expand Down Expand Up @@ -114,23 +199,6 @@ function run(task) {
}
}

function runExec(opts, execCode, execDirname) {
const sandbox = {
...helpers,
babelHelpers,
assert: chai.assert,
transform: babel.transform,
opts,
exports: {},
require(id) {
return require(id[0] === "." ? path.resolve(execDirname, id) : id);
}
};

const fn = new Function(...Object.keys(sandbox), execCode);
return fn.apply(null, Object.values(sandbox));
}

export default function (
fixturesLoc: string,
name: string,
Expand Down