From ba044b295336da3927d13724e422312eafe7598c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=E1=BB=8Dc=20Ph=C3=BA?= Date: Fri, 21 Aug 2020 13:28:42 +0700 Subject: [PATCH] fix - issue 302 --- lib/nconf/stores/argv.js | 8 +++++--- lib/nconf/stores/env.js | 4 +++- test/complete.test.js | 8 ++++---- test/hierarchy.test.js | 1 - test/stores/argv.test.js | 16 +++++++++++++++- test/stores/env.test.js | 9 +++++++++ 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index 772a2fd0..8bd8a259 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -106,7 +106,9 @@ Argv.prototype.loadArgv = function () { } if (self.separator) { - self.set(common.key.apply(common, key.split(self.separator)), val); + var path = common.path(key, self.separator); + + self.set(common.keyed.apply(common, [self.logicalSeparator].concat(path)), val); } else { self.set(key, val); @@ -114,8 +116,8 @@ Argv.prototype.loadArgv = function () { } }); - this.showHelp = yargs.showHelp - this.help = yargs.help + this.showHelp = yargs.showHelp; + this.help = yargs.help; if (tempWrite) { this.readOnly = true; diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index 37b2c81d..1b3d3415 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -98,7 +98,9 @@ Env.prototype.loadEnv = function () { } if (self.separator) { - self.set(common.key.apply(common, key.split(self.separator)), val); + var path = common.path(key, self.separator); + + self.set(common.keyed.apply(common, [self.logicalSeparator].concat(path)), val); } else { self.set(key, val); diff --git a/test/complete.test.js b/test/complete.test.js index c7f3dfb4..582501c2 100644 --- a/test/complete.test.js +++ b/test/complete.test.js @@ -119,12 +119,12 @@ describe('nconf/multiple-stores', () => { nconf.env({lowerCase: true}); done(); }) - }) + }); it("env vars keys also available as lower case", () => { Object.keys(process.env).forEach(function (key) { expect(nconf.get(key.toLowerCase())).toEqual(process.env[key]); }); - }) + }); afterAll(() => nconf.remove('env')) }); @@ -136,7 +136,7 @@ describe('nconf/multiple-stores', () => { nconf.env({parseValues: true}); done(); }) - }) + }); it("JSON keys properly parsed", () => { Object.keys(process.env).forEach(function (key) { var val = process.env[key]; @@ -147,7 +147,7 @@ describe('nconf/multiple-stores', () => { } expect(nconf.get(key)).toEqual(val); - }) + }); afterAll(() => nconf.remove('env')) }); diff --git a/test/hierarchy.test.js b/test/hierarchy.test.js index 9e6fc945..7facf9ed 100644 --- a/test/hierarchy.test.js +++ b/test/hierarchy.test.js @@ -113,7 +113,6 @@ describe('nconf/hierarchy, When using nconf', () => { }); child.on('close', function () { - console.log(data); expect(JSON.parse(data)).toEqual({ apples: true, candy: { diff --git a/test/stores/argv.test.js b/test/stores/argv.test.js index 5d3a8411..3badc91f 100644 --- a/test/stores/argv.test.js +++ b/test/stores/argv.test.js @@ -8,6 +8,8 @@ var yargs = require('yargs'); var nconf = require('../../lib/nconf'); +process.env.DEEP__NESTED__VALUE = 'foo'; + describe('nconf/stores/argv, An instance of nconf.Argv', () => { it("should have the correct methods defined", () => { @@ -17,6 +19,18 @@ describe('nconf/stores/argv, An instance of nconf.Argv', () => { expect(argv.options).toEqual({}); }); + it("should be able to retrieve a value using the logical separator", () => { + var argv = new nconf.Argv({ + deep__nested__value: {alias: 'nv', default: 'foo'}, + logicalSeparator: '.', + separator: '__' + }); + argv.loadSync(); + + expect(argv.logicalSeparator).toBe('.'); + expect(argv.get('deep.nested.value')).toBe('foo'); + }); + describe("can be created with a custom yargs", () => { var yargsInstance = yargs.alias('s', 'somearg').default('s', 'false'); @@ -55,7 +69,7 @@ describe('nconf/stores/argv, An instance of nconf.Argv', () => { }); }); describe("can be created with readOnly set to be false", () => { - + it("readOnly is actually false", () => { var argv = new nconf.Argv({readOnly: false}); expect(argv.readOnly).toBe(false); diff --git a/test/stores/env.test.js b/test/stores/env.test.js index 22837d1d..487b02e1 100644 --- a/test/stores/env.test.js +++ b/test/stores/env.test.js @@ -7,6 +7,8 @@ var nconf = require('../../lib/nconf'); +process.env.DEEP__NESTED__VALUE = 'foo'; + describe('nconf/stores/env, An instance of nconf.Env', () => { it("should have the correct methods defined", () => { var env = new nconf.Env(); @@ -25,4 +27,11 @@ describe('nconf/stores/env, An instance of nconf.Env', () => { expect(env.separator).toEqual(''); expect(env.readOnly).toBe(false); }); + it("should be able to retrieve a value using the logical separator", () => { + var env = new nconf.Env({logicalSeparator: '.', separator: '__'}); + env.loadSync(); + + expect(env.logicalSeparator).toBe('.'); + expect(env.get('DEEP.NESTED.VALUE')).toBe('foo'); + }) });