Skip to content

Commit

Permalink
Merge pull request #8072 from claui/on-error-suggest-update
Browse files Browse the repository at this point in the history
Suggest `brew update && …` when handling errors or missing methods
  • Loading branch information
rolandwalker committed Dec 12, 2014
2 parents d112167 + e3c617b commit 488df3b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
5 changes: 1 addition & 4 deletions lib/cask/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class Cask::CLI; end

class Cask::CLI

ISSUES_URL = "https://github.com/caskroom/homebrew-cask/issues"

ALIASES = {
'ls' => 'list',
'homepage' => 'home',
Expand Down Expand Up @@ -125,8 +123,7 @@ def self.process(arguments)
exit 1
rescue StandardError, ScriptError, NoMemoryError => e
onoe e
puts "#{Tty.white}Please report this bug:"
puts " #{Tty.em}#{ISSUES_URL}#{Tty.reset}"
puts Cask::Utils.error_message_with_suggestions
puts e.backtrace
exit 1
end
Expand Down
35 changes: 23 additions & 12 deletions lib/cask/utils.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# encoding: UTF-8

# see Homebrew Library/Homebrew/utils.rb

require 'yaml'
require 'open3'
require 'stringio'

UPDATE_CMD = "brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup"
ISSUES_URL = "https://github.com/caskroom/homebrew-cask/issues"

# monkeypatch Object - not a great idea
class Object
def utf8_inspect
Expand Down Expand Up @@ -161,20 +165,27 @@ def self.file_is_descendant(file, dir)
return false
end

def self.method_missing_message(method, token, section=nil)
during = section ? "during #{section} " : '';
poo = <<-EOPOO.undent
Unexpected method '#{method}' called #{during}on Cask #{token}.
def self.error_message_with_suggestions
<<-EOS.undent
#{ Tty.reset }
#{ Tty.white }Most likely, this means you have #{
}an outdated version of homebrew-cask. Please run:
#{ Tty.reset }#{ Tty.green }#{ UPDATE_CMD }#{ Tty.reset }
#{ Tty.white }If this doesn’t fix the problem, please report this bug:
If you are working on #{token}, this may point to a typo. Otherwise
it probably means this Cask is using a new feature. If that feature
has been released, running
#{ Tty.em }#{ Tty.white }#{ ISSUES_URL }#{ Tty.reset }
brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup
EOS
end

def self.method_missing_message(method, token, section=nil)
poo = []
poo << "Unexpected method '#{method}' called"
poo << "during #{section}" if section
poo << "on Cask #{token}."

should fix it. Otherwise you should wait to use #{token} until the
new feature is released.
EOPOO
poo.split("\n").each { |line| opoo line }
opoo(poo.join(' ') + "\n" + error_message_with_suggestions)
end
end
46 changes: 29 additions & 17 deletions test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@
test_cask.version.must_equal '1.2.3'
end

it "prevents the entire world from crashing when a Cask includes an unknown method" do
describe "when a Cask includes an unknown method" do
UnexpectedMethodCask = Class.new(Cask)
begin
TestHelper.must_output(self, lambda {
attempt_unknown_method = nil

before do
attempt_unknown_method = lambda {
UnexpectedMethodCask.class_eval do
future_feature :not_yet_on_your_machine
end
}, <<-WARNING.undent.chomp)
Warning: Unexpected method 'future_feature' called on Cask unexpected-method-cask.
Warning:#{' '}
Warning: If you are working on unexpected-method-cask, this may point to a typo. Otherwise
Warning: it probably means this Cask is using a new feature. If that feature
Warning: has been released, running
Warning:#{' '}
Warning: brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup
Warning:#{' '}
Warning: should fix it. Otherwise you should wait to use unexpected-method-cask until the
Warning: new feature is released.
WARNING
rescue Exception => e
flunk("Wanted unexpected method to simply warn, but got exception #{e}")
}
end

it "prints a warning that it has encountered an unexpected method" do
expected = r = Regexp.compile(<<-EOREGEX.undent.lines.map(&:chomp).join(''))
(?m)
Warning:
.*
Unexpected method 'future_feature' called on Cask unexpected-method-cask\\.
.*
brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup
.*
https://github.com/caskroom/homebrew-cask/issues
EOREGEX

TestHelper.must_output(self, attempt_unknown_method, expected)
end

it "will simply warn, not throw an exception" do
begin
capture_subprocess_io { attempt_unknown_method.call }
rescue Exception => e
flunk("Wanted unexpected method to simply warn, but got exception #{e}")
end
end
end

Expand Down

0 comments on commit 488df3b

Please sign in to comment.