Skip to content

Commit

Permalink
Fix indexzero#302 honor logicalSeparator for env and argv stores
Browse files Browse the repository at this point in the history
  • Loading branch information
p-tchaikovsky committed Jul 2, 2019
1 parent ee8e89c commit 872ced4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/nconf/stores/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ 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);
}
}
});

this.showHelp = yargs.showHelp
this.help = yargs.help
this.showHelp = yargs.showHelp;
this.help = yargs.help;

if (tempWrite) {
this.readOnly = true;
Expand Down
4 changes: 3 additions & 1 deletion lib/nconf/stores/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/complete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
});

Expand All @@ -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];
Expand All @@ -147,7 +147,7 @@ describe('nconf/multiple-stores', () => {
}

expect(nconf.get(key)).toEqual(val);
})
});
afterAll(() => nconf.remove('env'))
});

Expand Down
1 change: 0 additions & 1 deletion test/hierarchy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions test/stores/argv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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');

Expand Down
9 changes: 9 additions & 0 deletions test/stores/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
})
});

0 comments on commit 872ced4

Please sign in to comment.