Skip to content

Commit

Permalink
[fix test] Update to respected .sources option correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Nov 24, 2011
1 parent bbcb271 commit f4f1fdf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
48 changes: 33 additions & 15 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
});
}
}
Expand All @@ -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) {
Expand All @@ -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);
};

//
Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions lib/nconf/stores/literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
util.inherits(Literal, Memory);

//
// ### function loadSync (callback)
// Returns the data stored in `this.store` synchronously.
//
Literal.prototype.loadSync = function () {
return this.store;
};
17 changes: 17 additions & 0 deletions test/provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions test/stores/literal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit f4f1fdf

Please sign in to comment.