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

Commit 6039fd1

Browse files
committed
fix(config): use JSON.parse for plugins values from cli/env
1 parent cc88f2b commit 6039fd1

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

lib/config/util.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ function parseBoolean(value) {
4848
}
4949
}
5050

51+
function parsePrimitive(str) {
52+
let value;
53+
54+
try {
55+
value = JSON.parse(str);
56+
} catch (error) {
57+
// do nothing
58+
}
59+
60+
return value;
61+
}
62+
5163
function positiveIntegerOption(defaultValue) {
5264
return option({
5365
parseEnv: Number,
@@ -70,7 +82,10 @@ function positiveIntegerOption(defaultValue) {
7082
}
7183

7284
function anyObject() {
73-
return map(option({}));
85+
return map(option({
86+
parseEnv: parsePrimitive,
87+
parseCli: parsePrimitive
88+
}));
7489
}
7590

7691
exports.is = is;

test/unit/config-options/config-options.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ var Config = require('lib/config'),
88
describe('config', function() {
99
var VALID_OPTIONS = {
1010
system: {
11-
projectRoot: '/some/path'
11+
projectRoot: '/some/path',
12+
plugins: {
13+
plugin: {}
14+
}
1215
},
1316
rootUrl: 'http://example.com/root',
1417
gridUrl: 'http://example.com/root',
@@ -225,6 +228,38 @@ describe('config', function() {
225228
});
226229
}
227230

231+
function testObjectOption(name) {
232+
it('should parse any of primitive type from environment', () => {
233+
['string', 1.0, 1, false, null, [], {a: 1}].forEach((expected) => {
234+
const value = JSON.stringify(expected);
235+
assertParsesEnv({property: name, value, expected});
236+
});
237+
});
238+
239+
it('should not throws if value from environment is not valid', () => {
240+
['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => {
241+
assert.doesNotThrow(() => {
242+
assertParsesEnv({property: name, value});
243+
});
244+
});
245+
});
246+
247+
it('should parse any of primitive type from cli', () => {
248+
['string', 1.0, 1, false, null, [], {a: 1}].forEach((expected) => {
249+
const value = JSON.stringify(expected);
250+
assertParsesCli({property: name, value, expected});
251+
});
252+
});
253+
254+
it('should not throws if value from cli is not valid', () => {
255+
['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => {
256+
assert.doesNotThrow(() => {
257+
assertParsesCli({property: name, value});
258+
});
259+
});
260+
});
261+
}
262+
228263
beforeEach(function() {
229264
this.sinon = sinon.sandbox.create();
230265
});
@@ -439,6 +474,10 @@ describe('config', function() {
439474
});
440475
});
441476
});
477+
478+
describe.only('plugins', () => {
479+
testObjectOption('system.plugins.plugin');
480+
});
442481
});
443482

444483
describe('browser options', function() {

0 commit comments

Comments
 (0)