[fix] Ensure that all options are passed to Provider.prototype.add in Provider.prototype.file. Fixes #51

[doc] Update README.md and method documentation
[dist] Remove vim comments
This commit is contained in:
indexzero 2012-07-10 01:27:28 -04:00
parent eeddb70f20
commit 7515f66572
4 changed files with 42 additions and 40 deletions

View file

@ -88,7 +88,11 @@ A sane default for this could be:
// //
// 4. Values in `config.json` // 4. Values in `config.json`
// //
nconf.file({ file: 'config.json' }); nconf.file({
file: 'config.json',
dir: 'search/from/here',
search: true
});
// //
// 5. Any default values // 5. Any default values

View file

@ -36,23 +36,29 @@ var Provider = exports.Provider = function (options) {
}); });
// //
// Adds the file at `path` to the stores with `key` // ### function file (key, path)
// If key is not given, `file` will be used as the key // #### @key {string|Object} Fully qualified options, name of file store, or path.
// For backward compatibility, an object can still be passed like {file: '/etc/foo.conf'} // #### @path {string} **Optional** Path to the file for `key`.
// Adds a new `File` store to this instance. Accepts the following options
// //
Provider.prototype.file = function(key, path) { // nconf.file({ file: '.jitsuconf', dir: process.env.HOME, search: true })
var args = Array.prototype.slice.call(arguments, 0); // nconf.file('path/to/config/file')
if(args.length == 1) { // nconf.file('userconfig', 'path/to/config/file')
//
if(typeof key === 'object'){ Provider.prototype.file = function (key, path) {
path = key.file; var options;
}
else { if (arguments.length == 1) {
path = key; options = typeof key !== 'object'
key = 'file'; ? { type: 'file', file: key }
} : key;
key = 'file';
} }
return this.add(key, {type: 'file', file: path}); else {
options = { type: 'file', file: path };
}
return this.add(key, options);
}; };
// //

View file

@ -1,5 +1,8 @@
/* /*
* complete-test.js: Complete test with multiple providers * complete-test.js: Complete test for multiple stores.
*
* (C) 2011, Nodejitsu Inc.
*
*/ */
var fs = require('fs'), var fs = require('fs'),
@ -10,21 +13,25 @@ var fs = require('fs'),
data = require('./fixtures/data').data, data = require('./fixtures/data').data,
helpers = require('./helpers'); helpers = require('./helpers');
var complete = helpers.fixture('complete.json'); var completeTest = helpers.fixture('complete-test.json'),
var completeTest = helpers.fixture('complete-test.json'); complete = helpers.fixture('complete.json');
vows.describe('nconf').addBatch({ vows.describe('nconf/multiple-stores').addBatch({
"When using the nconf with multiple providers": { "When using the nconf with multiple providers": {
topic: function () { topic: function () {
var that = this; var that = this;
helpers.cp(complete, completeTest, function () { helpers.cp(complete, completeTest, function () {
nconf.env(); nconf.env();
nconf.file({file: completeTest}); nconf.file({ file: completeTest });
nconf.use('argv', {type: 'literal', store: data}); nconf.use('argv', { type: 'literal', store: data });
that.callback(); that.callback();
}); });
}, },
"should have the correct `stores`": function () {
assert.isObject(nconf.stores.env);
assert.isObject(nconf.stores.argv);
assert.isObject(nconf.stores.file);
},
"env vars": { "env vars": {
"are present": function () { "are present": function () {
Object.keys(process.env).forEach(function (key) { Object.keys(process.env).forEach(function (key) {
@ -32,12 +39,10 @@ vows.describe('nconf').addBatch({
}); });
} }
}, },
"json vars": { "json vars": {
topic: function () { topic: function () {
fs.readFile(complete, 'utf8', this.callback); fs.readFile(complete, 'utf8', this.callback);
}, },
"are present": function (err, data) { "are present": function (err, data) {
assert.isNull(err); assert.isNull(err);
data = JSON.parse(data); data = JSON.parse(data);
@ -46,7 +51,6 @@ vows.describe('nconf').addBatch({
}); });
} }
}, },
"literal vars": { "literal vars": {
"are present": function () { "are present": function () {
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
@ -54,24 +58,20 @@ vows.describe('nconf').addBatch({
}); });
} }
}, },
"and saving *synchronously*": { "and saving *synchronously*": {
topic: function () { topic: function () {
nconf.set('weebls', 'stuff'); nconf.set('weebls', 'stuff');
return nconf.save(); return nconf.save();
}, },
"correct return value": function (topic) { "correct return value": function (topic) {
Object.keys(topic).forEach(function (key) { Object.keys(topic).forEach(function (key) {
assert.deepEqual(topic[key], nconf.get(key)); assert.deepEqual(topic[key], nconf.get(key));
}); });
}, },
"the file": { "the file": {
topic: function () { topic: function () {
fs.readFile(completeTest, 'utf8', this.callback); fs.readFile(completeTest, 'utf8', this.callback);
}, },
"saved correctly": function (err, data) { "saved correctly": function (err, data) {
data = JSON.parse(data); data = JSON.parse(data);
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
@ -95,19 +95,16 @@ vows.describe('nconf').addBatch({
nconf.set('weebls', 'crap'); nconf.set('weebls', 'crap');
nconf.save(this.callback); nconf.save(this.callback);
}, },
"correct return value": function (err, data) { "correct return value": function (err, data) {
assert.isNull(err); assert.isNull(err);
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
assert.deepEqual(data[key], nconf.get(key)); assert.deepEqual(data[key], nconf.get(key));
}); });
}, },
"the file": { "the file": {
topic: function () { topic: function () {
fs.readFile(completeTest, 'utf8', this.callback); fs.readFile(completeTest, 'utf8', this.callback);
}, },
"saved correctly": function (err, data) { "saved correctly": function (err, data) {
assert.isNull(err); assert.isNull(err);
data = JSON.parse(data); data = JSON.parse(data);
@ -118,7 +115,6 @@ vows.describe('nconf').addBatch({
} }
} }
}, },
teardown: function () { teardown: function () {
fs.unlinkSync(completeTest); fs.unlinkSync(completeTest);
nconf.remove('file'); nconf.remove('file');
@ -127,6 +123,4 @@ vows.describe('nconf').addBatch({
nconf.remove('env'); nconf.remove('env');
} }
} }
}).export(module); }).export(module);
// vim: ts=2 shiftwidth=2 softtabstop=2

View file

@ -65,6 +65,4 @@ exports.cp = function (from, to, callback) {
exports.fixture = function (file) { exports.fixture = function (file) {
return require('path').join(__dirname, 'fixtures', file); return require('path').join(__dirname, 'fixtures', file);
}; };
// vim: ts=2 shiftwidth=2 softtabstop=2