From b3170fae15671d8d5df6a87d74eb2f5832bfd28c Mon Sep 17 00:00:00 2001 From: Erno Aapa Date: Sun, 27 Dec 2015 10:17:33 +0200 Subject: [PATCH] Keep hierarchy when updating store with `use()` function Previously, when store were updated with `Provider.use()` function it were moved as last in configuration hierarchy. This caused that usually some other configuration were overriding updated configurations and the updates never get applied. Updated `use()` function to not remove/add the store, but just re-define it so the store key keep the same position in the `this.stores` variable. --- lib/nconf/provider.js | 8 +++----- test/provider-test.js | 10 +++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index d35b8201..f13a9d2d 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -102,11 +102,9 @@ Provider.prototype.use = function (name, options) { var store = this.stores[name], update = store && !sameOptions(store); - if (!store || update) { - if (update) { - this.remove(name); - } - + if (!store) { + this.add(name, options); + } else if (store && update) { this.add(name, options); } diff --git a/test/provider-test.js b/test/provider-test.js index ac4c5fdc..d26703ce 100644 --- a/test/provider-test.js +++ b/test/provider-test.js @@ -71,7 +71,15 @@ vows.describe('nconf/provider').addBatch({ "when 'env' is set to true with a nested separator": helpers.assertSystemConf({ script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'), env: { SOME_THING: 'foobar' } - }) + }), + "calling the use() method after adding other stores": { + topic: new nconf.Provider().use('file', { file: files[0] }).defaults({candy: {something: 'should never get this'}}), + "should use a new instance of the store type": function (provider) { + provider.use('file', { file: files[1] }); + + assert.equal(provider.get('candy:something'), 'file2'); + } + }, } } }).addBatch({