diff --git a/lib/config/util.js b/lib/config/util.js index 18d6bb1cf..10c7a05df 100644 --- a/lib/config/util.js +++ b/lib/config/util.js @@ -48,6 +48,18 @@ function parseBoolean(value) { } } +function parsePrimitive(str) { + let value; + + try { + value = JSON.parse(str); + } catch (error) { + // do nothing + } + + return value; +} + function positiveIntegerOption(defaultValue) { return option({ parseEnv: Number, @@ -70,7 +82,10 @@ function positiveIntegerOption(defaultValue) { } function anyObject() { - return map(option({})); + return map(option({ + parseEnv: parsePrimitive, + parseCli: parsePrimitive + })); } exports.is = is; diff --git a/test/unit/config-options/config-options.test.js b/test/unit/config-options/config-options.test.js index 8579fa357..ec9fb68bf 100644 --- a/test/unit/config-options/config-options.test.js +++ b/test/unit/config-options/config-options.test.js @@ -8,7 +8,10 @@ var Config = require('lib/config'), describe('config', function() { var VALID_OPTIONS = { system: { - projectRoot: '/some/path' + projectRoot: '/some/path', + plugins: { + plugin: {} + } }, rootUrl: 'http://example.com/root', gridUrl: 'http://example.com/root', @@ -225,6 +228,38 @@ describe('config', function() { }); } + function testObjectOption(name) { + it('should parse any of primitive type from environment', () => { + ['string', 1.0, 1, false, null, [], {a: 1}].forEach((expected) => { + const value = JSON.stringify(expected); + assertParsesEnv({property: name, value, expected}); + }); + }); + + it('should not throws if value from environment is not valid', () => { + ['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => { + assert.doesNotThrow(() => { + assertParsesEnv({property: name, value}); + }); + }); + }); + + it('should parse any of primitive type from cli', () => { + ['string', 1.0, 1, false, null, [], {a: 1}].forEach((expected) => { + const value = JSON.stringify(expected); + assertParsesCli({property: name, value, expected}); + }); + }); + + it('should not throws if value from cli is not valid', () => { + ['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => { + assert.doesNotThrow(() => { + assertParsesCli({property: name, value}); + }); + }); + }); + } + beforeEach(function() { this.sinon = sinon.sandbox.create(); }); @@ -439,6 +474,10 @@ describe('config', function() { }); }); }); + + describe.only('plugins', () => { + testObjectOption('system.plugins.plugin'); + }); }); describe('browser options', function() {