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

Commit

Permalink
refactor: properly deprecate options
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Feb 5, 2019
1 parent bf5743a commit 8e11b58
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 58 deletions.
28 changes: 14 additions & 14 deletions lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,22 @@ ${packageData.name} v${packageData.version} \
}

run() {
for (const task of [
this.setOptimistArgv,
this.parseCustomConfig,
this.runExitingActions,
this.loadDreddFile,
this.checkRequiredArgs,
this.moveBlueprintArgToPath,
]) {
task.call(this);
if (this.finished) { return; }
}
try {
for (const task of [
this.setOptimistArgv,
this.parseCustomConfig,
this.runExitingActions,
this.loadDreddFile,
this.checkRequiredArgs,
this.moveBlueprintArgToPath,
]) {
task.call(this);
if (this.finished) { return; }
}

const configurationForDredd = this.initConfig();
this.logDebuggingInfo(configurationForDredd);
const configurationForDredd = this.initConfig();
this.logDebuggingInfo(configurationForDredd);

try {
this.dreddInstance = this.initDredd(configurationForDredd);
} catch (e) {
this.exitWithStatus(e);
Expand Down
126 changes: 82 additions & 44 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ const reporterOutputLogger = require('./reporters/reporterOutputLogger');


function coerceToArray(value) {
if (Array.isArray(value)) {
return value;
} if (typeof value === 'string') {
return [value];
} if ((value == null)) {
return [];
}
if (Array.isArray(value)) return value;
if (typeof value === 'string') return [value];
if (value == null) return [];
return value;
}


function coerceToBoolean(value) {
if (value === 'true') return true;
if (value === 'false') return true;
if (value) return true;
return false;
}


function applyLoggingOptions(options) {
if (options.color === false) {
logger.transports.console.colorize = false;
Expand All @@ -36,54 +40,77 @@ function applyLoggingOptions(options) {
} else if (['warn', 'error'].includes(loglevel)) {
logger.transports.console.level = loglevel;
} else {
throw new Error(`Unsupported logging level: '${loglevel}'`);
logger.transports.console.level = 'warn';
throw new Error(`The logging level '${loglevel}' is unsupported, `
+ 'supported are: silent, error, warning, debug');
}
} else {
logger.transports.console.level = 'warn';
}
}


function errorOnRemovedOptions(config = {}) {
function coerceRemovedOptions(config = {}) {
const coercedConfig = clone(config);

const errors = [];
const warnings = [];

if (config.options) {
if (config.options.c) {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ '-c anymore, use --color/--no-color instead');
coercedConfig.options.color = coerceToBoolean(config.options.c);
}
if (typeof config.options.color === 'string') {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ `--color=${config.options.color} anymore, use --color/--no-color instead`);
coercedConfig.options.color = coerceToBoolean(config.options.color);
}
if (config.options.level) {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ '--level anymore, use --loglevel instead');
if (['silly', 'debug', 'verbose'].includes(config.options.level)) {
coercedConfig.options.loglevel = 'debug';
} else if (config.options.level === 'error') {
coercedConfig.options.loglevel = 'error';
} else {
coercedConfig.options.loglevel = 'warn';
}
}
if (config.options.timestamp || config.options.t) {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ '--timestamp anymore, use --loglevel=debug instead');
coercedConfig.options.loglevel = 'debug';
}
if (config.options.silent || config.options.q) {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ '-q/--silent anymore, use --loglevel=silent instead');
coercedConfig.options.loglevel = 'silent';
}
if (config.options.sandbox || config.options.b) {
throw new Error('Dredd does not support '
errors.push('REMOVED: Dredd does not support '
+ 'sandboxed JS hooks anymore, use standard JS hooks instead');
}
}
if (config.hooksData) {
throw new Error('Dredd does not support '
errors.push('REMOVED: Dredd does not support '
+ 'sandboxed JS hooks anymore, use standard JS hooks instead');
}
if (config.blueprintPath) {
throw new Error('Dredd does not support '
warnings.push('DEPRECATED: Dredd does not support '
+ "the 'blueprintPath' option anymore, use 'path' instead");
coercedConfig.options = coercedConfig.options || {};
coercedConfig.options.path = coercedConfig.path || [];
coercedConfig.options.path.push(config.blueprintPath);
}

return { config: coercedConfig, errors, warnings };
}


function applyConfiguration(config) {
const configuration = {
function applyConfiguration(inConfig) {
const outConfig = {
server: null,
emitter: new EventEmitter(),
custom: { // Used for custom settings of various APIs or reporters
Expand All @@ -94,7 +121,6 @@ function applyConfiguration(config) {
'dry-run': false,
reporter: null,
output: null,
debug: false,
header: null,
user: null,
'inline-errors': false,
Expand All @@ -118,41 +144,53 @@ function applyConfiguration(config) {
},
};

// Normalize options and config
for (const key of Object.keys(config || {})) {
// Copy anything except "custom" hash
const value = config[key];
// Gracefully deal with the removed options
const coerceResult = coerceRemovedOptions(inConfig);
inConfig = coerceResult.config;

// Normalize
Object.keys(inConfig).forEach((key) => {
// Copy anything except the "custom" object
const value = inConfig[key];
if (key !== 'custom') {
configuration[key] = value;
outConfig[key] = value;
} else {
if (!configuration.custom) { configuration.custom = {}; }
const object = config.custom || {};
if (!outConfig.custom) { outConfig.custom = {}; }
const object = inConfig.custom || {};
for (const customKey of Object.keys(object || {})) {
const customVal = object[customKey];
configuration.custom[customKey] = clone(customVal, false);
outConfig.custom[customKey] = clone(customVal, false);
}
}
}
});

// Coerce single/multiple options into an array
configuration.options.reporter = coerceToArray(configuration.options.reporter);
configuration.options.output = coerceToArray(configuration.options.output);
configuration.options.header = coerceToArray(configuration.options.header);
configuration.options.method = coerceToArray(configuration.options.method);
configuration.options.only = coerceToArray(configuration.options.only);
configuration.options.path = coerceToArray(configuration.options.path);

configuration.options.method = configuration.options.method.map(method => method.toUpperCase());

if (configuration.options.user) {
const authHeader = `Authorization:Basic ${Buffer.from(configuration.options.user).toString('base64')}`;
configuration.options.header.push(authHeader);
outConfig.options.reporter = coerceToArray(outConfig.options.reporter);
outConfig.options.output = coerceToArray(outConfig.options.output);
outConfig.options.header = coerceToArray(outConfig.options.header);
outConfig.options.method = coerceToArray(outConfig.options.method);
outConfig.options.only = coerceToArray(outConfig.options.only);
outConfig.options.path = coerceToArray(outConfig.options.path);

outConfig.options.method = outConfig.options.method.map(method => method.toUpperCase());

if (outConfig.options.user) {
const authHeader = `Authorization:Basic ${Buffer.from(outConfig.options.user).toString('base64')}`;
outConfig.options.header.push(authHeader);
}

applyLoggingOptions(configuration.options);
errorOnRemovedOptions(config);
applyLoggingOptions(outConfig.options);

// Log accumulated errors and warnings
coerceResult.errors.forEach(message => logger.error(message));
coerceResult.warnings.forEach(message => logger.warn(message));

// Abort Dredd if there has been any errors
if (coerceResult.errors.length) {
throw new Error('Could not configure Dredd');
}

return configuration;
return outConfig;
}


Expand Down

0 comments on commit 8e11b58

Please sign in to comment.