From c9159f2a921847d790f5425269033e2e3dbd8b13 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sat, 6 Jun 2015 13:36:47 -0300 Subject: [PATCH] Closes #311. Show more informative error when beautifier not found --- spec/atom-beautify-spec.coffee | 80 +++++++++++++++++++++++-------- src/beautifiers/beautifier.coffee | 27 ++++++++++- src/beautify.coffee | 2 +- 3 files changed, 85 insertions(+), 24 deletions(-) diff --git a/spec/atom-beautify-spec.coffee b/spec/atom-beautify-spec.coffee index 80da94e..5ff3be7 100644 --- a/spec/atom-beautify-spec.coffee +++ b/spec/atom-beautify-spec.coffee @@ -1,29 +1,67 @@ -Beautify = require '../src/beautify' +Beautifiers = require "../src/beautifiers" +beautifiers = new Beautifiers() +Beautifier = require "../src/beautifiers/beautifier" # Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs. # # To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit` # or `fdescribe`). Remove the `f` to unfocus the block. -describe "AtomBeautify", -> - activationPromise = null +describe "Atom-Beautify", -> - beforeEach -> - atom.workspaceView = new WorkspaceView - activationPromise = atom.packages.activatePackage('atomBeautify') + beforeEach -> -# describe "when the beautify event is triggered", -> -# it "attaches and then detaches the view", -> -# expect(atom.workspaceView.find('.atom-beautify')).not.toExist() -# -# # This is an activation event, triggering it will cause the package to be -# # activated. -# atom.workspaceView.trigger 'beautify' -# -# waitsForPromise -> -# activationPromise -# -# runs -> -# expect(atom.workspaceView.find('.atom-beautify')).toExist() -# atom.workspaceView.trigger 'beautify' -# expect(atom.workspaceView.find('.atom-beautify')).not.toExist() + # Activate package + waitsForPromise -> + activationPromise = atom.packages.activatePackage('atom-beautify') + # Force activate package + pack = atom.packages.getLoadedPackage("atom-beautify") + pack.activateNow() + # Change logger level + # atom.config.set('atom-beautify._loggerLevel', 'verbose') + # Return promise + return activationPromise + + describe "Beautifiers", -> + + describe "Beautifier::run", -> + + beautifier = null + + beforeEach -> + beautifier = new Beautifier() + + it "should error when beautifier's program not found", -> + expect(beautifier).not.toBe(null) + expect(beautifier instanceof Beautifier).toBe(true) + + # waitsForRuns = (fn, message, timeout) -> + # isCompleted = false + # completed = -> + # console.log('completed') + # isCompleted = true + # runs -> + # console.log('runs') + # fn(completed) + # waitsFor(-> + # console.log('waitsFor', isCompleted) + # isCompleted + # , message, timeout) + # + # waitsForRuns((cb) -> + # console.log('waitsForRuns', cb) + # setTimeout(cb, 2000) + # , "Waiting for beautification to complete", 5000) + + waitsForPromise shouldReject: true, -> + p = beautifier.run("program", []) + expect(p).not.toBe(null) + expect(p instanceof beautifier.Promise).toBe(true) + cb = (v) -> + # console.log(v) + expect(v).not.toBe(null) + expect(v instanceof Error).toBe(true) + expect(v.code).toBe("CommandNotFound") + return v + p.then(cb, cb) + return p \ No newline at end of file diff --git a/src/beautifiers/beautifier.coffee b/src/beautifiers/beautifier.coffee index a10bc3b..a105aa3 100644 --- a/src/beautifiers/beautifier.coffee +++ b/src/beautifiers/beautifier.coffee @@ -135,7 +135,7 @@ module.exports = class Beautifier ### Run command-line interface command ### - run: (executable, args, {ignoreReturnCode} = {}) -> + run: (executable, args, {ignoreReturnCode, help} = {}) -> # Resolve executable Promise.resolve(executable) .then((exe) => @@ -173,7 +173,30 @@ module.exports = class Beautifier ) cmd.on('error', (err) => @debug('error', err) - reject(err) + # Check if error is ENOENT (command could not be found) + if err.code is 'ENOENT' or err.errno is 'ENOENT' + # Create new improved error + # notify user that it may not be installed or in path + message = "Could not find '#{exe}'. The program may not be installed." + er = new Error(message) # +(if help then "\n\n#{help}" else "") + if help? + if typeof help is "object" + helpStr = "See #{help.link} for program installation instructions.\n" + helpStr += "You can configure Atom Beautify with the absolute path \ + to '#{help.program or exe}' by setting '#{help.pathOption}' in \ + the Atom Beautify package settings.\n" if help.pathOption + helpStr += help.additional if help.additional + er.description = helpStr + else #if typeof help is "string" + er.description = help + er.code = 'CommandNotFound' + er.errno = er.code + er.syscall = 'beautifier::run' + er.file = exe + reject(er) + else + # continue as normal error + reject(err) ) ) ) diff --git a/src/beautify.coffee b/src/beautify.coffee index 1358cec..9d118e3 100644 --- a/src/beautify.coffee +++ b/src/beautify.coffee @@ -76,7 +76,7 @@ beautify = ({onSave}) -> # console.log(e) stack = error.stack - detail = error.message + detail = error.description or error.message atom.notifications?.addError(error.message, { stack, detail, dismissable : true})