2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/testing/stream"
|
2006-07-26 20:10:06 -04:00
|
|
|
|
2006-08-01 07:12:38 -04:00
|
|
|
class Deprecatee
|
2006-08-05 02:33:28 -04:00
|
|
|
def initialize
|
|
|
|
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
|
2016-08-06 12:03:25 -04:00
|
|
|
@_request = "there we go"
|
2006-08-05 02:33:28 -04:00
|
|
|
end
|
|
|
|
def request; @_request end
|
|
|
|
def old_request; @request end
|
|
|
|
|
2006-08-01 07:12:38 -04:00
|
|
|
def partially(foo = nil)
|
2016-08-06 12:03:25 -04:00
|
|
|
ActiveSupport::Deprecation.warn("calling with foo=nil is out") if foo.nil?
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
|
|
|
|
2006-08-01 07:12:38 -04:00
|
|
|
def not() 2 end
|
|
|
|
def none() 1 end
|
|
|
|
def one(a) a end
|
2016-10-28 23:05:58 -04:00
|
|
|
def multi(a, b, c) [a, b, c] end
|
2006-08-01 07:12:38 -04:00
|
|
|
deprecate :none, :one, :multi
|
2006-10-24 12:56:59 -04:00
|
|
|
|
|
|
|
def a; end
|
|
|
|
def b; end
|
|
|
|
def c; end
|
|
|
|
def d; end
|
|
|
|
def e; end
|
2016-08-06 13:38:33 -04:00
|
|
|
deprecate :a, :b, c: :e, d: "you now need to do something extra for this one"
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2009-06-24 18:59:58 -04:00
|
|
|
def f=(v); end
|
|
|
|
deprecate :f=
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
module B
|
|
|
|
C = 1
|
|
|
|
end
|
2016-08-06 12:03:25 -04:00
|
|
|
A = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("Deprecatee::A", "Deprecatee::B::C")
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
|
|
|
|
2008-06-10 17:01:05 -04:00
|
|
|
class DeprecationTest < ActiveSupport::TestCase
|
2015-01-15 01:42:33 -05:00
|
|
|
include ActiveSupport::Testing::Stream
|
|
|
|
|
2006-07-26 20:10:06 -04:00
|
|
|
def setup
|
2006-08-01 07:12:38 -04:00
|
|
|
# Track the last warning.
|
|
|
|
@old_behavior = ActiveSupport::Deprecation.behavior
|
|
|
|
@last_message = nil
|
|
|
|
ActiveSupport::Deprecation.behavior = Proc.new { |message| @last_message = message }
|
|
|
|
|
|
|
|
@dtc = Deprecatee.new
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-08-01 07:12:38 -04:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
ActiveSupport::Deprecation.behavior = @old_behavior
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-08-01 07:12:38 -04:00
|
|
|
|
|
|
|
def test_inline_deprecation_warning
|
|
|
|
assert_deprecated(/foo=nil/) do
|
|
|
|
@dtc.partially
|
|
|
|
end
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-09-03 22:57:40 -04:00
|
|
|
|
2006-08-01 07:12:38 -04:00
|
|
|
def test_undeprecated
|
|
|
|
assert_not_deprecated do
|
|
|
|
assert_equal 2, @dtc.not
|
|
|
|
end
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-08-01 07:12:38 -04:00
|
|
|
|
|
|
|
def test_deprecate_class_method
|
2011-05-21 03:29:30 -04:00
|
|
|
assert_deprecated(/none is deprecated/) do
|
2006-08-01 07:12:38 -04:00
|
|
|
assert_equal 1, @dtc.none
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_deprecated(/one is deprecated/) do
|
|
|
|
assert_equal 1, @dtc.one(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_deprecated(/multi is deprecated/) do
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [1, 2, 3], @dtc.multi(1, 2, 3)
|
2006-08-01 07:12:38 -04:00
|
|
|
end
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-08-01 07:12:38 -04:00
|
|
|
|
2013-01-22 17:16:17 -05:00
|
|
|
def test_deprecate_object
|
2016-08-06 12:03:25 -04:00
|
|
|
deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:")
|
2013-01-22 17:16:17 -05:00
|
|
|
assert_deprecated(/:bomb:/) { deprecated_object.to_s }
|
|
|
|
end
|
|
|
|
|
2006-08-01 07:12:38 -04:00
|
|
|
def test_nil_behavior_is_ignored
|
|
|
|
ActiveSupport::Deprecation.behavior = nil
|
|
|
|
assert_deprecated(/foo=nil/) { @dtc.partially }
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|
2006-08-05 02:33:28 -04:00
|
|
|
|
2010-07-01 04:26:45 -04:00
|
|
|
def test_several_behaviors
|
|
|
|
@a, @b = nil, nil
|
|
|
|
|
|
|
|
ActiveSupport::Deprecation.behavior = [
|
|
|
|
Proc.new { |msg, callstack| @a = msg },
|
|
|
|
Proc.new { |msg, callstack| @b = msg }
|
|
|
|
]
|
|
|
|
|
|
|
|
@dtc.partially
|
|
|
|
assert_match(/foo=nil/, @a)
|
|
|
|
assert_match(/foo=nil/, @b)
|
|
|
|
end
|
|
|
|
|
2013-08-12 19:38:48 -04:00
|
|
|
def test_raise_behaviour
|
|
|
|
ActiveSupport::Deprecation.behavior = :raise
|
2013-08-12 19:23:21 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
message = "Revise this deprecated stuff now!"
|
2016-02-10 13:02:18 -05:00
|
|
|
callstack = caller_locations
|
2013-08-12 19:23:21 -04:00
|
|
|
|
2013-12-19 06:03:39 -05:00
|
|
|
e = assert_raise ActiveSupport::DeprecationException do
|
2013-08-12 19:23:21 -04:00
|
|
|
ActiveSupport::Deprecation.behavior.first.call(message, callstack)
|
|
|
|
end
|
2013-12-19 06:03:39 -05:00
|
|
|
assert_equal message, e.message
|
2016-02-10 13:02:18 -05:00
|
|
|
assert_equal callstack.map(&:to_s), e.backtrace.map(&:to_s)
|
2013-08-12 19:23:21 -04:00
|
|
|
end
|
|
|
|
|
2012-04-28 17:22:11 -04:00
|
|
|
def test_default_stderr_behavior
|
|
|
|
ActiveSupport::Deprecation.behavior = :stderr
|
|
|
|
behavior = ActiveSupport::Deprecation.behavior.first
|
|
|
|
|
|
|
|
content = capture(:stderr) {
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_nil behavior.call("Some error!", ["call stack!"])
|
2012-04-28 17:22:11 -04:00
|
|
|
}
|
|
|
|
assert_match(/Some error!/, content)
|
|
|
|
assert_match(/call stack!/, content)
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_default_stderr_behavior_with_warn_method
|
|
|
|
ActiveSupport::Deprecation.behavior = :stderr
|
|
|
|
|
|
|
|
content = capture(:stderr) {
|
2016-08-06 12:03:25 -04:00
|
|
|
ActiveSupport::Deprecation.warn("Instance error!", ["instance call stack!"])
|
2012-09-13 02:38:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_match(/Instance error!/, content)
|
|
|
|
assert_match(/instance call stack!/, content)
|
|
|
|
end
|
|
|
|
|
2012-04-28 17:22:11 -04:00
|
|
|
def test_default_silence_behavior
|
|
|
|
ActiveSupport::Deprecation.behavior = :silence
|
|
|
|
behavior = ActiveSupport::Deprecation.behavior.first
|
|
|
|
|
2013-01-05 11:59:36 -05:00
|
|
|
stderr_output = capture(:stderr) {
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_nil behavior.call("Some error!", ["call stack!"])
|
2012-04-28 17:22:11 -04:00
|
|
|
}
|
2016-06-13 13:00:43 -04:00
|
|
|
assert stderr_output.empty?
|
2012-04-28 17:22:11 -04:00
|
|
|
end
|
|
|
|
|
2006-08-05 02:33:28 -04:00
|
|
|
def test_deprecated_instance_variable_proxy
|
|
|
|
assert_not_deprecated { @dtc.request.size }
|
2006-09-03 22:57:40 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_deprecated("@request.size") { assert_equal @dtc.request.size, @dtc.old_request.size }
|
|
|
|
assert_deprecated("@request.to_s") { assert_equal @dtc.request.to_s, @dtc.old_request.to_s }
|
2006-08-05 02:33:28 -04:00
|
|
|
end
|
2006-09-03 22:57:40 -04:00
|
|
|
|
2006-12-20 16:28:31 -05:00
|
|
|
def test_deprecated_instance_variable_proxy_shouldnt_warn_on_inspect
|
|
|
|
assert_not_deprecated { assert_equal @dtc.request.inspect, @dtc.old_request.inspect }
|
|
|
|
end
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def test_deprecated_constant_proxy
|
|
|
|
assert_not_deprecated { Deprecatee::B::C }
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_deprecated("Deprecatee::A") { assert_equal Deprecatee::B::C, Deprecatee::A }
|
2013-01-22 17:22:05 -05:00
|
|
|
assert_not_deprecated { assert_equal Deprecatee::B::C.class, Deprecatee::A.class }
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
|
|
|
|
2015-10-16 12:38:58 -04:00
|
|
|
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
|
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
def test_assert_deprecation_without_match
|
|
|
|
assert_deprecated do
|
|
|
|
@dtc.partially
|
|
|
|
end
|
|
|
|
end
|
2006-09-03 22:57:40 -04:00
|
|
|
|
2006-09-14 23:56:25 -04:00
|
|
|
def test_assert_deprecated_matches_any_warning
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_deprecated "abc" do
|
|
|
|
ActiveSupport::Deprecation.warn "abc"
|
|
|
|
ActiveSupport::Deprecation.warn "def"
|
2006-09-14 23:56:25 -04:00
|
|
|
end
|
2013-12-18 02:51:24 -05:00
|
|
|
rescue Minitest::Assertion
|
2016-08-06 12:03:25 -04:00
|
|
|
flunk "assert_deprecated should match any warning in block, not just the last one"
|
2006-09-14 23:56:25 -04:00
|
|
|
end
|
|
|
|
|
2006-09-15 01:54:18 -04:00
|
|
|
def test_assert_not_deprecated_returns_result_of_block
|
|
|
|
assert_equal 123, assert_not_deprecated { 123 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_deprecated_returns_result_of_block
|
2016-08-06 12:03:25 -04:00
|
|
|
result = assert_deprecated("abc") do
|
|
|
|
ActiveSupport::Deprecation.warn "abc"
|
2006-09-15 01:54:18 -04:00
|
|
|
123
|
|
|
|
end
|
|
|
|
assert_equal 123, result
|
|
|
|
end
|
|
|
|
|
2010-07-01 05:59:10 -04:00
|
|
|
def test_assert_deprecated_warn_work_with_default_behavior
|
2016-08-06 12:03:25 -04:00
|
|
|
ActiveSupport::Deprecation.instance_variable_set("@behavior", nil)
|
|
|
|
assert_deprecated("abc") do
|
|
|
|
ActiveSupport::Deprecation.warn "abc"
|
2010-07-01 05:59:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-14 14:15:44 -04:00
|
|
|
def test_silence
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
assert_not_deprecated { @dtc.partially }
|
|
|
|
end
|
2006-09-04 01:55:55 -04:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silenced = true
|
|
|
|
assert_not_deprecated { @dtc.partially }
|
|
|
|
ActiveSupport::Deprecation.silenced = false
|
2006-08-14 14:15:44 -04:00
|
|
|
end
|
2006-10-24 12:56:59 -04:00
|
|
|
|
|
|
|
def test_deprecation_without_explanation
|
|
|
|
assert_deprecated { @dtc.a }
|
|
|
|
assert_deprecated { @dtc.b }
|
2009-06-24 18:59:58 -04:00
|
|
|
assert_deprecated { @dtc.f = :foo }
|
2006-10-24 12:56:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecation_with_alternate_method
|
|
|
|
assert_deprecated(/use e instead/) { @dtc.c }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecation_with_explicit_message
|
|
|
|
assert_deprecated(/you now need to do something extra for this one/) { @dtc.d }
|
|
|
|
end
|
2011-07-25 15:05:06 -04:00
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_deprecation_in_other_object
|
2011-07-25 15:05:06 -04:00
|
|
|
messages = []
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
klass = Class.new do
|
|
|
|
delegate :warn, :behavior=, to: ActiveSupport::Deprecation
|
|
|
|
end
|
|
|
|
|
|
|
|
o = klass.new
|
|
|
|
o.behavior = Proc.new { |message, callstack| messages << message }
|
|
|
|
assert_difference("messages.size") do
|
|
|
|
o.warn("warning")
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_deprecated_method_with_custom_method_warning
|
2011-07-25 15:05:06 -04:00
|
|
|
deprecator = deprecator_with_messages
|
2012-09-13 02:38:34 -04:00
|
|
|
|
|
|
|
class << deprecator
|
|
|
|
private
|
|
|
|
def deprecated_method_warning(method, message)
|
|
|
|
"deprecator.deprecated_method_warning.#{method}"
|
|
|
|
end
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
deprecatee = Class.new do
|
2011-07-25 15:05:06 -04:00
|
|
|
def method
|
|
|
|
end
|
2012-09-13 02:38:34 -04:00
|
|
|
deprecate :method, deprecator: deprecator
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
deprecatee.new.method
|
|
|
|
assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method")
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_deprecate_with_custom_deprecator
|
2015-06-11 12:18:33 -04:00
|
|
|
custom_deprecator = Struct.new(:deprecation_warning).new
|
2012-09-13 02:38:34 -04:00
|
|
|
|
2015-06-11 12:18:33 -04:00
|
|
|
assert_called_with(custom_deprecator, :deprecation_warning, [:method, nil]) do
|
|
|
|
klass = Class.new do
|
|
|
|
def method
|
|
|
|
end
|
|
|
|
deprecate :method, deprecator: custom_deprecator
|
2012-09-13 02:38:34 -04:00
|
|
|
end
|
|
|
|
|
2015-06-11 12:18:33 -04:00
|
|
|
klass.new.method
|
|
|
|
end
|
2012-09-13 02:38:34 -04:00
|
|
|
end
|
|
|
|
|
2011-07-25 15:05:06 -04:00
|
|
|
def test_deprecated_constant_with_deprecator_given
|
|
|
|
deprecator = deprecator_with_messages
|
2012-09-13 02:38:34 -04:00
|
|
|
klass = Class.new
|
2016-10-28 23:05:58 -04:00
|
|
|
klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new("klass::OLD", "Object", deprecator))
|
2011-07-25 15:05:06 -04:00
|
|
|
assert_difference("deprecator.messages.size") do
|
|
|
|
klass::OLD.to_s
|
|
|
|
end
|
|
|
|
end
|
2013-01-05 11:59:36 -05:00
|
|
|
|
2017-01-20 18:02:34 -05:00
|
|
|
def test_deprecated_constant_with_custom_message
|
|
|
|
deprecator = deprecator_with_messages
|
|
|
|
|
|
|
|
klass = Class.new
|
|
|
|
klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new("klass::OLD", "Object", deprecator, message: "foo"))
|
|
|
|
|
|
|
|
klass::OLD.to_s
|
|
|
|
assert_match "foo", deprecator.messages.last
|
|
|
|
end
|
|
|
|
|
2011-07-25 15:05:06 -04:00
|
|
|
def test_deprecated_instance_variable_with_instance_deprecator
|
|
|
|
deprecator = deprecator_with_messages
|
2013-01-05 11:59:36 -05:00
|
|
|
|
2011-07-25 15:05:06 -04:00
|
|
|
klass = Class.new() do
|
2012-09-13 02:38:34 -04:00
|
|
|
def initialize(deprecator)
|
|
|
|
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
|
2011-07-25 15:05:06 -04:00
|
|
|
@_request = :a_request
|
|
|
|
end
|
|
|
|
def request; @_request end
|
|
|
|
def old_request; @request end
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
assert_difference("deprecator.messages.size") { klass.new(deprecator).old_request.to_s }
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecated_instance_variable_with_given_deprecator
|
|
|
|
deprecator = deprecator_with_messages
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
klass = Class.new do
|
2011-07-25 15:05:06 -04:00
|
|
|
define_method(:initialize) do
|
|
|
|
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
|
|
|
|
@_request = :a_request
|
|
|
|
end
|
|
|
|
def request; @_request end
|
|
|
|
def old_request; @request end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_difference("deprecator.messages.size") { klass.new.old_request.to_s }
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_delegate_deprecator_instance
|
|
|
|
klass = Class.new do
|
2011-07-25 15:05:06 -04:00
|
|
|
attr_reader :last_message
|
2012-09-13 02:38:34 -04:00
|
|
|
delegate :warn, :behavior=, to: ActiveSupport::Deprecation
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
self.behavior = [Proc.new { |message| @last_message = message }]
|
|
|
|
end
|
|
|
|
|
2011-07-25 15:05:06 -04:00
|
|
|
def deprecated_method
|
|
|
|
warn(deprecated_method_warning(:deprecated_method, "You are calling deprecated method"))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-09-13 02:38:34 -04:00
|
|
|
def deprecated_method_warning(method_name, message = nil)
|
|
|
|
message || "#{method_name} is deprecated and will be removed from This Library"
|
|
|
|
end
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
object = klass.new
|
|
|
|
object.deprecated_method
|
|
|
|
assert_match(/You are calling deprecated method/, object.last_message)
|
|
|
|
end
|
|
|
|
|
2015-12-19 02:35:38 -05:00
|
|
|
def test_default_deprecation_horizon_should_always_bigger_than_current_rails_version
|
2016-05-10 00:40:52 -04:00
|
|
|
assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING
|
2015-12-19 02:35:38 -05:00
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_default_gem_name
|
|
|
|
deprecator = ActiveSupport::Deprecation.new
|
2011-07-25 15:05:06 -04:00
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
|
|
|
|
assert_match(/is deprecated and will be removed from Rails/, message)
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
def test_custom_gem_name
|
2016-08-06 12:03:25 -04:00
|
|
|
deprecator = ActiveSupport::Deprecation.new("2.0", "Custom")
|
2011-07-25 15:05:06 -04:00
|
|
|
|
2012-09-13 02:38:34 -04:00
|
|
|
deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
|
|
|
|
assert_match(/is deprecated and will be removed from Custom/, message)
|
2011-07-25 15:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-13 02:38:34 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def deprecator_with_messages
|
|
|
|
klass = Class.new(ActiveSupport::Deprecation)
|
|
|
|
deprecator = klass.new
|
2016-08-16 03:30:11 -04:00
|
|
|
deprecator.behavior = Proc.new { |message, callstack| deprecator.messages << message }
|
2012-09-13 02:38:34 -04:00
|
|
|
def deprecator.messages
|
|
|
|
@messages ||= []
|
|
|
|
end
|
|
|
|
deprecator
|
|
|
|
end
|
2006-07-26 20:10:06 -04:00
|
|
|
end
|