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

Commit

Permalink
refactor: produces "warnings" and "errors" lists upon config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Apr 8, 2019
1 parent 283d6af commit 0561654
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 67 deletions.
30 changes: 14 additions & 16 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,20 @@ const coerceOptions = R.compose(
/**
* @todo Still confusing how to adequately compose { errors, warnings }
*/
const deprecationWarnings = {
level: 'Do not use `c`',
const deprecatedOptions = {
c: 'DEPRECATED: The data `c` configuration option is deprecated. Plese use `color` instead.',
data: '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.',
};

const deprecationErrors = {
level: 'REMOVED: level',
timestamp: 'REMOVED: The "timestamp" option has been removed.',
const unsupportedOptions = {
level: 'REMOVED: The `level` configuration option is removed. Please use `--loglevel` instead.',
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.',
};

// TODO
Expand All @@ -221,8 +228,8 @@ function flushMessages(rules, config) {
* Returns the errors and warnings relative to the given config.
*/
const validateConfig = config => ({
errors: flushMessages(deprecationErrors, config),
warnings: flushMessages(deprecationWarnings, config),
warnings: flushMessages(deprecatedOptions, config),
errors: flushMessages(unsupportedOptions, config),
});

const normalizeConfig = R.compose(
Expand Down Expand Up @@ -250,15 +257,6 @@ function flattenConfig(config) {
};
}

// function coerceRemovedOptions(config = {}) {
// if (config.data) {
// warnings.push("DEPRECATED: The 'data' configuration property is deprecated "
// + "in favor of 'apiDescriptions', please see https://dredd.org");
// }

// return { errors, warnings };
// }

const defaultConfig = {
http: {},
server: null,
Expand Down
85 changes: 34 additions & 51 deletions test/unit/configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,225 +172,213 @@ describe('configuration.applyLoggingOptions()', () => {
describe('configuration._coerceRemovedOptions()', () => {
describe("with -c set to string 'true'", () => {
const config = { c: 'true' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets removed', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.notProperty(normalizedConfig, 'c');
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
it('produces one warning', () => {
assert.lengthOf(warnings, 1);
});
it('produces one error', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 1);
it('produces no errors', () => {
assert.lengthOf(errors, 0);
});
});

describe("with --color set to string 'true'", () => {
const config = { color: 'true' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to color set to boolean true', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'color', true);
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});

describe("with --color set to string 'false'", () => {
const config = { color: 'false' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to color set to boolean false', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'color', false);
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});

describe('with --color set to true', () => {
const config = { color: true };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to color set to boolean true', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'color', true);
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});

describe('with --color set to false', () => {
const config = { color: false };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to color set to boolean false', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'color', false);
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});

describe('with --level/-l set to a supported value', () => {
const config = { l: 'debug', level: 'debug' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to loglevel set to the value', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'loglevel', 'debug');
assert.notProperty(normalizedConfig, 'l');
assert.notProperty(normalizedConfig, 'level');
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 1);
});
});

describe('with --level/-l set to a consolidated value', () => {
const config = { l: 'verbose', level: 'verbose' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to loglevel set to a corresponding value', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'loglevel', 'debug');
assert.notProperty(normalizedConfig, 'l');
assert.notProperty(normalizedConfig, 'level');
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 1);
});
});

describe('with --level/-l set to a removed value', () => {
const config = { l: 'complete', level: 'complete' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to loglevel set to the default value', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'loglevel', 'warn');
assert.notProperty(normalizedConfig, 'l');
assert.notProperty(normalizedConfig, 'level');
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces one error', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 1);
});
});

describe("with -l set to 'silent'", () => {
const config = { l: 'silent' };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets coerced to loglevel set to silent', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.propertyVal(normalizedConfig, 'loglevel', 'silent');
assert.notProperty(normalizedConfig, 'l');
assert.notProperty(normalizedConfig, 'level');
});
it('produces no warnings', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 0);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});

describe('with --timestamp/-t set', () => {
const config = { timestamp: true, t: true };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

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

describe('with --silent/-q set', () => {
const config = { silent: true, q: true };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

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

describe('with --sandbox/-b set', () => {
const config = { sandbox: true, b: true };
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

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

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

it('gets reformatted', () => {
const normalizedConfig = configuration._normalizeConfig(config);

assert.deepEqual(normalizedConfig.apiDescriptions, [
{
location: 'filename.api',
Expand All @@ -399,11 +387,9 @@ describe('configuration._coerceRemovedOptions()', () => {
]);
});
it('produces one warning', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 1);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});
Expand All @@ -417,9 +403,10 @@ describe('configuration._coerceRemovedOptions()', () => {
},
},
};
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets reformatted', () => {
const normalizedConfig = configuration._normalizeConfig(config);
assert.deepEqual(normalizedConfig.apiDescriptions, [
{
location: 'filename.api',
Expand All @@ -428,11 +415,9 @@ describe('configuration._coerceRemovedOptions()', () => {
]);
});
it('produces one warning', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 1);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});
Expand All @@ -445,10 +430,10 @@ describe('configuration._coerceRemovedOptions()', () => {
content: 'FORMAT: 1A\n# Sample API v2\n',
}],
};
const normalizedConfig = configuration._normalizeConfig(config);
const { warnings, errors } = configuration._validateConfig(config);

it('gets reformatted', () => {
const normalizedConfig = configuration._normalizeConfig(config);

assert.deepEqual(normalizedConfig.apiDescriptions, [
{
location: 'configuration.apiDescriptions[0]',
Expand All @@ -461,11 +446,9 @@ describe('configuration._coerceRemovedOptions()', () => {
]);
});
it('produces one warning', () => {
const { warnings } = configuration._validateConfig(config);
assert.lengthOf(warnings, 1);
});
it('produces no errors', () => {
const { errors } = configuration._validateConfig(config);
assert.lengthOf(errors, 0);
});
});
Expand Down

0 comments on commit 0561654

Please sign in to comment.