Skip to content

Commit

Permalink
make sure esformatter.rc doesn't load config file if user provides a …
Browse files Browse the repository at this point in the history
…'preset' or if 'root == true', also make sure it merges the customOptions
  • Loading branch information
millermedeiros committed Jun 23, 2014
1 parent 1bdb1ee commit e4da5b1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
6 changes: 6 additions & 0 deletions lib/options.js
Expand Up @@ -57,6 +57,12 @@ exports.get = function(prop) {

exports.getRc = getRc;
function getRc(filePath, customOptions) {
// if user sets the "preset" we don't load any other config file
// we assume the "preset" overrides any user settings
if (customOptions && (customOptions.preset || customOptions.root)) {
return customOptions;
}

if (isObject(filePath)) {
customOptions = filePath;
filePath = null;
Expand Down
75 changes: 75 additions & 0 deletions test/api.spec.js
@@ -0,0 +1,75 @@
//jshint node:true
/*global describe, it*/
"use strict";

var expect = require('chai').expect;
var esformatter = require('../lib/esformatter');


describe('API', function() {

describe('exposed API', function() {
// plugins might need to access some internal methods from esformatter
// so we check if these methods are really available
var indent = require('../lib/indent');
var limit = require('../lib/limit');
var options = require('../lib/options');
var br = require('../lib/lineBreak');
var ws = require('../lib/whiteSpace');

it('shoud expose useful methods to plugin authors', function() {
// we don't need to check implementation here since these methods are
// used internally
expect(indent.indentInBetween).to.be.a('function');
expect(indent.indentBefore).to.be.a('function');
expect(limit.before).to.be.a('function');
expect(limit.after).to.be.a('function');
expect(limit.around).to.be.a('function');
expect(br.limit).to.be.a('function');
expect(br.limitBefore).to.be.a('function');
expect(br.limitAfter).to.be.a('function');
expect(ws.limit).to.be.a('function');
expect(ws.limitBefore).to.be.a('function');
expect(ws.limitAfter).to.be.a('function');
expect(options.set).to.be.a('function');
expect(options.get).to.be.a('function');
expect(options.getRc).to.be.a('function');
expect(options.loadAndParseConfig).to.be.a('function');
expect(esformatter.rc).to.be.a('function');
});
});

describe('esformatter.rc', function() {
// PS: CLI is already testing this method indirectly, we are only checking
// for edge cases for now

it('should return custom options if root == true', function() {
expect(
esformatter.rc(null, {root:true})
).to.be.eql({root:true});
});

it('should return custom options if preset', function() {
expect(
esformatter.rc(null, {preset:'default'})
).to.be.eql({preset:'default'});
});

it('should merge the custom option', function() {
var customOpts = {
whiteSpace: {
before: {
"ArrayExpressionClosing" : 1
},
after: {
"ArrayExpressionOpening" : 1
}
}
};
var result = esformatter.rc('test/compare/rc/top-in.js', customOpts);
expect(result.whiteSpace.before.FunctionDeclarationOpeningBrace).to.be.eql(0);
expect(result.whiteSpace.before.ArrayExpressionClosing).to.be.eql(1);
expect(result.whiteSpace.after.ArrayExpressionOpening).to.be.eql(1);
});
});
});
33 changes: 0 additions & 33 deletions test/plugins.spec.js
Expand Up @@ -102,39 +102,6 @@ describe('plugin API', function() {
});

});


describe('exposed API', function() {
// plugins might need to access some internal methods from esformatter
// so we check if these methods are really available
var indent = require('../lib/indent');
var limit = require('../lib/limit');
var options = require('../lib/options');
var br = require('../lib/lineBreak');
var ws = require('../lib/whiteSpace');

it('shoud expose useful methods to plugin authors', function() {
// we don't need to check implementation here since these methods are
// used internally
expect(indent.indentInBetween).to.be.a('function');
expect(indent.indentBefore).to.be.a('function');
expect(limit.before).to.be.a('function');
expect(limit.after).to.be.a('function');
expect(limit.around).to.be.a('function');
expect(br.limit).to.be.a('function');
expect(br.limitBefore).to.be.a('function');
expect(br.limitAfter).to.be.a('function');
expect(ws.limit).to.be.a('function');
expect(ws.limitBefore).to.be.a('function');
expect(ws.limitAfter).to.be.a('function');
expect(options.set).to.be.a('function');
expect(options.get).to.be.a('function');
expect(options.getRc).to.be.a('function');
expect(options.loadAndParseConfig).to.be.a('function');
expect(esformatter.rc).to.be.a('function');
});
});

});


Expand Down
1 change: 1 addition & 0 deletions test/runner.js
Expand Up @@ -34,6 +34,7 @@ if (process.env.INVERT) {
m.addFile('test/format.spec.js');
m.addFile('test/transform.spec.js');
m.addFile('test/cli.spec.js');
m.addFile('test/api.spec.js');
m.addFile('test/plugins.spec.js');

m.run(function(err) {
Expand Down

0 comments on commit e4da5b1

Please sign in to comment.