Permalink
Browse files

better plugin stub.

  • Loading branch information...
godsflaw committed Mar 7, 2012
1 parent 53a55b4 commit 0819e86ee70cefb6d1659d981b29052939ceacfd
View
@@ -15,8 +15,13 @@ exports.register = function() {
var always_ok = ini.general['always_ok'] || false;
if (always_ok && (always_ok >= 1 || always_ok.toLowerCase() === 'true')) {
- this.always_ok = always_ok = true;
+ always_ok = true;
}
+ else {
+ always_ok = false;
+ }
+
+ this.always_ok = always_ok;
if (pid && (pid >= 1 || pid.toLowerCase() === 'true')) {
options |= Syslog.LOG_PID;
@@ -2,7 +2,7 @@
// This cannot be used on its own. You need to inherit from it.
// See plugins/queue/smtp_forward.js for an example.
-var sock = require('line_socket');
+var sock = require('./line_socket');
// XXX: pass in host, port, and timeout (read from config)
// XXX: auto-register event handlers
View
@@ -2,6 +2,8 @@
"use strict";
+require.paths.unshift(__dirname);
+require.paths.unshift(__dirname + "/plugins");
require.paths.unshift(__dirname + "/tests/fixtures");
try {
@@ -1,22 +1,70 @@
"use strict";
-var stub = require('tests/fixtures/stub');
-
-var plugin = exports;
+var stub = require('tests/fixtures/stub'),
+ path = require('path'),
+ constants = require('../../constants'),
+ vm = require('vm'),
+ fs = require('fs');
function Plugin(name) {
+ if (false === (this instanceof Plugin)) {
+ return new Plugin(name);
+ }
+
this.inherits = stub();
+ this.register_hook = stub();
+ this.config = stub();
+
+ return this.load_plugin(name);
}
-plugin.createPlugin = function(name) {
- var obj = new Plugin(name);
- var plug = require(name);
+Plugin.prototype.load_plugin = function(name) {
+ var rf;
+ var last_err;
+ var full_paths = [];
+
+ require.paths.forEach(function (pp) {
+ full_paths.push(path.resolve(pp, name) + '.js');
+ });
- for (var k in plug) {
- if (plug.hasOwnProperty(k)) {
- obj[k] = plug[k];
+ for (var i=0, j=full_paths.length; i<j; i++) {
+ try {
+ rf = fs.readFileSync(full_paths[i]);
+ break;
+ }
+ catch (err) {
+ last_err = err;
+ continue;
}
}
+ if (!rf) {
+ throw "Loading test plugin " + name + " failed: " + last_err;
+ }
- return obj;
+ var code = '"use strict";' + rf;
+ var sandbox = {
+ require: require,
+ __filename: full_paths[i],
+ __dirname: path.dirname(full_paths[i]),
+ exports: this,
+ console: console,
+ setTimeout: setTimeout,
+ clearTimeout: clearTimeout,
+ setInterval: setInterval,
+ clearInterval: clearInterval,
+ process: process,
+ Buffer: Buffer,
+ Math: Math,
+ };
+ constants.import(sandbox);
+ try {
+ vm.runInNewContext(code, sandbox, name);
+ }
+ catch (err) {
+ throw err;
+ }
+
+ return this;
};
+
+module.exports = Plugin;
View
@@ -11,18 +11,12 @@ function _set_up(callback) {
this.backup = {};
// needed for tests
- this.plugin = Plugin.createPlugin('plugins/aliases');
+ this.plugin = new Plugin('plugins/aliases');
this.connection = Connection.createConnection();
this.recip = new Address('<test1@example.com>');
this.params = [this.recip];
- // backup modifications
-
- this.backup.plugin = {};
- this.backup.plugin.register_hook = this.plugin.register_hook;
// stub out functions
- this.plugin.config = stub();
- this.plugin.register_hook = stub();
this.connection.loginfo = stub();
this.connection.logdebug = stub();
this.connection.notes = stub();
@@ -53,9 +47,6 @@ function _set_up(callback) {
}
function _tear_down(callback) {
- // restore backed up functions
- this.plugin.register_hook = this.backup.plugin.register_hook;
-
callback();
}
@@ -10,17 +10,14 @@ function _set_up(callback) {
this.backup = {};
// needed for tests
- this.plugin = Plugin.createPlugin('plugins/log.syslog');
+ this.plugin = new Plugin('plugins/log.syslog');
this.logger = Logger.createLogger();
// backup modifications
this.backup.plugin = {};
this.backup.plugin.Syslog = {};
- this.backup.plugin.register_hook = this.plugin.register_hook;
// stub out functions
- this.plugin.register_hook = stub();
- this.plugin.config = stub();
this.log = stub();
this.log.level = 'info';
this.log.data = "this is a test log message";
@@ -42,16 +39,10 @@ function _set_up(callback) {
return this.configfile;
}.bind(this);
- // going to need these in multiple tests
- this.plugin.register();
-
callback();
}
function _tear_down(callback) {
- // restore backed up functions
- this.plugin.register_hook = this.backup.plugin.register_hook;
-
callback();
}
@@ -65,16 +56,19 @@ exports.log_syslog = {
test.done();
},
'register function should call register_hook()' : function (test) {
+ this.plugin.register();
test.expect(1);
test.ok(this.plugin.register_hook.called);
test.done();
},
'register_hook() should register for propper hook' : function (test) {
+ this.plugin.register();
test.expect(1);
test.equals(this.plugin.register_hook.args[0], 'log');
test.done();
},
'register_hook() should register available function' : function (test) {
+ this.plugin.register();
test.expect(3);
test.equals(this.plugin.register_hook.args[1], 'syslog');
test.isNotNull(this.plugin.syslog);
@@ -10,7 +10,7 @@ function _set_up(callback) {
this.backup = {};
// needed for tests
- this.plugin = Plugin.createPlugin('plugins/queue/queue_base');
+ this.plugin = new Plugin('plugins/queue/queue_base');
this.connection = Connection.createConnection();
// backup modifications
View
@@ -10,16 +10,11 @@ function _set_up(callback) {
this.backup = {};
// needed for tests
- this.plugin = Plugin.createPlugin('plugins/relay_all');
+ this.plugin = Plugin('plugins/relay_all');
this.connection = Connection.createConnection();
this.params = ['foo@bar.com'];
- // backup modifications
- this.backup.plugin = {};
- this.backup.plugin.register_hook = this.plugin.register_hook;
-
// stub out functions
- this.plugin.register_hook = stub();
this.connection.loginfo = stub();
// going to need these in multiple tests
@@ -29,9 +24,6 @@ function _set_up(callback) {
}
function _tear_down(callback) {
- // restore backed up functions
- this.plugin.register_hook = this.backup.plugin.register_hook;
-
callback();
}

0 comments on commit 0819e86

Please sign in to comment.