From b4ff36a41b242c0b379d3c27fb30818de54fe2d4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 14 Apr 2011 23:33:43 -0700 Subject: [PATCH] Add --cov code coverage option --- lib/module.js | 16 ++++++++++++++++ src/node.cc | 6 ++++++ src/node.js | 17 +++++++++++++++++ test/common.js | 4 ++++ 4 files changed, 43 insertions(+) diff --git a/lib/module.js b/lib/module.js index d806a4ae821..308fd4e8e30 100644 --- a/lib/module.js +++ b/lib/module.js @@ -347,6 +347,22 @@ Module.prototype._compile = function(content, filename) { // remove shebang content = content.replace(/^\#\!.*/, ''); + // add coverage + if (process.cov) { + var lines = content.split('\n'); + + if (lines.length > 0) { + lines[0] = '__cov[__filename] = { 0: true}; ' + lines[0]; + } + + for (var i = 0; i < lines.length; i++) { + lines[i] = + lines[i].replace(/;$/, '; __cov[__filename][' + i + '] = true;'); + } + + content = lines.join('\n'); + } + function require(path) { return Module._load(path, self); } diff --git a/src/node.cc b/src/node.cc index ad96512ff01..f36cd73bbab 100644 --- a/src/node.cc +++ b/src/node.cc @@ -107,6 +107,7 @@ static char *eval_string = NULL; static int option_end_index = 0; static bool use_debug_agent = false; static bool debug_wait_connect = false; +static bool cov = false; static int debug_port=5858; static int max_stack_size = 0; @@ -2029,6 +2030,7 @@ Handle SetupProcessObject(int argc, char *argv[]) { process->Set(String::NewSymbol("ENV"), ENV); process->Set(String::NewSymbol("pid"), Integer::New(getpid())); + process->Set(String::NewSymbol("cov"), cov ? True() : False()); // -e, --eval if (eval_string) { @@ -2171,6 +2173,7 @@ static void PrintHelp() { " --v8-options print v8 command line options\n" " --vars print various compiled-in variables\n" " --max-stack-size=val set max v8 stack size (bytes)\n" + " --cov code coverage; writes node-cov.json \n" "\n" "Enviromental variables:\n" "NODE_PATH ':'-separated list of directories\n" @@ -2193,6 +2196,9 @@ static void ParseArgs(int argc, char **argv) { if (strstr(arg, "--debug") == arg) { ParseDebugOpt(arg); argv[i] = const_cast(""); + } else if (!strcmp(arg, "--cov")) { + cov = true; + argv[i] = const_cast(""); } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) { printf("%s\n", NODE_VERSION); exit(0); diff --git a/src/node.js b/src/node.js index 8b1b2f6d3b4..d03cbd1a633 100644 --- a/src/node.js +++ b/src/node.js @@ -111,6 +111,9 @@ global.GLOBAL = global; global.root = global; global.Buffer = NativeModule.require('buffer').Buffer; + if (process.cov) { + global.__cov = {}; + } }; startup.globalTimeouts = function() { @@ -343,6 +346,20 @@ var path = NativeModule.require('path'); process.argv[0] = path.join(cwd, process.argv[0]); } + + if (process.cov) { + process.on('exit', function() { + var coverage = JSON.stringify(__cov); + var path = NativeModule.require('path'); + var fs = NativeModule.require('fs'); + var filename = path.join(cwd, 'node-cov.json'); + try { + fs.unlinkSync(filename); + } catch(e) { + } + fs.writeFileSync(filename, coverage); + }); + } }; // Below you find a minimal module system, which is used to load the node diff --git a/test/common.js b/test/common.js index 04cd6fec94a..044f74a93fc 100644 --- a/test/common.js +++ b/test/common.js @@ -64,6 +64,10 @@ process.on('exit', function() { knownGlobals.push(gc); } + if (global.__cov) { + knownGlobals.push(__cov); + } + if (global.DTRACE_HTTP_SERVER_RESPONSE) { knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);