diff options
author | Matthew Draper <matthew@trebex.net> | 2015-10-17 03:08:58 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2015-10-17 03:17:21 +1030 |
commit | 833aef438dab57c2c6523686fa9754faa023cd10 (patch) | |
tree | d399ae7ffb2276726b37131fe3059964f6fcebce /activesupport | |
parent | cd4575f98c11bfa698c0c7b6a3e1feae64006a76 (diff) | |
parent | 6c98fbd9c3cc28ccbfc1b46a9ce3d58fdb19f3c8 (diff) | |
download | rails-833aef438dab57c2c6523686fa9754faa023cd10.tar.gz rails-833aef438dab57c2c6523686fa9754faa023cd10.tar.bz2 rails-833aef438dab57c2c6523686fa9754faa023cd10.zip |
Merge pull request #21953 from bdunne/fix_dep_warn
Fix deprecation warning messages on deprecate_methods
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/deprecation/method_wrappers.rb | 48 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/deprecation.rb | 17 | ||||
-rw-r--r-- | activesupport/test/deprecation/method_wrappers_test.rb | 34 | ||||
-rw-r--r-- | activesupport/test/deprecation_test.rb | 8 |
5 files changed, 92 insertions, 20 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index b05a068dc1..92efd96de4 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix `ActiveSupport::Deprecation#deprecate_methods` to report using the + current deprecator instance, where applicable. + + *Brandon Dunne* + * `Cache#fetch` instrumentation marks whether it was a `:hit`. *Robin Clowers* diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb index 91050d18f0..32fe8025fe 100644 --- a/activesupport/lib/active_support/deprecation/method_wrappers.rb +++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb @@ -9,25 +9,49 @@ module ActiveSupport # module Fred # extend self # - # def foo; end - # def bar; end - # def baz; end + # def aaa; end + # def bbb; end + # def ccc; end + # def ddd; end + # def eee; end # end # - # ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead') - # # => [:foo, :bar, :baz] + # Using the default deprecator: + # ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead') + # # => [:aaa, :bbb, :ccc] # - # Fred.foo - # # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1." + # Fred.aaa + # # DEPRECATION WARNING: aaa is deprecated and will be removed from Rails 5.0. (called from irb_binding at (irb):10) + # # => nil # - # Fred.bar - # # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)." + # Fred.bbb + # # DEPRECATION WARNING: bbb is deprecated and will be removed from Rails 5.0 (use zzz instead). (called from irb_binding at (irb):11) + # # => nil # - # Fred.baz - # # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)." + # Fred.ccc + # # DEPRECATION WARNING: ccc is deprecated and will be removed from Rails 5.0 (use Bar#ccc instead). (called from irb_binding at (irb):12) + # # => nil + # + # Passing in a custom deprecator: + # custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem') + # ActiveSupport::Deprecation.deprecate_methods(Fred, ddd: :zzz, deprecator: custom_deprecator) + # # => [:ddd] + # + # Fred.ddd + # DEPRECATION WARNING: ddd is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):15) + # # => nil + # + # Using a custom deprecator directly: + # custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem') + # custom_deprecator.deprecate_methods(Fred, eee: :zzz) + # # => [:eee] + # + # Fred.eee + # DEPRECATION WARNING: eee is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):18) + # # => nil def deprecate_methods(target_module, *method_names) options = method_names.extract_options! - deprecator = options.delete(:deprecator) || ActiveSupport::Deprecation.instance + deprecator = options.delete(:deprecator) || self method_names += options.keys mod = Module.new do diff --git a/activesupport/lib/active_support/testing/deprecation.rb b/activesupport/lib/active_support/testing/deprecation.rb index 6c94c611b6..5dfa14eeba 100644 --- a/activesupport/lib/active_support/testing/deprecation.rb +++ b/activesupport/lib/active_support/testing/deprecation.rb @@ -3,8 +3,8 @@ require 'active_support/deprecation' module ActiveSupport module Testing module Deprecation #:nodoc: - def assert_deprecated(match = nil, &block) - result, warnings = collect_deprecations(&block) + def assert_deprecated(match = nil, deprecator = nil, &block) + result, warnings = collect_deprecations(deprecator, &block) assert !warnings.empty?, "Expected a deprecation warning within the block but received none" if match match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp) @@ -13,22 +13,23 @@ module ActiveSupport result end - def assert_not_deprecated(&block) - result, deprecations = collect_deprecations(&block) + def assert_not_deprecated(deprecator = nil, &block) + result, deprecations = collect_deprecations(deprecator, &block) assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}" result end - def collect_deprecations - old_behavior = ActiveSupport::Deprecation.behavior + def collect_deprecations(deprecator = nil) + deprecator ||= ActiveSupport::Deprecation + old_behavior = deprecator.behavior deprecations = [] - ActiveSupport::Deprecation.behavior = Proc.new do |message, callstack| + deprecator.behavior = Proc.new do |message, callstack| deprecations << message end result = yield [result, deprecations] ensure - ActiveSupport::Deprecation.behavior = old_behavior + deprecator.behavior = old_behavior end end end diff --git a/activesupport/test/deprecation/method_wrappers_test.rb b/activesupport/test/deprecation/method_wrappers_test.rb new file mode 100644 index 0000000000..9a4ca2b217 --- /dev/null +++ b/activesupport/test/deprecation/method_wrappers_test.rb @@ -0,0 +1,34 @@ +require 'abstract_unit' +require 'active_support/deprecation' + +class MethodWrappersTest < ActiveSupport::TestCase + def setup + @klass = Class.new do + def new_method; "abc" end + alias_method :old_method, :new_method + end + end + + def test_deprecate_methods_warning_default + warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/ + ActiveSupport::Deprecation.deprecate_methods(@klass, :old_method => :new_method) + + assert_deprecated(warning) { assert_equal "abc", @klass.new.old_method } + end + + def test_deprecate_methods_warning_with_optional_deprecator + warning = /old_method is deprecated and will be removed from MyGem next-release \(use new_method instead\)/ + deprecator = ActiveSupport::Deprecation.new("next-release", "MyGem") + ActiveSupport::Deprecation.deprecate_methods(@klass, :old_method => :new_method, :deprecator => deprecator) + + assert_deprecated(warning, deprecator) { assert_equal "abc", @klass.new.old_method } + end + + def test_deprecate_methods_warning_when_deprecated_with_custom_deprecator + warning = /old_method is deprecated and will be removed from MyGem next-release \(use new_method instead\)/ + deprecator = ActiveSupport::Deprecation.new("next-release", "MyGem") + deprecator.deprecate_methods(@klass, :old_method => :new_method) + + assert_deprecated(warning, deprecator) { assert_equal "abc", @klass.new.old_method } + end +end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 7e8844b301..cd02ad3f3f 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -163,6 +163,14 @@ class DeprecationTest < ActiveSupport::TestCase assert_not_deprecated { assert_equal Deprecatee::B::C.class, Deprecatee::A.class } end + def test_assert_deprecated_raises_when_method_not_deprecated + assert_raises(Minitest::Assertion) { assert_deprecated { @dtc.not } } + end + + def test_assert_not_deprecated + assert_raises(Minitest::Assertion) { assert_not_deprecated { @dtc.partially } } + end + def test_assert_deprecation_without_match assert_deprecated do @dtc.partially |