Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
fix(config): use JSON.parse for plugins values from cli/env
Browse files Browse the repository at this point in the history
  • Loading branch information
seth2810 committed Nov 3, 2016
1 parent cc88f2b commit 6039fd1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/config/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -70,7 +82,10 @@ function positiveIntegerOption(defaultValue) {
}

function anyObject() {
return map(option({}));
return map(option({
parseEnv: parsePrimitive,
parseCli: parsePrimitive
}));
}

exports.is = is;
Expand Down
41 changes: 40 additions & 1 deletion test/unit/config-options/config-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -439,6 +474,10 @@ describe('config', function() {
});
});
});

describe.only('plugins', () => {
testObjectOption('system.plugins.plugin');
});
});

describe('browser options', function() {
Expand Down

0 comments on commit 6039fd1

Please sign in to comment.