Added instrumentation support for the client during tests.

This commit is contained in:
Guillermo Rauch
2012-01-03 13:11:09 -08:00
parent c20bdadef4
commit 28f7d4d715

View File

@@ -1,4 +1,36 @@
/**
* Instrument.
*/
var fs = require('fs');
if (process.env.DEBUG) {
require.extensions['.js'] = function(mod, filename){
var js = fs.readFileSync(filename, 'utf8');
// Profiling support
js = js.replace(/^ *\/\/ *(start|end): *([^\n]+)/gm, function(_, type, expr){
switch (type) {
case 'start': return 'console.time(' + expr + ');';
case 'end': return 'console.timeEnd(' + expr + ');';
}
});
// Debugging
js = js.replace(/^ *\/\/ *debug: *([^\n,]+) *([^\n]+)?/gm, function(_, fmt, args){
fmt = fmt.replace(/"/g, '\\"');
return 'console.error(" client\033[90m ' + fmt + '\033[0m"' + (args || '') + ');';
});
js = js.replace(/^ *\/\/ *assert: ([^,]+) *, *([^\n]+)/gm, function(_, expr, msg){
return 'if (!(' + expr + ')) console.error(" client assert\033[31m %s. (%s)\033[0m", ' + msg + ', "' + expr + '");';
});
mod._compile(js, filename);
};
}
/**
* Expose `eio` global.
*/