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

Commit

Permalink
Configuration: tests, overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Oct 26, 2014
1 parent 0a09896 commit 0d6d62f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ module.exports = function(program) {
}

if (program.preset) {
config.preset = program.preset;
checker.getConfiguration().override({preset: program.preset});
}

if (program.maxErrors) {
config.maxErrors = Number(program.maxErrors);
checker.getConfiguration().override({maxErrors: Number(program.maxErrors)});
}

if (program.reporter) {
Expand Down
48 changes: 42 additions & 6 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var BUILTIN_OPTIONS = {
excludeFiles: true,
additionalRules: true,
fileExtensions: true,
maxErrors: true
maxErrors: true,
configPath: true
};

/**
Expand All @@ -27,6 +28,8 @@ function Configuration() {
this._ruleSettings = {};
this._maxErrors = null;
this._basePath = '.';
this._overrides = {};
this._presetName = null;
}

/**
Expand All @@ -35,7 +38,18 @@ function Configuration() {
* @param {Object} config
*/
Configuration.prototype.load = function(config) {
var ruleSettings = this._processConfig(config);
var overrides = this._overrides;
var currentConfig = {};
Object.keys(config).forEach(function(key) {
currentConfig[key] = config[key];
});
if (overrides) {
Object.keys(overrides).forEach(function(key) {
currentConfig[key] = overrides[key];
});
}

var ruleSettings = this._processConfig(currentConfig);
var unsupportedRules = [];
Object.keys(ruleSettings).forEach(function(optionName) {
var rule = this._rules[optionName];
Expand Down Expand Up @@ -63,6 +77,7 @@ Configuration.prototype.getProcessedConfig = function() {
result.excludeFiles = this._excludedFileMasks;
result.fileExtensions = this._fileExtensions;
result.maxErrors = this._maxErrors;
result.preset = this._presetName;
return result;
};

Expand Down Expand Up @@ -124,6 +139,17 @@ Configuration.prototype.getBasePath = function() {
return this._basePath;
};

/**
* Overrides specified settings.
*
* @param {String} overrides
*/
Configuration.prototype.override = function(overrides) {
Object.keys(overrides).forEach(function(key) {
this._overrides[key] = overrides[key];
}, this);
};

/**
* Processes configuration and returns config options.
*
Expand All @@ -140,8 +166,9 @@ Configuration.prototype._processConfig = function(config) {
}

// Apply presets
if (config.preset) {
var presetName = config.preset;
var presetName = config.preset;
if (presetName) {
this._presetName = presetName;
assert(typeof presetName === 'string', '`preset` option requires string value');
var presetData = this._presets[presetName];
assert(Boolean(presetData), 'Preset "' + presetName + '" does not exist');
Expand All @@ -167,7 +194,7 @@ Configuration.prototype._processConfig = function(config) {
// Base path
if (config.configPath) {
assert(
typeof config.configPath === 'string',
typeof config.configPath === 'string',
'`configPath` option requires string value'
);
this._basePath = path.dirname(config.configPath);
Expand All @@ -187,7 +214,7 @@ Configuration.prototype._processConfig = function(config) {
// Additional rules
if (config.additionalRules) {
assert(Array.isArray(config.additionalRules), '`additionalRules` option requires array value');
config.additionalRules.plugins.forEach(this._loadAdditionalRule, this);
config.additionalRules.forEach(this._loadAdditionalRule, this);
}

if (config.hasOwnProperty('maxErrors')) {
Expand Down Expand Up @@ -219,6 +246,15 @@ Configuration.prototype._loadPlugin = function(plugin) {
plugin(this);
};

/**
* Includes plugin in the configuration environment.
*
* @param {function(Configuration)|*} plugin
*/
Configuration.prototype.usePlugin = function(plugin) {
this._loadPlugin(plugin);
};

/**
* Loads additional rule.
*
Expand Down
103 changes: 103 additions & 0 deletions test/config/configuration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var assert = require('assert');
var sinon = require('sinon');
var Configuration = require('../../lib/config/configuration');

describe('modules/config/configuration', function() {
Expand Down Expand Up @@ -149,4 +150,106 @@ describe('modules/config/configuration', function() {
assert(configuration.getConfiguredRules()[0] === rule);
});
});

describe('isFileExcluded', function() {
it('should return `false` if no `excludeFiles` are defined', function() {
assert(!configuration.isFileExcluded('1.js'));
assert(!configuration.isFileExcluded(''));
assert(!configuration.isFileExcluded('*'));
});

it('should return `true` for excluded file', function() {
configuration.load({excludeFiles: ['1.js', 'app/1.js']});
assert(configuration.isFileExcluded('1.js'));
assert(configuration.isFileExcluded('app/1.js'));
assert(!configuration.isFileExcluded('share/1.js'));
assert(!configuration.isFileExcluded('2.js'));
assert(!configuration.isFileExcluded(''));
});

it('should return resolve given path', function() {
configuration.load({excludeFiles: ['app/1.js']});
assert(configuration.isFileExcluded('app/lib/../1.js'));
});
});

describe('usePlugin', function() {
it('should run plugin with configuration specified', function() {
var plugin = function() {};
var spy = sinon.spy(plugin);
configuration.usePlugin(spy);
assert(spy.called);
assert(spy.callCount === 1);
assert(spy.getCall(0).args[0] === configuration);
});
});

describe('override', function() {
it('should override `preset` setting', function() {
configuration.registerPreset('1', {});
configuration.registerPreset('2', {});
configuration.override({preset: '2'});
configuration.load({preset: '1'});
assert(configuration.getProcessedConfig().preset === '2');
});

it('should override `maxErrors` setting', function() {
configuration.override({maxErrors: 2});
configuration.load({maxErrors: 1});
assert(configuration.getProcessedConfig().maxErrors === 2);
});
});

describe('load', function() {
it('should load specified preset', function() {
configuration.registerPreset('preset', {maxErrors: 1});
configuration.load({preset: 'preset'});
assert(configuration.getProcessedConfig().preset === 'preset');
assert(configuration.getProcessedConfig().maxErrors === 1);
});

it('should accept `maxErrors`', function() {
configuration.load({maxErrors: 1});
assert(configuration.getMaxErrors() === 1);
});

it('should accept `excludeFiles`', function() {
configuration.load({excludeFiles: ['**']});
assert(configuration.getExcludedFileMasks().length === 1);
assert(configuration.getExcludedFileMasks()[0] === '**');
});

it('should accept `fileExtensions`', function() {
configuration.load({fileExtensions: ['.jsx']});
assert(configuration.getFileExtensions().length === 1);
assert(configuration.getFileExtensions()[0] === '.jsx');
});

it('should accept `additionalRules`', function() {
var rule = {
getOptionName: function() {
return 'ruleName';
},
configure: function() {}
};
configuration.load({additionalRules: [rule]});
assert(configuration.getRegisteredRules());
assert(configuration.getRegisteredRules().length === 1);
assert(configuration.getRegisteredRules()[0] === rule);
});

it('should accept `configPath`', function() {
configuration.load({configPath: 'app/1.js'});
assert(configuration.getBasePath() === 'app');
});

it('should accept `plugins`', function() {
var plugin = function() {};
var spy = sinon.spy(plugin);
configuration.load({plugins: [spy]});
assert(spy.called);
assert(spy.callCount === 1);
assert(spy.getCall(0).args[0] === configuration);
});
});
});

0 comments on commit 0d6d62f

Please sign in to comment.