Skip to content

Commit

Permalink
add test coverage for process.env settings
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 28, 2016
1 parent 57c0df2 commit f56b6a9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 31 deletions.
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = new Config();

function Config (root_path) {
this.root_path = root_path || cfreader.config_path;
// console.log('root_path: ' + this.root_path);
this.module_config = function (defaults_path, overrides_path) {
var cfg = new Config(path.join(defaults_path, 'config'));
if (overrides_path) {
Expand Down
1 change: 1 addition & 0 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var config_dir_candidates = [

function get_path_to_config_dir () {
if (process.env.HARAKA) {
// console.log('process.env.HARAKA: ' + process.env.HARAKA);
cfreader.config_path = path.join(process.env.HARAKA, 'config');
return;
}
Expand Down
102 changes: 71 additions & 31 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,172 @@

process.env.NODE_ENV = 'test'

var path = require('path');

var config = require('../config');
var path = require('path');

var cb = function () { return false; };
var opts = { booleans: ['arg1'] };

function clearRequireCache () {
// node_unit runs all the tests in the same process, so the process.env
// changes affect other tests. Icky. Work around by invalidating
// the require cache, so config and configfile re-initialize
delete require.cache[
path.resolve(__dirname, '../config') + '.js'
];
delete require.cache[
path.resolve(__dirname, '../configfile') + '.js'
];
}

function setUp (done) {
process.env.HARAKA = '';
clearRequireCache();
this.config = require('../config');
done();
};

exports.config = {
'setUp' : setUp,
'new' : function (test) {
test.expect(1);
// console.log(config);
test.ok(/haraka\-config\/test\/config$/.test(config.root_path));
test.ok(/haraka\-config\/test\/config$/.test(this.config.root_path));
test.done();
},
'module_config' : function (test) {
test.expect(2);
var c = config.module_config('foo', 'bar');
var c = this.config.module_config('foo', 'bar');
test.equal(c.root_path, 'foo/config');
test.equal(c.overrides_path, 'bar/config');
test.done();
},
};

exports.config_path = {
'config_path process.env.HARAKA': function (test) {
test.expect(1);
process.env.HARAKA = '/tmp';
clearRequireCache();
var config = require('../config');
// console.log(config);
test.equal(config.root_path, '/tmp/config');
test.done();
},
'config_path process.env.NODE_ENV': function (test) {
test.expect(1);
process.env.HARAKA = '';
process.env.NODE_ENV = 'not-test';
clearRequireCache();
var config = require('../config');
// ./config doesn't exist so path will be resolved ./
test.ok(/haraka\-config$/.test(config.root_path));
process.env.NODE_ENV = 'test';
test.done();
},
};

exports.arrange_args = {
'setUp' : setUp,
// config.get('name');
'name' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini']),
this.config.arrange_args(['test.ini']),
['test.ini', 'ini', undefined, undefined]);
test.done();
},
// config.get('name', type);
'name, type' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','ini']),
this.config.arrange_args(['test.ini','ini']),
['test.ini', 'ini', undefined, undefined]);
test.done();
},
// config.get('name', cb);
'name, callback' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini',cb]),
this.config.arrange_args(['test.ini',cb]),
['test.ini', 'ini', cb, undefined]);
test.done();
},
// config.get('name', cb, options);
'name, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini',cb,opts]),
this.config.arrange_args(['test.ini',cb,opts]),
['test.ini', 'ini', cb, opts]);
test.done();
},
// config.get('name', options);
'name, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini',opts]),
this.config.arrange_args(['test.ini',opts]),
['test.ini', 'ini', undefined, opts]);
test.done();
},
// config.get('name', type, cb);
'name, type, callback' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','ini',cb]),
this.config.arrange_args(['test.ini','ini',cb]),
['test.ini', 'ini', cb, undefined]);
test.done();
},
// config.get('name', type, options);
'name, type, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','ini',opts]),
this.config.arrange_args(['test.ini','ini',opts]),
['test.ini', 'ini', undefined, opts]);
test.done();
},
// config.get('name', type, cb, options);
'name, type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','ini',cb, opts]),
this.config.arrange_args(['test.ini','ini',cb, opts]),
['test.ini', 'ini', cb, opts]);
test.done();
},
// config.get('name', list, cb, options);
'name, list type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','list',cb, opts]),
this.config.arrange_args(['test.ini','list',cb, opts]),
['test.ini', 'list', cb, opts]);
test.done();
},
// config.get('name', binary, cb, options);
'name, binary type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','binary',cb, opts]),
this.config.arrange_args(['test.ini','binary',cb, opts]),
['test.ini', 'binary', cb, opts]);
test.done();
},
// config.get('name', type, cb, options);
'name, value type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','value',cb, opts]),
this.config.arrange_args(['test.ini','value',cb, opts]),
['test.ini', 'value', cb, opts]);
test.done();
},
// config.get('name', type, cb, options);
'name, json type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','json',cb, opts]),
this.config.arrange_args(['test.ini','json',cb, opts]),
['test.ini', 'json', cb, opts]);
test.done();
},
// config.get('name', type, cb, options);
'name, data type, callback, options' : function (test) {
test.expect(1);
test.deepEqual(
config.arrange_args(['test.ini','data',cb, opts]),
this.config.arrange_args(['test.ini','data',cb, opts]),
['test.ini', 'data', cb, opts]);
test.done();
},
Expand Down Expand Up @@ -164,22 +205,21 @@ var yamlRes = {

function _test_get(test, name, type, callback, options, expected) {
test.expect(1);
var config = require('../config');
var cfg = config.get(name, type, callback, options);
if (cfg && typeof cfg === 'object' && cfg.cached !== undefined) {
// delete cfg.cached;
}
test.deepEqual(cfg, expected);
test.done();
}

exports.get = {
'setUp' : setUp,
// config.get('name');
'test (non-existing)' : function (test) {
_test_get(test, 'test', null, null, null, null);
},
'test (non-existing, cached)' : function (test) {
test.expect(1);
var cfg = config.get('test', null, null);
var cfg = this.config.get('test', null, null);
test.deepEqual(cfg, null);
test.done();
},
Expand Down Expand Up @@ -253,43 +293,43 @@ exports.get = {
// config.get('test.bin', 'binary');
'test.bin, type=binary' : function (test) {
test.expect(2);
var res = config.get('test.binary', 'binary');
var res = this.config.get('test.binary', 'binary');
test.equal(res.length, 120);
test.ok(Buffer.isBuffer(res));
test.done();
},
};

exports.merged = {
'setUp' : setUp,
'before_merge' : function (test) {
test.expect(1);
this.config = require('../config').module_config(
var lc = this.config.module_config(
path.join('test','default')
);
test.deepEqual(this.config.get('test.ini'),
test.deepEqual(lc.get('test.ini'),
{ main: {}, defaults: { one: 'one', two: 'two' } }
);
test.done();
},
'after_merge': function (test) {
test.expect(1);
this.config = require('../config').module_config(
var lc = this.config.module_config(
path.join('test','default'),
path.join('test','override')
);
test.deepEqual(this.config.get('test.ini'),
test.deepEqual(lc.get('test.ini'),
{ main: {}, defaults: { one: 'three', two: 'four' } }
);
test.done();
},
'flat overridden' : function (test) {
test.expect(1);
this.config = require('../config').module_config(
var lc = this.config.module_config(
path.join('test','default'),
path.join('test','override')
);
var cfg = this.config.get('test.flat');
test.equal(cfg, 'flatoverrode');
test.equal(lc.get('test.flat'), 'flatoverrode');
test.done();
},
}

0 comments on commit f56b6a9

Please sign in to comment.