Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
refactor: handles option alias during config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Apr 29, 2019
1 parent 6f37517 commit d183e5c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 30 deletions.
64 changes: 42 additions & 22 deletions lib/configuration/validateConfig.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
/**
* TODO Account for option aliases (i.e. "l", "q").
* Enforce strict rules not to be able to coerce removed options, only the deprecated ones.
*/
const deprecatedOptions = {
c: 'DEPRECATED: The `c` configuration option is deprecated. Plese use `color` instead.',
data: 'DEPRECATED: The `data` configuration option is deprecated '
const deprecatedOptions = [
{
options: ['c'],
message: 'DEPRECATED: The -c configuration option is deprecated. Plese use --color instead.',
},
{
options: ['data'],
message: 'DEPRECATED: The --data configuration option is deprecated '
+ 'in favor of `apiDescriptions`, please see https://dredd.org',
blueprintPath: 'DEPRECATED: The `blueprintPath` configuration option is deprecated, '
+ 'please use `path` instead.',
level: 'DEPRECATED: The `level` configuration option is deprecated. Please use `loglevel` instead.',
};
},
{
options: ['blueprintPath'],
message: 'DEPRECATED: The --blueprintPath configuration option is deprecated, '
+ 'please use --path instead.',
},
{
options: ['level'],
message: 'DEPRECATED: The --level configuration option is deprecated. Please use --loglevel instead.',
},
];

const unsupportedOptions = {
timestamp: 'REMOVED: The `timestamp` configuration option is removed. Please use `--loglevel=debug` instead.',
silent: 'REMOVED: The `silent` configuration option is removed. Please use `--loglevel=silent` instead.',
sandbox: 'REMOVED: Dredd does not support sandboxed JS hooks anymore, use standard JS hooks instead.',
hooksData: 'REMOVED: Dredd does not support sandboxed JS hooks anymore, use standard JS hooks instead.',
};
const unsupportedOptions = [
{
options: ['timestamp', 't'],
message: 'REMOVED: The --timestamp/-t configuration option is no longer supported. Please use --loglevel=debug instead.',
},
{
options: ['silent', 'q'],
message: 'REMOVED: The --silent/-q configuration option is no longer supported. Please use --loglevel=silent instead.',
},
{
options: ['sandbox', 'b'],
message: 'REMOVED: Dredd does not support sandboxed JS hooks anymore, use standard JS hooks instead.',
},
{
options: ['hooksData'],
message: 'REMOVED: Dredd does not support sandboxed JS hooks anymore, use standard JS hooks instead.',
},
];

function flushMessages(options, config) {
return Object.keys(options).reduce((messages, optionName) => (config[optionName]
? messages.concat(options[optionName])
: messages
), []);
function flushMessages(rules, config) {
return Object.keys(config).reduce((messages, configKey) => {
const warning = rules.find(rule => rule.options.includes(configKey));
return warning ? messages.concat(warning.message) : messages;
}, []);
}

/**
Expand Down
61 changes: 53 additions & 8 deletions test/unit/configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,12 @@ describe('configuration', () => {
});
});

describe('with --timestamp/-t set', () => {
const config = { timestamp: true, t: true };
describe('with --timestamp set', () => {
const config = { timestamp: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 't');
assert.notProperty(normalizedConfig, 'timestamp');
});
it('produces no warnings', () => {
Expand All @@ -339,13 +338,28 @@ describe('configuration', () => {
});
});

describe('with --silent/-q set', () => {
const config = { silent: true, q: true };
describe('with -t set', () => {
const config = { t: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 't');
});
it('produces no warnings', () => {
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
assert.lengthOf(errors, 1);
});
});

describe('with --silent set', () => {
const config = { silent: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 'q');
assert.notProperty(normalizedConfig, 'silent');
});
it('produces no warnings', () => {
Expand All @@ -356,13 +370,28 @@ describe('configuration', () => {
});
});

describe('with -q set', () => {
const config = { q: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 'q');
});
it('produces no warnings', () => {
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
assert.lengthOf(errors, 1);
});
});

describe('with --sandbox/-b set', () => {
const config = { sandbox: true, b: true };
const config = { sandbox: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 'b');
assert.notProperty(normalizedConfig, 'sandbox');
});
it('produces no warnings', () => {
Expand All @@ -373,6 +402,22 @@ describe('configuration', () => {
});
});

describe('with -b set', () => {
const config = { b: true };
const normalizedConfig = normalizeConfig(config);
const { warnings, errors } = validateConfig(config);

it('gets removed', () => {
assert.notProperty(normalizedConfig, 'b');
});
it('produces no warnings', () => {
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
assert.lengthOf(errors, 1);
});
});

describe('with data set to { filename: apiDescription }', () => {
const config = { data: { 'filename.api': 'FORMAT: 1A\n# Sample API\n' } };
const { warnings, errors } = validateConfig(config);
Expand Down

0 comments on commit d183e5c

Please sign in to comment.