diff --git a/lib/configuration/validateConfig.js b/lib/configuration/validateConfig.js index ff0ea86a9..5b872d638 100644 --- a/lib/configuration/validateConfig.js +++ b/lib/configuration/validateConfig.js @@ -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; + }, []); } /** diff --git a/test/unit/configuration-test.js b/test/unit/configuration-test.js index 6497fe7c5..3a69668fe 100644 --- a/test/unit/configuration-test.js +++ b/test/unit/configuration-test.js @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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);