Skip to content

Commit

Permalink
Be more user-friendly when unable to find a command
Browse files Browse the repository at this point in the history
* do not blow up with a stack trace if xprop is not found
* output how to debug and file a bug report when a command is not found

Fixes Issue #42
  • Loading branch information
copiousfreetime committed Mar 18, 2012
1 parent 3e0b34f commit f7da658
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/launchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def dry_run?
end

def bug_report_message
"Please file a bug at https://github.com/copiousfreetime/launchy/issues/new"
"Please rerun with environment variable LAUNCHY_DEBUG=true or the '-d' commandline option and file a bug at https://github.com/copiousfreetime/launchy/issues/new"
end

def log(msg)
Expand Down
8 changes: 5 additions & 3 deletions lib/launchy/applications/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def browser_cmdline
possibilities.each do |p|
Launchy.log "#{self.class.name} : possibility : #{p}"
end
browser = possibilities.shift
Launchy.log "#{self.class.name} : Using browser value '#{browser}'"
return browser
if browser = possibilities.shift then
Launchy.log "#{self.class.name} : Using browser value '#{browser}'"
return browser
end
raise Launchy::CommandNotFoundError, "Unable to find a browser command. If this is unexpected, #{Launchy.bug_report_message}"
end

def cmd_and_args( uri, options = {} )
Expand Down
2 changes: 1 addition & 1 deletion lib/launchy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Cli
def initialize
@options = {}
end

def parser
@parser ||= OptionParser.new do |op|
op.banner = "Usage: launchy [options] thing-to-launch"
Expand Down
8 changes: 6 additions & 2 deletions lib/launchy/detect/nix_desktop_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NotFoundError < Launchy::Error; end
# NixDekstopEnvironment::Unknown
def self.detect
found = find_child( :is_current_desktop_environment? )
Launchy.log("Current Desktop environment not flound. #{Launchy.bug_report_message}") unless found
Launchy.log("Current Desktop environment not found. #{Launchy.bug_report_message}") unless found
return found
end

Expand Down Expand Up @@ -48,7 +48,11 @@ def self.browser

class Xfce < NixDesktopEnvironment
def self.is_current_desktop_environment?
%x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0
if Launchy::Application.find_executable( 'xprop' ) then
%x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0
else
false
end
end

def self.browser
Expand Down
3 changes: 2 additions & 1 deletion lib/launchy/detect/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.detect
require 'spoon'
return Jruby.new
end
return Forkable.new
return Forkable.new
end

#
Expand All @@ -55,6 +55,7 @@ def dry_run( cmd, *args )
end

def run( cmd, *args )
raise Launchy::CommandNotFoundError, "No command found to run with args '#{args.join(' ')}'. If this is unexpected, #{Launchy.bug_report_message}" unless cmd
if Launchy.dry_run? then
$stdout.puts dry_run( cmd, *args )
else
Expand Down
1 change: 1 addition & 0 deletions lib/launchy/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Launchy
class Error < ::StandardError; end
class ApplicationNotFoundError < Error; end
class CommandNotFoundError < Error; end
end
4 changes: 4 additions & 0 deletions spec/detect/nix_desktop_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
end
end

it "returns false for XFCE if xprop is not found" do
Launchy.host_os = "linux"
Launchy::Detect::NixDesktopEnvironment::Xfce.is_current_desktop_environment?.must_equal( false )
end

it "returns nil if it cannot determine the *nix desktop environment" do
Launchy.host_os = "linux"
Expand Down
5 changes: 5 additions & 0 deletions spec/detect/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
lambda{ Launchy::Detect::Runner.detect }.must_raise Launchy::Detect::RubyEngine::NotFoundError
end

it "raises and error when there is no command found" do
runner = Launchy::Detect::Runner.detect
lambda{ runner.run( nil, *%w[ arg1 arg2 arg 3] ) }.must_raise Launchy::CommandNotFoundError
end

# On anything that has fork, use Forkable
%w[ linux darwin cygwin ].each do |host_os|
%w[ ruby rbx macruby ].each do |engine_name|
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gem 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
require 'launchy'
require 'stringio'
require 'minitest/autorun'
require 'minitest/pride'

0 comments on commit f7da658

Please sign in to comment.