From 7515f66572adda6f2b9c3bfd91749fb2391f8f1a Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 10 Jul 2012 01:27:28 -0400 Subject: [PATCH] [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 --- README.md | 6 +++++- lib/nconf/provider.js | 36 +++++++++++++++++++++--------------- test/complete-test.js | 36 +++++++++++++++--------------------- test/helpers.js | 4 +--- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 01a71f1..c335428 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,11 @@ A sane default for this could be: // // 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 diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 536781f..1aea6f1 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -36,23 +36,29 @@ var Provider = exports.Provider = function (options) { }); // -// Adds the file at `path` to the stores with `key` -// If key is not given, `file` will be used as the key -// For backward compatibility, an object can still be passed like {file: '/etc/foo.conf'} +// ### function file (key, path) +// #### @key {string|Object} Fully qualified options, name of file store, or path. +// #### @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) { - var args = Array.prototype.slice.call(arguments, 0); - if(args.length == 1) { - - if(typeof key === 'object'){ - path = key.file; - } - else { - path = key; - key = 'file'; - } +// nconf.file({ file: '.jitsuconf', dir: process.env.HOME, search: true }) +// nconf.file('path/to/config/file') +// nconf.file('userconfig', 'path/to/config/file') +// +Provider.prototype.file = function (key, path) { + var options; + + if (arguments.length == 1) { + options = typeof key !== 'object' + ? { 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); }; // diff --git a/test/complete-test.js b/test/complete-test.js index 4efeedd..6ac8ef7 100644 --- a/test/complete-test.js +++ b/test/complete-test.js @@ -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'), @@ -10,21 +13,25 @@ var fs = require('fs'), data = require('./fixtures/data').data, 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": { topic: function () { var that = this; helpers.cp(complete, completeTest, function () { nconf.env(); - nconf.file({file: completeTest}); - nconf.use('argv', {type: 'literal', store: data}); + nconf.file({ file: completeTest }); + nconf.use('argv', { type: 'literal', store: data }); 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": { "are present": function () { Object.keys(process.env).forEach(function (key) { @@ -32,12 +39,10 @@ vows.describe('nconf').addBatch({ }); } }, - "json vars": { topic: function () { fs.readFile(complete, 'utf8', this.callback); }, - "are present": function (err, data) { assert.isNull(err); data = JSON.parse(data); @@ -46,7 +51,6 @@ vows.describe('nconf').addBatch({ }); } }, - "literal vars": { "are present": function () { Object.keys(data).forEach(function (key) { @@ -54,24 +58,20 @@ vows.describe('nconf').addBatch({ }); } }, - "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) { @@ -95,19 +95,16 @@ vows.describe('nconf').addBatch({ 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); @@ -118,7 +115,6 @@ vows.describe('nconf').addBatch({ } } }, - teardown: function () { fs.unlinkSync(completeTest); nconf.remove('file'); @@ -127,6 +123,4 @@ vows.describe('nconf').addBatch({ nconf.remove('env'); } } -}).export(module); - -// vim: ts=2 shiftwidth=2 softtabstop=2 +}).export(module); \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js index d62f87e..f04c444 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -65,6 +65,4 @@ exports.cp = function (from, to, callback) { exports.fixture = function (file) { return require('path').join(__dirname, 'fixtures', file); -}; - -// vim: ts=2 shiftwidth=2 softtabstop=2 +}; \ No newline at end of file