Skip to content

Commit

Permalink
Throw out rewire
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmartin committed Nov 18, 2015
1 parent f005c1f commit ba156c2
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 68 deletions.
4 changes: 2 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var program = require('commander');
var path = require('path');
var checkEnv = require('check-env');
var createBots = require('./helpers').createBots;
var helpers = require('./helpers');

function run() {
program
Expand All @@ -19,7 +19,7 @@ function run() {

var configFile = require(path.resolve(process.cwd(), process.env.CONFIG_FILE));

createBots(configFile);
helpers.createBots(configFile);
}

module.exports = run;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"jscs": "~1.13.1",
"jshint": "~2.7.0",
"mocha": "~2.2.5",
"rewire": "~2.3.1",
"sinon": "~1.14.1",
"sinon-chai": "~2.7.0"
}
Expand Down
70 changes: 38 additions & 32 deletions test/bot-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,47 @@
var chai = require('chai');
var sinonChai = require('sinon-chai');
var sinon = require('sinon');
var rewire = require('rewire');
var irc = require('irc');
var logger = require('winston');
var Bot = rewire('../lib/bot');
var Bot = require('../lib/bot');
var SlackStub = require('./stubs/slack-stub');
var ChannelStub = require('./stubs/channel-stub');
var ClientStub = require('./stubs/irc-client-stub');
var config = require('./fixtures/single-test-config.json');

chai.should();
chai.use(sinonChai);

describe('Bot Events', function() {
before(function() {
this.infoSpy = sinon.spy(logger, 'info');
this.debugSpy = sinon.spy(logger, 'debug');
this.errorSpy = sinon.spy(logger, 'error');
irc.Client = ClientStub;
Bot.__set__('Slack', SlackStub);
Bot.prototype.sendToIRC = sinon.stub();
Bot.prototype.sendToSlack = sinon.stub();
var sandbox = sinon.sandbox.create({
useFakeTimers: false,
useFakeServer: false
});

beforeEach(function() {
this.infoStub = sandbox.stub(logger, 'info');
this.debugStub = sandbox.stub(logger, 'debug');
this.errorStub = sandbox.stub(logger, 'error');
sandbox.stub(irc, 'Client', ClientStub);
SlackStub.prototype.login = sandbox.stub();
ClientStub.prototype.send = sandbox.stub();
ClientStub.prototype.join = sandbox.stub();
this.bot = new Bot(config);
this.bot.sendToIRC = sandbox.stub();
this.bot.sendToSlack = sandbox.stub();
this.bot.slack = new SlackStub();
this.bot.connect();
});

afterEach(function() {
Bot.prototype.sendToIRC.reset();
Bot.prototype.sendToSlack.reset();
ClientStub.prototype.send.reset();
ClientStub.prototype.join.reset();
this.infoSpy.reset();
this.debugSpy.reset();
this.errorSpy.reset();
sandbox.restore();
this.bot.slack.resetStub();
ChannelStub.prototype.postMessage.reset();
});

it('should log on slack open event', function() {
this.bot.slack.emit('open');
this.debugSpy.should.have.been.calledWithExactly('Connected to Slack');
this.debugStub.should.have.been.calledWithExactly('Connected to Slack');
});

it('should try to send autoSendCommands on registered IRC event', function() {
Expand All @@ -53,26 +57,26 @@ describe('Bot Events', function() {
var ircError = new Error('irc');
this.bot.slack.emit('error', slackError);
this.bot.ircClient.emit('error', ircError);
this.errorSpy.getCall(0).args[0].should.equal('Received error event from Slack');
this.errorSpy.getCall(0).args[1].should.equal(slackError);
this.errorSpy.getCall(1).args[0].should.equal('Received error event from IRC');
this.errorSpy.getCall(1).args[1].should.equal(ircError);
this.errorStub.getCall(0).args[0].should.equal('Received error event from Slack');
this.errorStub.getCall(0).args[1].should.equal(slackError);
this.errorStub.getCall(1).args[0].should.equal('Received error event from IRC');
this.errorStub.getCall(1).args[1].should.equal(ircError);
});

it('should send messages to irc if correct', function() {
var message = {
type: 'message'
};
this.bot.slack.emit('message', message);
Bot.prototype.sendToIRC.should.have.been.calledWithExactly(message);
this.bot.sendToIRC.should.have.been.calledWithExactly(message);
});

it('should not send messages to irc if the type isn\'t message', function() {
var message = {
type: 'notmessage'
};
this.bot.slack.emit('message', message);
Bot.prototype.sendToIRC.should.have.not.have.been.called;
this.bot.sendToIRC.should.have.not.have.been.called;
});

it('should not send messages to irc if it has an invalid subtype', function() {
Expand All @@ -81,15 +85,15 @@ describe('Bot Events', function() {
subtype: 'bot_message'
};
this.bot.slack.emit('message', message);
Bot.prototype.sendToIRC.should.have.not.have.been.called;
this.bot.sendToIRC.should.have.not.have.been.called;
});

it('should send messages to slack', function() {
var channel = '#channel';
var author = 'user';
var text = 'hi';
this.bot.ircClient.emit('message', author, channel, text);
Bot.prototype.sendToSlack.should.have.been.calledWithExactly(author, channel, text);
this.bot.sendToSlack.should.have.been.calledWithExactly(author, channel, text);
});

it('should send notices to slack', function() {
Expand All @@ -98,7 +102,7 @@ describe('Bot Events', function() {
var text = 'hi';
var formattedText = '*' + text + '*';
this.bot.ircClient.emit('notice', author, channel, text);
Bot.prototype.sendToSlack.should.have.been.calledWithExactly(author, channel, formattedText);
this.bot.sendToSlack.should.have.been.calledWithExactly(author, channel, formattedText);
});

it('should send actions to slack', function() {
Expand All @@ -108,35 +112,37 @@ describe('Bot Events', function() {
var formattedText = '_hi_';
var message = {};
this.bot.ircClient.emit('action', author, channel, text, message);
Bot.prototype.sendToSlack.should.have.been.calledWithExactly(author, channel, formattedText);
this.bot.sendToSlack.should.have.been.calledWithExactly(author, channel, formattedText);
});

it('should join channels when invited', function() {
var channel = '#irc';
var author = 'user';
this.debugStub.reset();
this.bot.ircClient.emit('invite', channel, author);
var firstCall = this.debugSpy.getCall(0);
var firstCall = this.debugStub.getCall(0);
firstCall.args[0].should.equal('Received invite:');
firstCall.args[1].should.equal(channel);
firstCall.args[2].should.equal(author);

ClientStub.prototype.join.should.have.been.calledWith(channel);
var secondCall = this.debugSpy.getCall(1);
var secondCall = this.debugStub.getCall(1);
secondCall.args[0].should.equal('Joining channel:');
secondCall.args[1].should.equal(channel);
});

it('should not join channels that aren\'t in the channel mapping', function() {
var channel = '#wrong';
var author = 'user';
this.debugStub.reset();
this.bot.ircClient.emit('invite', channel, author);
var firstCall = this.debugSpy.getCall(0);
var firstCall = this.debugStub.getCall(0);
firstCall.args[0].should.equal('Received invite:');
firstCall.args[1].should.equal(channel);
firstCall.args[2].should.equal(author);

ClientStub.prototype.join.should.not.have.been.called;
var secondCall = this.debugSpy.getCall(1);
var secondCall = this.debugStub.getCall(1);
secondCall.args[0].should.equal('Channel not found in config, not joining:');
secondCall.args[1].should.equal(channel);
});
Expand Down
25 changes: 19 additions & 6 deletions test/bot.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint no-unused-expressions: 0 */
var chai = require('chai');
var sinon = require('sinon');
var logger = require('winston');
var sinonChai = require('sinon-chai');
var rewire = require('rewire');
var irc = require('irc');
var Bot = rewire('../lib/bot');
var Bot = require('../lib/bot');
var SlackStub = require('./stubs/slack-stub');
var ChannelStub = require('./stubs/channel-stub');
var ClientStub = require('./stubs/irc-client-stub');
Expand All @@ -13,16 +14,28 @@ chai.should();
chai.use(sinonChai);

describe('Bot', function() {
before(function() {
irc.Client = ClientStub;
Bot.__set__('Slack', SlackStub);
var sandbox = sinon.sandbox.create({
useFakeTimers: false,
useFakeServer: false
});

beforeEach(function() {
sandbox.stub(logger, 'info');
sandbox.stub(logger, 'debug');
sandbox.stub(logger, 'error');
sandbox.stub(irc, 'Client', ClientStub);
ClientStub.prototype.say = sandbox.stub();
ClientStub.prototype.send = sandbox.stub();
ClientStub.prototype.join = sandbox.stub();
SlackStub.prototype.login = sandbox.stub();
this.bot = new Bot(config);
this.bot.slack = new SlackStub();
this.bot.connect();
});

afterEach(function() {
sandbox.restore();
this.bot.slack.resetStub();
ClientStub.prototype.say.reset();
ChannelStub.prototype.postMessage.reset();
});

Expand Down
11 changes: 1 addition & 10 deletions test/channel-mapping.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
var chai = require('chai');
var rewire = require('rewire');
var irc = require('irc');
var ConfigurationError = require('../lib/errors').ConfigurationError;
var validateChannelMapping = require('../lib/validators').validateChannelMapping;
var Bot = rewire('../lib/bot');
var Bot = require('../lib/bot');
var config = require('./fixtures/single-test-config.json');
var caseConfig = require('./fixtures/case-sensitivity-config.json');
var SlackStub = require('./stubs/slack-stub');
var ClientStub = require('./stubs/irc-client-stub');

chai.should();

describe('Channel Mapping', function() {
before(function() {
irc.Client = ClientStub;
Bot.__set__('Slack', SlackStub);
});

it('should fail when not given proper JSON', function() {
var wrongMapping = 'not json';
function wrap() {
Expand Down
16 changes: 10 additions & 6 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var rewire = require('rewire');
var cli = rewire('../lib/cli');
var cli = require('../lib/cli');
var helpers = require('../lib/helpers');
var testConfig = require('./fixtures/test-config.json');
var singleTestConfig = require('./fixtures/single-test-config.json');

chai.should();
chai.use(sinonChai);

describe('CLI', function() {
before(function() {
this.createBotsStub = sinon.stub();
cli.__set__('createBots', this.createBotsStub);
var sandbox = sinon.sandbox.create({
useFakeTimers: false,
useFakeServer: false
});

beforeEach(function() {
this.createBotsStub = sandbox.stub(helpers, 'createBots');
});

afterEach(function() {
this.createBotsStub.reset();
sandbox.restore();
});

it('should be possible to give the config as an env var', function() {
Expand Down
8 changes: 0 additions & 8 deletions test/stubs/irc-client-stub.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
var util = require('util');
var events = require('events');
var sinon = require('sinon');

function ClientStub() {}

util.inherits(ClientStub, events.EventEmitter);

ClientStub.prototype.say = sinon.stub();

ClientStub.prototype.send = sinon.stub();

ClientStub.prototype.join = sinon.stub();

module.exports = ClientStub;
3 changes: 0 additions & 3 deletions test/stubs/slack-stub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var util = require('util');
var events = require('events');
var sinon = require('sinon');
var ChannelStub = require('./channel-stub');

function SlackStub() {
Expand Down Expand Up @@ -30,8 +29,6 @@ SlackStub.prototype.getUserByID = function() {
};
};

SlackStub.prototype.login = sinon.stub();

SlackStub.prototype.resetStub = function() {
this.returnWrongStubInfo = false;
};
Expand Down

0 comments on commit ba156c2

Please sign in to comment.