8000 Use JSON.parse for plugins values from cli/env by seth2810 · Pull Request #661 · gemini-testing/gemini · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/config/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ function parseBoolean(value) {
}
}

function parsePrimitive(str) {
try {
return JSON.parse(str);
} catch (error) {
throw new GeminiError('a value must be a primitive type');
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function parsePrimitive(str) {
    try {
        return JSON.parse(str);
    } catch (err) {}
}

А мы не хотим вываливать какой-нибудь warning, если не получилось распарсить?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@j0tunn во всех остальных типах, в случае не валидных значений мы кидаем GeminiError. Не будет ли это создавать лишнюю путаницу?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну давай тогда и тут бросать


function positiveIntegerOption(defaultValue) {
return option({
parseEnv: Number,
Expand All @@ -70,7 +78,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 throw if value from environment is not valid', () => {
['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => {
assert.throw(() => {
assertParsesEnv({property: name, value});
}, GeminiError);
});
});

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 throw if value from cli is not valid', () => {
['{a:1}', '{', ']', '\'string\'', '\n'].forEach((value) => {
assert.throw(() => {
assertParsesCli({property: name, value});
}, GeminiError);
});
});
}

beforeEach(function() {
this.sinon = sinon.sandbox.create();
});
Expand Down Expand Up @@ -439,6 +474,10 @@ describe('config', function() {
});
});
});

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

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