Skip to content
Snippets Groups Projects
Commit e6b45842 authored by Markus Reiter's avatar Markus Reiter
Browse files

Refactor `odeprecated`.

parent cb918731
No related branches found
No related tags found
No related merge requests found
......@@ -135,6 +135,7 @@ rescue MethodDeprecatedError => e
$stderr.puts "If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):"
$stderr.puts " #{Formatter.url(e.issues_url)}"
end
$stderr.puts e.backtrace if ARGV.debug?
exit 1
rescue Exception => e # rubocop:disable Lint/RescueException
onoe e
......
......@@ -154,7 +154,7 @@ module Hbc
Tap.default_cask_tap.install unless Tap.default_cask_tap.installed?
self.class.run_command(command, *args)
rescue CaskError, ArgumentError, OptionParser::InvalidOption => e
rescue CaskError, MethodDeprecatedError, ArgumentError, OptionParser::InvalidOption => e
msg = e.message
msg << e.backtrace.join("\n").prepend("\n") if ARGV.debug?
onoe msg
......
......@@ -253,7 +253,7 @@ describe "globally-scoped helper methods" do
end
describe "#odeprecated" do
it "raises a MethodDeprecatedError" do
it "raises a MethodDeprecatedError when `disable` is true" do
ENV.delete("HOMEBREW_DEVELOPER")
expect {
odeprecated(
......@@ -261,7 +261,7 @@ describe "globally-scoped helper methods" do
caller: ["#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core/"],
disable: true
)
}.to raise_error(MethodDeprecatedError, %r{method.*replacement.*homebrew/homebrew-core.*homebrew/core}m)
}.to raise_error(MethodDeprecatedError, %r{method.*replacement.*homebrew/core.*\/Taps\/homebrew\/homebrew-core\/}m)
end
end
......
......@@ -11,6 +11,7 @@ require "utils/link"
require "utils/popen"
require "utils/svn"
require "utils/tty"
require "tap_constants"
require "time"
def require?(path)
......@@ -86,7 +87,6 @@ def odeprecated(method, replacement = nil, disable: false, disable_on: nil, call
# - Location outside of 'compat/'.
# - Location of caller of deprecated method (if all else fails).
backtrace = caller
tap_message = nil
# Don't throw deprecations at all for cached, .brew or .metadata files.
return if backtrace.any? do |line|
......@@ -95,31 +95,26 @@ def odeprecated(method, replacement = nil, disable: false, disable_on: nil, call
line.include?("/.metadata/")
end
caller_message = backtrace.detect do |line|
next unless line =~ %r{^#{Regexp.escape(HOMEBREW_LIBRARY)}/Taps/([^/]+/[^/]+)/}
tap = Tap.fetch Regexp.last_match(1)
tap_message = "\nPlease report this to the #{tap} tap!"
true
end
caller_message ||= backtrace.detect do |line|
!line.start_with?("#{HOMEBREW_LIBRARY_PATH}/compat/")
tap_message = nil
backtrace.each do |line|
next unless match = line.match(HOMEBREW_TAP_PATH_REGEX)
tap = Tap.fetch(match[:user], match[:repo])
tap_message = "\nPlease report this to the #{tap} tap"
tap_message += ", or even better, submit a PR to fix it" if replacement
tap_message << ":\n #{line.sub(/^(.*\:\d+)\:.*$/, '\1')}\n\n"
break
end
caller_message ||= backtrace[1]
message = <<~EOS
Calling #{method} is #{verb}!
#{replacement_message}
#{caller_message}#{tap_message}
EOS
message = "Calling #{method} is #{verb}! #{replacement_message}"
message << tap_message if tap_message
if ARGV.homebrew_developer? || disable ||
Homebrew.raise_deprecation_exceptions?
if replacement || tap_message
message += "Or, even better, submit a PR to fix it!"
end
raise MethodDeprecatedError, message
if ARGV.homebrew_developer? || disable || Homebrew.raise_deprecation_exceptions?
exception = MethodDeprecatedError.new(message)
exception.set_backtrace(backtrace)
raise exception
elsif !Homebrew.auditing?
opoo "#{message}\n"
opoo message
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment