Skip to content
Browse files

Make all hooking non-destructive by never unloading already loaded mo…

…dules
  • Loading branch information...
1 parent 23fdf13 commit 10fc72d12744b61d1404e4ca480632c373683f61 @gotwarlost gotwarlost committed Nov 2, 2012
Showing with 14 additions and 21 deletions.
  1. +2 −0 README.md
  2. +5 −1 lib/command/common/run-with-cover.js
  3. +4 −19 lib/hook.js
  4. +1 −1 package.json
  5. +1 −0 test/other/test-hook.js
  6. +1 −0 test/run.js
View
2 README.md
@@ -133,6 +133,8 @@ All the features of istanbul can be accessed as a library using its [public API]
Changelog
---------
+* v0.1.19 : make all hooking non-destructive in that already loaded modules are never reloaded.
+Add self-test mode so that already loaded istanbul modules can be unloaded prior to hooking.
* v0.1.18 : Add option to hook in non-destructive mode; i.e. the require cache is not unloaded when hooking
* v0.1.17 : Export some more objects; undocumented for now
* v0.1.16 : Fix npm keywords for istanbul which expects an array of strings but was being fed a single string with keywords instead
View
6 lib/command/common/run-with-cover.js
@@ -42,7 +42,8 @@ function run(args, commandName, enableHooks, callback) {
verbose: Boolean,
yui: Boolean,
'default-excludes': Boolean,
- print: String
+ print: String,
+ 'self-test': Boolean
},
opts = nopt(config, { v : '--verbose' }, args, 0),
cmdAndArgs = opts.argv.remain,
@@ -120,6 +121,9 @@ function run(args, commandName, enableHooks, callback) {
if (opts.yui) { //EXPERIMENTAL code: do not rely on this in anyway until the docs say it is allowed
hookOpts.postLoadHook = require('../../util/yui-load-hook').getPostLoadHook(matchFn, transformer, opts.verbose);
}
+ if (opts['self-test']) {
+ hook.unloadRequireCache(matchFn);
+ }
hook.hookRequire(matchFn, transformer, hookOpts);
process.once('exit', function () {
var file = path.resolve(reportingDir, 'coverage.json'),
View
23 lib/hook.js
@@ -34,13 +34,7 @@ var fs = require('fs'),
Module = require('module'),
vm = require('vm'),
originalLoader = Module._extensions['.js'],
- originalCreateScript = vm.createScript,
- lastMatcher,
- destructive = true;
-
-function setDestructive(flag) {
- destructive = !!flag;
-}
+ originalCreateScript = vm.createScript;
function transformFn(matcher, transformer, verbose) {
@@ -91,7 +85,6 @@ function unloadRequireCache(matcher) {
*/
function hookRequire(matcher, transformer, options) {
options = options || {};
- lastMatcher = matcher;
var fn = transformFn(matcher, transformer, options.verbose),
postLoadHook = options.postLoadHook &&
typeof options.postLoadHook === 'function' ? options.postLoadHook : null;
@@ -107,22 +100,14 @@ function hookRequire(matcher, transformer, options) {
postLoadHook(filename);
}
};
- if (destructive) {
- unloadRequireCache(matcher);
- }
}
/**
- * unhook `require` to restore it to its original state. Also unloads the modules in
- * the `require` cache that would have matched the matcher provide in the
- * `hookRequire` method.
+ * unhook `require` to restore it to its original state.
* @method unhookRequire
* @static
*/
function unhookRequire() {
Module._extensions['.js'] = originalLoader;
- if (destructive) {
- unloadRequireCache(lastMatcher);
- }
}
/**
* hooks `vm.createScript` to return transformed code out of which a `Script` object will be created.
@@ -155,11 +140,11 @@ function unhookCreateScript() {
}
module.exports = {
- setDestructive: setDestructive,
hookRequire: hookRequire,
unhookRequire: unhookRequire,
hookCreateScript: hookCreateScript,
- unhookCreateScript: unhookCreateScript
+ unhookCreateScript: unhookCreateScript,
+ unloadRequireCache: unloadRequireCache
};
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "istanbul",
- "version": "0.1.18",
+ "version": "0.1.19",
"description": "Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale",
"keywords": [ "coverage", "code coverage", "JS code coverage", "JS coverage" ],
"author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>",
View
1 test/other/test-hook.js
@@ -15,6 +15,7 @@ module.exports = {
cb();
},
tearDown: function (cb) {
+ hook.unloadRequireCache(matcher);
require('module')._extensions['.js'] = currentHook;
cb();
},
View
1 test/run.js
@@ -59,6 +59,7 @@ function runTests(pat, forceCover) {
args = [
path.resolve(__dirname, '..', 'lib', 'cli.js'),
'cover',
+ '--self-test',
'--dir',
coverageDir,
'--report',

0 comments on commit 10fc72d

Please sign in to comment.
Something went wrong with that request. Please try again.