From e4da5b1bd5e6844e17634eff79bf5a21e422aaec Mon Sep 17 00:00:00 2001 From: Miller Medeiros Date: Mon, 23 Jun 2014 11:01:40 -0300 Subject: [PATCH] make sure esformatter.rc doesn't load config file if user provides a 'preset' or if 'root == true', also make sure it merges the customOptions --- lib/options.js | 6 ++++ test/api.spec.js | 75 ++++++++++++++++++++++++++++++++++++++++++++ test/plugins.spec.js | 33 ------------------- test/runner.js | 1 + 4 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 test/api.spec.js diff --git a/lib/options.js b/lib/options.js index 88bcbc1..cb8b0ae 100644 --- a/lib/options.js +++ b/lib/options.js @@ -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; diff --git a/test/api.spec.js b/test/api.spec.js new file mode 100644 index 0000000..a3727b4 --- /dev/null +++ b/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); + }); + }); +}); diff --git a/test/plugins.spec.js b/test/plugins.spec.js index 4f02f54..e4d1e80 100644 --- a/test/plugins.spec.js +++ b/test/plugins.spec.js @@ -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'); - }); - }); - }); diff --git a/test/runner.js b/test/runner.js index f6462bf..4db95db 100644 --- a/test/runner.js +++ b/test/runner.js @@ -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) {