ruby-changes:61069
From: bronzdoc <ko1@a...>
Date: Fri, 8 May 2020 07:39:39 +0900 (JST)
Subject: [ruby-changes:61069] 0e85a39dc7 (master): [rubygems/rubygems] Restore and deprecate old deprecate method
https://git.ruby-lang.org/ruby.git/commit/?id=0e85a39dc7 From 0e85a39dc70328641c3155f66568feedbe6dd15f Mon Sep 17 00:00:00 2001 From: bronzdoc <lsagastume1990@g...> Date: Sun, 19 Apr 2020 08:18:29 -0600 Subject: [rubygems/rubygems] Restore and deprecate old deprecate method https://github.com/rubygems/rubygems/commit/024267fa60 diff --git a/lib/rubygems/deprecate.rb b/lib/rubygems/deprecate.rb index 5f2140c..31981b1 100644 --- a/lib/rubygems/deprecate.rb +++ b/lib/rubygems/deprecate.rb @@ -49,6 +49,31 @@ module Gem::Deprecate https://github.com/ruby/ruby/blob/trunk/lib/rubygems/deprecate.rb#L49 # Simple deprecation method that deprecates +name+ by wrapping it up # in a dummy method. It warns on each call to the dummy method # telling the user of +repl+ (unless +repl+ is :none) and the + # year/month that it is planned to go away. + + def deprecate(name, repl, year, month) + class_eval do + old = "_deprecated_#{name}" + alias_method old, name + define_method name do |*args, &block| + klass = self.kind_of? Module + target = klass ? "#{self}." : "#{self.class}#" + msg = [ "NOTE: #{target}#{name} is deprecated", + repl == :none ? " with no replacement" : "; use #{repl} instead", + ". It will be removed on or after %4d-%02d-01." % [year, month], + "\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}", + ] + warn "Gem::Deprecate#deprecate has been deprecated with no replacement and it will be removed in Rubygems 4.\n" unless Gem::Deprecate.skip + warn "#{msg.join}." unless Gem::Deprecate.skip + send old, *args, &block + end + end + end + + ## + # Simple deprecation method that deprecates +name+ by wrapping it up + # in a dummy method. It warns on each call to the dummy method + # telling the user of +repl+ (unless +repl+ is :none) and the # Rubygems version that it is planned to go away. def rubygems_deprecate(name, replacement=:none) diff --git a/test/rubygems/test_deprecate.rb b/test/rubygems/test_deprecate.rb index 27f7243..e5e9350 100644 --- a/test/rubygems/test_deprecate.rb +++ b/test/rubygems/test_deprecate.rb @@ -54,6 +54,20 @@ class TestDeprecate < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_deprecate.rb#L54 end + class OtherThing + + extend Gem::Deprecate + attr_accessor :message + def foo + @message = "foo" + end + def bar + @message = "bar" + end + deprecate :foo, :bar, 2099, 3 + + end + def test_deprecated_method_calls_the_old_method capture_io do thing = Thing.new @@ -91,4 +105,16 @@ class TestDeprecate < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_deprecate.rb#L105 Gem::Commands.send(:remove_const, :FooCommand) end + def test_deprecated_method_outputs_a_warning_old_way + out, err = capture_io do + thing = OtherThing.new + thing.foo + end + + assert_equal "", out + assert_match(/Gem::Deprecate#deprecate has been deprecated with no replacement and it will be removed in Rubygems 4\./, err) + assert_match(/Thing#foo is deprecated; use bar instead\./, err) + assert_match(/on or after 2099-03-01/, err) + end + end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/