[fix test] Update to respected .sources option correctly

This commit is contained in:
indexzero 2011-11-23 16:41:07 -05:00
parent bbcb2712f1
commit f4f1fdf464
4 changed files with 68 additions and 21 deletions

View file

@ -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

View file

@ -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);
//
// ### function loadSync (callback)
// Returns the data stored in `this.store` synchronously.
//
Literal.prototype.loadSync = function () {
return this.store;
};

View file

@ -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());
}
}
}
}

View file

@ -13,14 +13,17 @@ var vows = require('vows'),
vows.describe('nconf/stores/literal').addBatch({
"An instance of nconf.Literal": {
topic: new nconf.Literal({
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');