From 168f4a97a002a1ed46503ad09019797eb8350bd2 Mon Sep 17 00:00:00 2001 From: Michael Orchard Date: Wed, 27 Apr 2016 16:06:27 +0200 Subject: [PATCH 1/2] Allow configuration of built in source transformers. --- lib/sandboxed_module.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js index 6bf483c..17aeef0 100644 --- a/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js @@ -8,6 +8,7 @@ var builtinModules = require('./builtin_modules.json'); var parent = module.parent; var globalOptions = {}; var registeredBuiltInSourceTransformers = ['coffee']; +var builtInSourceTransformersConfig = {}; module.exports = SandboxedModule; function SandboxedModule() { @@ -27,7 +28,7 @@ SandboxedModule.load = function(moduleId, options, trace) { var sandboxedModule = new SandboxedModule(); sandboxedModule._init(moduleId, trace, options); - sandboxedModule._compile() + sandboxedModule._compile(); return sandboxedModule; }; @@ -42,10 +43,11 @@ SandboxedModule.configure = function(options) { }); }; -SandboxedModule.registerBuiltInSourceTransformer = function(name) { +SandboxedModule.registerBuiltInSourceTransformer = function(name, config) { if(registeredBuiltInSourceTransformers.indexOf(name) === -1) { - registeredBuiltInSourceTransformers.push(name) + registeredBuiltInSourceTransformers.push(name); } + builtInSourceTransformersConfig[name] = config; }; @@ -308,24 +310,33 @@ function getStartingSourceTransformers() { var builtInSourceTransformers = { coffee: function(source) { if (this.filename.search(/\.coffee$/) !== -1){ - return require('coffee-script').compile(source); + 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 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) {} From 057d5052caf6e6225cf96e0996d40b7e918291ce Mon Sep 17 00:00:00 2001 From: Michael Orchard Date: Wed, 27 Apr 2016 17:07:35 +0200 Subject: [PATCH 2/2] Allow filtering of files transformed by built-in source transformers. --- lib/sandboxed_module.js | 12 ++++++++++-- test/fixture/filteredBaz.js | 8 ++++++++ test/fixture/filteredCoffee.coffee | 5 +++++ test/integration/test-coffee.js | 12 ++++++++++-- test/integration/test-istanbul.js | 20 ++++++++++++++------ 5 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test/fixture/filteredBaz.js create mode 100644 test/fixture/filteredCoffee.coffee diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js index 17aeef0..c68d1e0 100644 --- a/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js @@ -9,6 +9,7 @@ var parent = module.parent; var globalOptions = {}; var registeredBuiltInSourceTransformers = ['coffee']; var builtInSourceTransformersConfig = {}; +var builtInSourceTransformersFilters = {}; module.exports = SandboxedModule; function SandboxedModule() { @@ -43,11 +44,12 @@ SandboxedModule.configure = function(options) { }); }; -SandboxedModule.registerBuiltInSourceTransformer = function(name, config) { +SandboxedModule.registerBuiltInSourceTransformer = function(name, config, filter) { if(registeredBuiltInSourceTransformers.indexOf(name) === -1) { registeredBuiltInSourceTransformers.push(name); } builtInSourceTransformersConfig[name] = config; + builtInSourceTransformersFilters[name] = filter; }; @@ -309,13 +311,19 @@ function getStartingSourceTransformers() { var builtInSourceTransformers = { coffee: function(source) { - if (this.filename.search(/\.coffee$/) !== -1){ + 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 fileFilter = builtInSourceTransformersFilters.istanbul; + if(fileFilter && !fileFilter.test(this.filename)) { + return source; + } + var config = {}; if(builtInSourceTransformersConfig.istanbul) { Object.keys(builtInSourceTransformersConfig.istanbul).forEach(function(option) { diff --git a/test/fixture/filteredBaz.js b/test/fixture/filteredBaz.js new file mode 100644 index 0000000..5b4f4a8 --- /dev/null +++ b/test/fixture/filteredBaz.js @@ -0,0 +1,8 @@ +module.exports = { + biz: function(){ + return 1 + 3; + }, + bang: function() { + return require('./foo') + someLocal + someGlobal + 3; + } +}; diff --git a/test/fixture/filteredCoffee.coffee b/test/fixture/filteredCoffee.coffee new file mode 100644 index 0000000..2cd5fde --- /dev/null +++ b/test/fixture/filteredCoffee.coffee @@ -0,0 +1,5 @@ +class coffeeClass + simpleData: -> + 1 + 1 + +module.exports = coffeeClass diff --git a/test/integration/test-coffee.js b/test/integration/test-coffee.js index 75cb65a..57839cc 100644 --- a/test/integration/test-coffee.js +++ b/test/integration/test-coffee.js @@ -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'); +} diff --git a/test/integration/test-istanbul.js b/test/integration/test-istanbul.js index 1900e80..dee40d1 100644 --- a/test/integration/test-istanbul.js +++ b/test/integration/test-istanbul.js @@ -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);