Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
[Bot][+test] Load custom config via file
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenice committed Jan 13, 2014
1 parent e607895 commit d77121e
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function(grunt) {
ui: 'bdd',
require: ['test/init.js']
},
src: ['test/**/*.js', '!test/init.js']
src: ['test/**/*.js', '!test/init.js', '!test/_fixtures/**']
}
},
replace: {
Expand Down
3 changes: 2 additions & 1 deletion lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function Bot(settings) {
} else if (typeof settings === 'string') {
var filepath = path.resolve(settings);
debug('loading custom settings from: ' + filepath);
// TODO
delete require.cache[filepath];
customConfig = require(filepath);
}

conf = require('nconf')
Expand Down
15 changes: 15 additions & 0 deletions test/_fixtures/customConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
bot: {
plugins: ['plugin1', 'plugin2']
},
irc: {
server: 'irc.freenode.net',
port: 6697,
secure: true,
nick: 'testnick',
userName: 'testusername',
realName: 'testrealname',
channels: ['#channel1', '#channel2'],
retryCount: 5
}
};
13 changes: 13 additions & 0 deletions test/_fixtures/customConfig2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
bot: {
plugins: ['my-plugin', 'plugin2']
},
irc: {
server: 'irc.freenode.net',
port: 6697,
nick: 'testnick',
realName: 'testrealname',
channels: ['&anotherChannel'],
retryCount: 5
}
};
7 changes: 7 additions & 0 deletions test/_fixtures/customConfig3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
irc: {
port: 6667,
nick: 'nicktester',
retryCount: 8
}
};
17 changes: 17 additions & 0 deletions test/_fixtures/defaultConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
bot: {
plugins: []
},

irc: {
server: null,
port: 6667,
secure: false,
password: null,
nick: null,
userName: null,
realName: null,
channels: [],
retryCount: 10
}
};
99 changes: 61 additions & 38 deletions test/bot/config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
var os = require('os');
var fs = require('fs');
var path = require('path');
var _ = require('lodash-node');
var app = require('../../index.js');

var defaultConfig = {
bot: {
plugins: []
},

irc: {
server: null,
port: 6667,
secure: false,
password: null,
nick: null,
userName: null,
realName: null,
channels: [],
retryCount: 10
}
};

var customConfig = {
bot: {
plugins: ['plugin1', 'plugin2']
},
irc: {
server: 'irc.freenode.net',
port: 6697,
secure: true,
nick: 'testnick',
userName: 'testusername',
realName: 'testrealname',
channels: ['#channel1', '#channel2'],
retryCount: 5
}
};
var defaultConfig = require('../_fixtures/defaultConfig.js');
var customConfig = require('../_fixtures/customConfig.js');
var customConfig2 = require('../_fixtures/customConfig2.js');
var customConfig3 = require('../_fixtures/customConfig3.js');

describe('The Bot config', function() {
describe('with default settings', function() {
Expand Down Expand Up @@ -67,15 +41,64 @@ describe('The Bot config', function() {
});

it('should be reloadable with another config', function() {
var customConfig2 = _.cloneDeep(customConfig);
customConfig2.bot.plugins[0] = 'my-plugin';
delete customConfig2.irc.userName;
delete customConfig2.irc.secure;
customConfig2.irc.channels = ['&anotherChannel'];
bot.loadConfig(customConfig2);
var expected = _.merge(_.cloneDeep(defaultConfig), customConfig2);
bot.config('bot').should.deep.equal(expected.bot);
bot.config('irc').should.deep.equal(expected.irc);
});
});

describe('with custom settings via file', function() {
var srcFile = path.resolve(__dirname, '../_fixtures/customConfig.js');
var srcFile2 = path.resolve(__dirname, '../_fixtures/customConfig2.js');
var srcFile3 = path.resolve(__dirname, '../_fixtures/customConfig3.js');
var tmpDir = os.tmpDir();
var tmpFile = path.resolve(tmpDir, 'irc-apparatus-temp-config.js');
var tmpFile2 = path.resolve(tmpDir, 'irc-apparatus-temp-config2.js');
var bot;

before(function(done) {
bot = app.Bot();
// copy our fixtures
fs.createReadStream(srcFile).pipe(fs.createWriteStream(tmpFile)).on('finish', function() {
fs.createReadStream(srcFile2).pipe(fs.createWriteStream(tmpFile2)).on('finish', function() {
done();
});
});
});

after(function() {
// clean up
fs.unlinkSync(tmpFile);
fs.unlinkSync(tmpFile2);
});

it('should be initialized with the custom config via an absolute path', function() {
bot.loadConfig(tmpFile);
var expected = _.merge(_.cloneDeep(defaultConfig), customConfig);
bot.config('bot').should.deep.equal(expected.bot);
bot.config('irc').should.deep.equal(expected.irc);
});

it('should be reloadable with another file via a relative path', function() {
var cwd = process.cwd();
process.chdir(tmpDir);
bot.loadConfig(tmpFile2);
process.chdir(cwd);

var expected = _.merge(_.cloneDeep(defaultConfig), customConfig2);
bot.config('bot').should.deep.equal(expected.bot);
bot.config('irc').should.deep.equal(expected.irc);
});

it('should re-read a modified file, which was already loaded before', function(done) {
fs.createReadStream(srcFile3).pipe(fs.createWriteStream(tmpFile)).on('finish', function() {
bot.loadConfig(tmpFile);
var expected = _.merge(_.cloneDeep(defaultConfig), customConfig3);
bot.config('bot').should.deep.equal(expected.bot);
bot.config('irc').should.deep.equal(expected.irc);
done();
});
});
});
});

0 comments on commit d77121e

Please sign in to comment.