Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…dules
  • Loading branch information...
commit 10fc72d12744b61d1404e4ca480632c373683f61 1 parent 23fdf13
Krishnan Anantheswaran gotwarlost authored
2  README.md
Source Rendered
@@ -133,6 +133,8 @@ All the features of istanbul can be accessed as a library using its [public API]
133 133 Changelog
134 134 ---------
135 135
  136 +* v0.1.19 : make all hooking non-destructive in that already loaded modules are never reloaded.
  137 +Add self-test mode so that already loaded istanbul modules can be unloaded prior to hooking.
136 138 * v0.1.18 : Add option to hook in non-destructive mode; i.e. the require cache is not unloaded when hooking
137 139 * v0.1.17 : Export some more objects; undocumented for now
138 140 * v0.1.16 : Fix npm keywords for istanbul which expects an array of strings but was being fed a single string with keywords instead
6 lib/command/common/run-with-cover.js
@@ -42,7 +42,8 @@ function run(args, commandName, enableHooks, callback) {
42 42 verbose: Boolean,
43 43 yui: Boolean,
44 44 'default-excludes': Boolean,
45   - print: String
  45 + print: String,
  46 + 'self-test': Boolean
46 47 },
47 48 opts = nopt(config, { v : '--verbose' }, args, 0),
48 49 cmdAndArgs = opts.argv.remain,
@@ -120,6 +121,9 @@ function run(args, commandName, enableHooks, callback) {
120 121 if (opts.yui) { //EXPERIMENTAL code: do not rely on this in anyway until the docs say it is allowed
121 122 hookOpts.postLoadHook = require('../../util/yui-load-hook').getPostLoadHook(matchFn, transformer, opts.verbose);
122 123 }
  124 + if (opts['self-test']) {
  125 + hook.unloadRequireCache(matchFn);
  126 + }
123 127 hook.hookRequire(matchFn, transformer, hookOpts);
124 128 process.once('exit', function () {
125 129 var file = path.resolve(reportingDir, 'coverage.json'),
23 lib/hook.js
@@ -34,13 +34,7 @@ var fs = require('fs'),
34 34 Module = require('module'),
35 35 vm = require('vm'),
36 36 originalLoader = Module._extensions['.js'],
37   - originalCreateScript = vm.createScript,
38   - lastMatcher,
39   - destructive = true;
40   -
41   -function setDestructive(flag) {
42   - destructive = !!flag;
43   -}
  37 + originalCreateScript = vm.createScript;
44 38
45 39 function transformFn(matcher, transformer, verbose) {
46 40
@@ -91,7 +85,6 @@ function unloadRequireCache(matcher) {
91 85 */
92 86 function hookRequire(matcher, transformer, options) {
93 87 options = options || {};
94   - lastMatcher = matcher;
95 88 var fn = transformFn(matcher, transformer, options.verbose),
96 89 postLoadHook = options.postLoadHook &&
97 90 typeof options.postLoadHook === 'function' ? options.postLoadHook : null;
@@ -107,22 +100,14 @@ function hookRequire(matcher, transformer, options) {
107 100 postLoadHook(filename);
108 101 }
109 102 };
110   - if (destructive) {
111   - unloadRequireCache(matcher);
112   - }
113 103 }
114 104 /**
115   - * unhook `require` to restore it to its original state. Also unloads the modules in
116   - * the `require` cache that would have matched the matcher provide in the
117   - * `hookRequire` method.
  105 + * unhook `require` to restore it to its original state.
118 106 * @method unhookRequire
119 107 * @static
120 108 */
121 109 function unhookRequire() {
122 110 Module._extensions['.js'] = originalLoader;
123   - if (destructive) {
124   - unloadRequireCache(lastMatcher);
125   - }
126 111 }
127 112 /**
128 113 * hooks `vm.createScript` to return transformed code out of which a `Script` object will be created.
@@ -155,11 +140,11 @@ function unhookCreateScript() {
155 140 }
156 141
157 142 module.exports = {
158   - setDestructive: setDestructive,
159 143 hookRequire: hookRequire,
160 144 unhookRequire: unhookRequire,
161 145 hookCreateScript: hookCreateScript,
162   - unhookCreateScript: unhookCreateScript
  146 + unhookCreateScript: unhookCreateScript,
  147 + unloadRequireCache: unloadRequireCache
163 148 };
164 149
165 150
2  package.json
... ... @@ -1,6 +1,6 @@
1 1 {
2 2 "name": "istanbul",
3   - "version": "0.1.18",
  3 + "version": "0.1.19",
4 4 "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",
5 5 "keywords": [ "coverage", "code coverage", "JS code coverage", "JS coverage" ],
6 6 "author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>",
1  test/other/test-hook.js
@@ -15,6 +15,7 @@ module.exports = {
15 15 cb();
16 16 },
17 17 tearDown: function (cb) {
  18 + hook.unloadRequireCache(matcher);
18 19 require('module')._extensions['.js'] = currentHook;
19 20 cb();
20 21 },
1  test/run.js
@@ -59,6 +59,7 @@ function runTests(pat, forceCover) {
59 59 args = [
60 60 path.resolve(__dirname, '..', 'lib', 'cli.js'),
61 61 'cover',
  62 + '--self-test',
62 63 '--dir',
63 64 coverageDir,
64 65 '--report',

0 comments on commit 10fc72d

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