From f4f1fdf464003802f0193303ae82fc4d75b8a879 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 23 Nov 2011 16:41:07 -0500 Subject: [PATCH] [fix test] Update to respected `.sources` option correctly --- lib/nconf/provider.js | 48 +++++++++++++++++++++++++------------ lib/nconf/stores/literal.js | 17 +++++++++---- test/provider-test.js | 17 +++++++++++++ test/stores/literal-test.js | 7 ++++-- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index b830b0e..22a6c1a 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -15,8 +15,6 @@ var async = require('async'), // for exposing the pluggable storage features of `nconf`. // var Provider = exports.Provider = function (options) { - var self = this; - // // Setup default options for working with `stores`, // `overrides`, `process.env` and `process.argv`. @@ -132,7 +130,15 @@ Provider.prototype.create = function (type, options) { return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options); }; +// +// ### function init (options) +// #### @options {Object} Options to initialize this instance with. +// Initializes this instance with additional `stores` or `sources` in the +// `options` supplied. +// Provider.prototype.init = function (options) { + var self = this; + // // Add any stores passed in through the options // to this instance. @@ -281,8 +287,13 @@ Provider.prototype.merge = function () { // Responds with an Object representing all keys associated in this instance. // Provider.prototype.load = function (callback) { - var self = this, - stores = Object.keys(this.stores).map(function (name) { return self.stores[name] }); + var self = this; + + function getStores () { + return Object.keys(self.stores).map(function (name) { + return self.stores[name]; + }); + } function loadStoreSync(store) { if (!store.loadSync) { @@ -318,8 +329,9 @@ Provider.prototype.load = function (callback) { // the system store. // if (data && typeof data === 'object') { - Object.keys(data).forEach(function (key) { - self.system.merge(key, data[key]); + self.use('sources', { + type: 'literal', + store: data }); } } @@ -332,7 +344,7 @@ Provider.prototype.load = function (callback) { // if (!callback) { mergeSources(loadBatch(self.sources)); - return loadBatch(stores); + return loadBatch(getStores()); } loadBatch(self.sources, function (err, data) { @@ -341,13 +353,13 @@ Provider.prototype.load = function (callback) { } mergeSources(data); - return loadBatch(stores, callback); + return loadBatch(getStores(), callback); }); } return self.sources.length ? loadSources() - : loadBatch(stores, callback); + : loadBatch(getStores(), callback); }; // @@ -369,18 +381,24 @@ Provider.prototype.save = function (value, callback) { function saveStoreSync(name) { var store = self.stores[name]; - if (!store.saveSync) { - throw new Error('nconf store ' + store.type + ' has no saveSync() method'); - } - - return store.saveSync(); + // + // If the `store` doesn't have a `saveSync` method, + // just ignore it and continue. + // + return store.saveSync + ? store.saveSync() + : null; } function saveStore(name, next) { var store = self.stores[name]; + // + // If the `store` doesn't have a `save` or saveSync` + // method(s), just ignore it and continue. + // if (!store.save && !store.saveSync) { - return next(new Error('nconf store ' + store.type + ' has no save() method')); + return next(); } return store.saveSync diff --git a/lib/nconf/stores/literal.js b/lib/nconf/stores/literal.js index 37a723c..b2424b1 100644 --- a/lib/nconf/stores/literal.js +++ b/lib/nconf/stores/literal.js @@ -8,13 +8,22 @@ var util = require('util'), Memory = require('./memory').Memory -var Literal = exports.Literal = function Literal (store) { - Memory.call(this); +var Literal = exports.Literal = function Literal (options) { + Memory.call(this, options); + options = options || {} this.type = 'literal'; this.readOnly = true; - this.store = store || {}; + this.store = options.store || {}; }; // Inherit from Memory store. -util.inherits(Literal, Memory); \ No newline at end of file +util.inherits(Literal, Memory); + +// +// ### function loadSync (callback) +// Returns the data stored in `this.store` synchronously. +// +Literal.prototype.loadSync = function () { + return this.store; +}; \ No newline at end of file diff --git a/test/provider-test.js b/test/provider-test.js index e3d7286..407e81b 100644 --- a/test/provider-test.js +++ b/test/provider-test.js @@ -73,6 +73,23 @@ vows.describe('nconf/provider').addBatch({ provider.merge(override); helpers.assertMerged(null, provider.stores.file.store); } + }, + "when sources are passed in": { + topic: new nconf.Provider({ + sources: { + user: { + type: 'file', + file: files[0] + }, + global: { + type: 'file', + file: files[1] + } + } + }), + "should have the result merged in": function (provider) { + helpers.assertMerged(null, provider.load()); + } } } } diff --git a/test/stores/literal-test.js b/test/stores/literal-test.js index 9876c3d..93feafe 100644 --- a/test/stores/literal-test.js +++ b/test/stores/literal-test.js @@ -13,14 +13,17 @@ var vows = require('vows'), vows.describe('nconf/stores/literal').addBatch({ "An instance of nconf.Literal": { topic: new nconf.Literal({ - foo: 'bar', - one: 2 + store: { + foo: 'bar', + one: 2 + } }), "should have the correct methods defined": function (literal) { assert.equal(literal.type, 'literal'); assert.isFunction(literal.get); assert.isFunction(literal.set); assert.isFunction(literal.merge); + assert.isFunction(literal.loadSync); }, "should have the correct values in the store": function (literal) { assert.equal(literal.store.foo, 'bar');