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

Commit

Permalink
Configuration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Oct 28, 2014
1 parent 0d6d62f commit 2e9fafa
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 14 deletions.
16 changes: 7 additions & 9 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ Configuration.prototype.load = function(config) {
Object.keys(config).forEach(function(key) {
currentConfig[key] = config[key];
});
if (overrides) {
Object.keys(overrides).forEach(function(key) {
currentConfig[key] = overrides[key];
});
}
Object.keys(overrides).forEach(function(key) {
currentConfig[key] = overrides[key];
});

var ruleSettings = this._processConfig(currentConfig);
var processedSettings = {};
var unsupportedRules = [];
Object.keys(ruleSettings).forEach(function(optionName) {
var rule = this._rules[optionName];
Expand All @@ -58,6 +57,7 @@ Configuration.prototype.load = function(config) {
if (optionValue !== null) {
rule.configure(ruleSettings[optionName]);
this._configuredRules.push(rule);
processedSettings[optionName] = ruleSettings[optionName];
}
} else {
unsupportedRules.push(optionName);
Expand All @@ -66,7 +66,7 @@ Configuration.prototype.load = function(config) {
if (unsupportedRules.length > 0) {
throw new Error('Unsupported rules: ' + unsupportedRules.join(', '));
}
this._ruleSettings = ruleSettings;
this._ruleSettings = processedSettings;
};

Configuration.prototype.getProcessedConfig = function() {
Expand Down Expand Up @@ -174,9 +174,7 @@ Configuration.prototype._processConfig = function(config) {
assert(Boolean(presetData), 'Preset "' + presetName + '" does not exist');
var presetResult = this._processConfig(presetData);
Object.keys(presetResult).forEach(function(key) {
if (!BUILTIN_OPTIONS[key]) {
ruleSettings[key] = presetResult[key];
}
ruleSettings[key] = presetResult[key];
});
}

Expand Down
99 changes: 94 additions & 5 deletions test/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,130 @@ describe('modules/config/configuration', function() {
});

describe('load', function() {
it('should load specified preset', function() {
it('should configure rules', function() {
var rule = {
getOptionName: function() {
return 'ruleName';
},
configure: function() {}
};
var configureSpy = sinon.spy(rule, 'configure');
configuration.registerRule(rule);
configuration.load({ruleName: true});
assert(configuration.getProcessedConfig().ruleName === true);
assert(configureSpy.callCount === 1);
assert(configureSpy.getCall(0).args.length === 1);
assert(configureSpy.getCall(0).args[0] === true);
});

it('should throw error on unsupported rule', function() {
try {
configuration.load({ruleName: true});
assert(false);
} catch (e) {
assert.equal(e.message, 'Unsupported rules: ruleName');
}
});

it('should throw error on list of unsupported rules', function() {
try {
configuration.load({ruleName1: true, ruleName2: true});
assert(false);
} catch (e) {
assert.equal(e.message, 'Unsupported rules: ruleName1, ruleName2');
}
});

it('should not configure rule on null', function() {
var rule = {
getOptionName: function() {
return 'ruleName';
},
configure: function() {}
};
var configureSpy = sinon.spy(rule, 'configure');
configuration.registerRule(rule);
configuration.load({ruleName: null});
assert(!configuration.getProcessedConfig().hasOwnProperty('ruleName'));
assert(configureSpy.callCount === 0);
});

it('should load `preset` options', 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() {
it('should load `preset` rule settings', function() {
var rule = {
getOptionName: function() {
return 'ruleName';
},
configure: function() {}
};
configuration.registerRule(rule);
configuration.registerPreset('preset', {ruleName: true});
configuration.load({preset: 'preset'});
assert(configuration.getProcessedConfig().preset === 'preset');
assert(configuration.getProcessedConfig().ruleName === true);
});

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

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

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

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

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

it('should accept `additionalRules` option', function() {
configuration.load({additionalRules: []});
});

it('should accept `additionalRules` to register rules', 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);
assert(configuration.getConfiguredRules().length === 0);
});

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

it('should accept `configPath`', function() {
Expand Down

0 comments on commit 2e9fafa

Please sign in to comment.