From e3c617b6f4a8ac60bac0c210aff89016b54c8d5f Mon Sep 17 00:00:00 2001 From: Claudia Date: Fri, 12 Dec 2014 18:49:43 +0100 Subject: [PATCH] When handling errors or missing methods, the CLI now suggests updating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This helps cut trivial issues that would be easily fixable by doing `brew update && …`. --- lib/cask/cli.rb | 5 +---- lib/cask/utils.rb | 35 +++++++++++++++++++++----------- test/cask/dsl_test.rb | 46 +++++++++++++++++++++++++++---------------- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index adbdcf3f4095..c781f8c544ff 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -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', @@ -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 diff --git a/lib/cask/utils.rb b/lib/cask/utils.rb index d549ec7a11f8..8494cf6d6a25 100644 --- a/lib/cask/utils.rb +++ b/lib/cask/utils.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 # see Homebrew Library/Homebrew/utils.rb @@ -5,6 +6,9 @@ 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 @@ -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 diff --git a/test/cask/dsl_test.rb b/test/cask/dsl_test.rb index 643ebb26b6e4..34633ecf786c 100644 --- a/test/cask/dsl_test.rb +++ b/test/cask/dsl_test.rb @@ -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