2012-05-02 02:33:00 +00:00
|
|
|
/*
|
2012-07-10 05:27:28 +00:00
|
|
|
* complete-test.js: Complete test for multiple stores.
|
|
|
|
*
|
2014-11-26 06:31:48 +00:00
|
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
2012-07-10 05:27:28 +00:00
|
|
|
*
|
2012-05-02 02:33:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
vows = require('vows'),
|
|
|
|
assert = require('assert'),
|
|
|
|
nconf = require('../lib/nconf'),
|
|
|
|
data = require('./fixtures/data').data,
|
|
|
|
helpers = require('./helpers');
|
|
|
|
|
2012-07-10 05:27:28 +00:00
|
|
|
var completeTest = helpers.fixture('complete-test.json'),
|
|
|
|
complete = helpers.fixture('complete.json');
|
2012-05-02 02:33:00 +00:00
|
|
|
|
2015-08-04 16:54:59 +00:00
|
|
|
// prime the process.env
|
|
|
|
process.env['NCONF_foo'] = 'bar';
|
|
|
|
process.env.FOO = 'bar';
|
|
|
|
process.env.BAR = 'zalgo';
|
|
|
|
process.env.NODE_ENV = 'debug';
|
|
|
|
process.env.FOOBAR = 'should not load';
|
2017-09-27 20:32:26 +00:00
|
|
|
process.env.json_array = JSON.stringify(['foo', 'bar', 'baz']);
|
|
|
|
process.env.json_obj = JSON.stringify({foo: 'bar', baz: 'foo'});
|
2015-08-04 16:54:59 +00:00
|
|
|
|
2012-07-10 05:27:28 +00:00
|
|
|
vows.describe('nconf/multiple-stores').addBatch({
|
2012-05-02 02:33:00 +00:00
|
|
|
"When using the nconf with multiple providers": {
|
|
|
|
topic: function () {
|
|
|
|
var that = this;
|
|
|
|
helpers.cp(complete, completeTest, function () {
|
2015-08-04 16:54:59 +00:00
|
|
|
nconf.env({
|
|
|
|
// separator: '__',
|
|
|
|
match: /^NCONF_/,
|
|
|
|
whitelist: ['NODE_ENV', 'FOO', 'BAR']
|
|
|
|
});
|
2012-07-10 05:27:28 +00:00
|
|
|
nconf.file({ file: completeTest });
|
|
|
|
nconf.use('argv', { type: 'literal', store: data });
|
2012-05-02 02:33:00 +00:00
|
|
|
that.callback();
|
|
|
|
});
|
|
|
|
},
|
2012-07-10 05:27:28 +00:00
|
|
|
"should have the correct `stores`": function () {
|
|
|
|
assert.isObject(nconf.stores.env);
|
|
|
|
assert.isObject(nconf.stores.argv);
|
|
|
|
assert.isObject(nconf.stores.file);
|
|
|
|
},
|
2012-05-02 02:33:00 +00:00
|
|
|
"env vars": {
|
|
|
|
"are present": function () {
|
2015-08-04 16:54:59 +00:00
|
|
|
['NODE_ENV', 'FOO', 'BAR', 'NCONF_foo'].forEach(function (key) {
|
2012-05-02 02:33:00 +00:00
|
|
|
assert.equal(nconf.get(key), process.env[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json vars": {
|
|
|
|
topic: function () {
|
|
|
|
fs.readFile(complete, 'utf8', this.callback);
|
|
|
|
},
|
|
|
|
"are present": function (err, data) {
|
|
|
|
assert.isNull(err);
|
|
|
|
data = JSON.parse(data);
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
assert.deepEqual(nconf.get(key), data[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"literal vars": {
|
|
|
|
"are present": function () {
|
|
|
|
Object.keys(data).forEach(function (key) {
|
2012-06-13 07:14:56 +00:00
|
|
|
assert.deepEqual(nconf.get(key), data[key]);
|
2012-05-02 02:33:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"and saving *synchronously*": {
|
|
|
|
topic: function () {
|
|
|
|
nconf.set('weebls', 'stuff');
|
|
|
|
return nconf.save();
|
|
|
|
},
|
|
|
|
"correct return value": function (topic) {
|
|
|
|
Object.keys(topic).forEach(function (key) {
|
|
|
|
assert.deepEqual(topic[key], nconf.get(key));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"the file": {
|
|
|
|
topic: function () {
|
|
|
|
fs.readFile(completeTest, 'utf8', this.callback);
|
|
|
|
},
|
|
|
|
"saved correctly": function (err, data) {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
assert.deepEqual(data[key], nconf.get(key));
|
|
|
|
});
|
|
|
|
assert.equal(nconf.get('weebls'), 'stuff');
|
|
|
|
}
|
|
|
|
}
|
2012-05-03 04:16:13 +00:00
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
// remove the file so that we can test saving it async
|
|
|
|
fs.unlinkSync(completeTest);
|
2012-05-02 02:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).addBatch({
|
|
|
|
// Threw this in it's own batch to make sure it's run separately from the
|
|
|
|
// sync check
|
|
|
|
"When using the nconf with multiple providers": {
|
|
|
|
"and saving *asynchronously*": {
|
|
|
|
topic: function () {
|
|
|
|
nconf.set('weebls', 'crap');
|
|
|
|
nconf.save(this.callback);
|
|
|
|
},
|
|
|
|
"correct return value": function (err, data) {
|
|
|
|
assert.isNull(err);
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
assert.deepEqual(data[key], nconf.get(key));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"the file": {
|
|
|
|
topic: function () {
|
|
|
|
fs.readFile(completeTest, 'utf8', this.callback);
|
|
|
|
},
|
|
|
|
"saved correctly": function (err, data) {
|
|
|
|
assert.isNull(err);
|
|
|
|
data = JSON.parse(data);
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
assert.deepEqual(nconf.get(key), data[key]);
|
|
|
|
});
|
|
|
|
assert.equal(nconf.get('weebls'), 'crap');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
teardown: function () {
|
2012-05-03 04:16:13 +00:00
|
|
|
fs.unlinkSync(completeTest);
|
2012-05-02 02:33:00 +00:00
|
|
|
nconf.remove('file');
|
|
|
|
nconf.remove('memory');
|
|
|
|
nconf.remove('argv');
|
|
|
|
nconf.remove('env');
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 10:56:43 +00:00
|
|
|
}).addBatch({
|
|
|
|
// Threw this in it's own batch to make sure it's run separately from the
|
|
|
|
// sync check
|
|
|
|
"When using env with lowerCase:true": {
|
|
|
|
topic: function () {
|
|
|
|
var that = this;
|
|
|
|
helpers.cp(complete, completeTest, function () {
|
|
|
|
nconf.env({ lowerCase: true });
|
|
|
|
that.callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"env vars": {
|
|
|
|
"keys also available as lower case": function () {
|
|
|
|
Object.keys(process.env).forEach(function (key) {
|
|
|
|
assert.equal(nconf.get(key.toLowerCase()), process.env[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
nconf.remove('env');
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 20:32:26 +00:00
|
|
|
}).addBatch({
|
|
|
|
// Threw this in it's own batch to make sure it's run separately from the
|
|
|
|
// sync check
|
2017-10-21 19:39:16 +00:00
|
|
|
"When using env with parseValues:true": {
|
2017-09-27 20:32:26 +00:00
|
|
|
topic: function () {
|
|
|
|
var that = this;
|
|
|
|
helpers.cp(complete, completeTest, function () {
|
|
|
|
nconf.env({ parseJson: true });
|
|
|
|
that.callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"env vars": {
|
|
|
|
"JSON keys properly parsed": function () {
|
|
|
|
Object.keys(process.env).forEach(function (key) {
|
|
|
|
var val = process.env[key];
|
|
|
|
|
|
|
|
try {
|
2017-10-21 19:39:16 +00:00
|
|
|
val = JSON.parse(val);
|
2017-09-27 20:32:26 +00:00
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
assert.deepEqual(nconf.get(key), val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
nconf.remove('env');
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 05:27:28 +00:00
|
|
|
}).export(module);
|