[api test] Update `nconf.Provider` to create a new instance of the store if the options are different

master
indexzero 2011-06-25 00:34:07 -04:00
parent 1b0f34793b
commit 7e4623ec46
2 changed files with 45 additions and 1 deletions

View File

@ -26,9 +26,21 @@ var Provider = exports.Provider = function (options) {
// specified `type`.
//
Provider.prototype.use = function (type, options) {
if (!this.store || type.toLowerCase() !== this.store.type) {
var self = this;
options = options || {};
function sameOptions () {
return Object.keys(options).every(function (key) {
return options[key] === self.store[key];
});
}
if (!this.store || type.toLowerCase() !== this.store.type
|| !sameOptions()) {
this.store = stores.create(type, options);
}
return this;
};
//

32
test/provider-test.js Normal file
View File

@ -0,0 +1,32 @@
/*
* file-store-test.js: Tests for the nconf File store.
*
* (C) 2011, Charlie Robbins
*
*/
var fs = require('fs'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../lib/nconf')
var first = '/path/to/file1',
second = '/path/to/file2';
vows.describe('nconf/provider').addBatch({
"When using an instance of nconf.Provier": {
"calling the use() method with the same store type and different options": {
topic: new nconf.Provider().use('file', { file: first }),
"should use a new instance of the store type": function (provider) {
var old = provider.store;
assert.equal(provider.store.file, first);
provider.use('file', { file: second });
assert.notStrictEqual(old, provider.store);
assert.equal(provider.store.file, second);
}
}
}
}).export(module);