diff --git a/spec/atom-beautify-spec.coffee b/spec/atom-beautify-spec.coffee index 9ca2aee..f006084 100644 --- a/spec/atom-beautify-spec.coffee +++ b/spec/atom-beautify-spec.coffee @@ -3,6 +3,11 @@ beautifiers = new Beautifiers() Beautifier = require "../src/beautifiers/beautifier" Languages = require('../src/languages/') _ = require('lodash') +fs = require('fs') +path = require('path') +Promise = require("bluebird") +temp = require('temp') +temp.track() # Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs. # @@ -29,6 +34,9 @@ describe "Atom-Beautify", -> # Return promise return activationPromise + afterEach -> + temp.cleanupSync() + describe "Beautifiers", -> beautifier = null @@ -263,6 +271,76 @@ describe "Atom-Beautify", -> describe ".jsbeautifyrc", -> + it "should look at directories above file", -> + isDone = false + cb = (err) -> + isDone = true + expect(err).toBe(undefined) + runs -> + try + # console.log('runs') + # Make top directory + temp.mkdir('dir1', (err, dirPath) -> + # console.log(arguments) + return cb(err) if err + # Add .jsbeautifyrc file + rcPath = path.join(dirPath, '.jsbeautifyrc') + myData1 = { + indent_size: 1, + indent_char: '\t' + } + myData = JSON.stringify(myData1) + fs.writeFile(rcPath, myData, (err) -> + # console.log(arguments) + return cb(err) if err + # Make next directory + dirPath = path.join(dirPath, 'dir2') + fs.mkdir(dirPath, (err) -> + # console.log(arguments) + return cb(err) if err + # Add .jsbeautifyrc file + rcPath = path.join(dirPath, '.jsbeautifyrc') + myData2 = { + indent_size: 2, + indent_char: ' ' + } + myData = JSON.stringify(myData2) + fs.writeFile(rcPath, myData, (err) -> + # console.log(arguments) + return cb(err) if err + Promise.all(beautifier.getOptionsForPath(rcPath, null)) + .then((allOptions) -> + # console.log('allOptions', allOptions) + + # Extract options + [ + editorOptions + configOptions + homeOptions + editorConfigOptions + ] = allOptions + projectOptions = allOptions[4..] + + # Check that we extracted .jsbeautifyrc files + [config1, config2] = projectOptions[-2..] + + expect(_.get(config1,'_default.indent_size')).toBe(myData1.indent_size) + expect(_.get(config2,'_default.indent_size')).toBe(myData2.indent_size) + expect(_.get(config1,'_default.indent_char')).toBe(myData1.indent_char) + expect(_.get(config2,'_default.indent_char')).toBe(myData2.indent_char) + + cb() + ) + ) + ) + ) + ) + catch err + cb(err) + waitsFor -> + isDone + + describe "Package settings", -> getOptions = (callback) -> diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 81ee292..c88f4b5 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -414,7 +414,7 @@ module.exports = class Beautifiers extends EventEmitter # Look for .jsbeautifierrc in file and home path, check env variables getConfig : (startPath, upwards = true) -> - + # console.log('getConfig', startPath, upwards) # Verify that startPath is a string startPath = ( if ( typeof startPath is "string") then startPath else "") return {} unless startPath @@ -422,6 +422,7 @@ module.exports = class Beautifiers extends EventEmitter # Get the path to the config file configPath = @findConfig(".jsbeautifyrc", startPath, upwards) + logger.verbose('configPath', configPath, startPath, upwards) externalOptions = undefined if configPath fs ?= require("fs") @@ -449,7 +450,8 @@ module.exports = class Beautifiers extends EventEmitter externalOptions = {} else externalOptions = {} - externalOptions + return externalOptions + getOptionsForPath : (editedFilePath, editor) -> languageNamespaces = @languages.namespaces