Skip to content

Commit 82b3d0c

Browse files
author
Jan Beletskiy
authored
Merge pull request #119 from gemini-testing/parseEnv
fix: Boolean env and cli vars reading
2 parents d514ff1 + fb5e932 commit 82b3d0c

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

lib/config/options-builder.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const assertNonNegativeInteger = utils.assertNonNegativeInteger;
1111
const assertPositiveInteger = utils.assertPositiveInteger;
1212
const assertOptionalObject = utils.assertOptionalObject;
1313
const parseBoolean = utils.parseBoolean;
14+
const parsePrimitive = utils.parsePrimitive;
1415
const is = utils.is;
1516

1617
module.exports = (defaultFactory) => {
@@ -52,7 +53,10 @@ module.exports = (defaultFactory) => {
5253
}
5354

5455
function anyObject() {
55-
return map(option({}));
56+
return map(option({
57+
parseEnv: parsePrimitive,
58+
parseCli: parsePrimitive
59+
}));
5660
}
5761

5862
function nonNegativeInteger(name) {

lib/config/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ exports.parseBoolean = exports.parseBoolean = (value, name) => {
4444
}
4545
};
4646

47+
exports.parsePrimitive = exports.parsePrimitive = (str) => {
48+
try {
49+
return JSON.parse(str);
50+
} catch (error) {
51+
throw new Error('a value must be a primitive type');
52+
}
53+
};
54+
4755
exports.resolveWithProjectDir = (value) => value ? path.resolve(process.cwd(), value) : value;

test/lib/config/options.js

+62
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const _ = require('lodash');
44
const Config = require('../../../lib/config');
55
const defaults = require('../../../lib/config/defaults');
66

7+
const parser = require('../../../lib/config/options');
8+
79
describe('config options', () => {
810
const sandbox = sinon.sandbox.create();
911

@@ -108,4 +110,64 @@ describe('config options', () => {
108110
assert.deepEqual(config.prepareEnvironment, newFunc);
109111
});
110112
});
113+
114+
describe('plugins', () => {
115+
const parse_ = (opts) => parser(_.defaults(opts, {env: {}, argv: []}));
116+
117+
it('should parse boolean value from environment', () => {
118+
const result = parse_({
119+
options: {plugins: {foo: {}}},
120+
env: {'hermione_plugins_foo': 'true'}
121+
});
122+
123+
assert.strictEqual(result.plugins.foo, true);
124+
});
125+
126+
it('should parse object value from environment', () => {
127+
const result = parse_({
128+
options: {plugins: {foo: {}}},
129+
env: {'hermione_plugins_foo': '{"opt": 1}'}
130+
});
131+
132+
assert.deepEqual(result.plugins.foo, {opt: 1});
133+
});
134+
135+
it('should throw error on invalid values from environment', () => {
136+
assert.throws(
137+
() => parse_({
138+
options: {plugins: {foo: {}}},
139+
env: {'hermione_plugins_foo': '{key: 1}'}
140+
}),
141+
'a value must be a primitive type'
142+
);
143+
});
144+
145+
it('should parse boolean value from cli', () => {
146+
const result = parse_({
147+
options: {plugins: {foo: {}}},
148+
argv: ['--plugins-foo', 'true']
149+
});
150+
151+
assert.strictEqual(result.plugins.foo, true);
152+
});
153+
154+
it('should parse object value from cli', () => {
155+
const result = parse_({
156+
options: {plugins: {foo: {}}},
157+
argv: ['--plugins-foo', '{"opt": 1}']
158+
});
159+
160+
assert.deepEqual(result.plugins.foo, {opt: 1});
161+
});
162+
163+
it('should throw error on invalid values from cli', () => {
164+
assert.throws(
165+
() => parse_({
166+
options: {plugins: {foo: {}}},
167+
argv: ['--plugins-foo', '{key: 1}']
168+
}),
169+
'a value must be a primitive type'
170+
);
171+
});
172+
});
111173
});

0 commit comments

Comments
 (0)