Skip to content

Commit

Permalink
[rubygems/rubygems] major_deprecation accepts :removed_message
Browse files Browse the repository at this point in the history
If supplied, it uses that in place of the message for the case where the
deprecation version is already past.

rubygems/rubygems@1deb73663c
  • Loading branch information
nevinera authored and matzbot committed Dec 1, 2023
1 parent e5e1f98 commit 079dfa1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/bundler/shared_helpers.rb
Expand Up @@ -117,16 +117,18 @@ def filesystem_access(path, action = :write, &block)
raise GenericSystemCallError.new(e, "There was an error accessing `#{path}`.")
end

def major_deprecation(major_version, message, print_caller_location: false)
def major_deprecation(major_version, message, removed_message: nil, print_caller_location: false)
if print_caller_location
caller_location = caller_locations(2, 2).first
message = "#{message} (called at #{caller_location.path}:#{caller_location.lineno})"
suffix = " (called at #{caller_location.path}:#{caller_location.lineno})"
message += suffix
removed_message += suffix if removed_message
end

bundler_major_version = Bundler.bundler_major_version
if bundler_major_version > major_version
require_relative "errors"
raise DeprecatedError, "[REMOVED] #{message}"
raise DeprecatedError, "[REMOVED] #{removed_message || message}"
end

return unless bundler_major_version >= major_version && prints_major_deprecations?
Expand Down
30 changes: 30 additions & 0 deletions spec/bundler/bundler/shared_helpers_spec.rb
Expand Up @@ -522,4 +522,34 @@
end
end
end

describe "#major_deprecation" do
before { allow(Bundler).to receive(:bundler_major_version).and_return(37) }
before { allow(Bundler.ui).to receive(:warn) }

it "prints and raises nothing below the deprecated major version" do
subject.major_deprecation(38, "Message")
subject.major_deprecation(39, "Message", :removed_message => "Removal", :print_caller_location => true)
expect(Bundler.ui).not_to have_received(:warn)
end

it "prints but does not raise _at_ the deprecated major version" do
subject.major_deprecation(37, "Message")
subject.major_deprecation(37, "Message", :removed_message => "Removal")
expect(Bundler.ui).to have_received(:warn).with("[DEPRECATED] Message").twice

subject.major_deprecation(37, "Message", :print_caller_location => true)
expect(Bundler.ui).to have_received(:warn).
with(a_string_matching(/^\[DEPRECATED\] Message \(called at .*:\d+\)$/))
end

it "raises the appropriate errors when _past_ the deprecated major version" do
expect { subject.major_deprecation(36, "Message") }.
to raise_error(Bundler::DeprecatedError, "[REMOVED] Message")
expect { subject.major_deprecation(36, "Message", :removed_message => "Removal") }.
to raise_error(Bundler::DeprecatedError, "[REMOVED] Removal")
expect { subject.major_deprecation(35, "Message", :removed_message => "Removal", :print_caller_location => true) }.
to raise_error(Bundler::DeprecatedError, /^\[REMOVED\] Removal \(called at .*:\d+\)$/)
end
end
end

0 comments on commit 079dfa1

Please sign in to comment.