Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions lib/sandboxed_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var builtinModules = require('./builtin_modules.json');
var parent = module.parent;
var globalOptions = {};
var registeredBuiltInSourceTransformers = ['coffee'];
var builtInSourceTransformersConfig = {};
var builtInSourceTransformersFilters = {};

module.exports = SandboxedModule;
function SandboxedModule() {
Expand All @@ -27,7 +29,7 @@ SandboxedModule.load = function(moduleId, options, trace) {

var sandboxedModule = new SandboxedModule();
sandboxedModule._init(moduleId, trace, options);
sandboxedModule._compile()
sandboxedModule._compile();
return sandboxedModule;
};

Expand All @@ -42,10 +44,12 @@ SandboxedModule.configure = function(options) {
});
};

SandboxedModule.registerBuiltInSourceTransformer = function(name) {
SandboxedModule.registerBuiltInSourceTransformer = function(name, config, filter) {
if(registeredBuiltInSourceTransformers.indexOf(name) === -1) {
registeredBuiltInSourceTransformers.push(name)
registeredBuiltInSourceTransformers.push(name);
}
builtInSourceTransformersConfig[name] = config;
builtInSourceTransformersFilters[name] = filter;
};


Expand Down Expand Up @@ -307,25 +311,40 @@ function getStartingSourceTransformers() {

var builtInSourceTransformers = {
coffee: function(source) {
if (this.filename.search(/\.coffee$/) !== -1){
return require('coffee-script').compile(source);
var fileFilter = builtInSourceTransformersFilters.coffee;
if (this.filename.search(/\.coffee$/) !== -1 && (!fileFilter || fileFilter.test(this.filename))){
return require('coffee-script').compile(source, builtInSourceTransformersConfig.coffee);
} else {
return source;
}
},
istanbul: function(source) {
var coverageVariable, istanbulCoverageMayBeRunning = false;
Object.keys(global).forEach(function(name) {
if((name.indexOf("$$cov_") === 0 || name === '__coverage__') && global[name]) {
istanbulCoverageMayBeRunning = true;
coverageVariable = name;
}
});
var fileFilter = builtInSourceTransformersFilters.istanbul;
if(fileFilter && !fileFilter.test(this.filename)) {
return source;
}

var config = {};
if(builtInSourceTransformersConfig.istanbul) {
Object.keys(builtInSourceTransformersConfig.istanbul).forEach(function(option) {
config[option] = builtInSourceTransformersConfig.istanbul[option];
});
}

var istanbulCoverageMayBeRunning = config.coverageVariable && global[config.coverageVariable];
if(!istanbulCoverageMayBeRunning) {
Object.keys(global).forEach(function(name) {
if((name.indexOf("$$cov_") === 0 || name === '__coverage__') && global[name]) {
istanbulCoverageMayBeRunning = true;
config.coverageVariable = name;
}
});
}

if(istanbulCoverageMayBeRunning) {
try {
var istanbul = require('istanbul'),
instrumenter = new istanbul.Instrumenter({coverageVariable: coverageVariable}),
instrumenter = new istanbul.Instrumenter(config),
instrumentMethod = instrumenter.instrumentSync.bind(instrumenter);
source = instrumentMethod(source, this.filename);
} catch(e) {}
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/filteredBaz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
biz: function(){
return 1 + 3;
},
bang: function() {
return require('./foo') + someLocal + someGlobal + 3;
}
};
5 changes: 5 additions & 0 deletions test/fixture/filteredCoffee.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class coffeeClass
simpleData: ->
1 + 1

module.exports = coffeeClass
12 changes: 10 additions & 2 deletions test/integration/test-coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ try {
hasCoffee = true;
} catch (e) {}

if (hasCoffee) {
var CoffeeClass = SandboxedModule.load('../fixture/coffeeClass').exports;
function testCoffee(file) {
var CoffeeClass = SandboxedModule.load(file).exports;
assert.strictEqual(new CoffeeClass().simpleData(), 2);
}

if (hasCoffee) {
testCoffee('../fixture/coffeeClass');

SandboxedModule.registerBuiltInSourceTransformer('coffee', null, /.*filtered.*$/);
assert.throws(testCoffee.bind(null, '../fixture/coffeeClass'));
testCoffee('../fixture/filteredCoffee');
}
20 changes: 14 additions & 6 deletions test/integration/test-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ var assert = require('assert');
var SandboxedModule = require('../..');
SandboxedModule.registerBuiltInSourceTransformer('istanbul');

function testIt(coverageVariable) {
var rawFunction = /^\s*function \(\){\s*return 1 \+ 3;\s*}\s*$/,
instrumentedFunction = /^function \(\){__cov_.*\.f\['1'\]\+\+;__cov_.*\.s\['2'\]\+\+;return 1\+3;}$/;

function testIt(file, coverageVariable, functionMatch) {
global[coverageVariable] = {};
var baz = SandboxedModule.load('../fixture/baz').exports,
instrumentedFunction = /^function \(\){__cov_.*\.f\['1'\]\+\+;__cov_.*\.s\['2'\]\+\+;return 1\+3;}$/;
var baz = SandboxedModule.load(file).exports;

assert.strictEqual(baz.biz.toString().match(instrumentedFunction).length, 1);
assert.strictEqual(baz.biz.toString().match(functionMatch).length, 1);

delete global[coverageVariable];
}

testIt('$$cov_1234');
testIt('__coverage__');
testIt('../fixture/baz', '$$cov_1234', instrumentedFunction);
testIt('../fixture/baz', '__coverage__', instrumentedFunction);

SandboxedModule.registerBuiltInSourceTransformer('istanbul', null, /.*filtered.*$/);
testIt('../fixture/baz', '$$cov_1234', rawFunction);
testIt('../fixture/baz', '__coverage__', rawFunction);
testIt('../fixture/filteredBaz', '$$cov_1234', instrumentedFunction);
testIt('../fixture/filteredBaz', '__coverage__', instrumentedFunction);