Fixes to Provider.save() and tests.

Fixed `Provider.save()` to properly ignore stores which do not provide
a saveSync method.  Also, fixed `save()` to properly save asynchronously
when an async `save()` method on a store is provided.

Removed the tests from `nconf-test.js` which expected `save()` to throw
or return an error when a store without `save()` methods was
encountered. Also removed a `console.log` from `provider-test.js`.
This commit is contained in:
Russell Frank 2012-05-01 22:31:53 -04:00
parent 29eb5f905d
commit 36e061c4bd
4 changed files with 35 additions and 39 deletions

View file

@ -373,11 +373,14 @@ Provider.prototype.load = function (callback) {
}; };
// //
// ### function save (value, callback) // ### function save (callback)
// #### @value {Object} **Optional** Config object to set for this instance // #### @callback {function} **optional** Continuation to respond to when
// #### @callback {function} Continuation to respond to when complete. // complete.
// Removes any existing configuration settings that may exist in this // Instructs each provider to save. If a callback is provided, we will attempt
// instance and then adds all key-value pairs in `value`. // asynchronous saves on the providers, falling back to synchronous saves if
// this isn't possible. If a provider does not know how to save, it will be
// ignored. Returns an object consisting of all of the data which was
// actually saved.
// //
Provider.prototype.save = function (value, callback) { Provider.prototype.save = function (value, callback) {
if (!callback && typeof value === 'function') { if (!callback && typeof value === 'function') {
@ -388,32 +391,41 @@ Provider.prototype.save = function (value, callback) {
var self = this, var self = this,
names = Object.keys(this.stores); names = Object.keys(this.stores);
function saveStoreSync(name) { function saveStoreSync(memo, name) {
var store = self.stores[name]; var store = self.stores[name];
// //
// If the `store` doesn't have a `saveSync` method, // If the `store` doesn't have a `saveSync` method,
// just ignore it and continue. // just ignore it and continue.
// //
return store.saveSync if (store.saveSync) {
? store.saveSync() var ret = store.saveSync();
: null; if (typeof ret == 'object' && ret !== null) {
memo.push(ret);
}
}
return memo;
} }
function saveStore(name, next) { function saveStore(memo, name, next) {
var store = self.stores[name]; var store = self.stores[name];
// //
// If the `store` doesn't have a `save` or saveSync` // If the `store` doesn't have a `save` or saveSync`
// method(s), just ignore it and continue. // method(s), just ignore it and continue.
// //
if (!store.save && !store.saveSync) {
return next();
}
return store.saveSync if (store.save) {
? next(null, store.saveSync()) store.save(function (err, data) {
: store.save(next); if (err) return next(err);
if (typeof data == 'object' && data !== null) {
memo.push(data);
}
});
} else if (store.saveSync) {
memo.push(store.saveSync());
}
next(null, memo);
} }
// //
@ -422,11 +434,11 @@ Provider.prototype.save = function (value, callback) {
// then do so. // then do so.
// //
if (!callback) { if (!callback) {
return common.merge(names.map(saveStoreSync)); return common.merge(names.reduce(saveStoreSync, []));
} }
async.map(names, saveStore, function (err, objs) { async.reduce(names, [], saveStore, function (err, objs) {
return err ? callback(err) : callback(); return err ? callback(err) : callback(null, common.merge(objs));
}); });
}; };

View file

@ -101,21 +101,6 @@ vows.describe('nconf').addBatch({
}); });
} }
} }
},
"the save() method": {
"without a callback": {
"should throw an exception": function () {
assert.throws(function () { nconf.save() });
}
},
"with a callback": {
topic: function () {
nconf.save(this.callback.bind(null, null));
},
"should respond with an error": function (ign, err) {
assert.isNotNull(err);
}
}
} }
} }
} }

View file

@ -99,7 +99,6 @@ vows.describe('nconf/provider').addBatch({
helpers.assertMerged(null, merged); helpers.assertMerged(null, merged);
assert.equal(merged.candy.something, 'file1'); assert.equal(merged.candy.something, 'file1');
console.log(provider.sources);
} }
}, },
"when multiple stores are used": { "when multiple stores are used": {