Permalink
Browse files

Add ability to resolve post-require-hook as the main file of a depend…

…ent module in addition to absolute file path
  • Loading branch information...
gotwarlost committed Jan 28, 2013
1 parent e497007 commit 9183649ab1420561a555e530f0476224ba13d2d1
@@ -16,6 +16,7 @@ var Module = require('module'),
formatOption = require('../../util/help-formatter').formatOption,
hook = require('../../hook'),
Report = require('../../report'),
+ resolve = require('resolve'),
DEFAULT_REPORT_FORMAT = 'lcov';
function usage(arg0, command) {
@@ -25,7 +26,7 @@ function usage(arg0, command) {
formatOption('-x <exclude-pattern> [-x <exclude-pattern>]', 'one or more fileset patterns e.g. "**/vendor/**"'),
formatOption('--[no-]default-excludes', 'apply default excludes [ **/node_modules/**, **/test/**, **/tests/** ], defaults to true'),
formatOption('--hook-run-in-context', 'hook vm.runInThisContext in addition to require (supports RequireJS), defaults to false'),
- formatOption('--post-require-hook <file>', 'JS module that exports a function for post-require processing'),
+ formatOption('--post-require-hook <file> | <module>', 'JS module that exports a function for post-require processing'),
formatOption('--report <report-type>', 'report type, one of html, lcov, lcovonly, none, defaults to lcov (= lcov.info + HTML)'),
formatOption('--dir <report-dir>', 'report directory, defaults to ./coverage'),
formatOption('--print <type>', 'type of report to print to console, one of summary (default), detail, both or none'),
@@ -47,7 +48,7 @@ function run(args, commandName, enableHooks, callback) {
print: String,
'self-test': Boolean,
'hook-run-in-context': Boolean,
- 'post-require-hook': path
+ 'post-require-hook': String
},
opts = nopt(config, { v : '--verbose' }, args, 0),
cmdAndArgs = opts.argv.remain,
@@ -121,13 +122,24 @@ function run(args, commandName, enableHooks, callback) {
instrumenter = new Instrumenter({ coverageVariable: coverageVar }),
transformer = instrumenter.instrumentSync.bind(instrumenter),
hookOpts = { verbose: opts.verbose },
+ postRequireHook = opts['post-require-hook'],
postLoadHookFile;
- if (opts['post-require-hook']) {
- postLoadHookFile = path.resolve(opts['post-require-hook']);
+ if (postRequireHook) {
+ postLoadHookFile = path.resolve(postRequireHook);
} else if (opts.yui) { //EXPERIMENTAL code: do not rely on this in anyway until the docs say it is allowed
postLoadHookFile = path.resolve(__dirname, '../../util/yui-load-hook');
}
+
+ if (postRequireHook) {
+ if (!existsSync(postLoadHookFile)) { //assume it is a module name and resolve it
+ try {
+ postLoadHookFile = resolve.sync(postRequireHook, { basedir: process.cwd() });
+ } catch (ex) {
+ if (opts.verbose) { console.error('Unable to resolve [' + postRequireHook + '] as a node module'); }
+ }
+ }
+ }
if (postLoadHookFile) {
if (opts.verbose) { console.log('Use post-load-hook: ' + postLoadHookFile); }
hookOpts.postLoadHook = require(postLoadHookFile)(matchFn, transformer, opts.verbose);
View
@@ -31,7 +31,8 @@
"which": "1.0.x",
"async": "0.1.x",
"abbrev": "1.0.x",
- "wordwrap": "0.0.x"
+ "wordwrap": "0.0.x",
+ "resolve": "0.2.x"
},
"devDependencies": {
"rimraf": "*",

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -125,7 +125,7 @@ module.exports = {
test.done();
});
},
- "should apply post-require-hook correctly": function (test) {
+ "should apply post-require-hook correctly when absolute path specified": function (test) {
helper.setOpts({ lazyHook : true });
run([ 'test/run.js', '-v', '-x', '**/foo.js', '--post-require-hook', 'node_modules/post-require/hook.js' ], function (results) {
test.ok(results.succeeded());
@@ -138,5 +138,26 @@ module.exports = {
test.ok(results.grepError(/PRH: Saw bar\.js/));
test.done();
});
+ },
+ "should apply post-require-hook correctly when module name specified": function (test) {
+ helper.setOpts({ lazyHook : true });
+ run([ 'test/run.js', '-v', '-x', '**/foo.js', '--post-require-hook', 'post-require' ], function (results) {
+ test.ok(results.succeeded());
+ test.ok(results.grepError(/PRH: MatchFn was a function/));
+ test.ok(results.grepError(/PRH: TransformFn was a function/));
+ test.ok(results.grepError(/PRH: Verbose was true/));
+ //yes, post require hook must be called always even when a file is not covered
+ test.ok(results.grepError(/PRH: Saw foo\.js/));
+ //and, of course, for covered files as well
+ test.ok(results.grepError(/PRH: Saw bar\.js/));
+ test.done();
+ });
+ },
+ "should barf when post-require-hook not available": function (test) {
+ run([ 'test/run.js', '-v', '-x', '**/foo.js', '--post-require-hook', 'does-not-exist' ], function (results) {
+ test.ok(!results.succeeded());
+ test.ok(results.grepError(/Unable to resolve \[does-not-exist\] as a node module/));
+ test.done();
+ });
}
};

0 comments on commit 9183649

Please sign in to comment.