Add --cov code coverage option
This commit is contained in:
parent
80711b0ff9
commit
b4ff36a41b
@ -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);
|
||||
}
|
||||
|
@ -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<Object> 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<char*>("");
|
||||
} else if (!strcmp(arg, "--cov")) {
|
||||
cov = true;
|
||||
argv[i] = const_cast<char*>("");
|
||||
} else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
|
||||
printf("%s\n", NODE_VERSION);
|
||||
exit(0);
|
||||
|
17
src/node.js
17
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
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user