Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest brew update && … when handling errors or missing methods #8072

Merged
merged 1 commit into from
Dec 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 #{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only question is what this empty #{} does

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve been taught that lines longer than 80 chars are bad.
Sometimes this makes me feel so guilty that I even try to follow this rule in herestrings. 😊

Am I free to ignore this rule in herestrings? Or maybe you happen to know a less confusing way to soft-wrap long lines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it is a clever trick.

You are free to do as you please, in heredocs and elsewhere. We can adopt a style re: wrapping across the codebase if it benefits you. I tend to let code run wide, which attracts criticism.

}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