Skip to content

Commit

Permalink
Merge pull request #119 from gemini-testing/parseEnv
Browse files Browse the repository at this point in the history
fix: Boolean env and cli vars reading
  • Loading branch information
Jan Beletskiy authored Feb 22, 2017
2 parents d514ff1 + fb5e932 commit 82b3d0c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/config/options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const assertNonNegativeInteger = utils.assertNonNegativeInteger;
const assertPositiveInteger = utils.assertPositiveInteger;
const assertOptionalObject = utils.assertOptionalObject;
const parseBoolean = utils.parseBoolean;
const parsePrimitive = utils.parsePrimitive;
const is = utils.is;

module.exports = (defaultFactory) => {
Expand Down Expand Up @@ -52,7 +53,10 @@ module.exports = (defaultFactory) => {
}

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

function nonNegativeInteger(name) {
Expand Down
8 changes: 8 additions & 0 deletions lib/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ exports.parseBoolean = exports.parseBoolean = (value, name) => {
}
};

exports.parsePrimitive = exports.parsePrimitive = (str) => {
try {
return JSON.parse(str);
} catch (error) {
throw new Error('a value must be a primitive type');
}
};

exports.resolveWithProjectDir = (value) => value ? path.resolve(process.cwd(), value) : value;
62 changes: 62 additions & 0 deletions test/lib/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const _ = require('lodash');
const Config = require('../../../lib/config');
const defaults = require('../../../lib/config/defaults');

const parser = require('../../../lib/config/options');

describe('config options', () => {
const sandbox = sinon.sandbox.create();

Expand Down Expand Up @@ -108,4 +110,64 @@ describe('config options', () => {
assert.deepEqual(config.prepareEnvironment, newFunc);
});
});

describe('plugins', () => {
const parse_ = (opts) => parser(_.defaults(opts, {env: {}, argv: []}));

it('should parse boolean value from environment', () => {
const result = parse_({
options: {plugins: {foo: {}}},
env: {'hermione_plugins_foo': 'true'}
});

assert.strictEqual(result.plugins.foo, true);
});

it('should parse object value from environment', () => {
const result = parse_({
options: {plugins: {foo: {}}},
env: {'hermione_plugins_foo': '{"opt": 1}'}
});

assert.deepEqual(result.plugins.foo, {opt: 1});
});

it('should throw error on invalid values from environment', () => {
assert.throws(
() => parse_({
options: {plugins: {foo: {}}},
env: {'hermione_plugins_foo': '{key: 1}'}
}),
'a value must be a primitive type'
);
});

it('should parse boolean value from cli', () => {
const result = parse_({
options: {plugins: {foo: {}}},
argv: ['--plugins-foo', 'true']
});

assert.strictEqual(result.plugins.foo, true);
});

it('should parse object value from cli', () => {
const result = parse_({
options: {plugins: {foo: {}}},
argv: ['--plugins-foo', '{"opt": 1}']
});

assert.deepEqual(result.plugins.foo, {opt: 1});
});

it('should throw error on invalid values from cli', () => {
assert.throws(
() => parse_({
options: {plugins: {foo: {}}},
argv: ['--plugins-foo', '{key: 1}']
}),
'a value must be a primitive type'
);
});
});
});

0 comments on commit 82b3d0c

Please sign in to comment.