From c8ee1eaaec06aab209b64eff43f8985507716753 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Mon, 9 Nov 2015 18:41:04 -0600 Subject: [PATCH] Add support for passing in custom env file to nconf. --- test/api/v3/unit/libs/setupNconf.test.js | 24 +++++++++++++++++------- test/helpers/globals.helper.js | 2 +- website/src/libs/api-v3/setupNconf.js | 6 ++++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/test/api/v3/unit/libs/setupNconf.test.js b/test/api/v3/unit/libs/setupNconf.test.js index e34d87c16b..1fbb046a85 100644 --- a/test/api/v3/unit/libs/setupNconf.test.js +++ b/test/api/v3/unit/libs/setupNconf.test.js @@ -3,29 +3,39 @@ import setupNconf from '../../../../../website/src/libs/api-v3/setupNconf'; import nconf from 'nconf'; describe('setupNconf', () => { - before(() => { - sandbox.spy(nconf, 'argv'); - sandbox.spy(nconf, 'env'); - sandbox.spy(nconf, 'file'); - - setupNconf(); + beforeEach(() => { + sandbox.stub(nconf, 'argv').returnsThis(); + sandbox.stub(nconf, 'env').returnsThis(); + sandbox.stub(nconf, 'file').returnsThis(); }); - after(() => { + afterEach(() => { sandbox.restore(); }); it('sets up nconf', () => { + setupNconf(); + expect(nconf.argv).to.be.calledOnce; expect(nconf.env).to.be.calledOnce; expect(nconf.file).to.be.calledOnce; + expect(nconf.file).to.be.calledWithMatch('user', /\/config.json$/); }); it('sets IS_PROD variable', () => { + setupNconf(); expect(nconf.get('IS_PROD')).to.exist; }); it('sets IS_DEV variable', () => { + setupNconf(); expect(nconf.get('IS_DEV')).to.exist; }); + + it('allows a custom config.json file to be passed in', () => { + setupNconf('customfile.json'); + + expect(nconf.file).to.be.calledOnce; + expect(nconf.file).to.be.calledWithMatch('user', 'customfile.json'); + }); }); diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index b825a527dc..52f9096315 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -14,4 +14,4 @@ global.sandbox = sinon.sandbox.create(); //------------------------------ // Load nconf for unit tests //------------------------------ -require('../../website/src/libs/api-v3/setupNconf')(); +require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); diff --git a/website/src/libs/api-v3/setupNconf.js b/website/src/libs/api-v3/setupNconf.js index ff0da651df..a3ba29f779 100644 --- a/website/src/libs/api-v3/setupNconf.js +++ b/website/src/libs/api-v3/setupNconf.js @@ -3,11 +3,13 @@ import { join, resolve } from 'path'; const PATH_TO_CONFIG = join(resolve(__dirname, '../../../../config.json')); -export default function setupNconf () { +export default function setupNconf (file) { + file = file || PATH_TO_CONFIG; + nconf .argv() .env() - .file('user', PATH_TO_CONFIG); + .file('user', file); nconf.set('IS_PROD', nconf.get('NODE_ENV') === 'production'); nconf.set('IS_DEV', nconf.get('NODE_ENV') === 'development');